traceroute: untangle main loop

function                                             old     new   delta
common_traceroute_main                              1785    1730     -55

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-12-12 21:44:32 +01:00
parent e76f03b267
commit 724c7df683

View File

@ -463,6 +463,8 @@ wait_for_reply(unsigned *timestamp_us, int *left_ms)
recv_pkt, sizeof(recv_pkt), recv_pkt, sizeof(recv_pkt),
/*flags:*/ MSG_DONTWAIT, /*flags:*/ MSG_DONTWAIT,
&G.from_lsa->u.sa, G.to, G.from_lsa->len); &G.from_lsa->u.sa, G.to, G.from_lsa->len);
if (read_len < 0)
bb_perror_msg_and_die("recv");
t = monotonic_us(); t = monotonic_us();
*left_ms -= (t - *timestamp_us) / 1000; *left_ms -= (t - *timestamp_us) / 1000;
*timestamp_us = t; *timestamp_us = t;
@ -1076,51 +1078,48 @@ common_traceroute_main(int op, char **argv)
for (ttl = G.first_ttl; ttl <= G.max_ttl; ++ttl) { for (ttl = G.first_ttl; ttl <= G.max_ttl; ++ttl) {
int probe; int probe;
int unreachable = 0; /* counter */ int unreachable = 0; /* counter */
int gotlastaddr = 0; /* flags */
int got_there = 0; int got_there = 0;
printf("%2d", ttl); printf("%2d", ttl);
for (probe = 0; probe < G.nprobes; ++probe) { for (probe = 0; probe < G.nprobes; ++probe) {
int read_len;
unsigned t1; unsigned t1;
unsigned t2; unsigned t2;
int left_ms; int left_ms;
int read_len;
int icmp_code;
fflush_all(); fflush_all();
if (probe != 0) if (probe != 0)
msleep(G.pausemsecs); msleep(G.pausemsecs);
send_probe(++seq, ttl); send_probe(++seq, ttl);
t2 = t1 = monotonic_us();
t2 = t1 = monotonic_us();
left_ms = waittime * 1000; left_ms = waittime * 1000;
for (;;) {
/* NB: wait_for_reply() fills "G.from_lsa" and "G.to" with /* NB: wait_for_reply() fills "G.from_lsa" and "G.to" with
* "where it came from" and "what local address it arrived to" * "where it came from" and "what local address it arrived to"
* addresses. * addresses. Sets t2 = monotonic_us(), updates left_ms.
*/ */
while ((read_len = wait_for_reply(&t2, &left_ms)) != 0) { read_len = wait_for_reply(&t2, &left_ms);
int icmp_code;
/* Recv'ed a packet, or read error */ if (read_len == 0) { /* there was no packet at all? */
/* t2 = monotonic_us() - set by wait_for_reply */ printf(" *");
goto next_probe;
if (read_len < 0) }
continue;
icmp_code = packet_ok(read_len, seq); icmp_code = packet_ok(read_len, seq);
/* Skip short packet */ if (icmp_code != 0)
if (icmp_code == 0) break; /* got a good response */
continue; /* unrecognized type/code or too short, back to recv */
}
if (!gotlastaddr if (probe == 0
|| (memcmp(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len) != 0) || (memcmp(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len) != 0)
) { ) {
print(read_len); print(read_len);
memcpy(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len); memcpy(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len);
gotlastaddr = 1;
} }
print_delta_ms(t1, t2); print_delta_ms(t1, t2);
if (G.from_lsa->u.sa.sa_family == AF_INET) { if (G.from_lsa->u.sa.sa_family == AF_INET) {
if (op & OPT_TTL_FLAG) { if (op & OPT_TTL_FLAG) {
struct ip *ip = (struct ip *)recv_pkt; struct ip *ip = (struct ip *)recv_pkt;
@ -1130,7 +1129,7 @@ common_traceroute_main(int op, char **argv)
/* Got a "time exceeded in transit" icmp message? */ /* Got a "time exceeded in transit" icmp message? */
if (icmp_code == -1) if (icmp_code == -1)
break; continue;
icmp_code--; icmp_code--;
switch (icmp_code) { switch (icmp_code) {
@ -1211,12 +1210,7 @@ common_traceroute_main(int op, char **argv)
++unreachable; ++unreachable;
break; break;
} }
break; next_probe: ;
} /* while (wait and read a packet) */
/* there was no packet at all? */
if (read_len == 0)
printf(" *");
} /* for (nprobes) */ } /* for (nprobes) */
bb_putchar('\n'); bb_putchar('\n');