traceroute: code shrink

Move init code to a separate function.

function                                             old     new   delta
traceroute_init                                        -    1203   +1203
common_traceroute_main                              3391    2141   -1250
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 1203/-1250)        Total: -47 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2020-12-12 18:05:09 +01:00
parent 70726640b3
commit cc55bbef75

View File

@ -394,8 +394,12 @@ struct globals {
int packlen; /* total length of packet */ int packlen; /* total length of packet */
int pmtu; /* Path MTU Discovery (RFC1191) */ int pmtu; /* Path MTU Discovery (RFC1191) */
uint32_t ident; uint32_t ident;
uint16_t port; // 33434; /* start udp dest port # for probe packets */ uint16_t port; /* start udp dest port # for probe packets */
int waittime; // 5; /* time to wait for response (in seconds) */ int waittime; /* time to wait for response (in seconds) */
int first_ttl;
int nprobes;
unsigned pausemsecs;
int max_ttl;
unsigned char recv_pkt[512]; /* last inbound (icmp) packet */ unsigned char recv_pkt[512]; /* last inbound (icmp) packet */
}; };
@ -412,8 +416,6 @@ struct globals {
#define gwlist (G.gwlist ) #define gwlist (G.gwlist )
#define INIT_G() do { \ #define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
port = 33434; \
waittime = 5; \
} while (0) } while (0)
#define outudp ((struct udphdr *)(outip + 1)) #define outudp ((struct udphdr *)(outip + 1))
@ -862,22 +864,16 @@ print_delta_ms(unsigned t1p, unsigned t2p)
printf(" %u.%03u ms", tt / 1000, tt % 1000); printf(" %u.%03u ms", tt / 1000, tt % 1000);
} }
/* /* Keeping init code in a separate (not inlined!) function
* Usage: [-dFIlnrvx] [-g gateway] [-i iface] [-f first_ttl] * for stack use reduction and better register allocation in main loop.
* [-m max_ttl] [ -p port] [-q nqueries] [-s src_addr] [-t tos]
* [-w waittime] [-z pausemsecs] host [packetlen]"
*/ */
static int static NOINLINE void
common_traceroute_main(int op, char **argv) traceroute_init(int op, char **argv)
{ {
int minpacket; int minpacket;
#ifdef IP_TOS #ifdef IP_TOS
int tos = 0; int tos = 0;
#endif #endif
int max_ttl = 30;
int nprobes = 3;
int first_ttl = 1;
unsigned pausemsecs = 0;
char *source; char *source;
char *device; char *device;
char *tos_str; char *tos_str;
@ -893,14 +889,16 @@ common_traceroute_main(int op, char **argv)
#else #else
enum { af = AF_INET }; enum { af = AF_INET };
#endif #endif
int ttl;
int seq;
struct sockaddr *lastaddr;
/* Ensure the socket fds won't be 0, 1 or 2 */ /* Ensure the socket fds won't be 0, 1 or 2 */
bb_sanitize_stdio(); bb_sanitize_stdio();
INIT_G(); INIT_G();
port = 33434;
waittime = 5;
G.first_ttl = 1;
G.nprobes = 3;
G.max_ttl = 30;
op |= getopt32(argv, "^" op |= getopt32(argv, "^"
OPT_STRING OPT_STRING
@ -919,17 +917,17 @@ common_traceroute_main(int op, char **argv)
tos = xatou_range(tos_str, 0, 255); tos = xatou_range(tos_str, 0, 255);
#endif #endif
if (op & OPT_MAX_TTL) if (op & OPT_MAX_TTL)
max_ttl = xatou_range(max_ttl_str, 1, 255); G.max_ttl = xatou_range(max_ttl_str, 1, 255);
if (op & OPT_PORT) if (op & OPT_PORT)
port = xatou16(port_str); port = xatou16(port_str);
if (op & OPT_NPROBES) if (op & OPT_NPROBES)
nprobes = xatou_range(nprobes_str, 1, INT_MAX); G.nprobes = xatou_range(nprobes_str, 1, INT_MAX);
if (op & OPT_WAITTIME) if (op & OPT_WAITTIME)
waittime = xatou_range(waittime_str, 1, 24 * 60 * 60); waittime = xatou_range(waittime_str, 1, 24 * 60 * 60);
if (op & OPT_PAUSE_MS) if (op & OPT_PAUSE_MS)
pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000); G.pausemsecs = xatou_range(pausemsecs_str, 0, 60 * 60 * 1000);
if (op & OPT_FIRST_TTL) if (op & OPT_FIRST_TTL)
first_ttl = xatou_range(first_ttl_str, 1, max_ttl); G.first_ttl = xatou_range(first_ttl_str, 1, G.max_ttl);
/* Process destination and optional packet size */ /* Process destination and optional packet size */
minpacket = sizeof(struct ip) minpacket = sizeof(struct ip)
@ -961,6 +959,9 @@ common_traceroute_main(int op, char **argv)
#else #else
dest_lsa = xhost2sockaddr(argv[0], port); dest_lsa = xhost2sockaddr(argv[0], port);
#endif #endif
G.from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len);
G.to = xzalloc(dest_lsa->len);
packlen = minpacket; packlen = minpacket;
if (argv[1]) if (argv[1])
packlen = xatoul_range(argv[1], minpacket, 32 * 1024); packlen = xatoul_range(argv[1], minpacket, 32 * 1024);
@ -1102,20 +1103,28 @@ common_traceroute_main(int op, char **argv)
if (op & OPT_SOURCE) if (op & OPT_SOURCE)
printf(" from %s", source); printf(" from %s", source);
printf(", %d hops max, %d byte packets\n", max_ttl, packlen); printf(", %d hops max, %d byte packets\n", G.max_ttl, packlen);
}
static int
common_traceroute_main(int op, char **argv)
{
int ttl;
int seq;
struct sockaddr *lastaddr;
traceroute_init(op, argv);
lastaddr = xzalloc(dest_lsa->len); lastaddr = xzalloc(dest_lsa->len);
G.from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len);
G.to = xzalloc(dest_lsa->len);
seq = 0; seq = 0;
for (ttl = first_ttl; ttl <= 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 gotlastaddr = 0; /* flags */
int got_there = 0; int got_there = 0;
printf("%2d", ttl); printf("%2d", ttl);
for (probe = 0; probe < nprobes; ++probe) { for (probe = 0; probe < G.nprobes; ++probe) {
int read_len; int read_len;
unsigned t1; unsigned t1;
unsigned t2; unsigned t2;
@ -1123,14 +1132,15 @@ common_traceroute_main(int op, char **argv)
fflush_all(); fflush_all();
if (probe != 0) if (probe != 0)
msleep(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;
/* NB: wait_for_reply() fills "G.from_lsa" and "G.to" /* NB: wait_for_reply() fills "G.from_lsa" and "G.to" with
* with "where it came from" and "to which local address it arrived". * "where it came from" and "what local address it arrived to"
* addresses.
*/ */
while ((read_len = wait_for_reply(&t2, &left_ms)) != 0) { while ((read_len = wait_for_reply(&t2, &left_ms)) != 0) {
int icmp_code; int icmp_code;
@ -1255,7 +1265,7 @@ common_traceroute_main(int op, char **argv)
bb_putchar('\n'); bb_putchar('\n');
if (got_there if (got_there
|| (unreachable > 0 && unreachable >= nprobes - 1) || (unreachable > 0 && unreachable >= G.nprobes - 1)
) { ) {
break; break;
} }