slattach: new applet.
[oweals/busybox.git] / networking / route.c
index c6fb5a371510f511078603a487bdb3dbda8e1c01..b5490ddd8d12e08710ea63133a4fa4e66f7be4bd 100644 (file)
@@ -12,7 +12,6 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  *
- * $Id: route.c,v 1.26 2004/03/19 23:27:08 mjn3 Exp $
  *
  * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
  * adjustments by Larry Doolittle  <LRDoolittle@lbl.gov>
  * remove ridiculous amounts of bloat.
  */
 
-#include "busybox.h"
-#include "inet_common.h"
 #include <getopt.h>
 #include <net/route.h>
 #include <net/if.h>
 
+#include "libbb.h"
+#include "inet_common.h"
+
 
 #ifndef RTF_UP
 /* Keep this in sync with /usr/src/linux/include/linux/route.h */
@@ -50,7 +50,7 @@
 #define RTF_REJECT      0x0200 /* Reject route                 */
 #endif
 
-#if defined (SIOCADDRTOLD) || defined (RTF_IRTT)       /* route */
+#if defined(SIOCADDRTOLD) || defined(RTF_IRTT) /* route */
 #define HAVE_NEW_ADDRT 1
 #endif
 
@@ -175,7 +175,7 @@ static void INET_setroute(int action, char **args)
 
                /* recognize x.x.x.x/mask format. */
                prefix = strchr(target, '/');
-               if(prefix) {
+               if (prefix) {
                        int prefix_len;
 
                        prefix_len = xatoul_range(prefix+1, 0, 32);
@@ -194,7 +194,7 @@ static void INET_setroute(int action, char **args)
                if (isnet < 0) {
                        bb_error_msg_and_die("resolving %s", target);
                }
-               if(prefix) {
+               if (prefix) {
                        /* do not destroy prefix for process args */
                        *prefix = '/';
                }
@@ -297,7 +297,7 @@ static void INET_setroute(int action, char **args)
 
 #ifdef RTF_REJECT
        if ((rt.rt_flags & RTF_REJECT) && !rt.rt_dev) {
-               rt.rt_dev = "lo";
+               rt.rt_dev = (char*)"lo";
        }
 #endif
 
@@ -478,8 +478,8 @@ static void set_flags(char *flagstr, int flags)
 /* also used in netstat */
 void bb_displayroutes(int noresolve, int netstatfmt)
 {
-       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;
@@ -520,15 +520,14 @@ void bb_displayroutes(int noresolve, int netstatfmt)
                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 '*' */
                s_addr.sin_addr.s_addr = g;
-               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 */
                mask.s_addr = m;
-               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);
                if (netstatfmt) {
                        printf("%5d %-5d %6d %s\n", mtu, win, ir, devname);
                } else {
@@ -541,7 +540,7 @@ void bb_displayroutes(int noresolve, int netstatfmt)
 
 static void INET6_displayroutes(int noresolve)
 {
-       char addr6[128], naddr6[128];
+       char addr6[128], *naddr6;
        /* 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
@@ -581,7 +580,7 @@ static void INET6_displayroutes(int noresolve)
 
                        do {
                                if (!*p) {
-                                       if (i==40) { /* nul terminator for 1st address? */
+                                       if (i == 40) { /* nul terminator for 1st address? */
                                                addr6x[39] = 0; /* Fixup... need 0 instead of ':'. */
                                                ++p;    /* Skip and continue. */
                                                continue;
@@ -606,18 +605,19 @@ static void INET6_displayroutes(int noresolve)
                        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,
+                       naddr6 = INET6_rresolve((struct sockaddr_in6 *) &snaddr6,
                                                   0x0fff /* Apparently, upstream never resolves. */
                                                   );
 
                        if (!r) {                       /* 1st pass */
                                snprintf(addr6, sizeof(addr6), "%s/%d", naddr6, prefix_len);
                                r += 40;
+                               free(naddr6);
                        } else {                        /* 2nd pass */
                                /* Print the info. */
                                printf("%-43s %-39s %-5s %-6d %-2d %7d %-8s\n",
-                                                 addr6, naddr6, flags, metric, refcnt, use, iface);
+                                               addr6, naddr6, flags, metric, refcnt, use, iface);
+                               free(naddr6);
                                break;
                        }
                } while (1);
@@ -639,6 +639,7 @@ static const char tbl_verb[] =      /* 2nd byte matches RTACTION_* code */
        "\010\002delete"                        /* Since last, we can save a byte. */
 ;
 
+int route_main(int argc, char **argv);
 int route_main(int argc, char **argv)
 {
        unsigned opt;