comparison toys/pending/ifconfig.c @ 864:764df39f62b4

More ifconfig cleanup.
author Rob Landley <rob@landley.net>
date Tue, 16 Apr 2013 23:49:47 -0500
parents 493f412fc5da
children 0fa773e2a4fe
comparison
equal deleted inserted replaced
863:493f412fc5da 864:764df39f62b4
70 unsigned long transmit_compressed; //num_tr_compressed; 70 unsigned long transmit_compressed; //num_tr_compressed;
71 } PROC_NET_DEV_INFO; 71 } PROC_NET_DEV_INFO;
72 72
73 // man netdevice 73 // man netdevice
74 typedef struct _iface_list { 74 typedef struct _iface_list {
75 struct _iface_list *next;
75 int hw_type; 76 int hw_type;
76 short ifrflags; //used for addr, broadcast, and mask. 77 short ifrflags; //used for addr, broadcast, and mask.
77 short ifaddr; //if set print ifraddr, irrdstaddr, ifrbroadaddr and ifrnetmask. 78 short ifaddr; //if set print ifraddr, irrdstaddr, ifrbroadaddr and ifrnetmask.
78 struct sockaddr ifraddr; 79 struct sockaddr ifraddr;
79 struct sockaddr ifrdstaddr; 80 struct sockaddr ifrdstaddr;
84 int ifrmetric; 85 int ifrmetric;
85 PROC_NET_DEV_INFO dev_info; 86 PROC_NET_DEV_INFO dev_info;
86 int txqueuelen; 87 int txqueuelen;
87 struct ifmap ifrmap; 88 struct ifmap ifrmap;
88 int non_virtual_iface; 89 int non_virtual_iface;
89 struct _iface_list *next; //, *prev;
90 } IFACE_LIST; 90 } IFACE_LIST;
91 91
92 92
93 #define HW_NAME_LEN 20 93 #define HW_NAME_LEN 20
94 #define HW_TITLE_LEN 30 94 #define HW_TITLE_LEN 30
112 static void print_ip6_addr(IFACE_LIST *l_ptr); 112 static void print_ip6_addr(IFACE_LIST *l_ptr);
113 static void clear_list(void); 113 static void clear_list(void);
114 114
115 //from /net/if.h 115 //from /net/if.h
116 static char *iface_flags_str[] = { 116 static char *iface_flags_str[] = {
117 "UP", 117 "UP", "BROADCAST", "DEBUG", "LOOPBACK", "POINTOPOINT", "NOTRAILERS",
118 "BROADCAST", 118 "RUNNING", "NOARP", "PROMISC", "ALLMULTI", "MASTER", "SLAVE", "MULTICAST",
119 "DEBUG", 119 "PORTSEL", "AUTOMEDIA", "DYNAMIC", NULL
120 "LOOPBACK",
121 "POINTOPOINT",
122 "NOTRAILERS",
123 "RUNNING",
124 "NOARP",
125 "PROMISC",
126 "ALLMULTI",
127 "MASTER",
128 "SLAVE",
129 "MULTICAST",
130 "PORTSEL",
131 "AUTOMEDIA",
132 "DYNAMIC",
133 NULL
134 }; 120 };
135 //from /usr/include/linux/netdevice.h 121 //from /usr/include/linux/netdevice.h
136 #ifdef IFF_PORTSEL 122 #ifdef IFF_PORTSEL
137 //Media selection options. 123 //Media selection options.
138 # ifndef IF_PORT_UNKNOWN 124 # ifndef IF_PORT_UNKNOWN
153 #define IPV6_ADDR_LOOPBACK 0x0010U 139 #define IPV6_ADDR_LOOPBACK 0x0010U
154 #define IPV6_ADDR_LINKLOCAL 0x0020U 140 #define IPV6_ADDR_LINKLOCAL 0x0020U
155 #define IPV6_ADDR_SITELOCAL 0x0040U 141 #define IPV6_ADDR_SITELOCAL 0x0040U
156 #define IPV6_ADDR_COMPATv4 0x0080U 142 #define IPV6_ADDR_COMPATv4 0x0080U
157 143
158 //==================================================================================
159 //for the param settings. 144 //for the param settings.
160 145
161 //for ipv6 add/del 146 //for ipv6 add/del
162 struct ifreq_inet6 { 147 struct ifreq_inet6 {
163 struct in6_addr ifrinte6_addr; 148 struct in6_addr ifrinte6_addr;
177 162
178 #ifndef INFINIBAND_ALEN 163 #ifndef INFINIBAND_ALEN
179 # define INFINIBAND_ALEN 20 164 # define INFINIBAND_ALEN 20
180 #endif 165 #endif
181 166
182 static void set_data(int sockfd, struct ifreq *ifre, char *kval, int request, char *req_name);
183 static void set_flags(int sockfd, struct ifreq *ifre, int arg_flag, int flag); //verify 167 static void set_flags(int sockfd, struct ifreq *ifre, int arg_flag, int flag); //verify
184 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu); //verify 168 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu); //verify
185 static void set_metric(int sockfd, struct ifreq *ifre, char *metric); //verify 169 static void set_metric(int sockfd, struct ifreq *ifre, char *metric); //verify
186 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen); //verify 170 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen); //verify
187 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name); 171 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name);
189 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name); 173 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name);
190 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name); 174 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name);
191 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name); 175 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name);
192 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name); 176 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name);
193 177
178 void xioctl(int fd, int request, void *data)
179 {
180 if (ioctl(fd, request, data) < 0) perror_exit("ioctl %d", request);
181 }
182
194 char *omit_whitespace(char *s) 183 char *omit_whitespace(char *s)
195 { 184 {
196 while(*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9)) s++; 185 while(*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9)) s++;
197 return (char *) s; 186 return (char *) s;
198 } 187 }
208 * verify the host is local unix path. 197 * verify the host is local unix path.
209 * if so, set the swl input param accordingly. 198 * if so, set the swl input param accordingly.
210 */ 199 */
211 static int is_host_unix(char *host, sockaddr_with_len **swl) 200 static int is_host_unix(char *host, sockaddr_with_len **swl)
212 { 201 {
213 if(strncmp(host, "local:", 6) == 0) { 202 if (strncmp(host, "local:", 6) == 0) {
214 struct sockaddr_un *sockun; 203 struct sockaddr_un *sockun;
204
215 *swl = xzalloc(sizeof(struct sockaddr_with_len)); 205 *swl = xzalloc(sizeof(struct sockaddr_with_len));
216 (*swl)->socklen = sizeof(struct sockaddr_un); 206 (*swl)->socklen = sizeof(struct sockaddr_un);
217 (*swl)->sock_u.sock.sa_family = AF_UNIX; 207 (*swl)->sock_u.sock.sa_family = AF_UNIX;
218 sockun = (struct sockaddr_un *)&(*swl)->sock_u.sock; 208 sockun = (struct sockaddr_un *)&(*swl)->sock_u.sock;
219 safe_strncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path)); 209 safe_strncpy(sockun->sun_path, host + 6, sizeof(sockun->sun_path));
227 */ 217 */
228 static void get_host_and_port(char **host, int *port) 218 static void get_host_and_port(char **host, int *port)
229 { 219 {
230 char *ch_ptr; 220 char *ch_ptr;
231 char *org_host = *host; 221 char *org_host = *host;
232 if(*host[0] == '[') { 222 if (*host[0] == '[') {
233 (*host)++; 223 (*host)++;
234 ch_ptr = strchr(*host, ']'); 224 ch_ptr = strchr(*host, ']');
235 if(!ch_ptr || (ch_ptr[1] != ':' && ch_ptr[1] != '\0')) 225 if (!ch_ptr || (ch_ptr[1] != ':' && ch_ptr[1] != '\0'))
236 error_exit("bad address '%s'", org_host); 226 error_exit("bad address '%s'", org_host);
237 } 227 } else {
238 else {
239 ch_ptr = strrchr(*host, ':'); 228 ch_ptr = strrchr(*host, ':');
240 //There is more than one ':' like "::1" 229 //There is more than one ':' like "::1"
241 if(ch_ptr && strchr(*host, ':') != ch_ptr) 230 if(ch_ptr && strchr(*host, ':') != ch_ptr) ch_ptr = NULL;
242 ch_ptr = NULL; 231 }
243 } 232 if (ch_ptr) { //pointer to ":" or "]:"
244 if(ch_ptr) { //pointer to ":" or "]:"
245 int size = ch_ptr - (*host) + 1; 233 int size = ch_ptr - (*host) + 1;
246 safe_strncpy(*host, *host, size); 234 safe_strncpy(*host, *host, size);
247 if(*ch_ptr != ':') { 235 if (*ch_ptr != ':') {
248 ch_ptr++; //skip ']' 236 ch_ptr++; //skip ']'
249 //[nn] without port 237 //[nn] without port
250 if(!*ch_ptr) return; 238 if (!*ch_ptr) return;
251 } 239 }
252 ch_ptr++; //skip ':' to get the port number. 240 ch_ptr++; //skip ':' to get the port number.
253 *port = get_strtou(ch_ptr, NULL, 10); 241 *port = get_strtou(ch_ptr, NULL, 10);
254 if(errno || (unsigned)*port > 65535) 242 if (errno || (unsigned)*port > 65535) error_exit("bad port '%s'", org_host);
255 error_exit("bad port spec '%s'", org_host); 243 }
256 }
257 } 244 }
258 245
259 /* 246 /*
260 * used to extract the address info from the given host ip 247 * used to extract the address info from the given host ip
261 * and update the swl param accordingly. 248 * and update the swl param accordingly.
268 memset(&hints, 0 , sizeof(struct addrinfo)); 255 memset(&hints, 0 , sizeof(struct addrinfo));
269 hints.ai_family = af; 256 hints.ai_family = af;
270 hints.ai_socktype = SOCK_STREAM; 257 hints.ai_socktype = SOCK_STREAM;
271 258
272 status = getaddrinfo(host, NULL, &hints, &result); 259 status = getaddrinfo(host, NULL, &hints, &result);
273 if (status) perror_exit("bad address '%s' : %s", host, gai_strerror(status)); 260 if (status) error_exit("bad address '%s' : %s", host, gai_strerror(status));
274 261
275 for(rp = result; rp; rp = rp->ai_next) { 262 for (rp = result; rp; rp = rp->ai_next) {
276 if(rp->ai_family == AF_INET || rp->ai_family == AF_INET6) { 263 if (rp->ai_family == AF_INET || rp->ai_family == AF_INET6) {
277 *swl = xmalloc(sizeof(struct sockaddr_with_len)); 264 *swl = xmalloc(sizeof(struct sockaddr_with_len));
278 (*swl)->socklen = rp->ai_addrlen; 265 (*swl)->socklen = rp->ai_addrlen;
279 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen); 266 memcpy(&((*swl)->sock_u.sock), rp->ai_addr, rp->ai_addrlen);
280 break; 267 break;
281 } 268 }
313 char *address_to_name(struct sockaddr *sock) 300 char *address_to_name(struct sockaddr *sock)
314 { 301 {
315 //man page of getnameinfo. 302 //man page of getnameinfo.
316 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,}; 303 char hbuf[NI_MAXHOST] = {0,}, sbuf[NI_MAXSERV] = {0,};
317 int status = 0; 304 int status = 0;
305
318 if(sock->sa_family == AF_INET) { 306 if(sock->sa_family == AF_INET) {
319 socklen_t len = sizeof(struct sockaddr_in); 307 socklen_t len = sizeof(struct sockaddr_in);
320 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) 308 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0)
321 return xmsprintf("%s:%s", hbuf, sbuf); 309 return xmsprintf("%s:%s", hbuf, sbuf);
322 else { 310 else {
323 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); 311 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
324 return NULL; 312 return NULL;
325 } 313 }
326 } 314 } else if(sock->sa_family == AF_INET6) {
327 else if(sock->sa_family == AF_INET6) {
328 socklen_t len = sizeof(struct sockaddr_in6); 315 socklen_t len = sizeof(struct sockaddr_in6);
329 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) { 316 if((status = getnameinfo(sock, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) == 0) {
330 //verification for resolved hostname. 317 //verification for resolved hostname.
331 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf); 318 if(strchr(hbuf, ':')) return xmsprintf("[%s]:%s", hbuf, sbuf);
332 else return xmsprintf("%s:%s", hbuf, sbuf); 319 else return xmsprintf("%s:%s", hbuf, sbuf);
333 } 320 } else {
334 else {
335 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status)); 321 fprintf(stderr, "getnameinfo: %s\n", gai_strerror(status));
336 return NULL; 322 return NULL;
337 } 323 }
338 } 324 } else if(sock->sa_family == AF_UNIX) {
339 else if(sock->sa_family == AF_UNIX) {
340 struct sockaddr_un *sockun = (void*)sock; 325 struct sockaddr_un *sockun = (void*)sock;
341 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path); 326 return xmsprintf("local:%.*s", (int) sizeof(sockun->sun_path), sockun->sun_path);
342 } 327 } else return NULL;
343 else
344 return NULL;
345 } 328 }
346 329
347 /* 330 /*
348 * used to converts string into int and validate the input str for invalid int value or out-of-range. 331 * used to converts string into int and validate the input str for invalid int value or out-of-range.
349 */ 332 */
397 int sockfd = 0; 380 int sockfd = 0;
398 //get interface name 381 //get interface name
399 memset(&ifre, 0, sizeof(struct ifreq)); 382 memset(&ifre, 0, sizeof(struct ifreq));
400 strncpy(ifre.ifr_name, *argv, IFNAMSIZ); 383 strncpy(ifre.ifr_name, *argv, IFNAMSIZ);
401 ifre.ifr_name[IFNAMSIZ-1] = 0; 384 ifre.ifr_name[IFNAMSIZ-1] = 0;
402 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 385 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) perror_exit("socket");
403 perror_exit("socket");
404 386
405 while(*++argv != NULL) { 387 while(*++argv != NULL) {
406 /* flags settings */ 388 /* flags settings */
407 if (!strcmp(*argv, "up")) 389 if (!strcmp(*argv, "up"))
408 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); 390 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
465 } else if (!strcmp(*argv, "txqueuelen")) { 447 } else if (!strcmp(*argv, "txqueuelen")) {
466 if (!*++argv) show_help(); 448 if (!*++argv) show_help();
467 set_qlen(sockfd, &ifre, *argv); 449 set_qlen(sockfd, &ifre, *argv);
468 } else if (!strcmp(*argv, "keepalive")) { 450 } else if (!strcmp(*argv, "keepalive")) {
469 if (!*++argv) show_help(); 451 if (!*++argv) show_help();
470 set_data(sockfd, &ifre, *argv, SIOCSKEEPALIVE, "SIOCSKEEPALIVE"); 452 ifre.ifr_data = (void *)strtoul(*argv, 0, 0);
471 }//end of keepalive 453 xioctl(sockfd, SIOCSKEEPALIVE, &ifre);
472 else if (!strcmp(*argv, "outfill")) { 454 } else if (!strcmp(*argv, "outfill")) {
473 if (!*++argv) show_help(); 455 if (!*++argv) show_help();
474 set_data(sockfd, &ifre, *argv, SIOCSOUTFILL, "SIOCSOUTFILL"); 456 ifre.ifr_data = (void *)strtoul(*argv, 0, 0);
457 xioctl(sockfd, SIOCSOUTFILL, &ifre);
475 } else if (!strcmp(*argv, "add")) { 458 } else if (!strcmp(*argv, "add")) {
476 if (!*++argv) show_help(); 459 if (!*++argv) show_help();
477 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR"); 460 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR");
478 } else if (!strcmp(*argv, "del")) { 461 } else if (!strcmp(*argv, "del")) {
479 if (!*++argv) show_help(); 462 if (!*++argv) show_help();
486 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); 469 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
487 } else if (!strcmp(*argv, "irq")) { 470 } else if (!strcmp(*argv, "irq")) {
488 if (!*++argv) show_help(); 471 if (!*++argv) show_help();
489 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); 472 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
490 } else { 473 } else {
491 if(isdigit(**argv) || !strcmp(*argv, "default")) { 474 if (isdigit(**argv) || !strcmp(*argv, "default")) {
492 char *iface_name = ifre.ifr_name; 475 char *iface_name = ifre.ifr_name;
493 short int is_colon = 0; 476 short int is_colon = 0;
494 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR"); 477 set_address(sockfd, *argv, &ifre, SIOCSIFADDR, "SIOCSIFADDR");
495 while(*iface_name) { 478 while (*iface_name) {
496 if(*iface_name == ':') { 479 if (*iface_name == ':') {
497 is_colon = 1; 480 is_colon = 1;
498 break; 481 break;
499 } 482 }
500 iface_name++; 483 iface_name++;
501 } 484 }
502 //if the interface name is not an alias; set the flag and continue. 485 //if the interface name is not an alias; set the flag and continue.
503 if(!is_colon) set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0); 486 if(!is_colon) set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
504 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) 487 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) continue;
505 continue;
506 else { 488 else {
507 errno = EINVAL; 489 errno = EINVAL;
508 toys.exithelp++; 490 toys.exithelp++;
509 error_exit("bad argument"); 491 error_exit("bad argument");
510 } 492 }
516 } 498 }
517 499
518 500
519 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag) 501 static void set_flags(int sockfd, struct ifreq *ifre, int set_flag, int reset_flag)
520 { 502 {
521 if(ioctl(sockfd, SIOCGIFFLAGS, ifre) < 0) 503 xioctl(sockfd, SIOCGIFFLAGS, ifre);
522 perror_exit("SIOCGIFFLAGS");
523 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag; 504 ifre->ifr_flags = (ifre->ifr_flags & (~reset_flag)) | set_flag;
524 if(ioctl(sockfd, SIOCSIFFLAGS, ifre) < 0) 505 xioctl(sockfd, SIOCSIFFLAGS, ifre);
525 perror_exit("SIOCSIFFLAGS"); 506 }
526 return; 507
527 }
528
529 static void set_data(int sockfd, struct ifreq *ifre, char *kval, int request, char *req_name)
530 {
531 unsigned long val = strtoul(kval, NULL, 0);
532 char *ptr;
533 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_data);
534 (*(char * *)ptr) = (char *)val;
535
536 if(ioctl(sockfd, request, ifre) < 0) {
537 perror_exit((char *)req_name);
538 }
539 return;
540 }
541 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu) 508 static void set_mtu(int sockfd, struct ifreq *ifre, char *mtu)
542 { 509 {
543 ifre->ifr_mtu = strtoul(mtu, NULL, 0); 510 ifre->ifr_mtu = strtoul(mtu, NULL, 0);
544 if(ioctl(sockfd, SIOCSIFMTU, ifre) < 0) 511 xioctl(sockfd, SIOCSIFMTU, ifre);
545 perror_exit("SIOCSIFMTU");
546 return;
547 } 512 }
548 513
549 static void set_metric(int sockfd, struct ifreq *ifre, char *metric) 514 static void set_metric(int sockfd, struct ifreq *ifre, char *metric)
550 { 515 {
551 ifre->ifr_metric = strtoul(metric, NULL, 0); 516 ifre->ifr_metric = strtoul(metric, NULL, 0);
552 if(ioctl(sockfd, SIOCSIFMETRIC, ifre) < 0) 517 xioctl(sockfd, SIOCSIFMETRIC, ifre);
553 perror_exit("SIOCSIFMETRIC");
554 return;
555 } 518 }
556 519
557 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen) 520 static void set_qlen(int sockfd, struct ifreq *ifre, char *qlen)
558 { 521 {
559 ifre->ifr_qlen = strtoul(qlen, NULL, 0); 522 ifre->ifr_qlen = strtoul(qlen, NULL, 0);
560 if(ioctl(sockfd, SIOCSIFTXQLEN, ifre) < 0) 523 xioctl(sockfd, SIOCSIFTXQLEN, ifre);
561 perror_exit("SIOCSIFTXQLEN");
562 return;
563 } 524 }
564 525
565 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name) 526 static void set_ipv6_addr(int sockfd, struct ifreq *ifre, char *ipv6_addr, int request, char *req_name)
566 { 527 {
567 char *prefix; 528 char *prefix;
581 (char *) &(swl->sock_u.sock_in6.sin6_addr), 542 (char *) &(swl->sock_u.sock_in6.sin6_addr),
582 sizeof(struct in6_addr)); 543 sizeof(struct in6_addr));
583 //Create a channel to the NET kernel. 544 //Create a channel to the NET kernel.
584 if( (sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 545 if( (sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
585 perror_exit("AF_INET6 SOCK_DGRAM", 0); 546 perror_exit("AF_INET6 SOCK_DGRAM", 0);
586 if(ioctl(sockfd6, SIOGIFINDEX, ifre) < 0) 547 xioctl(sockfd6, SIOGIFINDEX, ifre);
587 perror_exit("SIOGIFINDEX");
588 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex; 548 ifre6.ifrinet6_ifindex = ifre->ifr_ifindex;
589 ifre6.ifrinet6_prefixlen = plen; 549 ifre6.ifrinet6_prefixlen = plen;
590 550
591 if(ioctl(sockfd6, request, &ifre6) < 0) 551 xioctl(sockfd6, request, &ifre6);
592 perror_exit((char *)req_name);
593 if(swl != NULL) { 552 if(swl != NULL) {
594 free(swl); 553 free(swl);
595 swl = NULL; 554 swl = NULL;
596 } 555 }
597 return;
598 } 556 }
599 557
600 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name) 558 static void set_address(int sockfd, char *host_name, struct ifreq *ifre, int request, char *req_name)
601 { 559 {
602 struct sockaddr_in sock_in; 560 struct sockaddr_in sock_in;
603 sockaddr_with_len *swl = NULL; 561 sockaddr_with_len *swl = NULL;
604 sock_in.sin_family = AF_INET; 562 sock_in.sin_family = AF_INET;
605 sock_in.sin_port = 0; 563 sock_in.sin_port = 0;
606 564
607 //Default 0.0.0.0 565 //Default 0.0.0.0
608 if(strcmp(host_name, "default") == 0) 566 if(strcmp(host_name, "default") == 0) sock_in.sin_addr.s_addr = INADDR_ANY;
609 sock_in.sin_addr.s_addr = INADDR_ANY;
610 else { 567 else {
611 swl = get_sockaddr(host_name, 0, AF_INET); 568 swl = get_sockaddr(host_name, 0, AF_INET);
612 if(!swl) error_exit("error in resolving host name"); 569 if(!swl) error_exit("error in resolving host name");
613 570
614 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr; 571 sock_in.sin_addr = swl->sock_u.sock_in.sin_addr;
615 } 572 }
616 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr)); 573 memcpy((char *)&ifre->ifr_addr, (char *) &sock_in, sizeof(struct sockaddr));
617 if(ioctl(sockfd, request, ifre) < 0) 574 xioctl(sockfd, request, ifre);
618 perror_exit((char *)req_name);
619 575
620 if(swl != NULL) { 576 if(swl != NULL) {
621 free(swl); 577 free(swl);
622 swl = NULL; 578 swl = NULL;
623 } 579 }
624 return;
625 } 580 }
626 581
627 static int hex_to_binary(char *hw_addr, struct sockaddr *sock, int count) 582 static int hex_to_binary(char *hw_addr, struct sockaddr *sock, int count)
628 { 583 {
629 int i = 0, j = 0; 584 int i = 0, j = 0;
630 unsigned char nib_val; 585 unsigned char nib_val, ch;
631 unsigned char ch;
632 586
633 char *ptr = (char *) sock->sa_data; 587 char *ptr = (char *) sock->sa_data;
634 if(count == ETH_ALEN) 588 if (count == ETH_ALEN) sock->sa_family = ARPHRD_ETHER;
635 sock->sa_family = ARPHRD_ETHER; 589 else if (count == INFINIBAND_ALEN) sock->sa_family = ARPHRD_INFINIBAND;
636 else if(count == INFINIBAND_ALEN) 590 else return -1;
637 sock->sa_family = ARPHRD_INFINIBAND;
638 else
639 return -1;
640 //e.g. hw_addr "62:2D:A6:9E:2D:BE" 591 //e.g. hw_addr "62:2D:A6:9E:2D:BE"
641 for(; *hw_addr && (i < count); i++) { 592 for (; *hw_addr && (i < count); i++) {
642 if(*hw_addr == ':') 593 if (*hw_addr == ':') hw_addr++;
643 hw_addr++;
644 j = nib_val = 0; 594 j = nib_val = 0;
645 for(;j < 2; j++) { 595 for (;j < 2; j++) {
646 ch = *hw_addr; 596 ch = *hw_addr;
647 //0-9 = 10 chars. 597 //0-9 = 10 chars.
648 if(((unsigned char)(ch - '0')) < 10) 598 if (((unsigned char)(ch - '0')) < 10) ch = (ch - '0');
649 ch = (ch - '0');
650 //a-f = 6 chars. 599 //a-f = 6 chars.
651 else if(((unsigned char)((ch) - 'a')) < 6) 600 else if (((unsigned char)((ch) - 'a')) < 6) ch = (ch - ('a'-10));
652 ch = (ch - ('a'-10));
653 //A-F = 6 chars. 601 //A-F = 6 chars.
654 else if(((unsigned char)((ch) - 'A')) < 6) 602 else if (((unsigned char)((ch) - 'A')) < 6) ch = (ch - ('A'-10));
655 ch = (ch - ('A'-10)); 603 else if (j && (ch == ':' || ch == 0)) break;
656 else if(j && (ch == ':' || ch == 0)) 604 else return -1;
657 break;
658 else
659 return -1;
660 hw_addr++; 605 hw_addr++;
661 nib_val <<= 4; 606 nib_val <<= 4;
662 nib_val += ch; 607 nib_val += ch;
663 } 608 }
664 *ptr++ = nib_val; 609 *ptr++ = nib_val;
665 } 610 }
666 if(*hw_addr) 611 if (*hw_addr) return -1;
667 return -1;
668 return 0; 612 return 0;
669 } 613 }
670 614
671 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name) 615 static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int request, char *req_name)
672 { 616 {
700 if(hex_to_binary(hw_addr, &sock, INFINIBAND_ALEN)) 644 if(hex_to_binary(hw_addr, &sock, INFINIBAND_ALEN))
701 error_exit("invalid hw-addr %s", hw_addr); 645 error_exit("invalid hw-addr %s", hw_addr);
702 } 646 }
703 ptr = (char *)&sock; 647 ptr = (char *)&sock;
704 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr)); 648 memcpy( ((char *) ifre) + offsetof(struct ifreq, ifr_hwaddr), ptr, sizeof(struct sockaddr));
705 if(ioctl(sockfd, request, ifre) < 0) 649 xioctl(sockfd, request, ifre);
706 perror_exit((char *)req_name);
707 return;
708 } 650 }
709 651
710 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name) 652 static void set_memstart(int sockfd, struct ifreq *ifre, char *start_addr, int request, char *req_name)
711 { 653 {
712 unsigned long mem_start = strtoul(start_addr, NULL, 0); 654 unsigned long mem_start = strtoul(start_addr, NULL, 0);
713 655
714 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) 656 xioctl(sockfd, SIOCGIFMAP, ifre);
715 perror_exit("SIOCGIFMAP");
716 ifre->ifr_map.mem_start = mem_start; 657 ifre->ifr_map.mem_start = mem_start;
717 if(ioctl(sockfd, request, ifre) < 0) 658 xioctl(sockfd, request, ifre);
718 perror_exit((char *)req_name);
719 return;
720 } 659 }
721 660
722 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name) 661 static void set_ioaddr(int sockfd, struct ifreq *ifre, char *baddr, int request, char *req_name)
723 { 662 {
724 unsigned short int base_addr = strtoul(baddr, NULL, 0); 663 unsigned short int base_addr = strtoul(baddr, NULL, 0);
725 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) 664 xioctl(sockfd, SIOCGIFMAP, ifre);
726 perror_exit("SIOCGIFMAP");
727 ifre->ifr_map.base_addr = base_addr; 665 ifre->ifr_map.base_addr = base_addr;
728 if(ioctl(sockfd, request, ifre) < 0) 666 xioctl(sockfd, request, ifre);
729 perror_exit((char *)req_name);
730 return;
731 } 667 }
732 668
733 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name) 669 static void set_irq(int sockfd, struct ifreq *ifre, char *irq_val, int request, char *req_name)
734 { 670 {
735 unsigned short int irq = strtoul(irq_val, NULL, 0); 671 unsigned short int irq = strtoul(irq_val, NULL, 0);
736 char *ptr; 672 char *ptr;
737 struct ifmap *map; 673 struct ifmap *map;
738 674
739 if(ioctl(sockfd, SIOCGIFMAP, ifre) < 0) perror_exit("SIOCGIFMAP"); 675 xioctl(sockfd, SIOCGIFMAP, ifre);
740 676
741 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map); 677 ptr = ((char *) ifre) + offsetof(struct ifreq, ifr_map);
742 map = (struct ifmap *)ptr; 678 map = (struct ifmap *)ptr;
743 map->irq = irq; 679 map->irq = irq;
744 if(ioctl(sockfd, request, ifre) < 0) perror_exit(req_name); 680 xioctl(sockfd, request, ifre);
745 return;
746 } 681 }
747 682
748 /* Display ifconfig info. */ 683 /* Display ifconfig info. */
749 static void get_proc_info(char *buff, IFACE_LIST *l_ptr, int version) 684 static void get_proc_info(char *buff, IFACE_LIST *l_ptr, int version)
750 { 685 {
785 720
786 if(version == 0) 721 if(version == 0)
787 l_ptr->dev_info.receive_bytes = l_ptr->dev_info.transmit_bytes = 0; 722 l_ptr->dev_info.receive_bytes = l_ptr->dev_info.transmit_bytes = 0;
788 if(version == 1) 723 if(version == 1)
789 l_ptr->dev_info.receive_multicast = l_ptr->dev_info.receive_compressed = l_ptr->dev_info.transmit_compressed = 0; 724 l_ptr->dev_info.receive_multicast = l_ptr->dev_info.receive_compressed = l_ptr->dev_info.transmit_compressed = 0;
790 return;
791 } 725 }
792 726
793 static void add_iface_to_list(IFACE_LIST *newnode) 727 static void add_iface_to_list(IFACE_LIST *newnode)
794 { 728 {
795 IFACE_LIST *head_ref = iface_list_head; 729 IFACE_LIST *head_ref = iface_list_head;
943 if(hw_type == ARPHRD_ETHER) { 877 if(hw_type == ARPHRD_ETHER) {
944 int i; 878 int i;
945 879
946 for (i=0; i<6; i++) xprintf(":%02X"+!i, address[i]); 880 for (i=0; i<6; i++) xprintf(":%02X"+!i, address[i]);
947 } 881 }
948
949 return;
950 } 882 }
951 883
952 static char *get_ip_addr(struct sockaddr *skaddr) 884 static char *get_ip_addr(struct sockaddr *skaddr)
953 { 885 {
954 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr; 886 struct sockaddr_in *sin = (struct sockaddr_in *)skaddr;
975 if(l_ptr->ifrflags & IFF_POINTOPOINT) 907 if(l_ptr->ifrflags & IFF_POINTOPOINT)
976 xprintf(" P-t-P:%s ", get_ip_addr(&l_ptr->ifrdstaddr)); 908 xprintf(" P-t-P:%s ", get_ip_addr(&l_ptr->ifrdstaddr));
977 if(l_ptr->ifrflags & IFF_BROADCAST) 909 if(l_ptr->ifrflags & IFF_BROADCAST)
978 xprintf(" Bcast:%s ", get_ip_addr(&l_ptr->ifrbroadaddr)); 910 xprintf(" Bcast:%s ", get_ip_addr(&l_ptr->ifrbroadaddr));
979 xprintf(" Mask:%s\n", get_ip_addr(&l_ptr->ifrnetmask)); 911 xprintf(" Mask:%s\n", get_ip_addr(&l_ptr->ifrnetmask));
980 return;
981 } 912 }
982 913
983 static void print_iface_flags(IFACE_LIST *l_ptr) 914 static void print_iface_flags(IFACE_LIST *l_ptr)
984 { 915 {
985 if (l_ptr->ifrflags != 0) { 916 if (l_ptr->ifrflags != 0) {
986 unsigned short mask = 1; 917 unsigned short mask = 1;
987 char **str = iface_flags_str; 918 char **str = iface_flags_str;
988 919
989 for(; *str != NULL; str++) { 920 for(; *str != NULL; str++) {
990 if(l_ptr->ifrflags & mask) 921 if(l_ptr->ifrflags & mask) xprintf("%s ", *str);
991 xprintf("%s ", *str);
992 mask = mask << 1; 922 mask = mask << 1;
993 } 923 }
994 } else xprintf("[NO FLAGS] "); 924 } else xprintf("[NO FLAGS] ");
995 return;
996 } 925 }
997 926
998 static void print_media(IFACE_LIST *l_ptr) 927 static void print_media(IFACE_LIST *l_ptr)
999 { 928 {
1000 #ifdef IFF_PORTSEL 929 #ifdef IFF_PORTSEL
1016 xprintf("%s", "100baseFX"); 945 xprintf("%s", "100baseFX");
1017 if(l_ptr->ifrflags & IFF_AUTOMEDIA) 946 if(l_ptr->ifrflags & IFF_AUTOMEDIA)
1018 xprintf("(auto)"); 947 xprintf("(auto)");
1019 } 948 }
1020 #endif 949 #endif
1021 return;
1022 } 950 }
1023 951
1024 static void print_ip6_addr(IFACE_LIST *l_ptr) 952 static void print_ip6_addr(IFACE_LIST *l_ptr)
1025 { 953 {
1026 char iface_name[IFNAMSIZ] = {0,}; 954 char iface_name[IFNAMSIZ] = {0,};
1027 char buf[BUFSIZ] = {0,}; 955 char buf[BUFSIZ] = {0,};
1028 int plen, scope; 956 int plen, scope;
1029 957
1030 FILE *fp = fopen("/proc/net/if_inet6", "r"); 958 FILE *fp = fopen("/proc/net/if_inet6", "r");
1031 if(fp == NULL) 959 if(!fp) return;
1032 return;
1033 960
1034 while(fgets(buf, BUFSIZ, fp)) { 961 while(fgets(buf, BUFSIZ, fp)) {
1035 int nitems = 0; 962 int nitems = 0;
1036 char ipv6_addr[40] = {0,}; 963 char ipv6_addr[40] = {0,};
1037 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n", 964 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n",
1067 else xprintf("Unknown"); 994 else xprintf("Unknown");
1068 xprintf("\n"); 995 xprintf("\n");
1069 } 996 }
1070 } 997 }
1071 } 998 }
1072 }//end of while. 999 }
1073 fclose(fp); 1000 fclose(fp);
1074 fp = NULL;
1075 return;
1076 } 1001 }
1077 1002
1078 static void display_ifconfig(IFACE_LIST *l_ptr) 1003 static void display_ifconfig(IFACE_LIST *l_ptr)
1079 { 1004 {
1080 HW_INFO hw_info; 1005 HW_INFO hw_info;
1133 if(l_ptr->ifrmap.dma) 1058 if(l_ptr->ifrmap.dma)
1134 xprintf("DMA chan:%x ", l_ptr->ifrmap.dma); 1059 xprintf("DMA chan:%x ", l_ptr->ifrmap.dma);
1135 xputc('\n'); 1060 xputc('\n');
1136 } 1061 }
1137 xputc('\n'); 1062 xputc('\n');
1138 return;
1139 } 1063 }
1140 1064
1141 static int readconf(void) 1065 static int readconf(void)
1142 { 1066 {
1143 int num_of_req = 30; 1067 int num_of_req = 30;
1163 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) { 1087 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) {
1164 num_of_req += 10; 1088 num_of_req += 10;
1165 continue; 1089 continue;
1166 } 1090 }
1167 break; 1091 break;
1168 }//End of while loop 1092 }
1169 1093
1170 ifre = ifcon.ifc_req; 1094 ifre = ifcon.ifc_req;
1171 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) { 1095 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) {
1172 //Escape duplicate values from the list. 1096 //Escape duplicate values from the list.
1173 IFACE_LIST *list_ptr; 1097 IFACE_LIST *list_ptr;
1243 while(iface_list_head != NULL) { 1167 while(iface_list_head != NULL) {
1244 temp_ptr = iface_list_head->next; 1168 temp_ptr = iface_list_head->next;
1245 free(iface_list_head); 1169 free(iface_list_head);
1246 iface_list_head = temp_ptr; 1170 iface_list_head = temp_ptr;
1247 } 1171 }
1248 return; 1172 }
1249 }