Only compile the files in this dir if the IP applet is enabled
[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 int preferred_family = AF_UNSPEC;
32 int oneline = 0;
33 char * _SL_ = NULL;
34
35 int ip_main(int argc, char **argv)
36 {
37         char *basename;
38
39         basename = strrchr(argv[0], '/');
40         if (basename == NULL)
41                 basename = argv[0];
42         else
43                 basename++;
44         
45         while (argc > 1) {
46                 char *opt = argv[1];
47                 if (strcmp(opt,"--") == 0) {
48                         argc--; argv++;
49                         break;
50                 }
51                 if (opt[0] != '-')
52                         break;
53                 if (opt[1] == '-')
54                         opt++;
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                         fprintf(stderr, "Option \"%s\" is unknown, try \"ip -help\".\n", opt);
76                         exit(-1);
77                 }
78                 argc--; argv++;
79         }
80
81         _SL_ = oneline ? "\\" : "\n" ;
82
83         if (argc > 1) {
84 #ifdef CONFIG_FEATURE_IP_ADDRESS
85                 if (matches(argv[1], "address") == 0)
86                         return do_ipaddr(argc-2, argv+2);
87 #endif
88 #ifdef CONFIG_FEATURE_IP_ROUTE
89                 if (matches(argv[1], "route") == 0)
90                         return do_iproute(argc-2, argv+2);
91 #endif
92 #ifdef CONFIG_FEATURE_IP_LINK
93                 if (matches(argv[1], "link") == 0)
94                         return do_iplink(argc-2, argv+2);
95 #endif
96 #ifdef CONFIG_FEATURE_IP_TUNNEL
97                 if (matches(argv[1], "tunnel") == 0 ||
98                     strcmp(argv[1], "tunl") == 0)
99                         return do_iptunnel(argc-2, argv+2);
100 #endif
101                 fprintf(stderr, "Object \"%s\" is unknown, try \"ip help\".\n", argv[1]);
102                 exit(-1);
103         }
104 }