Commit 8ab137ec authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Use getopt rather than homebrew code.

parent ea464f18
...@@ -102,12 +102,11 @@ int ...@@ -102,12 +102,11 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
struct sockaddr_in6 sin6; struct sockaddr_in6 sin6;
int rc, fd, rfd, i; int rc, fd, rfd, i, opt;
time_t expiry_time, source_expiry_time, kernel_dump_time; time_t expiry_time, source_expiry_time, kernel_dump_time;
char *config_file = NULL; char *config_file = NULL;
void *vrc; void *vrc;
unsigned int seed; unsigned int seed;
char **arg;
struct network *net; struct network *net;
gettime(&now); gettime(&now);
...@@ -127,108 +126,109 @@ main(int argc, char **argv) ...@@ -127,108 +126,109 @@ main(int argc, char **argv)
parse_address("ff02::cca6:c0f9:e182:5373", protocol_group, NULL); parse_address("ff02::cca6:c0f9:e182:5373", protocol_group, NULL);
protocol_port = 8475; protocol_port = 8475;
#define SHIFT() do { arg++; } while(0) while(1) {
#define SHIFTE() do { arg++; if(*arg == NULL) goto syntax; } while(0) opt = getopt(argc, argv, "m:p:h:H:i:k:PsS:d:g:lwt:T:c:C:DL:I:");
if(opt < 0)
arg = argv;
SHIFT();
while(*arg && (*arg)[0] == '-') {
if(strcmp(*arg, "--") == 0) {
SHIFTE();
break; break;
} else if(strcmp(*arg, "-m") == 0) {
SHIFTE(); switch(opt) {
rc = parse_address(*arg, protocol_group, NULL); case 'm':
rc = parse_address(optarg, protocol_group, NULL);
if(rc < 0) if(rc < 0)
goto syntax; goto usage;
if(protocol_group[0] != 0xff) { if(protocol_group[0] != 0xff) {
fprintf(stderr, fprintf(stderr,
"%s is not a multicast address\n", *arg); "%s is not a multicast address\n", optarg);
goto syntax; goto usage;
} }
if(protocol_group[1] != 2) { if(protocol_group[1] != 2) {
fprintf(stderr, fprintf(stderr,
"Warning: %s is not a link-local multicast address\n", "Warning: %s is not a link-local multicast address\n",
*arg); optarg);
} }
} else if(strcmp(*arg, "-p") == 0) { break;
SHIFTE(); case 'p':
protocol_port = atoi(*arg); protocol_port = atoi(optarg);
} else if(strcmp(*arg, "-h") == 0) { break;
SHIFTE(); case 'h':
wireless_hello_interval = parse_msec(*arg); wireless_hello_interval = parse_msec(optarg);
if(wireless_hello_interval <= 0 || if(wireless_hello_interval <= 0 ||
wireless_hello_interval > 0xFFFF * 10) wireless_hello_interval > 0xFFFF * 10)
goto syntax; goto usage;
} else if(strcmp(*arg, "-H") == 0) { break;
SHIFTE(); case 'H':
wired_hello_interval = parse_msec(*arg); wired_hello_interval = parse_msec(optarg);
if(wired_hello_interval <= 0 || wired_hello_interval > 0xFFFF * 10) if(wired_hello_interval <= 0 || wired_hello_interval > 0xFFFF * 10)
goto syntax; goto usage;
} else if(strcmp(*arg, "-i") == 0) { break;
SHIFTE(); case 'i':
idle_hello_interval = parse_msec(*arg); idle_hello_interval = parse_msec(optarg);
if(idle_hello_interval <= 0 || idle_hello_interval > 0xFFFF * 10) if(idle_hello_interval <= 0 || idle_hello_interval > 0xFFFF * 10)
goto syntax; goto usage;
} else if(strcmp(*arg, "-k") == 0) { break;
SHIFTE(); case 'k':
kernel_metric = atoi(*arg); kernel_metric = atoi(optarg);
if(kernel_metric < 0 || kernel_metric > 0xFFFF) if(kernel_metric < 0 || kernel_metric > 0xFFFF)
goto syntax; goto usage;
} else if(strcmp(*arg, "-P") == 0) { break;
case 'P':
parasitic = 1; parasitic = 1;
} else if(strcmp(*arg, "-s") == 0) { break;
case 's':
split_horizon = 0; split_horizon = 0;
} else if(strcmp(*arg, "-S") == 0) { break;
SHIFTE(); case 'S':
state_file = *arg; state_file = optarg;
} else if(strcmp(*arg, "-d") == 0) { break;
SHIFTE(); case 'd':
debug = atoi(*arg); debug = atoi(optarg);
#ifndef NO_LOCAL_INTERFACE break;
} else if(strcmp(*arg, "-g") == 0) { case 'g':
SHIFTE(); #ifdef NO_LOCAL_INTERFACE
local_server_port = atoi(*arg); fprintf(stderr, "Warning: no local interface in this version.\n");
#else
local_server_port = atoi(optarg);
#endif #endif
} else if(strcmp(*arg, "-l") == 0) { break;
case 'l':
link_detect = 1; link_detect = 1;
} else if(strcmp(*arg, "-w") == 0) { break;
case 'w':
all_wireless = 1; all_wireless = 1;
} else if(strcmp(*arg, "-t") == 0) { break;
SHIFTE(); case 't':
export_table = atoi(*arg); export_table = atoi(optarg);
if(export_table < 0 || export_table > 0xFFFF) if(export_table < 0 || export_table > 0xFFFF)
goto syntax; goto usage;
} else if(strcmp(*arg, "-T") == 0) { break;
SHIFTE(); case 'T':
import_table = atoi(*arg); import_table = atoi(optarg);
if(import_table < 0 || import_table > 0xFFFF) if(import_table < 0 || import_table > 0xFFFF)
goto syntax; goto usage;
} else if(strcmp(*arg, "-c") == 0) { break;
SHIFTE(); case 'c':
config_file = *arg; config_file = optarg;
} else if(strcmp(*arg, "-C") == 0) { break;
SHIFTE(); case 'C':
rc = parse_config_from_string(*arg); rc = parse_config_from_string(optarg);
if(rc < 0) { if(rc < 0) {
fprintf(stderr, fprintf(stderr,
"Couldn't parse configuration from command line.\n"); "Couldn't parse configuration from command line.\n");
exit(1); exit(1);
} }
} else if(strcmp(*arg, "-D") == 0) { break;
case 'D':
do_daemonise = 1; do_daemonise = 1;
} else if(strcmp(*arg, "-L") == 0) { break;
SHIFTE(); case 'L':
logfile = *arg; logfile = optarg;
} else if(strcmp(*arg, "-I") == 0) { break;
SHIFTE(); case 'I':
pidfile = *arg; pidfile = optarg;
} else { break;
goto syntax; default:
goto usage;
} }
SHIFT();
} }
...@@ -328,27 +328,17 @@ main(int argc, char **argv) ...@@ -328,27 +328,17 @@ main(int argc, char **argv)
goto fail_pid; goto fail_pid;
} }
if(*arg) {
unsigned char dummy[16];
rc = parse_address(*arg, dummy, NULL);
if(rc >= 0) {
fprintf(stderr, "Warning: obsolete router-id given.\n");
SHIFT();
}
}
rc = finalise_config(); rc = finalise_config();
if(rc < 0) { if(rc < 0) {
fprintf(stderr, "Couldn't finalise configuration.\n"); fprintf(stderr, "Couldn't finalise configuration.\n");
goto fail; goto fail;
} }
while(*arg) { for(i = optind; i < argc; i++) {
debugf("Adding network %s.\n", *arg); debugf("Adding network %s.\n", argv[i]);
vrc = add_network(*arg, NULL); vrc = add_network(argv[i], NULL);
if(vrc == NULL) if(vrc == NULL)
goto fail; goto fail;
SHIFT();
} }
if(networks == NULL) { if(networks == NULL) {
...@@ -775,7 +765,7 @@ main(int argc, char **argv) ...@@ -775,7 +765,7 @@ main(int argc, char **argv)
debugf("Done.\n"); debugf("Done.\n");
return 0; return 0;
syntax: usage:
fprintf(stderr, fprintf(stderr,
"Syntax: %s " "Syntax: %s "
"[-m multicast_address] [-p port] [-S state-file]\n" "[-m multicast_address] [-p port] [-S state-file]\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment