Ticket #21225: patch-tcpnice.c.diff
File patch-tcpnice.c.diff, 5.8 KB (added by hsivank@…, 15 years ago) |
---|
-
tcpnice.c
old new 41 41 } 42 42 43 43 static void 44 send_tcp_window_advertisement( int sock, struct libnet_ip_hdr *ip,44 send_tcp_window_advertisement(libnet_t *l, struct libnet_ipv4_hdr *ip, 45 45 struct libnet_tcp_hdr *tcp) 46 46 { 47 47 int len; 48 48 49 49 ip->ip_hl = 5; 50 ip->ip_len = htons( IP_H +TCP_H);51 ip->ip_id = libnet_get_prand( PRu16);52 memcpy(buf, (u_char *)ip, IP_H);50 ip->ip_len = htons(LIBNET_IPV4_H + LIBNET_TCP_H); 51 ip->ip_id = libnet_get_prand(LIBNET_PRu16); 52 memcpy(buf, (u_char *)ip, LIBNET_IPV4_H); 53 53 54 54 tcp->th_off = 5; 55 55 tcp->th_win = htons(MIN_WIN); 56 memcpy(buf + IP_H, (u_char *)tcp,TCP_H);56 memcpy(buf + LIBNET_IPV4_H, (u_char *)tcp, LIBNET_TCP_H); 57 57 58 libnet_do_checksum( buf, IPPROTO_TCP,TCP_H);58 libnet_do_checksum(l, buf, IPPROTO_TCP, LIBNET_TCP_H); 59 59 60 len = IP_H +TCP_H;60 len = LIBNET_IPV4_H + LIBNET_TCP_H; 61 61 62 if (libnet_write_ ip(sock, buf, len) != len)62 if (libnet_write_raw_ipv4(l, buf, len) != len) 63 63 warn("write"); 64 64 65 65 fprintf(stderr, "%s:%d > %s:%d: . ack %lu win %d\n", 66 libnet_ host_lookup(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport),67 libnet_ host_lookup(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport),66 libnet_addr2name4(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport), 67 libnet_addr2name4(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport), 68 68 ntohl(tcp->th_ack), 1); 69 69 } 70 70 71 71 static void 72 send_icmp_source_quench( int sock, struct libnet_ip_hdr *ip)72 send_icmp_source_quench(libnet_t *l, struct libnet_ipv4_hdr *ip) 73 73 { 74 struct libnet_icmp _hdr *icmp;74 struct libnet_icmpv4_hdr *icmp; 75 75 int len; 76 76 77 77 len = (ip->ip_hl * 4) + 8; 78 78 79 libnet_build_ip(ICMP_ECHO_H + len, 0, libnet_get_prand(PRu16), 80 0, 64, IPPROTO_ICMP, ip->ip_dst.s_addr, 81 ip->ip_src.s_addr, NULL, 0, buf); 82 83 icmp = (struct libnet_icmp_hdr *)(buf + IP_H); 79 icmp = (struct libnet_icmpv4_hdr *)(buf + LIBNET_IPV4_H); 84 80 icmp->icmp_type = ICMP_SOURCEQUENCH; 85 81 icmp->icmp_code = 0; 86 memcpy((u_char *)icmp + ICMP_ECHO_H, (u_char *)ip, len);82 memcpy((u_char *)icmp + LIBNET_ICMPV4_ECHO_H, (u_char *)ip, len); 87 83 88 l ibnet_do_checksum(buf, IPPROTO_ICMP, ICMP_ECHO_H + len);84 len += LIBNET_ICMPV4_ECHO_H; 89 85 90 len += (IP_H + ICMP_ECHO_H); 86 libnet_build_ipv4(LIBNET_IPV4_H + len, 0, 87 libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_ICMP, 88 0, ip->ip_dst.s_addr, ip->ip_src.s_addr, 89 (u_int8_t *) icmp, len, l, 0); 91 90 92 if (libnet_write _ip(sock, buf, len) != len)91 if (libnet_write(l) != len) 93 92 warn("write"); 94 93 95 94 fprintf(stderr, "%s > %s: icmp: source quench\n", 96 libnet_ host_lookup(ip->ip_dst.s_addr, 0),97 libnet_ host_lookup(ip->ip_src.s_addr, 0));95 libnet_addr2name4(ip->ip_dst.s_addr, 0), 96 libnet_addr2name4(ip->ip_src.s_addr, 0)); 98 97 } 99 98 100 99 static void 101 send_icmp_frag_needed( int sock, struct libnet_ip_hdr *ip)100 send_icmp_frag_needed(libnet_t *l, struct libnet_ipv4_hdr *ip) 102 101 { 103 struct libnet_icmp _hdr *icmp;102 struct libnet_icmpv4_hdr *icmp; 104 103 int len; 105 104 106 105 len = (ip->ip_hl * 4) + 8; 107 106 108 libnet_build_ip(ICMP_MASK_H + len, 4, libnet_get_prand(PRu16), 109 0, 64, IPPROTO_ICMP, ip->ip_dst.s_addr, 110 ip->ip_src.s_addr, NULL, 0, buf); 111 112 icmp = (struct libnet_icmp_hdr *)(buf + IP_H); 107 icmp = (struct libnet_icmpv4_hdr *)(buf + LIBNET_IPV4_H); 113 108 icmp->icmp_type = ICMP_UNREACH; 114 109 icmp->icmp_code = ICMP_UNREACH_NEEDFRAG; 115 110 icmp->hun.frag.pad = 0; 116 111 icmp->hun.frag.mtu = htons(MIN_MTU); 117 memcpy((u_char *)icmp + ICMP_MASK_H, (u_char *)ip, len);112 memcpy((u_char *)icmp + LIBNET_ICMPV4_MASK_H, (u_char *)ip, len); 118 113 119 libnet_do_checksum(buf, IPPROTO_ICMP, ICMP_MASK_H + len); 120 121 len += (IP_H + ICMP_MASK_H); 114 len += LIBNET_ICMPV4_MASK_H; 115 116 libnet_build_ipv4(LIBNET_IPV4_H + len, 4, 117 libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_ICMP, 118 0, ip->ip_dst.s_addr, ip->ip_src.s_addr, 119 (u_int8_t *) icmp, len, l, 0); 122 120 123 if (libnet_write _ip(sock, buf, len) != len)121 if (libnet_write(l) != len) 124 122 warn("write"); 125 123 126 124 fprintf(stderr, "%s > %s: icmp: ", 127 libnet_ host_lookup(ip->ip_dst.s_addr, 0),128 libnet_ host_lookup(ip->ip_src.s_addr, 0));125 libnet_addr2name4(ip->ip_dst.s_addr, 0), 126 libnet_addr2name4(ip->ip_src.s_addr, 0)); 129 127 fprintf(stderr, "%s unreachable - need to frag (mtu %d)\n", 130 libnet_ host_lookup(ip->ip_src.s_addr, 0), MIN_MTU);128 libnet_addr2name4(ip->ip_src.s_addr, 0), MIN_MTU); 131 129 } 132 130 133 131 static void 134 132 tcp_nice_cb(u_char *user, const struct pcap_pkthdr *pcap, const u_char *pkt) 135 133 { 136 struct libnet_ip _hdr *ip;134 struct libnet_ipv4_hdr *ip; 137 135 struct libnet_tcp_hdr *tcp; 138 int *sock, len; 136 int len; 137 libnet_t *l; 139 138 140 sock = (int *)user;139 l = (libnet_t *)user; 141 140 pkt += pcap_off; 142 141 len = pcap->caplen - pcap_off; 143 142 144 ip = (struct libnet_ip _hdr *)pkt;143 ip = (struct libnet_ipv4_hdr *)pkt; 145 144 if (ip->ip_p != IPPROTO_TCP) 146 145 return; 147 146 … … 151 150 152 151 if (ntohs(ip->ip_len) > (ip->ip_hl << 2) + (tcp->th_off << 2)) { 153 152 if (Opt_icmp) 154 send_icmp_source_quench( *sock, ip);153 send_icmp_source_quench(l, ip); 155 154 if (Opt_win) 156 send_tcp_window_advertisement( *sock, ip, tcp);155 send_tcp_window_advertisement(l, ip, tcp); 157 156 if (Opt_pmtu) 158 send_icmp_frag_needed( *sock, ip);157 send_icmp_frag_needed(l, ip); 159 158 } 160 159 } 161 160 … … 164 163 { 165 164 extern char *optarg; 166 165 extern int optind; 167 int c , sock;166 int c; 168 167 char *intf, *filter, ebuf[PCAP_ERRBUF_SIZE]; 168 char libnet_ebuf[LIBNET_ERRBUF_SIZE]; 169 libnet_t *l; 169 170 pcap_t *pd; 170 171 171 172 intf = NULL; … … 209 210 if ((pcap_off = pcap_dloff(pd)) < 0) 210 211 errx(1, "couldn't determine link layer offset"); 211 212 212 if (( sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1)213 if ((l = libnet_init(LIBNET_RAW4, intf, libnet_ebuf)) == NULL) 213 214 errx(1, "couldn't initialize sending"); 214 215 215 libnet_seed_prand( );216 libnet_seed_prand(l); 216 217 217 218 warnx("listening on %s [%s]", intf, filter); 218 219 219 pcap_loop(pd, -1, tcp_nice_cb, (u_char *) &sock);220 pcap_loop(pd, -1, tcp_nice_cb, (u_char *)l); 220 221 221 222 /* NOTREACHED */ 222 223