mod_ip_ttl.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * mod_ip_ttl.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_ttl_data {
  16. int ttl;
  17. };
  18. void *
  19. ip_ttl_close(void *d)
  20. {
  21. if (d != NULL)
  22. free(d);
  23. return (NULL);
  24. }
  25. void *
  26. ip_ttl_open(int argc, char *argv[])
  27. {
  28. struct ip_ttl_data *data;
  29. if (argc != 2)
  30. return (NULL);
  31. if ((data = calloc(1, sizeof(*data))) == NULL)
  32. return (NULL);
  33. if ((data->ttl = atoi(argv[1])) <= 0 || data->ttl > 255)
  34. return (ip_ttl_close(data));
  35. return (data);
  36. }
  37. int
  38. ip_ttl_apply(void *d, struct pktq *pktq)
  39. {
  40. struct ip_ttl_data *data = (struct ip_ttl_data *)d;
  41. struct pkt *pkt;
  42. int ttldec;
  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. ttldec = pkt->pkt_ip->ip_ttl - data->ttl;
  47. pkt->pkt_ip->ip_ttl = data->ttl;
  48. if (pkt->pkt_ip->ip_sum >= htons(0xffff - (ttldec << 8)))
  49. pkt->pkt_ip->ip_sum += htons(ttldec << 8) + 1;
  50. else
  51. pkt->pkt_ip->ip_sum += htons(ttldec << 8);
  52. } else if (eth_type == ETH_TYPE_IPV6) {
  53. pkt->pkt_ip6->ip6_hlim = data->ttl;
  54. }
  55. }
  56. return (0);
  57. }
  58. struct mod mod_ip_ttl = {
  59. "ip_ttl", /* name */
  60. "ip_ttl <ttl>", /* usage */
  61. ip_ttl_open, /* open */
  62. ip_ttl_apply, /* apply */
  63. ip_ttl_close /* close */
  64. };