2020-07-02 23:57:40 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2020-09-10 20:25:15 -04:00
|
|
|
#include <getopt.h>
|
2020-07-02 23:57:40 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2020-09-08 22:49:08 -04:00
|
|
|
#include <sysexits.h>
|
2020-07-02 23:57:40 -04:00
|
|
|
|
|
|
|
#include <patty/ax25.h>
|
|
|
|
#include <patty/print.h>
|
2020-12-18 15:50:51 -05:00
|
|
|
#include <patty/util.h>
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-09-10 01:44:10 -04:00
|
|
|
#include <patty/bin/kiss.h>
|
|
|
|
|
2020-10-05 12:02:14 -04:00
|
|
|
#define AX25DUMP_BUFSZ 4096
|
|
|
|
|
2020-07-02 23:57:40 -04:00
|
|
|
static void usage(int argc, char **argv, const char *message, ...) {
|
|
|
|
if (message != NULL) {
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, message);
|
|
|
|
vfprintf(stderr, message, args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
fprintf(stderr, "usage: %s [-s patty.sock] -i ifname\n"
|
2020-09-10 20:43:11 -04:00
|
|
|
" %s /dev/ttyXYZ [tioarg ...]\n"
|
2020-09-10 01:44:10 -04:00
|
|
|
" %s file.cap\n", argv[0], argv[0], argv[0]);
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
exit(EX_USAGE);
|
2020-07-02 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2020-09-13 16:56:10 -04:00
|
|
|
patty_client *client = NULL;
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-09-10 20:25:15 -04:00
|
|
|
struct option opts[] = {
|
2020-10-05 14:39:45 -04:00
|
|
|
{ "sock", required_argument, NULL, 's' },
|
|
|
|
{ "if", required_argument, NULL, 'i' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
2020-09-10 20:25:15 -04:00
|
|
|
};
|
|
|
|
|
2020-10-05 12:02:14 -04:00
|
|
|
void *buf;
|
2020-07-02 23:57:40 -04:00
|
|
|
ssize_t readlen;
|
|
|
|
|
2020-10-26 23:29:21 -04:00
|
|
|
char *sock = NULL,
|
2020-10-05 14:39:45 -04:00
|
|
|
*ifname = NULL;
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
patty_kiss_tnc_info info;
|
2020-07-08 18:29:27 -04:00
|
|
|
patty_kiss_tnc *raw;
|
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
int index,
|
|
|
|
ch;
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, "s:i:", opts, &index)) >= 0) {
|
|
|
|
switch (ch) {
|
|
|
|
case 's': sock = optarg; break;
|
|
|
|
case 'i': ifname = optarg; break;
|
2020-09-10 20:25:15 -04:00
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
default:
|
2020-10-05 23:28:45 -04:00
|
|
|
usage(argc, argv, NULL);
|
2020-10-05 14:39:45 -04:00
|
|
|
}
|
2020-09-10 20:25:15 -04:00
|
|
|
}
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
memset(&info, '\0', sizeof(info));
|
2020-09-10 01:44:10 -04:00
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
if (ifname) {
|
2020-09-08 22:49:08 -04:00
|
|
|
patty_client_setsockopt_if ifreq;
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-10-06 10:58:39 -04:00
|
|
|
if (optind < argc) {
|
|
|
|
usage(argc, argv, "Too many arguments provided");
|
|
|
|
}
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
info.flags = PATTY_KISS_TNC_FD;
|
2020-09-08 22:49:08 -04:00
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
if ((client = patty_client_new(sock)) == NULL) {
|
2020-09-08 22:49:08 -04:00
|
|
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
2020-10-05 14:39:45 -04:00
|
|
|
argv[0], "patty_client_new()", sock, strerror(errno));
|
2020-09-08 22:49:08 -04:00
|
|
|
|
|
|
|
goto error_client_new;
|
|
|
|
}
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
if ((info.fd = patty_client_socket(client, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_RAW)) < 0) {
|
2020-09-08 22:49:08 -04:00
|
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
|
|
argv[0], "patty_client_socket()", strerror(errno));
|
|
|
|
|
|
|
|
goto error_client_socket;
|
|
|
|
}
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-12-18 15:50:51 -05:00
|
|
|
patty_strlcpy(ifreq.name, ifname, sizeof(ifreq.name));
|
2020-07-03 15:21:32 -04:00
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
ifreq.state = PATTY_AX25_SOCK_PROMISC;
|
2020-08-05 02:05:15 -04:00
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
if (patty_client_setsockopt(client, info.fd, PATTY_AX25_SOCK_IF, &ifreq, sizeof(ifreq)) < 0) {
|
2020-10-05 14:39:45 -04:00
|
|
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
|
|
argv[0], "patty_client_setsockopt()", ifname, strerror(errno));
|
2020-07-03 15:21:32 -04:00
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
goto error_client_setsockopt;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-13 16:56:10 -04:00
|
|
|
patty_error e;
|
|
|
|
|
2020-10-05 23:28:45 -04:00
|
|
|
if (argc < 2) {
|
|
|
|
usage(argc, argv, "Not enough arguments provided");
|
|
|
|
}
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
if (patty_bin_kiss_config(argc - 1, argv + 1, &info, &e) < 0) {
|
2020-10-05 14:39:45 -04:00
|
|
|
fprintf(stderr, "%s: %s: %s: %s\n",
|
|
|
|
argv[0], "patty_bin_kiss_config()", sock, patty_error_string(&e));
|
2020-09-10 01:44:10 -04:00
|
|
|
|
|
|
|
goto error_kiss_config;
|
|
|
|
}
|
2020-07-03 15:21:32 -04:00
|
|
|
}
|
|
|
|
|
2020-09-13 16:56:10 -04:00
|
|
|
if ((raw = patty_kiss_tnc_new(&info)) == NULL) {
|
Implement new patty client code
Implement new patty client code, replacing src/call.c with src/client.c
providing clients with an interface dealing with file descriptors valid
in their process space; this also obviates the need to open a Unix
domain socket to a patty server explicitly, and helps keep track of
sockets opened on the server, locally
Changes:
* Implement patty_client_new() to handle opening the server Unix
domain socket, and to allocate a dict for mapping server-side
sockets with current process file descriptors
* Reimplement all server calls in src/call.c around the new
patty_client type; calls which result in the creation of a
Unix98 PTY by the patty server now handle opening the local PTY
and setting the file descriptor to raw mode. Furthermore, these
calls deal exclusively in terms of current process file
descriptors
* Refactor src/server.c to use the new patty_client type and calls
* Refactor examples/client.c, examples/server.c, examples/ax25dump.c
to use the new patty_client type and calls
* Fix a bug in src/server.c, respond_accept() wherein a 0, rather
than the file descriptor of the socket, is sent to the client as a
return value
2020-08-01 20:21:01 -04:00
|
|
|
fprintf(stderr, "%s: fd %d: %s: %s\n",
|
2020-09-13 16:56:10 -04:00
|
|
|
argv[0], info.fd, "patty_kiss_tnc_new()", strerror(errno));
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
goto error_kiss_tnc_new;
|
2020-07-02 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
2020-10-05 12:02:14 -04:00
|
|
|
if ((buf = malloc(AX25DUMP_BUFSZ)) == NULL) {
|
|
|
|
goto error_malloc_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((readlen = patty_kiss_tnc_recv(raw, buf, AX25DUMP_BUFSZ)) > 0) {
|
2020-07-17 22:57:23 -04:00
|
|
|
ssize_t decoded,
|
|
|
|
offset = 0;
|
|
|
|
|
2020-07-02 23:57:40 -04:00
|
|
|
patty_ax25_frame frame;
|
|
|
|
|
2020-07-10 00:01:50 -04:00
|
|
|
if ((decoded = patty_ax25_frame_decode_address(&frame, buf, readlen)) < 0) {
|
2020-07-19 20:16:08 -04:00
|
|
|
printf("Invalid frame address\n");
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-07-10 00:01:50 -04:00
|
|
|
goto error_ax25_frame_decode_address;
|
2020-07-17 22:57:23 -04:00
|
|
|
} else {
|
|
|
|
offset += decoded;
|
2020-07-10 00:01:50 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 22:57:23 -04:00
|
|
|
if ((decoded = patty_ax25_frame_decode_control(&frame, PATTY_AX25_FRAME_NORMAL, buf, decoded, readlen)) < 0) {
|
2020-07-19 20:16:08 -04:00
|
|
|
printf("Invalid frame control\n");
|
2020-07-10 00:01:50 -04:00
|
|
|
|
|
|
|
goto error_ax25_frame_decode_control;
|
2020-07-17 22:57:23 -04:00
|
|
|
} else {
|
|
|
|
offset += decoded;
|
2020-07-02 23:57:40 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 22:57:23 -04:00
|
|
|
if (patty_print_frame_header(stdout, &frame) < 0) {
|
2020-07-03 15:18:58 -04:00
|
|
|
fprintf(stderr, "%s: %s: %s\n",
|
2020-07-17 22:57:23 -04:00
|
|
|
argv[0], "patty_print_frame_header()", strerror(errno));
|
|
|
|
|
2020-07-18 16:26:41 -04:00
|
|
|
goto error_io;
|
2020-07-17 22:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame.type == PATTY_AX25_FRAME_XID) {
|
|
|
|
patty_ax25_params params;
|
|
|
|
|
|
|
|
if (patty_ax25_frame_decode_xid(¶ms,
|
|
|
|
buf,
|
|
|
|
offset,
|
|
|
|
readlen) < 0) {
|
2020-07-19 20:16:08 -04:00
|
|
|
printf("Invalid XID parameters\n");
|
2020-07-17 22:57:23 -04:00
|
|
|
|
|
|
|
goto error_ax25_frame_decode_xid;
|
2020-07-19 20:16:08 -04:00
|
|
|
} else {
|
|
|
|
if (patty_print_params(stdout, ¶ms) < 0) {
|
|
|
|
goto error_io;
|
|
|
|
}
|
2020-07-17 22:57:23 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-07-19 20:16:08 -04:00
|
|
|
error_ax25_frame_decode_xid:
|
|
|
|
error_ax25_frame_decode_control:
|
|
|
|
error_ax25_frame_decode_address:
|
2020-07-17 22:57:23 -04:00
|
|
|
if (patty_print_hexdump(stdout, buf, readlen) < 0) {
|
2020-07-18 16:26:41 -04:00
|
|
|
goto error_io;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fflush(stdout) < 0) {
|
|
|
|
goto error_io;
|
2020-07-02 23:57:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 12:02:14 -04:00
|
|
|
free(buf);
|
|
|
|
|
2020-07-08 18:29:27 -04:00
|
|
|
patty_kiss_tnc_destroy(raw);
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-10-05 14:39:45 -04:00
|
|
|
if (client) {
|
2020-09-13 16:56:10 -04:00
|
|
|
patty_client_close(client, info.fd);
|
2020-07-02 23:57:40 -04:00
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
patty_client_destroy(client);
|
|
|
|
}
|
2020-07-02 23:57:40 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2020-07-18 16:26:41 -04:00
|
|
|
error_io:
|
2020-10-05 12:02:14 -04:00
|
|
|
free(buf);
|
|
|
|
|
|
|
|
error_malloc_buf:
|
2020-07-08 18:29:27 -04:00
|
|
|
patty_kiss_tnc_destroy(raw);
|
|
|
|
|
2020-09-08 22:49:08 -04:00
|
|
|
error_kiss_tnc_new:
|
2020-09-10 01:44:10 -04:00
|
|
|
error_kiss_config:
|
Implement new patty client code
Implement new patty client code, replacing src/call.c with src/client.c
providing clients with an interface dealing with file descriptors valid
in their process space; this also obviates the need to open a Unix
domain socket to a patty server explicitly, and helps keep track of
sockets opened on the server, locally
Changes:
* Implement patty_client_new() to handle opening the server Unix
domain socket, and to allocate a dict for mapping server-side
sockets with current process file descriptors
* Reimplement all server calls in src/call.c around the new
patty_client type; calls which result in the creation of a
Unix98 PTY by the patty server now handle opening the local PTY
and setting the file descriptor to raw mode. Furthermore, these
calls deal exclusively in terms of current process file
descriptors
* Refactor src/server.c to use the new patty_client type and calls
* Refactor examples/client.c, examples/server.c, examples/ax25dump.c
to use the new patty_client type and calls
* Fix a bug in src/server.c, respond_accept() wherein a 0, rather
than the file descriptor of the socket, is sent to the client as a
return value
2020-08-01 20:21:01 -04:00
|
|
|
error_client_setsockopt:
|
|
|
|
error_client_socket:
|
2020-09-13 16:56:10 -04:00
|
|
|
if (client) patty_client_close(client, info.fd);
|
2020-07-02 23:57:40 -04:00
|
|
|
|
Implement new patty client code
Implement new patty client code, replacing src/call.c with src/client.c
providing clients with an interface dealing with file descriptors valid
in their process space; this also obviates the need to open a Unix
domain socket to a patty server explicitly, and helps keep track of
sockets opened on the server, locally
Changes:
* Implement patty_client_new() to handle opening the server Unix
domain socket, and to allocate a dict for mapping server-side
sockets with current process file descriptors
* Reimplement all server calls in src/call.c around the new
patty_client type; calls which result in the creation of a
Unix98 PTY by the patty server now handle opening the local PTY
and setting the file descriptor to raw mode. Furthermore, these
calls deal exclusively in terms of current process file
descriptors
* Refactor src/server.c to use the new patty_client type and calls
* Refactor examples/client.c, examples/server.c, examples/ax25dump.c
to use the new patty_client type and calls
* Fix a bug in src/server.c, respond_accept() wherein a 0, rather
than the file descriptor of the socket, is sent to the client as a
return value
2020-08-01 20:21:01 -04:00
|
|
|
error_client_new:
|
2020-09-13 16:56:10 -04:00
|
|
|
if (client) patty_client_destroy(client);
|
2020-09-08 22:49:08 -04:00
|
|
|
|
2020-07-02 23:57:40 -04:00
|
|
|
return 1;
|
|
|
|
}
|