Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/networking/traceroute.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/networking/traceroute.c
r2725 r3232 211 211 */ 212 212 213 //usage:#define traceroute_trivial_usage 214 //usage: "[-"IF_TRACEROUTE6("46")"FIldnrv] [-f 1ST_TTL] [-m MAXTTL] [-p PORT] [-q PROBES]\n" 215 //usage: " [-s SRC_IP] [-t TOS] [-w WAIT_SEC] [-g GATEWAY] [-i IFACE]\n" 216 //usage: " [-z PAUSE_MSEC] HOST [BYTES]" 217 //usage:#define traceroute_full_usage "\n\n" 218 //usage: "Trace the route to HOST\n" 219 //usage: IF_TRACEROUTE6( 220 //usage: "\n -4,-6 Force IP or IPv6 name resolution" 221 //usage: ) 222 //usage: "\n -F Set the don't fragment bit" 223 //usage: "\n -I Use ICMP ECHO instead of UDP datagrams" 224 //usage: "\n -l Display the TTL value of the returned packet" 225 //usage: "\n -d Set SO_DEBUG options to socket" 226 //usage: "\n -n Print numeric addresses" 227 //usage: "\n -r Bypass routing tables, send directly to HOST" 228 //usage: "\n -v Verbose" 229 //usage: "\n -m Max time-to-live (max number of hops)" 230 //usage: "\n -p Base UDP port number used in probes" 231 //usage: "\n (default 33434)" 232 //usage: "\n -q Number of probes per TTL (default 3)" 233 //usage: "\n -s IP address to use as the source address" 234 //usage: "\n -t Type-of-service in probe packets (default 0)" 235 //usage: "\n -w Time in seconds to wait for a response (default 3)" 236 //usage: "\n -g Loose source route gateway (8 max)" 237 //usage: 238 //usage:#define traceroute6_trivial_usage 239 //usage: "[-dnrv] [-m MAXTTL] [-p PORT] [-q PROBES]\n" 240 //usage: " [-s SRC_IP] [-t TOS] [-w WAIT_SEC] [-i IFACE]\n" 241 //usage: " HOST [BYTES]" 242 //usage:#define traceroute6_full_usage "\n\n" 243 //usage: "Trace the route to HOST\n" 244 //usage: "\n -d Set SO_DEBUG options to socket" 245 //usage: "\n -n Print numeric addresses" 246 //usage: "\n -r Bypass routing tables, send directly to HOST" 247 //usage: "\n -v Verbose" 248 //usage: "\n -m Max time-to-live (max number of hops)" 249 //usage: "\n -p Base UDP port number used in probes" 250 //usage: "\n (default is 33434)" 251 //usage: "\n -q Number of probes per TTL (default 3)" 252 //usage: "\n -s IP address to use as the source address" 253 //usage: "\n -t Type-of-service in probe packets (default 0)" 254 //usage: "\n -w Time in seconds to wait for a response (default 3)" 255 213 256 #define TRACEROUTE_SO_DEBUG 0 214 257 … … 248 291 249 292 250 #define OPT_STRING "FIlnrdvxt:i:m:p:q:s:w:z:f:" \ 251 IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:") \ 252 "4" IF_TRACEROUTE6("6") 293 #define OPT_STRING \ 294 "FIlnrdvxt:i:m:p:q:s:w:z:f:" \ 295 IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:") \ 296 "4" IF_TRACEROUTE6("6") 253 297 enum { 254 298 OPT_DONT_FRAGMNT = (1 << 0), /* F */ … … 354 398 355 399 static int 356 wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to )400 wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to, unsigned *timestamp_us, int *left_ms) 357 401 { 358 402 struct pollfd pfd[1]; … … 361 405 pfd[0].fd = rcvsock; 362 406 pfd[0].events = POLLIN; 363 if (safe_poll(pfd, 1, waittime * 1000) > 0) { 407 if (*left_ms >= 0 && safe_poll(pfd, 1, *left_ms) > 0) { 408 unsigned t; 409 364 410 read_len = recv_from_to(rcvsock, 365 411 recv_pkt, sizeof(recv_pkt), 366 /*flags:*/ 0,412 /*flags:*/ MSG_DONTWAIT, 367 413 &from_lsa->u.sa, to, from_lsa->len); 414 t = monotonic_us(); 415 *left_ms -= (t - *timestamp_us) / 1000; 416 *timestamp_us = t; 368 417 } 369 418 370 419 return read_len; 371 }372 373 /*374 * Checksum routine for Internet Protocol family headers (C Version)375 */376 static uint16_t377 in_cksum(uint16_t *addr, int len)378 {379 int nleft = len;380 uint16_t *w = addr;381 uint16_t answer;382 int sum = 0;383 384 /*385 * Our algorithm is simple, using a 32 bit accumulator (sum),386 * we add sequential 16 bit words to it, and at the end, fold387 * back all the carry bits from the top 16 bits into the lower388 * 16 bits.389 */390 while (nleft > 1) {391 sum += *w++;392 nleft -= 2;393 }394 395 /* mop up an odd byte, if necessary */396 if (nleft == 1)397 sum += *(unsigned char *)w;398 399 /* add back carry outs from top 16 bits to low 16 bits */400 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */401 sum += (sum >> 16); /* add carry */402 answer = ~sum; /* truncate to 16 bits */403 return answer;404 420 } 405 421 … … 430 446 /* Always calculate checksum for icmp packets */ 431 447 outicmp->icmp_cksum = 0; 432 outicmp->icmp_cksum = in _cksum((uint16_t *)outicmp,448 outicmp->icmp_cksum = inet_cksum((uint16_t *)outicmp, 433 449 packlen - (sizeof(*outip) + optlen)); 434 450 if (outicmp->icmp_cksum == 0) … … 483 499 out = outdata; 484 500 len -= sizeof(*outudp); 485 set_nport( dest_lsa, htons(port + seq));501 set_nport(&dest_lsa->u.sa, htons(port + seq)); 486 502 } 487 503 } … … 686 702 687 703 read_len -= sizeof(struct icmp6_hdr); 688 for (i = 0; i < read_len 704 for (i = 0; i < read_len; i++) { 689 705 if (i % 16 == 0) 690 706 printf("%04x:", i); … … 752 768 #endif 753 769 { 754 read_len -= ((struct ip*)recv_pkt)->ip_hl << 2; 770 struct ip *ip4packet = (struct ip*)recv_pkt; 771 read_len -= ip4packet->ip_hl << 2; 755 772 } 756 773 printf(" %d bytes to %s", read_len, ina); … … 774 791 common_traceroute_main(int op, char **argv) 775 792 { 776 int i;777 793 int minpacket; 778 794 int tos = 0; … … 928 944 if (lsrr > 0) { 929 945 unsigned char optlist[MAX_IPOPTLEN]; 946 unsigned size; 930 947 931 948 /* final hop */ … … 937 954 /* loose source route option */ 938 955 optlist[1] = IPOPT_LSRR; 939 i= lsrr * sizeof(gwlist[0]);940 optlist[2] = i+ 3;956 size = lsrr * sizeof(gwlist[0]); 957 optlist[2] = size + 3; 941 958 /* pointer to LSRR addresses */ 942 959 optlist[3] = IPOPT_MINOFF; 943 memcpy(optlist + 4, gwlist, i);960 memcpy(optlist + 4, gwlist, size); 944 961 945 962 if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS, 946 (char *)optlist, i+ sizeof(gwlist[0])) < 0) {963 (char *)optlist, size + sizeof(gwlist[0])) < 0) { 947 964 bb_perror_msg_and_die("IP_OPTIONS"); 948 965 } … … 1018 1035 if (op & OPT_DEVICE) 1019 1036 setsockopt_bindtodevice(probe_fd, device); 1020 set_nport( dest_lsa, htons(1025));1037 set_nport(&dest_lsa->u.sa, htons(1025)); 1021 1038 /* dummy connect. makes kernel pick source IP (and port) */ 1022 1039 xconnect(probe_fd, &dest_lsa->u.sa, dest_lsa->len); 1023 set_nport( dest_lsa, htons(port));1040 set_nport(&dest_lsa->u.sa, htons(port)); 1024 1041 1025 1042 /* read IP and port */ … … 1031 1048 1032 1049 /* bind our sockets to this IP (but not port) */ 1033 set_nport( source_lsa, 0);1050 set_nport(&source_lsa->u.sa, 0); 1034 1051 xbind(sndsock, &source_lsa->u.sa, source_lsa->len); 1035 1052 xbind(rcvsock, &source_lsa->u.sa, source_lsa->len); … … 1058 1075 int gotlastaddr = 0; /* flags */ 1059 1076 int got_there = 0; 1060 int first = 1;1061 1077 1062 1078 printf("%2d", ttl); … … 1065 1081 unsigned t1; 1066 1082 unsigned t2; 1083 int left_ms; 1067 1084 struct ip *ip; 1068 1085 1069 if (!first && pausemsecs > 0) 1086 fflush_all(); 1087 if (probe != 0 && pausemsecs > 0) 1070 1088 usleep(pausemsecs * 1000); 1071 fflush_all(); 1072 1073 t1 = monotonic_us(); 1089 1074 1090 send_probe(++seq, ttl); 1075 1076 first = 0; 1077 while ((read_len = wait_for_reply(from_lsa, to)) != 0) { 1078 t2 = monotonic_us(); 1079 i = packet_ok(read_len, from_lsa, to, seq); 1091 t2 = t1 = monotonic_us(); 1092 1093 left_ms = waittime * 1000; 1094 while ((read_len = wait_for_reply(from_lsa, to, &t2, &left_ms)) != 0) { 1095 int icmp_code; 1096 1097 /* Recv'ed a packet, or read error */ 1098 /* t2 = monotonic_us() - set by wait_for_reply */ 1099 1100 if (read_len < 0) 1101 continue; 1102 icmp_code = packet_ok(read_len, from_lsa, to, seq); 1080 1103 /* Skip short packet */ 1081 if (i == 0)1104 if (icmp_code == 0) 1082 1105 continue; 1083 1106 … … 1098 1121 1099 1122 /* time exceeded in transit */ 1100 if (i == -1)1101 break; 1102 i --;1103 switch (i ) {1123 if (icmp_code == -1) 1124 break; 1125 icmp_code--; 1126 switch (icmp_code) { 1104 1127 #if ENABLE_TRACEROUTE6 1105 1128 case ICMP6_DST_UNREACH_NOPORT << 8: … … 1174 1197 break; 1175 1198 default: 1176 printf(" !<%d>", i );1199 printf(" !<%d>", icmp_code); 1177 1200 ++unreachable; 1178 1201 break; 1179 1202 } 1180 1203 break; 1181 } 1204 } /* while (wait and read a packet) */ 1205 1182 1206 /* there was no packet at all? */ 1183 1207 if (read_len == 0) 1184 1208 printf(" *"); 1185 } 1209 } /* for (nprobes) */ 1210 1186 1211 bb_putchar('\n'); 1187 1212 if (got_there
Note:
See TracChangeset
for help on using the changeset viewer.