Use getopt_long() in bin/ax25dump.c

Use getopt_long() in bin/ax25dump.c to accept Unix domain socket and
interface name arguments for dumping packets from a connected pattyd(8)
process
This commit is contained in:
XANTRONIX Development 2020-10-05 14:39:45 -04:00 committed by XANTRONIX Industrial
parent 4fb64760b6
commit 535e71c472
2 changed files with 32 additions and 24 deletions

View file

@ -6,7 +6,8 @@
.Nd dump AX.25 traffic .Nd dump AX.25 traffic
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Ar patty.sock ifname .Op Fl s Ar patty.sock
.Fl i Ar ifname
.Nm .Nm
.Ar /dev/ttyXYZ Op tioarg ... .Ar /dev/ttyXYZ Op tioarg ...
.Nm .Nm

View file

@ -5,7 +5,6 @@
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <fcntl.h> #include <fcntl.h>
@ -14,6 +13,7 @@
#include <patty/ax25.h> #include <patty/ax25.h>
#include <patty/print.h> #include <patty/print.h>
#include <patty/daemon.h>
#include <patty/bin/kiss.h> #include <patty/bin/kiss.h>
@ -29,7 +29,7 @@ static void usage(int argc, char **argv, const char *message, ...) {
va_end(args); 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 /dev/ttyXYZ [tioarg ...]\n"
" %s file.cap\n", argv[0], argv[0], argv[0]); " %s file.cap\n", argv[0], argv[0], argv[0]);
@ -40,36 +40,44 @@ int main(int argc, char **argv) {
patty_client *client = NULL; patty_client *client = NULL;
struct option opts[] = { struct option opts[] = {
{ NULL, 0, NULL, 0 } { "sock", required_argument, NULL, 's' },
{ "if", required_argument, NULL, 'i' },
{ NULL, 0, NULL, 0 }
}; };
void *buf; void *buf;
ssize_t readlen; ssize_t readlen;
char *sock = PATTY_DAEMON_DEFAULT_SOCK,
*ifname = NULL;
patty_kiss_tnc_info info; patty_kiss_tnc_info info;
patty_kiss_tnc *raw; 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) { case 's': sock = optarg; break;
usage(argc, argv, NULL); case 'i': ifname = optarg; break;
default:
break;
}
} }
if (argc < 2) { if (argc < 2) {
usage(argc, argv, "Not enough arguments provided"); 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)); memset(&info, '\0', sizeof(info));
if ((st.st_mode & S_IFMT) == S_IFSOCK) { if (ifname) {
patty_client_setsockopt_if ifreq; patty_client_setsockopt_if ifreq;
if (argc < 3) { if (argc < 3) {
@ -78,9 +86,9 @@ int main(int argc, char **argv) {
info.flags = PATTY_KISS_TNC_FD; 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", 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; goto error_client_new;
} }
@ -92,13 +100,13 @@ int main(int argc, char **argv) {
goto error_client_socket; 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; ifreq.state = PATTY_AX25_SOCK_PROMISC;
if (patty_client_setsockopt(client, info.fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) { if (patty_client_setsockopt(client, info.fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) {
fprintf(stderr, "%s: %s: %s\n", fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "patty_client_setsockopt()", strerror(errno)); argv[0], "patty_client_setsockopt()", ifname, strerror(errno));
goto error_client_setsockopt; goto error_client_setsockopt;
} }
@ -106,8 +114,8 @@ int main(int argc, char **argv) {
patty_error e; patty_error e;
if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) { if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) {
fprintf(stderr, "%s: %s: %s\n", fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], argv[1], patty_error_string(&e)); argv[0], "patty_bin_kiss_config()", sock, patty_error_string(&e));
goto error_kiss_config; goto error_kiss_config;
} }
@ -186,7 +194,7 @@ error_ax25_frame_decode_address:
patty_kiss_tnc_destroy(raw); patty_kiss_tnc_destroy(raw);
if ((st.st_mode & S_IFMT) == S_IFSOCK) { if (client) {
patty_client_close(client, info.fd); patty_client_close(client, info.fd);
patty_client_destroy(client); patty_client_destroy(client);
@ -209,6 +217,5 @@ error_client_socket:
error_client_new: error_client_new:
if (client) patty_client_destroy(client); if (client) patty_client_destroy(client);
error_stat:
return 1; return 1;
} }