mod_ip_tos.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * mod_ip_tos.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 "argv.h"
  13. #include "mod.h"
  14. #include "pkt.h"
  15. struct ip_tos_data {
  16. int tos;
  17. };
  18. void *
  19. ip_tos_close(void *d)
  20. {
  21. if (d != NULL)
  22. free(d);
  23. return (NULL);
  24. }
  25. void *
  26. ip_tos_open(int argc, char *argv[])
  27. {
  28. struct ip_tos_data *data;
  29. if (argc != 2)
  30. return (NULL);
  31. if ((data = calloc(1, sizeof(*data))) == NULL)
  32. return (NULL);
  33. if (sscanf(argv[1], "%i", &data->tos) != 1 ||
  34. data->tos < 0 || data->tos > 255)
  35. return (ip_tos_close(data));
  36. return (data);
  37. }
  38. int
  39. ip_tos_apply(void *d, struct pktq *pktq)
  40. {
  41. struct ip_tos_data *data = (struct ip_tos_data *)d;
  42. struct pkt *pkt;
  43. TAILQ_FOREACH(pkt, pktq, pkt_next) {
  44. uint16_t eth_type = htons(pkt->pkt_eth->eth_type);
  45. if (eth_type == ETH_TYPE_IP) {
  46. pkt->pkt_ip->ip_tos = data->tos;
  47. /* XXX - do incremental checksum */
  48. ip_checksum(pkt->pkt_ip, pkt->pkt_ip_data - pkt->pkt_eth_data);
  49. }
  50. }
  51. return (0);
  52. }
  53. struct mod mod_ip_tos = {
  54. "ip_tos", /* name */
  55. "ip_tos <tos>", /* usage */
  56. ip_tos_open, /* open */
  57. ip_tos_apply, /* apply */
  58. ip_tos_close /* close */
  59. };