Ignore:
Timestamp:
Jan 1, 2014, 12:47:38 AM (10 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.21.1
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.2/mindi-busybox/networking/traceroute.c

    r2725 r3232  
    211211 */
    212212
     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
    213256#define TRACEROUTE_SO_DEBUG 0
    214257
     
    248291
    249292
    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")
    253297enum {
    254298    OPT_DONT_FRAGMNT = (1 << 0),    /* F */
     
    354398
    355399static int
    356 wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to)
     400wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to, unsigned *timestamp_us, int *left_ms)
    357401{
    358402    struct pollfd pfd[1];
     
    361405    pfd[0].fd = rcvsock;
    362406    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
    364410        read_len = recv_from_to(rcvsock,
    365411                recv_pkt, sizeof(recv_pkt),
    366                 /*flags:*/ 0,
     412                /*flags:*/ MSG_DONTWAIT,
    367413                &from_lsa->u.sa, to, from_lsa->len);
     414        t = monotonic_us();
     415        *left_ms -= (t - *timestamp_us) / 1000;
     416        *timestamp_us = t;
    368417    }
    369418
    370419    return read_len;
    371 }
    372 
    373 /*
    374  * Checksum routine for Internet Protocol family headers (C Version)
    375  */
    376 static uint16_t
    377 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, fold
    387      * back all the carry bits from the top 16 bits into the lower
    388      * 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;
    404420}
    405421
     
    430446            /* Always calculate checksum for icmp packets */
    431447            outicmp->icmp_cksum = 0;
    432             outicmp->icmp_cksum = in_cksum((uint16_t *)outicmp,
     448            outicmp->icmp_cksum = inet_cksum((uint16_t *)outicmp,
    433449                        packlen - (sizeof(*outip) + optlen));
    434450            if (outicmp->icmp_cksum == 0)
     
    483499            out = outdata;
    484500            len -= sizeof(*outudp);
    485             set_nport(dest_lsa, htons(port + seq));
     501            set_nport(&dest_lsa->u.sa, htons(port + seq));
    486502        }
    487503    }
     
    686702
    687703        read_len -= sizeof(struct icmp6_hdr);
    688         for (i = 0; i < read_len ; i++) {
     704        for (i = 0; i < read_len; i++) {
    689705            if (i % 16 == 0)
    690706                printf("%04x:", i);
     
    752768#endif
    753769        {
    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;
    755772        }
    756773        printf(" %d bytes to %s", read_len, ina);
     
    774791common_traceroute_main(int op, char **argv)
    775792{
    776     int i;
    777793    int minpacket;
    778794    int tos = 0;
     
    928944        if (lsrr > 0) {
    929945            unsigned char optlist[MAX_IPOPTLEN];
     946            unsigned size;
    930947
    931948            /* final hop */
     
    937954            /* loose source route option */
    938955            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;
    941958            /* pointer to LSRR addresses */
    942959            optlist[3] = IPOPT_MINOFF;
    943             memcpy(optlist + 4, gwlist, i);
     960            memcpy(optlist + 4, gwlist, size);
    944961
    945962            if (setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS,
    946                     (char *)optlist, i + sizeof(gwlist[0])) < 0) {
     963                    (char *)optlist, size + sizeof(gwlist[0])) < 0) {
    947964                bb_perror_msg_and_die("IP_OPTIONS");
    948965            }
     
    10181035        if (op & OPT_DEVICE)
    10191036            setsockopt_bindtodevice(probe_fd, device);
    1020         set_nport(dest_lsa, htons(1025));
     1037        set_nport(&dest_lsa->u.sa, htons(1025));
    10211038        /* dummy connect. makes kernel pick source IP (and port) */
    10221039        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));
    10241041
    10251042        /* read IP and port */
     
    10311048
    10321049        /* bind our sockets to this IP (but not port) */
    1033         set_nport(source_lsa, 0);
     1050        set_nport(&source_lsa->u.sa, 0);
    10341051        xbind(sndsock, &source_lsa->u.sa, source_lsa->len);
    10351052        xbind(rcvsock, &source_lsa->u.sa, source_lsa->len);
     
    10581075        int gotlastaddr = 0; /* flags */
    10591076        int got_there = 0;
    1060         int first = 1;
    10611077
    10621078        printf("%2d", ttl);
     
    10651081            unsigned t1;
    10661082            unsigned t2;
     1083            int left_ms;
    10671084            struct ip *ip;
    10681085
    1069             if (!first && pausemsecs > 0)
     1086            fflush_all();
     1087            if (probe != 0 && pausemsecs > 0)
    10701088                usleep(pausemsecs * 1000);
    1071             fflush_all();
    1072 
    1073             t1 = monotonic_us();
     1089
    10741090            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);
    10801103                /* Skip short packet */
    1081                 if (i == 0)
     1104                if (icmp_code == 0)
    10821105                    continue;
    10831106
     
    10981121
    10991122                /* 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) {
    11041127#if ENABLE_TRACEROUTE6
    11051128                case ICMP6_DST_UNREACH_NOPORT << 8:
     
    11741197                    break;
    11751198                default:
    1176                     printf(" !<%d>", i);
     1199                    printf(" !<%d>", icmp_code);
    11771200                    ++unreachable;
    11781201                    break;
    11791202                }
    11801203                break;
    1181             }
     1204            } /* while (wait and read a packet) */
     1205
    11821206            /* there was no packet at all? */
    11831207            if (read_len == 0)
    11841208                printf("  *");
    1185         }
     1209        } /* for (nprobes) */
     1210
    11861211        bb_putchar('\n');
    11871212        if (got_there
Note: See TracChangeset for help on using the changeset viewer.