diff --git a/bin/ax25dump.8 b/bin/ax25dump.8 index fb03f49..e2b615c 100644 --- a/bin/ax25dump.8 +++ b/bin/ax25dump.8 @@ -6,7 +6,8 @@ .Nd dump AX.25 traffic .Sh SYNOPSIS .Nm -.Ar patty.sock ifname +.Op Fl s Ar patty.sock +.Fl i Ar ifname .Nm .Ar /dev/ttyXYZ Op tioarg ... .Nm diff --git a/bin/ax25dump.c b/bin/ax25dump.c index 9cc2bb3..b7aab6d 100644 --- a/bin/ax25dump.c +++ b/bin/ax25dump.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -14,6 +13,7 @@ #include #include +#include #include @@ -29,7 +29,7 @@ static void usage(int argc, char **argv, const char *message, ...) { va_end(args); } - fprintf(stderr, "usage: %s /var/run/patty/patty.sock ifname\n" + fprintf(stderr, "usage: %s [-s patty.sock] -i ifname\n" " %s /dev/ttyXYZ [tioarg ...]\n" " %s file.cap\n", argv[0], argv[0], argv[0]); @@ -40,36 +40,44 @@ int main(int argc, char **argv) { patty_client *client = NULL; struct option opts[] = { - { NULL, 0, NULL, 0 } + { "sock", required_argument, NULL, 's' }, + { "if", required_argument, NULL, 'i' }, + { NULL, 0, NULL, 0 } }; void *buf; ssize_t readlen; + char *sock = PATTY_DAEMON_DEFAULT_SOCK, + *ifname = NULL; + patty_kiss_tnc_info info; patty_kiss_tnc *raw; - struct stat st; + int index, + ch; - int ch; + while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) { + switch (ch) { + case '?': + usage(argc, argv, NULL); + break; - if ((ch = getopt_long(argc, argv, "", opts, NULL)) >= 0) { - usage(argc, argv, NULL); + case 's': sock = optarg; break; + case 'i': ifname = optarg; break; + + default: + break; + } } if (argc < 2) { usage(argc, argv, "Not enough arguments provided"); } - if (stat(argv[1], &st) < 0) { - fprintf(stderr, "%s: %s: %s\n", argv[0], "stat()", strerror(errno)); - - goto error_stat; - } - memset(&info, '\0', sizeof(info)); - if ((st.st_mode & S_IFMT) == S_IFSOCK) { + if (ifname) { patty_client_setsockopt_if ifreq; if (argc < 3) { @@ -78,9 +86,9 @@ int main(int argc, char **argv) { info.flags = PATTY_KISS_TNC_FD; - if ((client = patty_client_new(argv[1])) == NULL) { + if ((client = patty_client_new(sock)) == NULL) { fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "patty_client_new()", argv[1], strerror(errno)); + argv[0], "patty_client_new()", sock, strerror(errno)); goto error_client_new; } @@ -92,13 +100,13 @@ int main(int argc, char **argv) { goto error_client_socket; } - strncpy(ifreq.name, argv[2], sizeof(ifreq.name)); + strncpy(ifreq.name, ifname, sizeof(ifreq.name)); ifreq.state = PATTY_AX25_SOCK_PROMISC; if (patty_client_setsockopt(client, info.fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) { - fprintf(stderr, "%s: %s: %s\n", - argv[0], "patty_client_setsockopt()", strerror(errno)); + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], "patty_client_setsockopt()", ifname, strerror(errno)); goto error_client_setsockopt; } @@ -106,8 +114,8 @@ int main(int argc, char **argv) { patty_error e; if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) { - fprintf(stderr, "%s: %s: %s\n", - argv[0], argv[1], patty_error_string(&e)); + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], "patty_bin_kiss_config()", sock, patty_error_string(&e)); goto error_kiss_config; } @@ -186,7 +194,7 @@ error_ax25_frame_decode_address: patty_kiss_tnc_destroy(raw); - if ((st.st_mode & S_IFMT) == S_IFSOCK) { + if (client) { patty_client_close(client, info.fd); patty_client_destroy(client); @@ -209,6 +217,5 @@ error_client_socket: error_client_new: if (client) patty_client_destroy(client); -error_stat: return 1; }