Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 31 | #if 0 |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | int preferred_family = AF_UNSPEC; |
| 33 | int oneline = 0; |
| 34 | char * _SL_ = NULL; |
| 35 | |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 36 | void ip_parse_common_args(int *argcp, char ***argvp) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 37 | { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 38 | int argc = *argcp; |
| 39 | char **argv = *argvp; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 40 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 41 | while (argc > 1) { |
| 42 | char *opt = argv[1]; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 44 | if (strcmp(opt,"--") == 0) { |
| 45 | argc--; argv++; |
| 46 | break; |
| 47 | } |
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[0] != '-') |
| 50 | break; |
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 (opt[1] == '-') |
| 53 | opt++; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_show_usage(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | } |
| 77 | argc--; argv++; |
| 78 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | _SL_ = oneline ? "\\" : "\n" ; |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 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); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | |
| 89 | if (argc > 1) { |
| 90 | #ifdef CONFIG_FEATURE_IP_ADDRESS |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 91 | if (matches(argv[1], "address") == 0) { |
| 92 | ret = do_ipaddr(argc-2, argv+2); |
| 93 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 94 | #endif |
| 95 | #ifdef CONFIG_FEATURE_IP_ROUTE |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 96 | if (matches(argv[1], "route") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 97 | ret = do_iproute(argc-2, argv+2); |
| 98 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | #endif |
| 100 | #ifdef CONFIG_FEATURE_IP_LINK |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 101 | if (matches(argv[1], "link") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 102 | ret = do_iplink(argc-2, argv+2); |
| 103 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef CONFIG_FEATURE_IP_TUNNEL |
Glenn L McGrath | 19efb3d | 2004-04-12 02:35:44 +0000 | [diff] [blame] | 106 | if (matches(argv[1], "tunnel") == 0 || strcmp(argv[1], "tunl") == 0) { |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 107 | ret = do_iptunnel(argc-2, argv+2); |
| 108 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 109 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | } |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 111 | if (ret) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | bb_show_usage(); |
Glenn L McGrath | f112daa | 2002-12-01 23:04:06 +0000 | [diff] [blame] | 113 | } |
| 114 | return(EXIT_SUCCESS); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 115 | } |