comparison toys/pending/ifconfig.c @ 863:493f412fc5da

Fix the help commit I screwed up, and replace leading tabs with spaces.
author Rob Landley <rob@landley.net>
date Tue, 16 Apr 2013 23:09:50 -0500
parents 53cee02f1811
children 764df39f62b4
comparison
equal deleted inserted replaced
862:53cee02f1811 863:493f412fc5da
379 379
380 void ifconfig_main(void) 380 void ifconfig_main(void)
381 { 381 {
382 char **argv = toys.optargs; 382 char **argv = toys.optargs;
383 383
384 if(*argv && (strcmp(*argv, "--help") == 0)) 384 if(*argv && (strcmp(*argv, "--help") == 0)) show_help();
385 show_help();
386 385
387 //"ifconfig" / "ifconfig eth0" 386 //"ifconfig" / "ifconfig eth0"
388 if(!argv[0] || !argv[1]) { //one or no argument 387 if(!argv[0] || !argv[1]) { //one or no argument
389 toys.exitval = show_iface(*argv); 388 toys.exitval = show_iface(*argv);
390 //free allocated memory. 389 //free allocated memory.
437 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC); 436 set_flags(sockfd, &ifre, 0, IFF_DYNAMIC);
438 else if (!strcmp(*argv, "-pointopoint")) 437 else if (!strcmp(*argv, "-pointopoint"))
439 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT); 438 set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT);
440 /*value setup */ 439 /*value setup */
441 else if (!strcmp(*argv, "pointopoint")) { 440 else if (!strcmp(*argv, "pointopoint")) {
442 show_help(); 441 if (!*++argv) show_help();
443 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); 442 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
444 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0); 443 set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0);
445 } else if (!strcmp(*argv, "netmask")) { 444 } else if (!strcmp(*argv, "netmask")) {
446 show_help(); 445 if (!*++argv) show_help();
447 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK"); 446 set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK");
448 } else if (!strcmp(*argv, "-broadcast")) { 447 } else if (!strcmp(*argv, "-broadcast")) {
449 set_flags(sockfd, &ifre, 0, IFF_BROADCAST); 448 set_flags(sockfd, &ifre, 0, IFF_BROADCAST);
450 } else if (!strcmp(*argv, "broadcast")) { 449 } else if (!strcmp(*argv, "broadcast")) {
451 show_help(); 450 if (!*++argv) show_help();
452 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR"); 451 set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR");
453 set_flags(sockfd, &ifre, IFF_BROADCAST, 0); 452 set_flags(sockfd, &ifre, IFF_BROADCAST, 0);
454 } else if (!strcmp(*argv, "dstaddr")) { 453 } else if (!strcmp(*argv, "dstaddr")) {
455 show_help(); 454 if (!*++argv) show_help();
456 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR"); 455 set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
457 } else if (!strcmp(*argv, "hw")) { 456 } else if (!strcmp(*argv, "hw")) {
458 show_help(); 457 if (!*++argv) show_help();
459 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR"); 458 set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR");
460 } else if (!strcmp(*argv, "mtu")) { 459 } else if (!strcmp(*argv, "mtu")) {
461 show_help(); 460 if (!*++argv) show_help();
462 set_mtu(sockfd, &ifre, *argv); 461 set_mtu(sockfd, &ifre, *argv);
463 } else if (!strcmp(*argv, "metric")) { 462 } else if (!strcmp(*argv, "metric")) {
464 show_help(); 463 if (!*++argv) show_help();
465 set_metric(sockfd, &ifre, *argv); 464 set_metric(sockfd, &ifre, *argv);
466 } else if (!strcmp(*argv, "txqueuelen")) { 465 } else if (!strcmp(*argv, "txqueuelen")) {
467 show_help(); 466 if (!*++argv) show_help();
468 set_qlen(sockfd, &ifre, *argv); 467 set_qlen(sockfd, &ifre, *argv);
469 } else if (!strcmp(*argv, "keepalive")) { 468 } else if (!strcmp(*argv, "keepalive")) {
470 show_help(); 469 if (!*++argv) show_help();
471 set_data(sockfd, &ifre, *argv, SIOCSKEEPALIVE, "SIOCSKEEPALIVE"); 470 set_data(sockfd, &ifre, *argv, SIOCSKEEPALIVE, "SIOCSKEEPALIVE");
472 }//end of keepalive 471 }//end of keepalive
473 else if (!strcmp(*argv, "outfill")) { 472 else if (!strcmp(*argv, "outfill")) {
474 show_help(); 473 if (!*++argv) show_help();
475 set_data(sockfd, &ifre, *argv, SIOCSOUTFILL, "SIOCSOUTFILL"); 474 set_data(sockfd, &ifre, *argv, SIOCSOUTFILL, "SIOCSOUTFILL");
476 } else if (!strcmp(*argv, "add")) { 475 } else if (!strcmp(*argv, "add")) {
477 show_help(); 476 if (!*++argv) show_help();
478 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR"); 477 set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR");
479 } else if (!strcmp(*argv, "del")) { 478 } else if (!strcmp(*argv, "del")) {
480 show_help(); 479 if (!*++argv) show_help();
481 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR"); 480 set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR");
482 } else if (!strcmp(*argv, "mem_start")) { 481 } else if (!strcmp(*argv, "mem_start")) {
483 show_help(); 482 if (!*++argv) show_help();
484 set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); 483 set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
485 } else if (!strcmp(*argv, "io_addr")) { 484 } else if (!strcmp(*argv, "io_addr")) {
486 show_help(); 485 if (!*++argv) show_help();
487 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); 486 set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
488 } else if (!strcmp(*argv, "irq")) { 487 } else if (!strcmp(*argv, "irq")) {
489 show_help(); 488 if (!*++argv) show_help();
490 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP"); 489 set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
491 } else { 490 } else {
492 if(isdigit(**argv) || !strcmp(*argv, "default")) { 491 if(isdigit(**argv) || !strcmp(*argv, "default")) {
493 char *iface_name = ifre.ifr_name; 492 char *iface_name = ifre.ifr_name;
494 short int is_colon = 0; 493 short int is_colon = 0;
499 break; 498 break;
500 } 499 }
501 iface_name++; 500 iface_name++;
502 } 501 }
503 //if the interface name is not an alias; set the flag and continue. 502 //if the interface name is not an alias; set the flag and continue.
504 if(!is_colon) 503 if(!is_colon) set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
505 set_flags(sockfd, &ifre, IFF_UP | IFF_RUNNING, 0);
506 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6")) 504 } else if (!strcmp(*argv, "inet") || !strcmp(*argv, "inet6"))
507 continue; 505 continue;
508 else { 506 else {
509 errno = EINVAL; 507 errno = EINVAL;
510 toys.exithelp++; 508 toys.exithelp++;
511 error_exit("bad argument"); 509 error_exit("bad argument");
512 } 510 }
513 } 511 }
514 512
515 } 513 }
516 if(sockfd > 0) close(sockfd); 514 if(sockfd > 0) close(sockfd);
1029 char buf[BUFSIZ] = {0,}; 1027 char buf[BUFSIZ] = {0,};
1030 int plen, scope; 1028 int plen, scope;
1031 1029
1032 FILE *fp = fopen("/proc/net/if_inet6", "r"); 1030 FILE *fp = fopen("/proc/net/if_inet6", "r");
1033 if(fp == NULL) 1031 if(fp == NULL)
1034 return; 1032 return;
1035 1033
1036 while(fgets(buf, BUFSIZ, fp)) { 1034 while(fgets(buf, BUFSIZ, fp)) {
1037 int nitems = 0; 1035 int nitems = 0;
1038 char ipv6_addr[40] = {0,}; 1036 char ipv6_addr[40] = {0,};
1039 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n", 1037 nitems = sscanf(buf, "%32s %*08x %02x %02x %*02x %15s\n",
1140 return; 1138 return;
1141 } 1139 }
1142 1140
1143 static int readconf(void) 1141 static int readconf(void)
1144 { 1142 {
1145 int num_of_req = 30; 1143 int num_of_req = 30;
1146 struct ifconf ifcon; 1144 struct ifconf ifcon;
1147 struct ifreq *ifre; 1145 struct ifreq *ifre;
1148 int num, status = -1, sokfd; 1146 int num, status = -1, sokfd;
1149 1147
1150 ifcon.ifc_buf = NULL; 1148 ifcon.ifc_buf = NULL;
1151 sokfd = socket(AF_INET, SOCK_DGRAM, 0); 1149 sokfd = socket(AF_INET, SOCK_DGRAM, 0);
1152 if(sokfd < 0) { 1150 if(sokfd < 0) {
1153 perror_msg("error: no inet socket available"); 1151 perror_msg("error: no inet socket available");
1154 return -1; 1152 return -1;
1155 } 1153 }
1156 for (;;) { 1154 for (;;) {
1157 ifcon.ifc_len = sizeof(struct ifreq) * num_of_req; //Size of buffer. 1155 ifcon.ifc_len = sizeof(struct ifreq) * num_of_req; //Size of buffer.
1158 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len); 1156 ifcon.ifc_buf = xrealloc(ifcon.ifc_buf, ifcon.ifc_len);
1159 1157
1160 if((status = ioctl(sokfd, SIOCGIFCONF, &ifcon)) == -1) { 1158 if((status = ioctl(sokfd, SIOCGIFCONF, &ifcon)) == -1) {
1161 perror_msg("ioctl %#x failed", SIOCGIFCONF); 1159 perror_msg("ioctl %#x failed", SIOCGIFCONF);
1162 goto LOOP_BREAK; 1160 goto LOOP_BREAK;
1163 } 1161 }
1164 //in case of overflow, increase number of requests and retry. 1162 //in case of overflow, increase number of requests and retry.
1165 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) { 1163 if (ifcon.ifc_len == (int)(sizeof(struct ifreq) * num_of_req)) {
1166 num_of_req += 10; 1164 num_of_req += 10;
1167 continue; 1165 continue;
1168 } 1166 }
1169 break; 1167 break;
1170 }//End of while loop 1168 }//End of while loop
1171 1169
1172 ifre = ifcon.ifc_req; 1170 ifre = ifcon.ifc_req;
1173 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) { 1171 for(num = 0; num < ifcon.ifc_len && ifre; num += sizeof(struct ifreq), ifre++) {
1174 //Escape duplicate values from the list. 1172 //Escape duplicate values from the list.
1175 IFACE_LIST *list_ptr; 1173 IFACE_LIST *list_ptr;
1176 int match_found = 0; 1174 int match_found = 0;
1177 for(list_ptr = iface_list_head; list_ptr != NULL; list_ptr = list_ptr->next) { 1175 for(list_ptr = iface_list_head; list_ptr != NULL; list_ptr = list_ptr->next) {
1178 //if interface already in the list then donot add it in the list. 1176 //if interface already in the list then donot add it in the list.
1179 if(!strcmp(ifre->ifr_name, list_ptr->dev_info.ifrname)) { 1177 if(!strcmp(ifre->ifr_name, list_ptr->dev_info.ifrname)) {
1180 match_found = 1; 1178 match_found = 1;
1181 break; 1179 break;
1182 } 1180 }
1183 } 1181 }
1184 if(!match_found) { 1182 if(!match_found) {
1185 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST)); 1183 IFACE_LIST *l_ptr = xzalloc(sizeof(IFACE_LIST));
1186 safe_strncpy(l_ptr->dev_info.ifrname, ifre->ifr_name, IFNAMSIZ); 1184 safe_strncpy(l_ptr->dev_info.ifrname, ifre->ifr_name, IFNAMSIZ);
1187 add_iface_to_list(l_ptr); 1185 add_iface_to_list(l_ptr);
1188 errno = 0; 1186 errno = 0;
1189 if(get_device_info(l_ptr) < 0) 1187 if(get_device_info(l_ptr) < 0)
1190 perror_exit("%s", l_ptr->dev_info.ifrname); 1188 perror_exit("%s", l_ptr->dev_info.ifrname);
1191 } 1189 }
1192 } 1190 }
1193 1191
1194 LOOP_BREAK: 1192 LOOP_BREAK:
1195 close(sokfd); 1193 close(sokfd);
1196 free(ifcon.ifc_buf); 1194 free(ifcon.ifc_buf);
1197 1195
1198 return status; 1196 return status;
1199 } 1197 }
1200 1198
1201 static int show_iface(char *iface_name) 1199 static int show_iface(char *iface_name)
1202 { 1200 {
1203 get_ifconfig_info(); 1201 get_ifconfig_info();