mod_ip_opt.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * mod_ip_opt.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id: mod_ip_opt.c 2000 2008-04-27 06:17:35Z aturner $
  7. */
  8. #include "config.h"
  9. #include <err.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "pkt.h"
  14. #include "mod.h"
  15. void *
  16. ip_opt_close(void *d)
  17. {
  18. if (d != NULL)
  19. free(d);
  20. return (NULL);
  21. }
  22. void *
  23. ip_opt_open(int argc, char *argv[])
  24. {
  25. struct ip_opt *opt;
  26. struct addr addr;
  27. int i, j;
  28. if (argc < 4)
  29. return (NULL);
  30. if ((opt = calloc(1, sizeof(*opt))) == NULL)
  31. return (NULL);
  32. if (strcasecmp(argv[1], "lsrr") == 0) {
  33. opt->opt_type = IP_OPT_LSRR;
  34. } else if (strcasecmp(argv[1], "ssrr") == 0) {
  35. opt->opt_type = IP_OPT_SSRR;
  36. } else
  37. return (ip_opt_close(opt));
  38. if ((i = atoi(argv[2])) < 4 || i > 0xff) {
  39. warnx("<ptr> must be >= 4, and should be a multiple of 4");
  40. return (ip_opt_close(opt));
  41. }
  42. opt->opt_data.rr.ptr = i;
  43. for (i = 3, j = 0; i < argc && j < 9; i++, j++) {
  44. if (addr_aton(argv[i], &addr) < 0) {
  45. return (ip_opt_close(opt));
  46. }
  47. opt->opt_data.rr.iplist[j] = addr.addr_ip;
  48. }
  49. opt->opt_len = IP_OPT_LEN + 1 + (IP_ADDR_LEN * j);
  50. return (opt);
  51. }
  52. int
  53. ip_opt_apply(void *d, struct pktq *pktq)
  54. {
  55. struct ip_opt *opt = (struct ip_opt *)d;
  56. struct pkt *pkt;
  57. size_t len;
  58. TAILQ_FOREACH(pkt, pktq, pkt_next) {
  59. len = ip_add_option(pkt->pkt_ip, PKT_BUF_LEN - ETH_HDR_LEN,
  60. IP_PROTO_IP, opt, opt->opt_len);
  61. if (len > 0) {
  62. pkt->pkt_end += len;
  63. pkt_decorate(pkt);
  64. ip_checksum(pkt->pkt_ip,
  65. pkt->pkt_end - pkt->pkt_eth_data);
  66. }
  67. }
  68. return (0);
  69. }
  70. struct mod mod_ip_opt = {
  71. "ip_opt", /* name */
  72. "ip_opt lsrr|ssrr <ptr> <ip-addr> ...", /* usage */
  73. ip_opt_open, /* open */
  74. ip_opt_apply, /* apply */
  75. ip_opt_close /* close */
  76. };