blob: 9cd883a82c4e550886db018986f479f524c23dad [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>
23
24#include <netutils/ifc.h>
25#include <netutils/dhcp.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
27static int verbose = 0;
28
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029
30void die(const char *reason)
31{
32 perror(reason);
33 exit(1);
34}
35
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040036const char *ipaddr(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037{
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040038 struct in_addr in_addr;
39
40 in_addr.s_addr = addr;
41 return inet_ntoa(in_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042}
43
44void usage(void)
45{
46 fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
47 exit(1);
48}
49
50int dump_interface(const char *name)
51{
52 unsigned addr, mask, flags;
53
54 if(ifc_get_info(name, &addr, &mask, &flags)) {
55 return 0;
56 }
57
58 printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN");
59 printf("%-16s", ipaddr(addr));
60 printf("%-16s", ipaddr(mask));
61 printf("0x%08x\n", flags);
62 return 0;
63}
64
65int dump_interfaces(void)
66{
67 DIR *d;
68 struct dirent *de;
69
70 d = opendir("/sys/class/net");
71 if(d == 0) return -1;
72
73 while((de = readdir(d))) {
74 if(de->d_name[0] == '.') continue;
75 dump_interface(de->d_name);
76 }
77 closedir(d);
78 return 0;
79}
80
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040081int set_hwaddr(const char *name, const char *asc) {
82 struct ether_addr *addr = ether_aton(asc);
83 if (!addr) {
84 printf("Failed to parse '%s'\n", asc);
85 return -1;
86 }
87 return ifc_set_hwaddr(name, addr->ether_addr_octet);
88}
89
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090struct
91{
92 const char *name;
93 int nargs;
94 void *func;
95} CMDS[] = {
96 { "dhcp", 1, do_dhcp },
97 { "up", 1, ifc_up },
98 { "down", 1, ifc_down },
99 { "flhosts", 1, ifc_remove_host_routes },
100 { "deldefault", 1, ifc_remove_default_route },
Szymon Jakubczak8c85a002010-06-09 16:11:09 -0400101 { "hwaddr", 2, set_hwaddr },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 { 0, 0, 0 },
103};
104
105static int call_func(void *_func, unsigned nargs, char **args)
106{
107 switch(nargs){
108 case 1: {
109 int (*func)(char *a0) = _func;
110 return func(args[0]);
111 }
112 case 2: {
113 int (*func)(char *a0, char *a1) = _func;
114 return func(args[0], args[1]);
115 }
116 case 3: {
117 int (*func)(char *a0, char *a1, char *a2) = _func;
118 return func(args[0], args[1], args[2]);
119 }
120 default:
121 return -1;
122 }
123}
124
125int main(int argc, char **argv)
126{
127 char *iname;
128 int n;
129
130 if(ifc_init()) {
131 die("Cannot perform requested operation");
132 }
133
134 if(argc == 1) {
135 int result = dump_interfaces();
136 ifc_close();
137 return result;
138 }
139
140 if(argc < 3) usage();
141
142 iname = argv[1];
143 if(strlen(iname) > 16) usage();
144
145 argc -= 2;
146 argv += 2;
147 while(argc > 0) {
148 for(n = 0; CMDS[n].name; n++){
149 if(!strcmp(argv[0], CMDS[n].name)) {
150 char *cmdname = argv[0];
151 int nargs = CMDS[n].nargs;
152
153 argv[0] = iname;
154 if(argc < nargs) {
155 fprintf(stderr, "not enough arguments for '%s'\n", cmdname);
156 ifc_close();
157 exit(1);
158 }
159 if(call_func(CMDS[n].func, nargs, argv)) {
160 fprintf(stderr, "action '%s' failed (%s)\n", cmdname, strerror(errno));
161 ifc_close();
162 exit(1);
163 }
164 argc -= nargs;
165 argv += nargs;
166 goto done;
167 }
168 }
169 fprintf(stderr,"no such action '%s'\n", argv[0]);
170 usage();
171 done:
172 ;
173 }
174 ifc_close();
175
176 return 0;
177}