blob: eec1b2f0f79db2891b8c4f453948da81356c6859 [file] [log] [blame]
Mark Salyzync9303f32014-04-30 15:52:16 -07001/*
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002** Copyright 2006, The Android Open Source Project
3**
4** Licensed under the Apache License, Version 2.0 (the "License");
5** you may not use this file except in compliance with the License.
6** You may obtain a copy of the License at
7**
8** http://www.apache.org/licenses/LICENSE-2.0
9**
10** Unless required by applicable law or agreed to in writing, software
11** distributed under the License is distributed on an "AS IS" BASIS,
12** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13** See the License for the specific language governing permissions and
14** limitations under the License.
15*/
16
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080017#include <errno.h>
18#include <dirent.h>
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040019#include <netinet/ether.h>
Dmitry Shmidt97186a92011-01-24 17:09:32 -080020#include <netinet/if_ether.h>
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040021#include <netutils/dhcp.h>
Mark Salyzync9303f32014-04-30 15:52:16 -070022#include <netutils/ifc.h>
23#include <stdio.h>
24#include <stdlib.h>
Elliott Hughes2b7d75d2015-01-24 20:03:09 -080025#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
Elliott Hughes187eade2015-02-03 11:59:22 -080027static const char *ipaddr(in_addr_t addr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028{
Szymon Jakubczak8c85a002010-06-09 16:11:09 -040029 struct in_addr in_addr;
30
31 in_addr.s_addr = addr;
32 return inet_ntoa(in_addr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033}
34
Elliott Hughes187eade2015-02-03 11:59:22 -080035static void usage(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036{
Elliott Hughes187eade2015-02-03 11:59:22 -080037 fprintf(stderr,"usage: netcfg [<interface> dhcp]\n");
Dmitry Shmidt97186a92011-01-24 17:09:32 -080038 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039}
40
Elliott Hughes187eade2015-02-03 11:59:22 -080041static int dump_interface(const char *name)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042{
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070043 unsigned addr, flags;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080044 unsigned char hwbuf[ETH_ALEN];
Lorenzo Colitti47ddb512011-09-26 10:49:41 -070045 int prefixLength;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080046
Robert Greenwalt177ca7c2011-02-15 11:39:55 -080047 if(ifc_get_info(name, &addr, &prefixLength, &flags)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048 return 0;
49 }
50
51 printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN");
Robert Greenwalt177ca7c2011-02-15 11:39:55 -080052 printf("%40s", ipaddr(addr));
53 printf("/%-4d", prefixLength);
Dmitry Shmidt97186a92011-01-24 17:09:32 -080054 printf("0x%08x ", flags);
55 if (!ifc_get_hwaddr(name, hwbuf)) {
56 int i;
57 for(i=0; i < (ETH_ALEN-1); i++)
58 printf("%02x:", hwbuf[i]);
59 printf("%02x\n", hwbuf[i]);
60 } else {
61 printf("\n");
62 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 return 0;
64}
65
Elliott Hughes187eade2015-02-03 11:59:22 -080066static int dump_interfaces(void)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067{
68 DIR *d;
69 struct dirent *de;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080070
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071 d = opendir("/sys/class/net");
72 if(d == 0) return -1;
Dmitry Shmidt97186a92011-01-24 17:09:32 -080073
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074 while((de = readdir(d))) {
75 if(de->d_name[0] == '.') continue;
76 dump_interface(de->d_name);
77 }
78 closedir(d);
79 return 0;
80}
81
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082int main(int argc, char **argv)
83{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084 if(ifc_init()) {
Elliott Hughes187eade2015-02-03 11:59:22 -080085 perror("Cannot perform requested operation");
86 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 }
88
89 if(argc == 1) {
90 int result = dump_interfaces();
91 ifc_close();
92 return result;
93 }
94
Elliott Hughes187eade2015-02-03 11:59:22 -080095 if(argc != 3) usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096
Elliott Hughes187eade2015-02-03 11:59:22 -080097 char* iname = argv[1];
98 char* action = argv[2];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 if(strlen(iname) > 16) usage();
100
Elliott Hughes187eade2015-02-03 11:59:22 -0800101 if (!strcmp(action, "dhcp")) {
102 if (do_dhcp(iname)) {
103 fprintf(stderr, "dhcp failed: %s\n", strerror(errno));
104 ifc_close();
105 exit(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 }
Elliott Hughes187eade2015-02-03 11:59:22 -0800107 } else {
108 fprintf(stderr,"no such action '%s'\n", action);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 usage();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 }
Elliott Hughesc4630252015-02-03 19:56:35 +0000111
Elliott Hughes187eade2015-02-03 11:59:22 -0800112 ifc_close();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 return 0;
114}