* Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
* (derived from FvK's 'route.c 1.70 01/04/94')
*
* Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
* (derived from FvK's 'route.c 1.70 01/04/94')
*
- * This program is free software; you can redistribute it
- * and/or modify it under the terms of the GNU General
- * Public License as published by the Free Software
- * Foundation; either version 2 of the License, or (at
- * your option) any later version.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
*
* displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
* adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
*
* displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
* adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
#ifndef RTF_UP
/* Keep this in sync with /usr/src/linux/include/linux/route.h */
#define RTF_UP 0x0001 /* route usable */
#ifndef RTF_UP
/* Keep this in sync with /usr/src/linux/include/linux/route.h */
#define RTF_UP 0x0001 /* route usable */
/* For the various tbl_*[] arrays, the 1st byte is the offset to
* the next entry and the 2nd byte is return value. */
/* For the various tbl_*[] arrays, the 1st byte is the offset to
* the next entry and the 2nd byte is return value. */
-#define KW_IPVx_METRIC 020
-#define KW_IPVx_NETMASK 021
-#define KW_IPVx_GATEWAY 022
-#define KW_IPVx_MSS 023
-#define KW_IPVx_WINDOW 024
-#define KW_IPVx_IRTT 025
-#define KW_IPVx_DEVICE 026
+#define KW_IPVx_METRIC 020
+#define KW_IPVx_NETMASK 021
+#define KW_IPVx_GATEWAY 022
+#define KW_IPVx_MSS 023
+#define KW_IPVx_WINDOW 024
+#define KW_IPVx_IRTT 025
+#define KW_IPVx_DEVICE 026
-#define KW_IPVx_FLAG_ONLY 040
-#define KW_IPVx_REJECT 040
-#define KW_IPVx_MOD 041
-#define KW_IPVx_DYN 042
-#define KW_IPVx_REINSTATE 043
+#define KW_IPVx_FLAG_ONLY 040
+#define KW_IPVx_REJECT 040
+#define KW_IPVx_MOD 041
+#define KW_IPVx_DYN 042
+#define KW_IPVx_REINSTATE 043
/* Grab the -net or -host options. Remember they were transformed. */
xflag = kw_lookup(tbl_hash_net_host, &args);
/* Grab the -net or -host options. Remember they were transformed. */
xflag = kw_lookup(tbl_hash_net_host, &args);
+ prefix_len = xatoul_range(prefix+1, 0, 32);
+ mask_in_addr(rt) = htonl( ~ (0xffffffffUL >> prefix_len));
+ *prefix = '\0';
+#if HAVE_NEW_ADDRT
+ rt.rt_genmask.sa_family = AF_INET;
+#endif
+ } else {
+ /* Default netmask. */
+ netmask = bb_str_default;
+ }
while (*args) {
int k = kw_lookup(tbl_ipvx, &args);
const char *args_m1 = args[-1];
while (*args) {
int k = kw_lookup(tbl_ipvx, &args);
const char *args_m1 = args[-1];
rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */
#if 0 /* FIXME: do we need to check anything of this? */
if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */
#if 0 /* FIXME: do we need to check anything of this? */
if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
/* Device is special in that it can be the last arg specified
* and doesn't requre the dev/device keyword in that case. */
if (!rt.rt_dev && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
/* Device is special in that it can be the last arg specified
* and doesn't requre the dev/device keyword in that case. */
if (!rt.rt_dev && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
- if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- bb_perror_msg_and_die("socket");
- }
+ skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
- if (ioctl(skfd, ((action==RTACTION_ADD) ? SIOCADDRT : SIOCDELRT), &rt)<0) {
- bb_perror_msg_and_die("SIOC[ADD|DEL]RT");
- }
+ if (action == RTACTION_ADD)
+ xioctl(skfd, SIOCADDRT, &rt);
+ else
+ xioctl(skfd, SIOCDELRT, &rt);
- if ((cp = strchr(target, '/'))) { /* Yes... const to non is ok. */
- *cp = 0;
- prefix_len = bb_xgetularg10_bnd(cp+1, 0, 128);
+ cp = strchr(target, '/'); /* Yes... const to non is ok. */
+ if (cp) {
+ *cp = '\0';
+ prefix_len = xatoul_range(cp + 1, 0, 128);
/* Device is special in that it can be the last arg specified
* and doesn't requre the dev/device keyword in that case. */
if (!devname && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
/* Device is special in that it can be the last arg specified
* and doesn't requre the dev/device keyword in that case. */
if (!devname && ((k == KW_IPVx_DEVICE) || (!k && !*++args))) {
- if ((skfd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
- bb_perror_msg_and_die("socket");
- }
+ skfd = xsocket(AF_INET6, SOCK_DGRAM, 0);
- strcpy(ifr.ifr_name, devname);
-
- if (ioctl(skfd, SIOGIFINDEX, &ifr) < 0) {
- bb_perror_msg_and_die("SIOGIFINDEX");
- }
+ strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name));
+ xioctl(skfd, SIOGIFINDEX, &ifr);
- if (ioctl(skfd, ((action==RTACTION_ADD) ? SIOCADDRT : SIOCDELRT), &rt)<0) {
- bb_perror_msg_and_die("SIOC[ADD|DEL]RT");
- }
+ if (action == RTACTION_ADD)
+ xioctl(skfd, SIOCADDRT, &rt);
+ else
+ xioctl(skfd, SIOCDELRT, &rt);
#define IPV4_MASK (RTF_GATEWAY|RTF_HOST|RTF_REINSTATE|RTF_DYNAMIC|RTF_MODIFIED)
#define IPV6_MASK (RTF_GATEWAY|RTF_HOST|RTF_DEFAULT|RTF_ADDRCONF|RTF_CACHE)
#define IPV4_MASK (RTF_GATEWAY|RTF_HOST|RTF_REINSTATE|RTF_DYNAMIC|RTF_MODIFIED)
#define IPV6_MASK (RTF_GATEWAY|RTF_HOST|RTF_DEFAULT|RTF_ADDRCONF|RTF_CACHE)
- char devname[64], flags[16], sdest[16], sgw[16];
- unsigned long int d, g, m;
+ char devname[64], flags[16], *sdest, *sgw;
+ unsigned long d, g, m;
int flgs, ref, use, metric, mtu, win, ir;
struct sockaddr_in s_addr;
struct in_addr mask;
int flgs, ref, use, metric, mtu, win, ir;
struct sockaddr_in s_addr;
struct in_addr mask;
- bb_printf("Kernel IP routing table\n"
- "Destination Gateway Genmask"
- " Flags %s Iface\n",
- netstatfmt ? " MSS Window irtt" : "Metric Ref Use");
+ printf("Kernel IP routing table\n"
+ "Destination Gateway Genmask Flags %s Iface\n",
+ netstatfmt ? " MSS Window irtt" : "Metric Ref Use");
if (fscanf(fp, "%*[^\n]\n") < 0) { /* Skip the first line. */
goto ERROR; /* Empty or missing line, or read error. */
if (fscanf(fp, "%*[^\n]\n") < 0) { /* Skip the first line. */
goto ERROR; /* Empty or missing line, or read error. */
memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = d;
memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_addr.sin_family = AF_INET;
s_addr.sin_addr.s_addr = d;
- INET_rresolve(sdest, sizeof(sdest), &s_addr,
- (noresolve | 0x8000), m); /* Default instead of *. */
-
+ sdest = INET_rresolve(&s_addr, (noresolve | 0x8000), m); /* 'default' instead of '*' */
- INET_rresolve(sgw, sizeof(sgw), &s_addr,
- (noresolve | 0x4000), m); /* Host instead of net. */
-
+ sgw = INET_rresolve(&s_addr, (noresolve | 0x4000), m); /* Host instead of net */
- bb_printf("%-16s%-16s%-16s%-6s", sdest, sgw, inet_ntoa(mask), flags);
+ /* "%15.15s" truncates hostnames, do we really want that? */
+ printf("%-15.15s %-15.15s %-16s%-6s", sdest, sgw, inet_ntoa(mask), flags);
+ free(sdest);
+ free(sgw);
- bb_printf("%5d %-5d %6d %s\n", mtu, win, ir, devname);
+ printf("%5d %-5d %6d %s\n", mtu, win, ir, devname);
- bb_printf("%-6d %-2d %7d %s\n", metric, ref, use, devname);
+ printf("%-6d %-2d %7d %s\n", metric, ref, use, devname);
/* In addr6x, we store both 40-byte ':'-delimited ipv6 addresses.
* We read the non-delimited strings into the tail of the buffer
* using fscanf and then modify the buffer by shifting forward
/* In addr6x, we store both 40-byte ':'-delimited ipv6 addresses.
* We read the non-delimited strings into the tail of the buffer
* using fscanf and then modify the buffer by shifting forward
int iflags, metric, refcnt, use, prefix_len, slen;
struct sockaddr_in6 snaddr6;
int iflags, metric, refcnt, use, prefix_len, slen;
struct sockaddr_in6 snaddr6;
"Flags Metric Ref Use Iface\n",
"Destination", "Next Hop");
"Flags Metric Ref Use Iface\n",
"Destination", "Next Hop");
inet_pton(AF_INET6, addr6x + r,
(struct sockaddr *) &snaddr6.sin6_addr);
snaddr6.sin6_family = AF_INET6;
inet_pton(AF_INET6, addr6x + r,
(struct sockaddr *) &snaddr6.sin6_addr);
snaddr6.sin6_family = AF_INET6;
- INET6_rresolve(naddr6, sizeof(naddr6),
- (struct sockaddr_in6 *) &snaddr6,
-#if 0
- (noresolve | 0x8000) /* Default instead of *. */
-#else
+ naddr6 = INET6_rresolve((struct sockaddr_in6 *) &snaddr6,
);
if (!r) { /* 1st pass */
snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len);
r += 40;
);
if (!r) { /* 1st pass */
snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len);
r += 40;
- bb_printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n",
- addr6, naddr6, flags, metric, refcnt, use, iface);
+ printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n",
+ addr6, naddr6, flags, metric, refcnt, use, iface);
+ free(naddr6);
-#define ROUTE_OPT_A 0x01
-#define ROUTE_OPT_n 0x02
-#define ROUTE_OPT_e 0x04
-#define ROUTE_OPT_INET6 0x08 /* Not an actual option. See below. */
+#define ROUTE_OPT_A 0x01
+#define ROUTE_OPT_n 0x02
+#define ROUTE_OPT_e 0x04
+#define ROUTE_OPT_INET6 0x08 /* Not an actual option. See below. */
-/* "\011\002delete\0" */
- "\010\002delete" /* Since last, we can save a byte. */
+/* "\011\002delete\0" */
+ "\010\002delete" /* Since it's last, we can save a byte. */
-int route_main(int argc, char **argv)
+int route_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int route_main(int argc ATTRIBUTE_UNUSED, char **argv)
if (strcmp(family, "inet6") == 0) {
opt |= ROUTE_OPT_INET6; /* Set flag for ipv6. */
} else
if (strcmp(family, "inet6") == 0) {
opt |= ROUTE_OPT_INET6; /* Set flag for ipv6. */
} else
/* No more args means display the routing table. */
if (!*argv) {
int noresolve = (opt & ROUTE_OPT_n) ? 0x0fff : 0;
/* No more args means display the routing table. */
if (!*argv) {
int noresolve = (opt & ROUTE_OPT_n) ? 0x0fff : 0;
}
/* Check verb. At the moment, must be add, del, or delete. */
}
/* Check verb. At the moment, must be add, del, or delete. */
if (opt & ROUTE_OPT_INET6)
INET6_setroute(what, argv);
else
if (opt & ROUTE_OPT_INET6)
INET6_setroute(what, argv);
else