mod_order.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * mod_order.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 "mod.h"
  13. #define ORDER_RANDOM 1
  14. #define ORDER_REVERSE 2
  15. struct order_data {
  16. rand_t *rnd;
  17. int type;
  18. };
  19. void *
  20. order_close(void *d)
  21. {
  22. struct order_data *data = (struct order_data *)d;
  23. if (data != NULL) {
  24. rand_close(data->rnd);
  25. free(data);
  26. }
  27. return (NULL);
  28. }
  29. void *
  30. order_open(int argc, char *argv[])
  31. {
  32. struct order_data *data;
  33. if (argc < 2)
  34. return (NULL);
  35. if ((data = malloc(sizeof(*data))) == NULL)
  36. return (NULL);
  37. data->rnd = rand_open();
  38. if (strcasecmp(argv[1], "random") == 0) {
  39. data->type = ORDER_RANDOM;
  40. } else if (strcasecmp(argv[1], "reverse") == 0) {
  41. data->type = ORDER_REVERSE;
  42. } else
  43. return (order_close(data));
  44. return (data);
  45. }
  46. int
  47. order_apply(void *d, struct pktq *pktq)
  48. {
  49. struct order_data *data = (struct order_data *)d;
  50. if (data->type == ORDER_RANDOM)
  51. pktq_shuffle(data->rnd, pktq);
  52. else
  53. pktq_reverse(pktq);
  54. return (0);
  55. }
  56. struct mod mod_order = {
  57. "order", /* name */
  58. "order random|reverse", /* usage */
  59. order_open, /* open */
  60. order_apply, /* apply */
  61. order_close /* close */
  62. };