mod_dup.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * mod_dup.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id$
  7. */
  8. #include "config.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "pkt.h"
  13. #include "mod.h"
  14. #define DUP_FIRST 1
  15. #define DUP_LAST 2
  16. #define DUP_RANDOM 3
  17. struct dup_data {
  18. rand_t *rnd;
  19. int which;
  20. int percent;
  21. };
  22. void *
  23. dup_close(void *d)
  24. {
  25. struct dup_data *data = (struct dup_data *)d;
  26. if (data != NULL) {
  27. rand_close(data->rnd);
  28. free(data);
  29. }
  30. return (NULL);
  31. }
  32. void *
  33. dup_open(int argc, char *argv[])
  34. {
  35. struct dup_data *data;
  36. if (argc != 3)
  37. return (NULL);
  38. if ((data = malloc(sizeof(*data))) == NULL)
  39. return (NULL);
  40. data->rnd = rand_open();
  41. if (strcasecmp(argv[1], "first") == 0)
  42. data->which = DUP_FIRST;
  43. else if (strcasecmp(argv[1], "last") == 0)
  44. data->which = DUP_LAST;
  45. else if (strcasecmp(argv[1], "random") == 0)
  46. data->which = DUP_RANDOM;
  47. else
  48. return (dup_close(data));
  49. if ((data->percent = atoi(argv[2])) <= 0 || data->percent > 100)
  50. return (dup_close(data));
  51. return (data);
  52. }
  53. int
  54. dup_apply(void *d, struct pktq *pktq)
  55. {
  56. struct dup_data *data = (struct dup_data *)d;
  57. struct pkt *pkt, *new;
  58. if (data->percent < 100 &&
  59. (rand_uint16(data->rnd) % 100) > data->percent)
  60. return (0);
  61. if (data->which == DUP_FIRST)
  62. pkt = TAILQ_FIRST(pktq);
  63. else if (data->which == DUP_LAST)
  64. pkt = TAILQ_LAST(pktq, pktq);
  65. else
  66. pkt = pktq_random(data->rnd, pktq);
  67. if (!pkt)
  68. return -1;
  69. new = pkt_dup(pkt);
  70. if (!new)
  71. return -1;
  72. TAILQ_INSERT_AFTER(pktq, pkt, new, pkt_next);
  73. return (0);
  74. }
  75. struct mod mod_dup = {
  76. "dup", /* name */
  77. "dup first|last|random <prob-%>", /* usage */
  78. dup_open, /* open */
  79. dup_apply, /* apply */
  80. dup_close /* close */
  81. };