Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ip.c "ip" utility frontend. |
| 3 | * |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 4 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 5 | * |
| 6 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 7 | * |
| 8 | * |
| 9 | * Changes: |
| 10 | * |
| 11 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses |
| 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
| 17 | #include <syslog.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <string.h> |
| 22 | |
Bernhard Reutner-Fischer | ab18782 | 2005-10-26 10:47:26 +0000 | [diff] [blame] | 23 | #include "libiproute/utils.h" |
| 24 | #include "libiproute/ip_common.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 25 | |
| 26 | #include "busybox.h" |
| 27 | |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 28 | #if 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 29 | int preferred_family = AF_UNSPEC; |
| 30 | int oneline = 0; |
| 31 | char * _SL_ = NULL; |
| 32 | |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 33 | void ip_parse_common_args(int *argcp, char ***argvp) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 35 | int argc = *argcp; |
| 36 | char **argv = *argvp; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 37 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | while (argc > 1) { |
| 39 | char *opt = argv[1]; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 40 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 41 | if (strcmp(opt,"--") == 0) { |
| 42 | argc--; argv++; |
| 43 | break; |
| 44 | } |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 45 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 46 | if (opt[0] != '-') |
| 47 | break; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 48 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | if (opt[1] == '-') |
| 50 | opt++; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 51 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | if (matches(opt, "-family") == 0) { |
| 53 | argc--; |
| 54 | argv++; |
| 55 | if (strcmp(argv[1], "inet") == 0) |
| 56 | preferred_family = AF_INET; |
| 57 | else if (strcmp(argv[1], "inet6") == 0) |
| 58 | preferred_family = AF_INET6; |
| 59 | else if (strcmp(argv[1], "link") == 0) |
| 60 | preferred_family = AF_PACKET; |
| 61 | else |
| 62 | invarg(argv[1], "invalid protocol family"); |
| 63 | } else if (strcmp(opt, "-4") == 0) { |
| 64 | preferred_family = AF_INET; |
| 65 | } else if (strcmp(opt, "-6") == 0) { |
| 66 | preferred_family = AF_INET6; |
| 67 | } else if (strcmp(opt, "-0") == 0) { |
| 68 | preferred_family = AF_PACKET; |
| 69 | } else if (matches(opt, "-oneline") == 0) { |
| 70 | ++oneline; |
| 71 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | bb_show_usage(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 73 | } |
| 74 | argc--; argv++; |
| 75 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | _SL_ = oneline ? "\\" : "\n" ; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 77 | } |
| 78 | #endif |
| 79 | |
| 80 | int ip_main(int argc, char **argv) |
| 81 | { |
| 82 | int ret = EXIT_FAILURE; |
| 83 | |
| 84 | ip_parse_common_args(&argc, &argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | |
| 86 | if (argc > 1) { |
| 87 | #ifdef CONFIG_FEATURE_IP_ADDRESS |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 88 | if (matches(argv[1], "address") == 0) { |
| 89 | ret = do_ipaddr(argc-2, argv+2); |
| 90 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | #endif |
| 92 | #ifdef CONFIG_FEATURE_IP_ROUTE |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 93 | if (matches(argv[1], "route") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 94 | ret = do_iproute(argc-2, argv+2); |
| 95 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 96 | #endif |
| 97 | #ifdef CONFIG_FEATURE_IP_LINK |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 98 | if (matches(argv[1], "link") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 99 | ret = do_iplink(argc-2, argv+2); |
| 100 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 101 | #endif |
| 102 | #ifdef CONFIG_FEATURE_IP_TUNNEL |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 103 | if (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 104 | ret = do_iptunnel(argc-2, argv+2); |
| 105 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 106 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | } |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 108 | if (ret) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | bb_show_usage(); |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 110 | } |
| 111 | return(EXIT_SUCCESS); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 112 | } |