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),
/*flags:*/ MSG_DONTWAIT,
&G.from_lsa->u.sa, G.to, G.from_lsa->len);
if (read_len < 0)
bb_perror_msg_and_die("recv");
t = monotonic_us();
*left_ms -= (t - *timestamp_us) / 1000;
*timestamp_us = t;
@ -1076,51 +1078,48 @@ common_traceroute_main(int op, char **argv)
for (ttl = G.first_ttl; ttl <= G.max_ttl; ++ttl) {
int probe;
int unreachable = 0; /* counter */
int gotlastaddr = 0; /* flags */
int got_there = 0;
printf("%2d", ttl);
for (probe = 0; probe < G.nprobes; ++probe) {
int read_len;
unsigned t1;
unsigned t2;
int left_ms;
int read_len;
int icmp_code;
fflush_all();
if (probe != 0)
msleep(G.pausemsecs);
send_probe(++seq, ttl);
t2 = t1 = monotonic_us();
t2 = t1 = monotonic_us();
left_ms = waittime * 1000;
for (;;) {
/* NB: wait_for_reply() fills "G.from_lsa" and "G.to" with
* "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) {
int icmp_code;
read_len = wait_for_reply(&t2, &left_ms);
/* Recv'ed a packet, or read error */
/* t2 = monotonic_us() - set by wait_for_reply */
if (read_len < 0)
continue;
if (read_len == 0) { /* there was no packet at all? */
printf(" *");
goto next_probe;
}
icmp_code = packet_ok(read_len, seq);
/* Skip short packet */
if (icmp_code == 0)
continue;
if (icmp_code != 0)
break; /* got a good response */
/* 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)
) {
print(read_len);
memcpy(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len);
gotlastaddr = 1;
}
print_delta_ms(t1, t2);
if (G.from_lsa->u.sa.sa_family == AF_INET) {
if (op & OPT_TTL_FLAG) {
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? */
if (icmp_code == -1)
break;
continue;
icmp_code--;
switch (icmp_code) {
@ -1211,12 +1210,7 @@ common_traceroute_main(int op, char **argv)
++unreachable;
break;
}
break;
} /* while (wait and read a packet) */
/* there was no packet at all? */
if (read_len == 0)
printf(" *");
next_probe: ;
} /* for (nprobes) */
bb_putchar('\n');