mod_tcp_opt.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * mod_tcp_opt.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. * $Id: mod_tcp_opt.c 2191 2009-02-01 21:34:27Z aturner $
  7. */
  8. #include "config.h"
  9. #include "defines.h"
  10. #include "common.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "pkt.h"
  15. #include "mod.h"
  16. void *
  17. tcp_opt_close(void *d)
  18. {
  19. if (d != NULL)
  20. free(d);
  21. return (NULL);
  22. }
  23. void *
  24. tcp_opt_open(int argc, char *argv[])
  25. {
  26. struct tcp_opt *opt;
  27. int i;
  28. if (argc < 3)
  29. return (NULL);
  30. if ((opt = calloc(1, sizeof(*opt))) == NULL)
  31. return (NULL);
  32. if (strcasecmp(argv[1], "mss") == 0) {
  33. opt->opt_type = TCP_OPT_MSS;
  34. opt->opt_len = TCP_OPT_LEN + 2;
  35. if ((i = atoi(argv[2])) <= 0 || i > 0xffff) {
  36. warn("mss <size> must be from 0-65535");
  37. return (tcp_opt_close(opt));
  38. }
  39. opt->opt_data.mss = htons(i);
  40. } else if (strcasecmp(argv[1], "wscale") == 0) {
  41. opt->opt_type = TCP_OPT_WSCALE;
  42. opt->opt_len = TCP_OPT_LEN + 2;
  43. if ((i = atoi(argv[2])) <= 0 || i > 0xff) {
  44. warn("wscale <size> must be from 0-255");
  45. return (tcp_opt_close(opt));
  46. }
  47. opt->opt_data.wscale = i;
  48. } else
  49. return (tcp_opt_close(opt));
  50. return (opt);
  51. }
  52. int
  53. tcp_opt_apply(void *d, struct pktq *pktq)
  54. {
  55. struct tcp_opt *opt = (struct tcp_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,
  60. sizeof(pkt->pkt_data) - ETH_HDR_LEN,
  61. IP_PROTO_TCP, opt, opt->opt_len);
  62. if (len > 0) {
  63. pkt->pkt_end += len;
  64. pkt_decorate(pkt);
  65. ip_checksum(pkt->pkt_ip, pkt->pkt_end -
  66. pkt->pkt_eth_data);
  67. }
  68. }
  69. return (0);
  70. }
  71. struct mod mod_tcp_opt = {
  72. "tcp_opt", /* name */
  73. "tcp_opt mss|wscale <size>", /* usage */
  74. tcp_opt_open, /* open */
  75. tcp_opt_apply, /* apply */
  76. tcp_opt_close /* close */
  77. };