blob: c5200751e22264cce8cfbd3ec825279cad42441a [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/* 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 Jakubczak8c85a002010-06-09 16:11:09 -040022#include <netinet/ether.h>
Dmitry Shmidt97186a92011-01-24 17:09:32 -080023#include <netinet/if_ether.h>
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040024
25#include <netutils/ifc.h>
26#include <netutils/dhcp.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027
28static int verbose = 0;
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030
31void die(const char *reason)
32{
33 perror(reason);
34 exit(1);
35}
36
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040037const char *ipaddr(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038{
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040039 struct in_addr in_addr;
40
41 in_addr.s_addr = addr;
42 return inet_ntoa(in_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043}
44
45void usage(void)
46{
47 fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
Dmitry Shmidt97186a92011-01-24 17:09:32 -080048 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049}
50
51int dump_interface(const char *name)
52{
Robert Greenwalt177ca7c2011-02-15 11:39:55 -080053 unsigned addr, prefixLength, flags;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080054 unsigned char hwbuf[ETH_ALEN];
55
Robert Greenwalt177ca7c2011-02-15 11:39:55 -080056 if(ifc_get_info(name, &addr, &prefixLength, &flags)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057 return 0;
58 }
59
60 printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN");
Robert Greenwalt177ca7c2011-02-15 11:39:55 -080061 printf("%40s", ipaddr(addr));
62 printf("/%-4d", prefixLength);
Dmitry Shmidt97186a92011-01-24 17:09:32 -080063 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 Projectdd7bc332009-03-03 19:32:55 -080072 return 0;
73}
74
75int dump_interfaces(void)
76{
77 DIR *d;
78 struct dirent *de;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080079
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 d = opendir("/sys/class/net");
81 if(d == 0) return -1;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080082
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 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 Jakubczak8c85a002010-06-09 16:11:09 -040091int 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 Projectdd7bc332009-03-03 19:32:55 -0800100struct
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 Jakubczak8c85a002010-06-09 16:11:09 -0400111 { "hwaddr", 2, set_hwaddr },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 { 0, 0, 0 },
113};
114
115static 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
135int 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}