#undef CONFIG_FEATURE_TRACEROUTE_USE_ICMP
//#define CONFIG_FEATURE_TRACEROUTE_USE_ICMP
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <endian.h>
-#include <getopt.h>
-
-#include <sys/param.h>
-#include <sys/file.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-#include <sys/select.h>
#include "inet_common.h"
#include <net/if.h>
-#include <netinet/in.h>
#include <arpa/inet.h>
-#include <netinet/protocols.h>
+#include <netinet/in.h>
#include <netinet/udp.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
* Definitions for internet protocol version 4.
* Per RFC 791, September 1981.
*/
-#define IPVERSION 4
+#define IPVERSION 4
+
+#ifndef IPPROTO_ICMP
+/* Grrrr.... */
+#define IPPROTO_ICMP 1
+#endif
+#ifndef IPPROTO_IP
+#define IPPROTO_IP 0
+#endif
/*
* Overlay for ip header used by other protocols (tcp, udp).
++al;
++nipaddr;
}
- if(nipaddr == 0)
+ if (nipaddr == 0)
bb_error_msg_and_die ("Can't find any network interfaces");
(void)close(fd);
*/
-/* String to value with optional min and max. Handles decimal and hex. */
-static int
-str2val(const char *str, const char *what, int mi, int ma)
-{
- const char *cp;
- int val;
- char *ep;
-
- if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) {
- cp = str + 2;
- val = (int)strtol(cp, &ep, 16);
- } else
- val = (int)strtol(str, &ep, 10);
- if (*ep != '\0') {
- bb_error_msg_and_die("\"%s\" bad value for %s", str, what);
- }
- if (val < mi && mi >= 0) {
- if (mi == 0)
- bb_error_msg_and_die("%s must be >= %d", what, mi);
- else
- bb_error_msg_and_die("%s must be > %d", what, mi - 1);
- }
- if (val > ma && ma >= 0)
- bb_error_msg_and_die("%s must be <= %d", what, ma);
- return val;
-}
-
-
/*
* Subtract 2 timeval structs: out = out - in.
* Out is assumed to be >= in.
outicmp->icmp_cksum = 0xffff;
} else
#endif
- if (doipcksum) {
+ if (doipcksum) {
/* Checksum (we must save and restore ip header) */
tip = *outip;
ui = (struct udpiphdr *)outip;
if (cc < 0)
bb_perror_msg_and_die("sendto");
printf("%s: wrote %s %d chars, ret=%d\n",
- bb_applet_name, hostname, packlen, cc);
+ applet_name, hostname, packlen, cc);
(void)fflush(stdout);
}
}
return (type == ICMP_TIMXCEED ? -1 : code + 1);
} else
#endif
- {
+ {
up = (struct udphdr *)((unsigned char *)hip + hlen);
/* XXX 8 is a magic number */
if (hlen + 12 <= cc &&
char name[257];
if (!nflag && from->sin_addr.s_addr != INADDR_ANY) {
- if(INET_rresolve(name, sizeof(name), from, 0x4000, 0xffffffff) >= 0)
+ if (INET_rresolve(name, sizeof(name), from, 0x4000, 0xffffffff) >= 0)
n = name;
}
ina = inet_ntoa(from->sin_addr);
opterr = 0;
#ifdef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
- bb_opt_complementally = "x-x:g::";
+ opt_complementary = "x-x:g::";
#else
- bb_opt_complementally = "x-x";
+ opt_complementary = "x-x";
#endif
- op = bb_getopt_ulflags(argc, argv, "FIlnrdvxt:i:m:p:q:s:w:z:f:"
+ op = getopt32(argc, argv, "FIlnrdvxt:i:m:p:q:s:w:z:f:"
#define USAGE_OP_DONT_FRAGMNT (1<<0) /* F */
#define USAGE_OP_USE_ICMP (1<<1) /* I */
#define USAGE_OP_TTL_FLAG (1<<2) /* l */
#endif
);
- if(op & USAGE_OP_DONT_FRAGMNT)
+ if (op & USAGE_OP_DONT_FRAGMNT)
off = IP_DF;
#ifdef CONFIG_FEATURE_TRACEROUTE_USE_ICMP
useicmp = op & USAGE_OP_USE_ICMP;
#ifdef CONFIG_FEATURE_TRACEROUTE_VERBOSE
verbose = op & USAGE_OP_VERBOSE;
#endif
- if(op & USAGE_OP_IP_CHKSUM) {
+ if (op & USAGE_OP_IP_CHKSUM) {
doipcksum = 0;
- bb_error_msg("Warning: ip checksums disabled");
+ bb_error_msg("warning: ip checksums disabled");
}
if (tos_str)
- tos = str2val(tos_str, "tos", 0, 255);
- if(max_ttl_str)
- max_ttl = str2val(max_ttl_str, "max ttl", 1, 255);
- if(port_str)
- port = (u_short)str2val(port_str, "port", 1, (1 << 16) - 1);
- if(nprobes_str)
- nprobes = str2val(nprobes_str, "nprobes", 1, -1);
- if(source) {
- /*
- * set the ip source address of the outbound
- * probe (e.g., on a multi-homed host).
- */
- if (getuid()) bb_error_msg_and_die("-s %s: Permission denied", source);
+ tos = xatoul_range(tos_str, 0, 255);
+ if (max_ttl_str)
+ max_ttl = xatoul_range(max_ttl_str, 1, 255);
+ if (port_str)
+ port = xatou16(port_str);
+ if (nprobes_str)
+ nprobes = xatoul_range(nprobes_str, 1, INT_MAX);
+ if (source) {
+ /*
+ * set the ip source address of the outbound
+ * probe (e.g., on a multi-homed host).
+ */
+ if (getuid()) bb_error_msg_and_die("-s %s: permission denied", source);
}
- if(waittime_str)
- waittime = str2val(waittime_str, "wait time", 2, 24 * 60 * 60);
- if(pausemsecs_str)
- pausemsecs = str2val(pausemsecs_str, "pause msecs", 0, 60 * 60 * 1000);
- if(first_ttl_str)
- first_ttl = str2val(first_ttl_str, "first ttl", 1, 255);
+ if (waittime_str)
+ waittime = xatoul_range(waittime_str, 2, 24 * 60 * 60);
+ if (pausemsecs_str)
+ pausemsecs = xatoul_range(pausemsecs_str, 0, 60 * 60 * 1000);
+ if (first_ttl_str)
+ first_ttl = xatoul_range(first_ttl_str, 1, 255);
#ifdef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
- if(sourse_route_list) {
+ if (sourse_route_list) {
llist_t *l_sr;
for(l_sr = sourse_route_list; l_sr; ) {
if (lsrr >= NGATEWAYS)
- bb_error_msg_and_die("No more than %d gateways", NGATEWAYS);
+ bb_error_msg_and_die("no more than %d gateways", NGATEWAYS);
getaddr(gwlist + lsrr, l_sr->data);
++lsrr;
l_sr = l_sr->link;
switch (argc - optind) {
case 2:
- packlen = str2val(argv[optind + 1],
- "packet length", minpacket, maxpacket);
+ packlen = xatoul_range(argv[optind + 1], minpacket, maxpacket);
/* Fall through */
case 1:
hi = gethostinfo(hostname);
setsin(to, hi->addrs[0]);
if (hi->n > 1)
- bb_error_msg(
- "Warning: %s has multiple addresses; using %s",
+ bb_error_msg("warning: %s has multiple addresses; using %s",
hostname, inet_ntoa(to->sin_addr));
hostname = hi->name;
hi->name = NULL;
if (n > 2)
close(n);
- s = xsocket(AF_INET, SOCK_RAW, IP_ICMP);
+ s = xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
#ifdef CONFIG_FEATURE_TRACEROUTE_SO_DEBUG
if (op & USAGE_OP_DEBUG)
if (lsrr > 0) {
unsigned char optlist[MAX_IPOPTLEN];
- cp = "ip";
- if ((pe = getprotobyname(cp)) == NULL)
- bb_perror_msg_and_die("unknown protocol");
-
/* final hop */
gwlist[lsrr] = to->sin_addr.s_addr;
++lsrr;
optlist[3] = IPOPT_MINOFF;
memcpy(optlist + 4, gwlist, i);
- if ((setsockopt(sndsock, pe->p_proto, IP_OPTIONS,
+ if ((setsockopt(sndsock, IPPROTO_IP, IP_OPTIONS,
(char *)optlist, i + sizeof(gwlist[0]))) < 0) {
bb_perror_msg_and_die("IP_OPTIONS");
- }
+ }
}
#endif /* IP_OPTIONS */
#endif /* CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE */