ifconfig.c (7239B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <arpa/inet.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 8 #include "net.h" 9 #include "util.h" 10 11 static void 12 usage(void) 13 { 14 eprintf("usage: %s [-a] [interface [action ...]]\n", argv0); 15 } 16 17 static void 18 display_interface(const struct NetInterface *iface) 19 { 20 struct NetStats stats; 21 char ipv6_str[INET6_ADDRSTRLEN]; 22 23 printf("%-9s Link encap:", iface->name); 24 if (iface->has_mac) { 25 printf( 26 "Ethernet HWaddr %02x:%02x:%02x:%02x:%02x:%02x\n", 27 iface->mac[0], 28 iface->mac[1], 29 iface->mac[2], 30 iface->mac[3], 31 iface->mac[4], 32 iface->mac[5] 33 ); 34 } else if (iface->flags & NET_IF_LOOPBACK) { 35 printf("Local Loopback\n"); 36 } else { 37 printf("UNSPEC\n"); 38 } 39 40 if (iface->has_ipv4) { 41 printf(" inet addr:%s", inet_ntoa(iface->ipv4_addr.sin_addr)); 42 printf(" Bcast:%s", inet_ntoa(iface->ipv4_brd.sin_addr)); 43 printf(" Mask:%s\n", inet_ntoa(iface->ipv4_mask.sin_addr)); 44 } 45 46 if (iface->has_ipv6) { 47 if (inet_ntop(AF_INET6, &iface->ipv6_addr.sin6_addr, ipv6_str, sizeof(ipv6_str))) { 48 printf(" inet6 addr: %s\n", ipv6_str); 49 } 50 } 51 52 printf(" "); 53 if (iface->flags & NET_IF_UP) 54 printf("UP "); 55 if (iface->flags & NET_IF_BROADCAST) 56 printf("BROADCAST "); 57 if (iface->flags & NET_IF_DEBUG) 58 printf("DEBUG "); 59 if (iface->flags & NET_IF_LOOPBACK) 60 printf("LOOPBACK "); 61 if (iface->flags & NET_IF_POINTOPOINT) 62 printf("POINTOPOINT "); 63 if (iface->flags & NET_IF_RUNNING) 64 printf("RUNNING "); 65 if (iface->flags & NET_IF_NOARP) 66 printf("NOARP "); 67 if (iface->flags & NET_IF_PROMISC) 68 printf("PROMISC "); 69 if (iface->flags & NET_IF_ALLMULTI) 70 printf("ALLMULTI "); 71 if (iface->flags & NET_IF_MULTICAST) 72 printf("MULTICAST "); 73 74 printf(" MTU:%d Metric:%d\n", iface->mtu, iface->metric); 75 76 if (net_get_stats(iface->name, &stats) >= 0) { 77 printf( 78 " RX packets:%llu errors:%llu dropped:%llu " 79 "overruns:0 frame:0\n", 80 stats.rx_packets, 81 stats.rx_errs, 82 stats.rx_drop 83 ); 84 printf( 85 " TX packets:%llu errors:%llu dropped:%llu " 86 "overruns:0 carrier:0\n", 87 stats.tx_packets, 88 stats.tx_errs, 89 stats.tx_drop 90 ); 91 printf(" RX bytes:%llu TX bytes:%llu\n", stats.rx_bytes, stats.tx_bytes); 92 } 93 printf("\n"); 94 } 95 96 static void 97 list_interfaces(int all) 98 { 99 struct NetInterface *ifaces = NULL; 100 int count = 0; 101 int i; 102 103 if (net_get_interfaces(&ifaces, &count) < 0) 104 eprintf("net_get_interfaces:"); 105 106 for (i = 0; i < count; i++) { 107 if (all || (ifaces[i].flags & NET_IF_UP)) { 108 display_interface(&ifaces[i]); 109 } 110 } 111 free(ifaces); 112 } 113 114 // ?man ifconfig: configure network interfaces 115 // ?man arguments: interface [action ...] 116 // ?man configure network interface parameters and view stats 117 int 118 main(int argc, char *argv[]) 119 { 120 struct NetInterface *ifaces = NULL; 121 const struct NetInterface *iface = NULL; 122 char *name; 123 char *arg; 124 char *slash; 125 int aflag = 0; 126 int i = 1; 127 int prefix; 128 int count = 0; 129 130 ARGBEGIN 131 { 132 // ?man -a: print or show all entries 133 case 'a': 134 aflag = 1; 135 break; 136 default: 137 usage(); 138 } 139 ARGEND 140 141 if (argc == 0) { 142 list_interfaces(aflag); 143 return 0; 144 } 145 146 name = argv[0]; 147 148 if (net_get_interfaces(&ifaces, &count) < 0) 149 eprintf("net_get_interfaces:"); 150 151 for (prefix = 0; prefix < count; prefix++) { 152 if (strcmp(ifaces[prefix].name, name) == 0) { 153 iface = &ifaces[prefix]; 154 break; 155 } 156 } 157 158 if (argc == 1) { 159 if (!iface) 160 eprintf("interface %s not found\n", name); 161 display_interface(iface); 162 free(ifaces); 163 return 0; 164 } 165 166 while (i < argc) { 167 arg = argv[i]; 168 169 if (strcmp(arg, "up") == 0) { 170 if (net_set_flags(name, NET_IF_UP | NET_IF_RUNNING, 1) < 0) 171 eprintf("net_set_flags up:"); 172 i++; 173 } else if (strcmp(arg, "down") == 0) { 174 if (net_set_flags(name, NET_IF_UP, 0) < 0) 175 eprintf("net_set_flags down:"); 176 i++; 177 } else if (strcmp(arg, "netmask") == 0) { 178 if (i + 1 >= argc) 179 eprintf("netmask needs an address\n"); 180 if (net_set_mask(name, argv[i + 1]) < 0) 181 eprintf("net_set_mask:"); 182 i += 2; 183 } else if (strcmp(arg, "broadcast") == 0) { 184 if (i + 1 >= argc) 185 eprintf("broadcast needs an address\n"); 186 if (net_set_broadcast(name, argv[i + 1]) < 0) 187 eprintf("net_set_broadcast:"); 188 i += 2; 189 } else if (strcmp(arg, "mtu") == 0) { 190 if (i + 1 >= argc) 191 eprintf("mtu needs a value\n"); 192 if (net_set_mtu(name, atoi(argv[i + 1])) < 0) 193 eprintf("net_set_mtu:"); 194 i += 2; 195 } else if (strcmp(arg, "metric") == 0) { 196 if (i + 1 >= argc) 197 eprintf("metric needs a value\n"); 198 if (net_set_metric(name, atoi(argv[i + 1])) < 0) 199 eprintf("net_set_metric:"); 200 i += 2; 201 } else if (strcmp(arg, "txqueuelen") == 0) { 202 if (i + 1 >= argc) 203 eprintf("txqueuelen needs a value\n"); 204 if (net_set_txqueuelen(name, atoi(argv[i + 1])) < 0) 205 eprintf("net_set_txqueuelen:"); 206 i += 2; 207 } else if (strcmp(arg, "dstaddr") == 0 || strcmp(arg, "pointopoint") == 0) { 208 if (i + 1 >= argc) 209 eprintf("%s needs an address\n", arg); 210 if (net_set_dstaddr(name, argv[i + 1]) < 0) 211 eprintf("net_set_dstaddr:"); 212 i += 2; 213 } else if (strcmp(arg, "arp") == 0) { 214 if (net_set_flags(name, NET_IF_NOARP, 0) < 0) 215 eprintf("net_set_flags arp:"); 216 i++; 217 } else if (strcmp(arg, "-arp") == 0) { 218 if (net_set_flags(name, NET_IF_NOARP, 1) < 0) 219 eprintf("net_set_flags -arp:"); 220 i++; 221 } else if (strcmp(arg, "promisc") == 0) { 222 if (net_set_flags(name, NET_IF_PROMISC, 1) < 0) 223 eprintf("net_set_flags promisc:"); 224 i++; 225 } else if (strcmp(arg, "-promisc") == 0) { 226 if (net_set_flags(name, NET_IF_PROMISC, 0) < 0) 227 eprintf("net_set_flags -promisc:"); 228 i++; 229 } else if (strcmp(arg, "allmulti") == 0) { 230 if (net_set_flags(name, NET_IF_ALLMULTI, 1) < 0) 231 eprintf("net_set_flags allmulti:"); 232 i++; 233 } else if (strcmp(arg, "-allmulti") == 0) { 234 if (net_set_flags(name, NET_IF_ALLMULTI, 0) < 0) 235 eprintf("net_set_flags -allmulti:"); 236 i++; 237 } else if (strcmp(arg, "multicast") == 0) { 238 if (net_set_flags(name, NET_IF_MULTICAST, 1) < 0) 239 eprintf("net_set_flags multicast:"); 240 i++; 241 } else if (strcmp(arg, "-multicast") == 0) { 242 if (net_set_flags(name, NET_IF_MULTICAST, 0) < 0) 243 eprintf("net_set_flags -multicast:"); 244 i++; 245 } else { 246 slash = strchr(arg, '/'); 247 prefix = -1; 248 if (slash) { 249 *slash = '\0'; 250 prefix = atoi(slash + 1); 251 } 252 if (net_add_addr(name, arg, prefix) < 0) 253 eprintf("net_add_addr:"); 254 i++; 255 } 256 } 257 258 free(ifaces); 259 260 if (fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>")) 261 return 1; 262 263 return 0; 264 }