#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/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).
struct ifreq ibuf[(32 * 1024) / sizeof(struct ifreq)], ifr;
struct IFADDRLIST *st_ifaddrlist;
- fd = bb_xsocket(AF_INET, SOCK_DGRAM, 0);
+ fd = xsocket(AF_INET, SOCK_DGRAM, 0);
ifc.ifc_len = sizeof(ibuf);
ifc.ifc_buf = (caddr_t)ibuf;
ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
nipaddr = 1 + (ifc.ifc_len / sizeof(struct ifreq));
- st_ifaddrlist = xcalloc(nipaddr, sizeof(struct IFADDRLIST));
+ st_ifaddrlist = xzalloc(nipaddr * sizeof(struct IFADDRLIST));
al = st_ifaddrlist;
nipaddr = 0;
struct IFADDRLIST *al;
char buf[256], tdevice[256], device[256];
- f = bb_xfopen(route, "r");
+ f = xfopen(route, "r");
/* Find the appropriate interface */
n = 0;
} else
val = (int)strtol(str, &ep, 10);
if (*ep != '\0') {
- bb_error_msg_and_die("\"%s\" bad value for %s \n", str, what);
+ 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\n", what, mi);
+ bb_error_msg_and_die("%s must be >= %d", what, mi);
else
- bb_error_msg_and_die("%s must be > %d\n", what, mi - 1);
+ 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\n", what, ma);
+ bb_error_msg_and_die("%s must be <= %d", what, ma);
return val;
}
wait_for_reply(int sock, struct sockaddr_in *fromp, const struct timeval *tp)
{
fd_set fds;
- struct timeval now, wait;
+ struct timeval now, tvwait;
struct timezone tz;
int cc = 0;
socklen_t fromlen = sizeof(*fromp);
FD_ZERO(&fds);
FD_SET(sock, &fds);
- wait.tv_sec = tp->tv_sec + waittime;
- wait.tv_usec = tp->tv_usec;
+ tvwait.tv_sec = tp->tv_sec + waittime;
+ tvwait.tv_usec = tp->tv_usec;
(void)gettimeofday(&now, &tz);
- tvsub(&wait, &now);
+ tvsub(&tvwait, &now);
- if (select(sock + 1, &fds, NULL, NULL, &wait) > 0)
+ if (select(sock + 1, &fds, NULL, NULL, &tvwait) > 0)
cc = recvfrom(sock, (char *)packet, sizeof(packet), 0,
(struct sockaddr *)fromp, &fromlen);
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 **p;
u_int32_t addr, *ap;
- hi = xcalloc(1, sizeof(*hi));
+ hi = xzalloc(sizeof(*hi));
addr = inet_addr(host);
if ((int32_t)addr != -1) {
- hi->name = bb_xstrdup(host);
+ hi->name = xstrdup(host);
hi->n = 1;
- hi->addrs = xcalloc(1, sizeof(hi->addrs[0]));
+ hi->addrs = xzalloc(sizeof(hi->addrs[0]));
hi->addrs[0] = addr;
return hi;
}
hp = xgethostbyname(host);
if (hp->h_addrtype != AF_INET || hp->h_length != 4)
bb_perror_msg_and_die("bad host %s", host);
- hi->name = bb_xstrdup(hp->h_name);
+ hi->name = xstrdup(hp->h_name);
for (n = 0, p = hp->h_addr_list; *p != NULL; ++n, ++p)
continue;
hi->n = n;
- hi->addrs = xcalloc(n, sizeof(hi->addrs[0]));
+ hi->addrs = xzalloc(n * sizeof(hi->addrs[0]));
for (ap = hi->addrs, p = hp->h_addr_list; *p != NULL; ++ap, ++p)
memcpy(ap, *p, sizeof(*ap));
return hi;
traceroute_main(int argc, char *argv[])
{
int code, n;
- char *cp;
unsigned char *outp;
u_int32_t *ap;
struct sockaddr_in *from = (struct sockaddr_in *)&wherefrom;
struct sockaddr_in *to = (struct sockaddr_in *)&whereto;
struct hostinfo *hi;
int on = 1;
- struct protoent *pe;
int ttl, probe, i;
int seq = 0;
int tos = 0;
#endif
u_short off = 0;
struct IFADDRLIST *al;
- int uid = getuid();
char *device = NULL;
int max_ttl = 30;
char *max_ttl_str = NULL;
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 */
if(port_str)
port = (u_short)str2val(port_str, "port", 1, (1 << 16) - 1);
if(nprobes_str)
- nprobes = str2val(optarg, "nprobes", 1, -1);
+ 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 (uid)
- bb_error_msg_and_die("-s %s: Permission denied", 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);
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;
bb_show_usage();
}
- cp = "icmp";
- if ((pe = getprotobyname(cp)) == NULL)
- bb_perror_msg_and_die("unknown protocol %s", cp);
-
/* Insure the socket fds won't be 0, 1 or 2 */
- do n = bb_xopen(bb_dev_null, O_RDONLY); while (n < 2);
+ do n = xopen(bb_dev_null, O_RDONLY); while (n < 2);
if (n > 2)
close(n);
- s = bb_xsocket(AF_INET, SOCK_RAW, pe->p_proto);
+ s = xsocket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
#ifdef CONFIG_FEATURE_TRACEROUTE_SO_DEBUG
if (op & USAGE_OP_DEBUG)
(void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&on,
sizeof(on));
- sndsock = bb_xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+ sndsock = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
#ifdef CONFIG_FEATURE_TRACEROUTE_SOURCE_ROUTE
#if defined(IP_OPTIONS)
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 */
sizeof(on));
/* Revert to non-privileged user after opening sockets */
- setgid(getgid());
- setuid(uid);
+ xsetgid(getgid());
+ xsetuid(getuid());
- outip = (struct ip *)xcalloc(1, (unsigned)packlen);
+ outip = (struct ip *)xzalloc(packlen);
outip->ip_v = IPVERSION;
if (tos_str)
outip->ip_src = from->sin_addr;
#ifndef IP_HDRINCL
- bb_xbind(sndsock, (struct sockaddr *)from, sizeof(*from));
+ xbind(sndsock, (struct sockaddr *)from, sizeof(*from));
#endif
fprintf(stderr, "traceroute to %s (%s)", hostname, inet_ntoa(to->sin_addr));