diff --git a/dnstun.c b/dnstun.c index f58e84e..17ab127 100644 --- a/dnstun.c +++ b/dnstun.c @@ -116,9 +116,11 @@ tunnel(int tun_fd, int dns_fd) return 0; } +extern char *__progname; + static void usage() { - printf("Usage: dnstun [-u user] nameserver topdomain\n"); + printf("Usage: %s [-u user] nameserver topdomain\n", __progname); exit(2); } @@ -132,15 +134,11 @@ main(int argc, char **argv) struct passwd *pw; username = NULL; + while ((choice = getopt(argc, argv, "u:")) != -1) { switch(choice) { case 'u': username = optarg; - pw = getpwnam(username); - if (!pw) { - printf("User %s does not exist!\n", username); - usage(); - } break; default: usage(); @@ -150,10 +148,19 @@ main(int argc, char **argv) argc -= optind; argv += optind; - + if (argc != 2) { usage(); } + + if(username) { + pw = getpwnam(username); + if (!pw) { + printf("User %s does not exist!\n", username); + usage(); + } + } + tun_fd = open_tun(); dns_fd = open_dns(argv[0], argv[1]); diff --git a/dnstund.c b/dnstund.c index e46330f..77961f2 100644 --- a/dnstund.c +++ b/dnstund.c @@ -113,9 +113,11 @@ tunnel(int tun_fd, int dns_fd) return 0; } +extern char *__progname; + static void usage() { - printf("Usage: dnstund [-u user] topdomain\n"); + printf("Usage: %s [-u user] topdomain\n", __progname); exit(2); } @@ -129,15 +131,11 @@ main(int argc, char **argv) struct passwd *pw; username = NULL; + while ((choice = getopt(argc, argv, "u:")) != -1) { switch(choice) { case 'u': username = optarg; - pw = getpwnam(username); - if (!pw) { - printf("User %s does not exist!\n", username); - usage(); - } break; default: usage(); @@ -152,6 +150,14 @@ main(int argc, char **argv) usage(); } + if (username) { + pw = getpwnam(username); + if (!pw) { + printf("User %s does not exist!\n", username); + usage(); + } + } + tun_fd = open_tun(); dnsd_fd = open_dnsd(argv[0]); printf("Listening to dns for domain %s\n", argv[0]);