aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-12-16 17:19:40 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-12-16 17:19:40 +0000
commit9ad2cb3f1a3a8999df1d08422ed7cf01242158a5 (patch)
tree3cbf88e201667880e8b22eb0682bfa534c2ad6ef
parent4ca2965b9901b80dbb538f84b17316873d49362f (diff)
downloadbusybox-w32-9ad2cb3f1a3a8999df1d08422ed7cf01242158a5.tar.gz
busybox-w32-9ad2cb3f1a3a8999df1d08422ed7cf01242158a5.tar.bz2
busybox-w32-9ad2cb3f1a3a8999df1d08422ed7cf01242158a5.zip
traceroute: stop using global data/bss
(add/remove: 0/15 grow/shrink: 0/1 up/down: 0/-95) Total: -95 bytes text data bss dec hex filename 777245 1094 9008 787347 c0393 busybox_old 777206 1084 8976 787266 c0342 busybox_unstripped
-rw-r--r--networking/traceroute.c106
1 files changed, 63 insertions, 43 deletions
diff --git a/networking/traceroute.c b/networking/traceroute.c
index 773fc6a8b..2ba558fc2 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -22,9 +22,6 @@
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 */ 23 */
24 24
25//#define version "1.4a12"
26
27
28/* 25/*
29 * traceroute host - trace the route ip packets follow going to "host". 26 * traceroute host - trace the route ip packets follow going to "host".
30 * 27 *
@@ -272,12 +269,12 @@ struct hostinfo {
272}; 269};
273 270
274/* Data section of the probe packet */ 271/* Data section of the probe packet */
275struct outdata { 272typedef struct outdata {
276 unsigned char seq; /* sequence number of this packet */ 273 unsigned char seq; /* sequence number of this packet */
277 unsigned char ttl; /* ttl packet left with */ 274 unsigned char ttl; /* ttl packet left with */
278// UNUSED. Retaining to have the same packet size. 275// UNUSED. Retaining to have the same packet size.
279 struct timeval tv_UNUSED ATTRIBUTE_PACKED; /* time packet left */ 276 struct timeval tv_UNUSED ATTRIBUTE_PACKED; /* time packet left */
280}; 277} outdata_t;
281 278
282struct IFADDRLIST { 279struct IFADDRLIST {
283 uint32_t addr; 280 uint32_t addr;
@@ -285,37 +282,6 @@ struct IFADDRLIST {
285}; 282};
286 283
287 284
288static struct ip *outip; /* last output (udp) packet */
289static struct udphdr *outudp; /* last output (udp) packet */
290static struct outdata *outdata; /* last output (udp) packet */
291
292#if ENABLE_FEATURE_TRACEROUTE_USE_ICMP
293static struct icmp *outicmp; /* last output (icmp) packet */
294#endif
295
296static int s; /* receive (icmp) socket file descriptor */
297static int sndsock; /* send (udp/icmp) socket file descriptor */
298
299static int packlen; /* total length of packet */
300static int minpacket; /* min ip packet size */
301static int maxpacket = 32 * 1024; /* max ip packet size */
302static int pmtu; /* Path MTU Discovery (RFC1191) */
303
304static char *hostname;
305
306static uint16_t ident;
307static uint16_t port = 32768 + 666; /* start udp dest port # for probe packets */
308
309static int waittime = 5; /* time to wait for response (in seconds) */
310static int doipcksum = 1; /* calculate ip checksums by default */
311
312#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
313static int optlen; /* length of ip options */
314#else
315#define optlen 0
316#endif
317
318
319/* Keep in sync with getopt32 call! */ 285/* Keep in sync with getopt32 call! */
320#define OPT_DONT_FRAGMNT (1<<0) /* F */ 286#define OPT_DONT_FRAGMNT (1<<0) /* F */
321#define OPT_USE_ICMP (1<<1) /* I */ 287#define OPT_USE_ICMP (1<<1) /* I */
@@ -346,6 +312,36 @@ static int optlen; /* length of ip options */
346 312
347 313
348struct globals { 314struct globals {
315 struct ip *outip; /* last output (udp) packet */
316 struct udphdr *outudp; /* last output (udp) packet */
317 struct outdata *outdata; /* last output (udp) packet */
318
319#if ENABLE_FEATURE_TRACEROUTE_USE_ICMP
320 struct icmp *outicmp; /* last output (icmp) packet */
321#endif
322
323 int rcvsock; /* receive (icmp) socket file descriptor */
324 int sndsock; /* send (udp/icmp) socket file descriptor */
325
326 int packlen; /* total length of packet */
327 int minpacket; /* min ip packet size */
328 int maxpacket; // 32 * 1024; /* max ip packet size */
329 int pmtu; /* Path MTU Discovery (RFC1191) */
330
331 char *hostname;
332
333 uint16_t ident;
334 uint16_t port; // 32768 + 666; /* start udp dest port # for probe packets */
335
336 int waittime; // 5; /* time to wait for response (in seconds) */
337 int doipcksum; // 1; /* calculate ip checksums by default */
338
339#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
340 int optlen; /* length of ip options */
341#else
342#define optlen 0
343#endif
344
349 struct sockaddr_storage whereto; /* Who to try to reach */ 345 struct sockaddr_storage whereto; /* Who to try to reach */
350 struct sockaddr_storage wherefrom; /* Who we are */ 346 struct sockaddr_storage wherefrom; /* Who we are */
351 /* last inbound (icmp) packet */ 347 /* last inbound (icmp) packet */
@@ -359,11 +355,35 @@ struct globals {
359}; 355};
360 356
361#define G (*ptr_to_globals) 357#define G (*ptr_to_globals)
362#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G)) 358#define outip (G.outip )
359#define outudp (G.outudp )
360#define outdata (G.outdata )
361#define outicmp (G.outicmp )
362#define rcvsock (G.rcvsock )
363#define sndsock (G.sndsock )
364#define packlen (G.packlen )
365#define minpacket (G.minpacket)
366#define maxpacket (G.maxpacket)
367#define pmtu (G.pmtu )
368#define hostname (G.hostname )
369#define ident (G.ident )
370#define port (G.port )
371#define waittime (G.waittime )
372#define doipcksum (G.doipcksum)
373#if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE
374#define optlen (G.optlen )
375#endif
363#define packet (G.packet ) 376#define packet (G.packet )
364#define whereto (G.whereto ) 377#define whereto (G.whereto )
365#define wherefrom (G.wherefrom) 378#define wherefrom (G.wherefrom)
366#define gwlist (G.gwlist ) 379#define gwlist (G.gwlist )
380#define INIT_G() do { \
381 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
382 maxpacket = 32 * 1024; \
383 port = 32768 + 666; \
384 waittime = 5; \
385 doipcksum = 1; \
386} while (0)
367 387
368 388
369/* 389/*
@@ -1035,15 +1055,15 @@ int traceroute_main(int argc, char **argv)
1035 /* Ensure the socket fds won't be 0, 1 or 2 */ 1055 /* Ensure the socket fds won't be 0, 1 or 2 */
1036 bb_sanitize_stdio(); 1056 bb_sanitize_stdio();
1037 1057
1038 s = xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 1058 rcvsock = xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
1039 1059
1040#if TRACEROUTE_SO_DEBUG 1060#if TRACEROUTE_SO_DEBUG
1041 if (op & OPT_DEBUG) 1061 if (op & OPT_DEBUG)
1042 setsockopt(s, SOL_SOCKET, SO_DEBUG, 1062 setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG,
1043 &const_int_1, sizeof(const_int_1)); 1063 &const_int_1, sizeof(const_int_1));
1044#endif 1064#endif
1045 if (op & OPT_BYPASS_ROUTE) 1065 if (op & OPT_BYPASS_ROUTE)
1046 setsockopt(s, SOL_SOCKET, SO_DONTROUTE, 1066 setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE,
1047 &const_int_1, sizeof(const_int_1)); 1067 &const_int_1, sizeof(const_int_1));
1048 1068
1049 sndsock = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); 1069 sndsock = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
@@ -1124,7 +1144,7 @@ int traceroute_main(int argc, char **argv)
1124 outicmp = (struct icmp *)outp; 1144 outicmp = (struct icmp *)outp;
1125 outicmp->icmp_type = ICMP_ECHO; 1145 outicmp->icmp_type = ICMP_ECHO;
1126 outicmp->icmp_id = htons(ident); 1146 outicmp->icmp_id = htons(ident);
1127 outdata = (struct outdata *)(outp + 8); /* XXX magic number */ 1147 outdata = (outdata_t *)(outp + 8); /* XXX magic number */
1128 } else 1148 } else
1129#endif 1149#endif
1130 { 1150 {
@@ -1132,7 +1152,7 @@ int traceroute_main(int argc, char **argv)
1132 outudp = (struct udphdr *)outp; 1152 outudp = (struct udphdr *)outp;
1133 outudp->source = htons(ident); 1153 outudp->source = htons(ident);
1134 outudp->len = htons((uint16_t)(packlen - (sizeof(*outip) + optlen))); 1154 outudp->len = htons((uint16_t)(packlen - (sizeof(*outip) + optlen)));
1135 outdata = (struct outdata *)(outudp + 1); 1155 outdata = (outdata_t *)(outudp + 1);
1136 } 1156 }
1137 1157
1138 /* Get the interface address list */ 1158 /* Get the interface address list */
@@ -1214,7 +1234,7 @@ int traceroute_main(int argc, char **argv)
1214 t1 = monotonic_us(); 1234 t1 = monotonic_us();
1215 send_probe(++seq, ttl); 1235 send_probe(++seq, ttl);
1216 ++sentfirst; 1236 ++sentfirst;
1217 while ((cc = wait_for_reply(s, from)) != 0) { 1237 while ((cc = wait_for_reply(rcvsock, from)) != 0) {
1218 t2 = monotonic_us(); 1238 t2 = monotonic_us();
1219 i = packet_ok(packet, cc, from, seq); 1239 i = packet_ok(packet, cc, from, seq);
1220 /* Skip short packet */ 1240 /* Skip short packet */