Enable ip commands to be compiled seperate from ip, modifed patch from Bastian Blank
[oweals/busybox.git] / networking / ip.c
1 /*
2  * ip.c         "ip" utility frontend.
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *
12  * Changes:
13  *
14  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <string.h>
25
26 #include "./libiproute/utils.h"
27 #include "./libiproute/ip_common.h"
28
29 #include "busybox.h"
30
31 #if 0
32 int preferred_family = AF_UNSPEC;
33 int oneline = 0;
34 char * _SL_ = NULL;
35
36 void ip_parse_common_args(int *argcp, char ***argvp)
37 {
38         int argc = *argcp;
39         char **argv = *argvp;
40
41         while (argc > 1) {
42                 char *opt = argv[1];
43
44                 if (strcmp(opt,"--") == 0) {
45                         argc--; argv++;
46                         break;
47                 }
48
49                 if (opt[0] != '-')
50                         break;
51
52                 if (opt[1] == '-')
53                         opt++;
54
55                 if (matches(opt, "-family") == 0) {
56                         argc--;
57                         argv++;
58                         if (strcmp(argv[1], "inet") == 0)
59                                 preferred_family = AF_INET;
60                         else if (strcmp(argv[1], "inet6") == 0)
61                                 preferred_family = AF_INET6;
62                         else if (strcmp(argv[1], "link") == 0)
63                                 preferred_family = AF_PACKET;
64                         else
65                                 invarg(argv[1], "invalid protocol family");
66                 } else if (strcmp(opt, "-4") == 0) {
67                         preferred_family = AF_INET;
68                 } else if (strcmp(opt, "-6") == 0) {
69                         preferred_family = AF_INET6;
70                 } else if (strcmp(opt, "-0") == 0) {
71                         preferred_family = AF_PACKET;
72                 } else if (matches(opt, "-oneline") == 0) {
73                         ++oneline;
74                 } else {
75                         show_usage();
76                 }
77                 argc--; argv++;
78         }
79         _SL_ = oneline ? "\\" : "\n" ;
80 }
81 #endif
82
83 int ip_main(int argc, char **argv)
84 {
85         int ret = EXIT_FAILURE;
86
87         ip_parse_common_args(&argc, &argv);
88
89         if (argc > 1) {
90 #ifdef CONFIG_FEATURE_IP_ADDRESS
91                 if (matches(argv[1], "address") == 0) {
92                         ret = do_ipaddr(argc-2, argv+2);
93                 }
94 #endif
95 #ifdef CONFIG_FEATURE_IP_ROUTE
96                 else if (matches(argv[1], "route") == 0) {
97                         ret = do_iproute(argc-2, argv+2);
98                 }
99 #endif
100 #ifdef CONFIG_FEATURE_IP_LINK
101                 else if (matches(argv[1], "link") == 0) {
102                         ret = do_iplink(argc-2, argv+2);
103                 }
104 #endif
105 #ifdef CONFIG_FEATURE_IP_TUNNEL
106                 else if (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0) {
107                         ret = do_iptunnel(argc-2, argv+2);
108                 }
109 #endif
110         }
111         if (ret) {
112                 show_usage();
113         }
114         return(EXIT_SUCCESS);
115 }