1
0

patch-tcpnice_c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. --- dsniff-2.4.orig/tcpnice.c 2001-03-17 08:41:51.000000000 +0100
  2. +++ dsniff-2.4/tcpnice.c 2009-12-11 13:01:35.000000000 +0100
  3. @@ -41,107 +41,106 @@ usage(void)
  4. }
  5. static void
  6. -send_tcp_window_advertisement(int sock, struct libnet_ip_hdr *ip,
  7. +send_tcp_window_advertisement(libnet_t *l, struct libnet_ipv4_hdr *ip,
  8. struct libnet_tcp_hdr *tcp)
  9. {
  10. int len;
  11. ip->ip_hl = 5;
  12. - ip->ip_len = htons(IP_H + TCP_H);
  13. - ip->ip_id = libnet_get_prand(PRu16);
  14. - memcpy(buf, (u_char *)ip, IP_H);
  15. + ip->ip_len = htons(LIBNET_IPV4_H + LIBNET_TCP_H);
  16. + ip->ip_id = libnet_get_prand(LIBNET_PRu16);
  17. + memcpy(buf, (u_char *)ip, LIBNET_IPV4_H);
  18. tcp->th_off = 5;
  19. tcp->th_win = htons(MIN_WIN);
  20. - memcpy(buf + IP_H, (u_char *)tcp, TCP_H);
  21. + memcpy(buf + LIBNET_IPV4_H, (u_char *)tcp, LIBNET_TCP_H);
  22. - libnet_do_checksum(buf, IPPROTO_TCP, TCP_H);
  23. + libnet_do_checksum(l, buf, IPPROTO_TCP, LIBNET_TCP_H);
  24. - len = IP_H + TCP_H;
  25. + len = LIBNET_IPV4_H + LIBNET_TCP_H;
  26. - if (libnet_write_ip(sock, buf, len) != len)
  27. + if (libnet_write_raw_ipv4(l, buf, len) != len)
  28. warn("write");
  29. fprintf(stderr, "%s:%d > %s:%d: . ack %lu win %d\n",
  30. - libnet_host_lookup(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport),
  31. - libnet_host_lookup(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport),
  32. + libnet_addr2name4(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport),
  33. + libnet_addr2name4(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport),
  34. ntohl(tcp->th_ack), 1);
  35. }
  36. static void
  37. -send_icmp_source_quench(int sock, struct libnet_ip_hdr *ip)
  38. +send_icmp_source_quench(libnet_t *l, struct libnet_ipv4_hdr *ip)
  39. {
  40. - struct libnet_icmp_hdr *icmp;
  41. + struct libnet_icmpv4_hdr *icmp;
  42. int len;
  43. len = (ip->ip_hl * 4) + 8;
  44. - libnet_build_ip(ICMP_ECHO_H + len, 0, libnet_get_prand(PRu16),
  45. - 0, 64, IPPROTO_ICMP, ip->ip_dst.s_addr,
  46. - ip->ip_src.s_addr, NULL, 0, buf);
  47. -
  48. - icmp = (struct libnet_icmp_hdr *)(buf + IP_H);
  49. + icmp = (struct libnet_icmpv4_hdr *)(buf + LIBNET_IPV4_H);
  50. icmp->icmp_type = ICMP_SOURCEQUENCH;
  51. icmp->icmp_code = 0;
  52. - memcpy((u_char *)icmp + ICMP_ECHO_H, (u_char *)ip, len);
  53. + memcpy((u_char *)icmp + LIBNET_ICMPV4_ECHO_H, (u_char *)ip, len);
  54. - libnet_do_checksum(buf, IPPROTO_ICMP, ICMP_ECHO_H + len);
  55. + len += LIBNET_ICMPV4_ECHO_H;
  56. - len += (IP_H + ICMP_ECHO_H);
  57. + libnet_build_ipv4(LIBNET_IPV4_H + len, 0,
  58. + libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_ICMP,
  59. + 0, ip->ip_dst.s_addr, ip->ip_src.s_addr,
  60. + (u_int8_t *) icmp, len, l, 0);
  61. - if (libnet_write_ip(sock, buf, len) != len)
  62. + if (libnet_write(l) != len)
  63. warn("write");
  64. fprintf(stderr, "%s > %s: icmp: source quench\n",
  65. - libnet_host_lookup(ip->ip_dst.s_addr, 0),
  66. - libnet_host_lookup(ip->ip_src.s_addr, 0));
  67. + libnet_addr2name4(ip->ip_dst.s_addr, 0),
  68. + libnet_addr2name4(ip->ip_src.s_addr, 0));
  69. }
  70. static void
  71. -send_icmp_frag_needed(int sock, struct libnet_ip_hdr *ip)
  72. +send_icmp_frag_needed(libnet_t *l, struct libnet_ipv4_hdr *ip)
  73. {
  74. - struct libnet_icmp_hdr *icmp;
  75. + struct libnet_icmpv4_hdr *icmp;
  76. int len;
  77. len = (ip->ip_hl * 4) + 8;
  78. - libnet_build_ip(ICMP_MASK_H + len, 4, libnet_get_prand(PRu16),
  79. - 0, 64, IPPROTO_ICMP, ip->ip_dst.s_addr,
  80. - ip->ip_src.s_addr, NULL, 0, buf);
  81. -
  82. - icmp = (struct libnet_icmp_hdr *)(buf + IP_H);
  83. + icmp = (struct libnet_icmpv4_hdr *)(buf + LIBNET_IPV4_H);
  84. icmp->icmp_type = ICMP_UNREACH;
  85. icmp->icmp_code = ICMP_UNREACH_NEEDFRAG;
  86. icmp->hun.frag.pad = 0;
  87. icmp->hun.frag.mtu = htons(MIN_MTU);
  88. - memcpy((u_char *)icmp + ICMP_MASK_H, (u_char *)ip, len);
  89. + memcpy((u_char *)icmp + LIBNET_ICMPV4_MASK_H, (u_char *)ip, len);
  90. - libnet_do_checksum(buf, IPPROTO_ICMP, ICMP_MASK_H + len);
  91. -
  92. - len += (IP_H + ICMP_MASK_H);
  93. + len += LIBNET_ICMPV4_MASK_H;
  94. +
  95. + libnet_build_ipv4(LIBNET_IPV4_H + len, 4,
  96. + libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_ICMP,
  97. + 0, ip->ip_dst.s_addr, ip->ip_src.s_addr,
  98. + (u_int8_t *) icmp, len, l, 0);
  99. - if (libnet_write_ip(sock, buf, len) != len)
  100. + if (libnet_write(l) != len)
  101. warn("write");
  102. fprintf(stderr, "%s > %s: icmp: ",
  103. - libnet_host_lookup(ip->ip_dst.s_addr, 0),
  104. - libnet_host_lookup(ip->ip_src.s_addr, 0));
  105. + libnet_addr2name4(ip->ip_dst.s_addr, 0),
  106. + libnet_addr2name4(ip->ip_src.s_addr, 0));
  107. fprintf(stderr, "%s unreachable - need to frag (mtu %d)\n",
  108. - libnet_host_lookup(ip->ip_src.s_addr, 0), MIN_MTU);
  109. + libnet_addr2name4(ip->ip_src.s_addr, 0), MIN_MTU);
  110. }
  111. static void
  112. tcp_nice_cb(u_char *user, const struct pcap_pkthdr *pcap, const u_char *pkt)
  113. {
  114. - struct libnet_ip_hdr *ip;
  115. + struct libnet_ipv4_hdr *ip;
  116. struct libnet_tcp_hdr *tcp;
  117. - int *sock, len;
  118. + int len;
  119. + libnet_t *l;
  120. - sock = (int *)user;
  121. + l = (libnet_t *)user;
  122. pkt += pcap_off;
  123. len = pcap->caplen - pcap_off;
  124. - ip = (struct libnet_ip_hdr *)pkt;
  125. + ip = (struct libnet_ipv4_hdr *)pkt;
  126. if (ip->ip_p != IPPROTO_TCP)
  127. return;
  128. @@ -151,11 +150,11 @@ tcp_nice_cb(u_char *user, const struct p
  129. if (ntohs(ip->ip_len) > (ip->ip_hl << 2) + (tcp->th_off << 2)) {
  130. if (Opt_icmp)
  131. - send_icmp_source_quench(*sock, ip);
  132. + send_icmp_source_quench(l, ip);
  133. if (Opt_win)
  134. - send_tcp_window_advertisement(*sock, ip, tcp);
  135. + send_tcp_window_advertisement(l, ip, tcp);
  136. if (Opt_pmtu)
  137. - send_icmp_frag_needed(*sock, ip);
  138. + send_icmp_frag_needed(l, ip);
  139. }
  140. }
  141. @@ -164,8 +163,10 @@ main(int argc, char *argv[])
  142. {
  143. extern char *optarg;
  144. extern int optind;
  145. - int c, sock;
  146. + int c;
  147. char *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
  148. + char libnet_ebuf[LIBNET_ERRBUF_SIZE];
  149. + libnet_t *l;
  150. pcap_t *pd;
  151. intf = NULL;
  152. @@ -209,14 +210,14 @@ main(int argc, char *argv[])
  153. if ((pcap_off = pcap_dloff(pd)) < 0)
  154. errx(1, "couldn't determine link layer offset");
  155. - if ((sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1)
  156. + if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL)
  157. errx(1, "couldn't initialize sending");
  158. - libnet_seed_prand();
  159. + libnet_seed_prand(l);
  160. warnx("listening on %s [%s]", intf, filter);
  161. - pcap_loop(pd, -1, tcp_nice_cb, (u_char *)&sock);
  162. + pcap_loop(pd, -1, tcp_nice_cb, (u_char *)l);
  163. /* NOTREACHED */