netcfg.c revision 97186a9c3d1874ac79f94676dc9f0df1f0815e1f
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>
22#include <netinet/ether.h>
23#include <netinet/if_ether.h>
24
25#include <netutils/ifc.h>
26#include <netutils/dhcp.h>
27
28static int verbose = 0;
29
30
31void die(const char *reason)
32{
33    perror(reason);
34    exit(1);
35}
36
37const char *ipaddr(in_addr_t addr)
38{
39    struct in_addr in_addr;
40
41    in_addr.s_addr = addr;
42    return inet_ntoa(in_addr);
43}
44
45void usage(void)
46{
47    fprintf(stderr,"usage: netcfg [<interface> {dhcp|up|down}]\n");
48    exit(1);
49}
50
51int dump_interface(const char *name)
52{
53    unsigned addr, mask, flags;
54    unsigned char hwbuf[ETH_ALEN];
55
56    if(ifc_get_info(name, &addr, &mask, &flags)) {
57        return 0;
58    }
59
60    printf("%-8s %s  ", name, flags & 1 ? "UP  " : "DOWN");
61    printf("%-16s", ipaddr(addr));
62    printf("%-16s", ipaddr(mask));
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    }
72    return 0;
73}
74
75int dump_interfaces(void)
76{
77    DIR *d;
78    struct dirent *de;
79
80    d = opendir("/sys/class/net");
81    if(d == 0) return -1;
82
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
91int 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
100struct
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 },
111    { "hwaddr", 2, set_hwaddr },
112    { 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}
188