Mark Salyzyn | c9303f3 | 2014-04-30 15:52:16 -0700 | [diff] [blame] | 1 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | ** 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <dirent.h> |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 19 | #include <netinet/ether.h> |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 20 | #include <netinet/if_ether.h> |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 21 | #include <netutils/dhcp.h> |
Mark Salyzyn | c9303f3 | 2014-04-30 15:52:16 -0700 | [diff] [blame] | 22 | #include <netutils/ifc.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Elliott Hughes | 2b7d75d | 2015-01-24 20:03:09 -0800 | [diff] [blame] | 25 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 27 | static const char *ipaddr(in_addr_t addr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | { |
Szymon Jakubczak | 8c85a00 | 2010-06-09 16:11:09 -0400 | [diff] [blame] | 29 | struct in_addr in_addr; |
| 30 | |
| 31 | in_addr.s_addr = addr; |
| 32 | return inet_ntoa(in_addr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 35 | static void usage(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | { |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 37 | fprintf(stderr,"usage: netcfg [<interface> dhcp]\n"); |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 38 | exit(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 41 | static int dump_interface(const char *name) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 42 | { |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 43 | unsigned addr, flags; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 44 | unsigned char hwbuf[ETH_ALEN]; |
Lorenzo Colitti | 47ddb51 | 2011-09-26 10:49:41 -0700 | [diff] [blame] | 45 | int prefixLength; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 46 | |
Robert Greenwalt | 177ca7c | 2011-02-15 11:39:55 -0800 | [diff] [blame] | 47 | if(ifc_get_info(name, &addr, &prefixLength, &flags)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | printf("%-8s %s ", name, flags & 1 ? "UP " : "DOWN"); |
Robert Greenwalt | 177ca7c | 2011-02-15 11:39:55 -0800 | [diff] [blame] | 52 | printf("%40s", ipaddr(addr)); |
| 53 | printf("/%-4d", prefixLength); |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 54 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | return 0; |
| 64 | } |
| 65 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 66 | static int dump_interfaces(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | { |
| 68 | DIR *d; |
| 69 | struct dirent *de; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 70 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | d = opendir("/sys/class/net"); |
| 72 | if(d == 0) return -1; |
Dmitry Shmidt | 97186a9 | 2011-01-24 17:09:32 -0800 | [diff] [blame] | 73 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | int main(int argc, char **argv) |
| 83 | { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | if(ifc_init()) { |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 85 | perror("Cannot perform requested operation"); |
| 86 | exit(1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if(argc == 1) { |
| 90 | int result = dump_interfaces(); |
| 91 | ifc_close(); |
| 92 | return result; |
| 93 | } |
| 94 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 95 | if(argc != 3) usage(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 97 | char* iname = argv[1]; |
| 98 | char* action = argv[2]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | if(strlen(iname) > 16) usage(); |
| 100 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 101 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | } |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 107 | } else { |
| 108 | fprintf(stderr,"no such action '%s'\n", action); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | usage(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | } |
Elliott Hughes | c463025 | 2015-02-03 19:56:35 +0000 | [diff] [blame] | 111 | |
Elliott Hughes | 187eade | 2015-02-03 11:59:22 -0800 | [diff] [blame] | 112 | ifc_close(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 113 | return 0; |
| 114 | } |