- int ret = -1;
- int silent = 0;
- int i;
- struct vc_ip_mask_pair ips[16];
- int nbaddrs = 0;
- unsigned long bcast = 0xffffffff;
- for (i=1; i<argc; i++){
- const char *arg = argv[i];
- const char *opt = argv[i+1];
- if (strcmp(arg,"--ip")==0){
- unsigned long addr,mask;
- if (nbaddrs == 16){
- fprintf (stderr,"Too many IP numbers, max 16, ignored\n");
-
- }else if (ifconfig_getaddr(opt,&addr,&mask,&bcast)==-1){
- unsigned long mask = 0x00ffffff;
- const char *pt = strchr(opt,'/');
- char tmpopt[strlen(opt)+1];
- struct hostent *h;
-
- if (pt != NULL){
- strcpy (tmpopt,opt);
- tmpopt[pt-opt] = '\0';
- opt = tmpopt;
- pt++;
- if (strchr(pt,'.')==NULL){
- // Ok, we have a network size, not a netmask
- int size = atoi(pt);
- int i;
- mask = 0;
- for (i=0; i<size; i++){
- mask = mask >> 1;
- mask |= 0x80000000;
- }
- mask = ntohl(mask);
- }else{
- struct hostent *h = gethostbyname (pt);
- if (h != NULL){
- memcpy (&mask,h->h_addr,sizeof(mask));
- }else{
- fprintf (stderr,"Invalid netmask: %s\n",pt);
- usage();
- }
- }
-
- }
-
- h = gethostbyname (opt);
- if (h == NULL){
- fprintf (stderr,"Invalid IP number or host name: %s\n",opt);
- usage();
- }else{
- memcpy (&addr,h->h_addr,sizeof(addr));
- ips[nbaddrs].ip = addr;
- ips[nbaddrs].mask = mask;
- ++nbaddrs;
- }
- }else{
- ips[nbaddrs].ip = addr;
- ips[nbaddrs].mask = mask;
- ++nbaddrs;
- }
- i++;
- }else if (strcmp(arg,"--bcast")==0){
- unsigned long tmp;
- if (ifconfig_getaddr(opt,&tmp,&tmp,&bcast)==-1){
- struct hostent *h = gethostbyname (opt);
- if (h == NULL){
- fprintf (stderr,"Invalid broadcast number: %s\n",opt);
- usage();
- }else{
- memcpy (&bcast,h->h_addr,sizeof(bcast));
- }
- }
- i++;
- }else if (strcmp(arg,"--silent")==0){
- silent = 1;
- }else{
- break;
- }
- }
- if (i == argc){
- usage();
- }else if (argv[i][0] == '-'){
- usage();
- }else{
- if (vc_set_ipv4root(bcast,nbaddrs,ips)==0){
- if (!silent){
- int i;
- printf ("ipv4root is now");
- for (i=0; i<nbaddrs; i++){
- unsigned long hostaddr = ntohl(ips[i].ip);
- printf (" %ld.%ld.%ld.%ld"
- ,hostaddr>>24
- ,(hostaddr>>16)&0xff
- ,(hostaddr>>8)&0xff
- ,hostaddr &0xff);
- }
- printf ("\n");
- }
- execvp (argv[i],argv+i);
- fprintf (stderr,"Can't exec %s (%s)\n",argv[i],strerror(errno));
- }else{
- fprintf (stderr,"Can't set the ipv4 root (%s)\n",strerror(errno));
- }
+ size_t const nb_ipv4root = vc_get_nb_ipv4root();
+ bool is_silent = false;
+ struct vc_ip_mask_pair ips[nb_ipv4root];
+ size_t nbaddrs = 0;
+ uint32_t bcast = 0xffffffff;
+
+ while (1) {
+ int c = getopt_long(argc, argv, "+", CMDLINE_OPTIONS, 0);
+ if (c==-1) break;
+
+ switch (c) {
+ case CMD_HELP : showHelp(1, argv[0], 0);
+ case CMD_VERSION : showVersion();
+ case CMD_SILENT : is_silent = true; break;
+ case CMD_BCAST : readBcast(optarg, &bcast); break;
+ case CMD_NID : WRITE_MSG(2, "WARNING: --nid is not supported by this version\n"); break;
+ case CMD_IP :
+ if (nbaddrs>=nb_ipv4root) {
+ WRITE_MSG(2, "Too many IP numbers, max 16\n");
+ exit(wrapper_exit_code);