The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* system/bin/netcfg/netcfg.c |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <errno.h> |
| 21 | #include <dirent.h> |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 22 | #include <netinet/ether.h> |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 23 | #include <netinet/if_ether.h> |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 24 | |
| 25 | #include <netutils/ifc.h> |
| 26 | #include <netutils/dhcp.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
| 28 | static int verbose = 0; |
| 29 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
| 31 | void die(const char *reason) |
| 32 | { |
| 33 | perror(reason); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 37 | const char *ipaddr(in_addr_t addr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | { |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 39 | struct in_addr in_addr; |
| 40 | |
| 41 | in_addr.s_addr = addr; |
| 42 | return inet_ntoa(in_addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void usage(void) |
| 46 | { |
| 47 | fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n"); |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 48 | exit(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int dump_interface(const char *name) |
| 52 | { |
Robert Greenwalt | 177ca7c | 2011-02-15 11:39:55 -0800 | [diff] [blame] | 53 | unsigned addr, prefixLength, flags; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 54 | unsigned char hwbuf[ETH_ALEN]; |
| 55 | |
Robert Greenwalt | 177ca7c | 2011-02-15 11:39:55 -0800 | [diff] [blame] | 56 | if(ifc_get_info(name, &addr, &prefixLength, &flags)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN"); |
Robert Greenwalt | 177ca7c | 2011-02-15 11:39:55 -0800 | [diff] [blame] | 61 | printf("%40s", ipaddr(addr)); |
| 62 | printf("/%-4d", prefixLength); |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 63 | printf("0x%08x ", flags); |
| 64 | if (!ifc_get_hwaddr(name, hwbuf)) { |
| 65 | int i; |
| 66 | for(i=0; i < (ETH_ALEN-1); i++) |
| 67 | printf("%02x:", hwbuf[i]); |
| 68 | printf("%02x\n", hwbuf[i]); |
| 69 | } else { |
| 70 | printf("\n"); |
| 71 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int dump_interfaces(void) |
| 76 | { |
| 77 | DIR *d; |
| 78 | struct dirent *de; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 79 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 80 | d = opendir("/sys/class/net"); |
| 81 | if(d == 0) return -1; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | while((de = readdir(d))) { |
| 84 | if(de->d_name[0] == '.') continue; |
| 85 | dump_interface(de->d_name); |
| 86 | } |
| 87 | closedir(d); |
| 88 | return 0; |
| 89 | } |
| 90 | |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 91 | int set_hwaddr(const char *name, const char *asc) { |
| 92 | struct ether_addr *addr = ether_aton(asc); |
| 93 | if (!addr) { |
| 94 | printf("Failed to parse '%s'\n", asc); |
| 95 | return -1; |
| 96 | } |
| 97 | return ifc_set_hwaddr(name, addr->ether_addr_octet); |
| 98 | } |
| 99 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | struct |
| 101 | { |
| 102 | const char *name; |
| 103 | int nargs; |
| 104 | void *func; |
| 105 | } CMDS[] = { |
| 106 | { "dhcp", 1, do_dhcp }, |
| 107 | { "up", 1, ifc_up }, |
| 108 | { "down", 1, ifc_down }, |
| 109 | { "flhosts", 1, ifc_remove_host_routes }, |
| 110 | { "deldefault", 1, ifc_remove_default_route }, |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 111 | { "hwaddr", 2, set_hwaddr }, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | { 0, 0, 0 }, |
| 113 | }; |
| 114 | |
| 115 | static int call_func(void *_func, unsigned nargs, char **args) |
| 116 | { |
| 117 | switch(nargs){ |
| 118 | case 1: { |
| 119 | int (*func)(char *a0) = _func; |
| 120 | return func(args[0]); |
| 121 | } |
| 122 | case 2: { |
| 123 | int (*func)(char *a0, char *a1) = _func; |
| 124 | return func(args[0], args[1]); |
| 125 | } |
| 126 | case 3: { |
| 127 | int (*func)(char *a0, char *a1, char *a2) = _func; |
| 128 | return func(args[0], args[1], args[2]); |
| 129 | } |
| 130 | default: |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | int main(int argc, char **argv) |
| 136 | { |
| 137 | char *iname; |
| 138 | int n; |
| 139 | |
| 140 | if(ifc_init()) { |
| 141 | die("Cannot perform requested operation"); |
| 142 | } |
| 143 | |
| 144 | if(argc == 1) { |
| 145 | int result = dump_interfaces(); |
| 146 | ifc_close(); |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | if(argc < 3) usage(); |
| 151 | |
| 152 | iname = argv[1]; |
| 153 | if(strlen(iname) > 16) usage(); |
| 154 | |
| 155 | argc -= 2; |
| 156 | argv += 2; |
| 157 | while(argc > 0) { |
| 158 | for(n = 0; CMDS[n].name; n++){ |
| 159 | if(!strcmp(argv[0], CMDS[n].name)) { |
| 160 | char *cmdname = argv[0]; |
| 161 | int nargs = CMDS[n].nargs; |
| 162 | |
| 163 | argv[0] = iname; |
| 164 | if(argc < nargs) { |
| 165 | fprintf(stderr, "not enough arguments for '%s'\n", cmdname); |
| 166 | ifc_close(); |
| 167 | exit(1); |
| 168 | } |
| 169 | if(call_func(CMDS[n].func, nargs, argv)) { |
| 170 | fprintf(stderr, "action '%s' failed (%s)\n", cmdname, strerror(errno)); |
| 171 | ifc_close(); |
| 172 | exit(1); |
| 173 | } |
| 174 | argc -= nargs; |
| 175 | argv += nargs; |
| 176 | goto done; |
| 177 | } |
| 178 | } |
| 179 | fprintf(stderr,"no such action '%s'\n", argv[0]); |
| 180 | usage(); |
| 181 | done: |
| 182 | ; |
| 183 | } |
| 184 | ifc_close(); |
| 185 | |
| 186 | return 0; |
| 187 | } |