From fa53b8d49d3be3d69cb148012a7d59e4122e85af Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Wed, 22 Jul 2020 19:45:20 -0400 Subject: [PATCH] Keep separate TX, RX single buffers in socks Changes: * Replace patty_ax25_sock member 'buf' with 'tx_buf' and 'rx_buf' for transmitting or receiving single, non-sequential packets * Make patty_ax25_sock_upgrade() reallocate all I/O buffers based on window size and MTU/MRU, useful when entering SABME mode --- include/patty/ax25/sock.h | 8 ++++--- src/server.c | 4 ++-- src/sock.c | 49 ++++++++++++++++++++++++--------------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/include/patty/ax25/sock.h b/include/patty/ax25/sock.h index 9b843ac..37c8c89 100644 --- a/include/patty/ax25/sock.h +++ b/include/patty/ax25/sock.h @@ -70,7 +70,9 @@ typedef struct _patty_ax25_sock { int fd; char path[PATTY_AX25_SOCK_PATH_SIZE]; - void *buf; + void *tx_buf, + *rx_buf; + void *tx_slots; patty_ax25_if *iface; @@ -87,10 +89,10 @@ patty_ax25_sock *patty_ax25_sock_new(enum patty_ax25_proto proto, void patty_ax25_sock_reset(patty_ax25_sock *sock); -void patty_ax25_sock_upgrade(patty_ax25_sock *sock); - void patty_ax25_sock_destroy(patty_ax25_sock *sock); +int patty_ax25_sock_upgrade(patty_ax25_sock *sock); + int patty_ax25_sock_params_set(patty_ax25_sock *sock, patty_ax25_params *params); diff --git a/src/server.c b/src/server.c index 124b22d..d020eea 100644 --- a/src/server.c +++ b/src/server.c @@ -1528,7 +1528,7 @@ static int handle_sock(uint32_t key, goto done; } - if ((len = read(sock->fd, sock->buf, sock->n_maxlen_rx)) < 0) { + if ((len = read(sock->fd, sock->rx_buf, sock->n_maxlen_rx)) < 0) { if (errno == EIO) { (void)sock_close(server, sock); @@ -1542,7 +1542,7 @@ static int handle_sock(uint32_t key, goto done; } - if (patty_ax25_sock_write(sock, sock->buf, len) < 0) { + if (patty_ax25_sock_write(sock, sock->rx_buf, len) < 0) { goto error_sock_write; } diff --git a/src/sock.c b/src/sock.c index c6341d3..c071905 100644 --- a/src/sock.c +++ b/src/sock.c @@ -54,7 +54,7 @@ error_open: return -1; } -static inline size_t tx_bufsz(patty_ax25_sock *sock) { +static inline size_t bufsz(patty_ax25_sock *sock) { return PATTY_AX25_FRAME_OVERHEAD + sock->n_maxlen_tx; } @@ -71,8 +71,12 @@ static inline void *tx_slot(patty_ax25_sock *sock, size_t i) { } static int init_bufs(patty_ax25_sock *sock) { - if ((sock->buf = realloc(sock->buf, tx_bufsz(sock))) == NULL) { - goto error_realloc_buf; + if ((sock->tx_buf = realloc(sock->tx_buf, bufsz(sock))) == NULL) { + goto error_realloc_tx_buf; + } + + if ((sock->rx_buf = realloc(sock->rx_buf, bufsz(sock))) == NULL) { + goto error_realloc_rx_buf; } if ((sock->tx_slots = realloc(sock->tx_slots, tx_slots_size(sock))) == NULL) { @@ -82,11 +86,14 @@ static int init_bufs(patty_ax25_sock *sock) { return 0; error_realloc_tx_slots: - free(sock->buf); + free(sock->rx_buf); + sock->rx_buf = NULL; - sock->buf = NULL; +error_realloc_rx_buf: + free(sock->tx_buf); + sock->tx_buf = NULL; -error_realloc_buf: +error_realloc_tx_buf: return -1; } @@ -121,7 +128,8 @@ patty_ax25_sock *patty_ax25_sock_new(enum patty_ax25_proto proto, error_bind_pty: error_init_bufs: if (sock->tx_slots) free(sock->tx_slots); - if (sock->buf) free(sock->buf); + if (sock->rx_buf) free(sock->rx_buf); + if (sock->tx_buf) free(sock->tx_buf); free(sock); @@ -145,15 +153,6 @@ void patty_ax25_sock_reset(patty_ax25_sock *sock) { sock->seq_recv = 0; } -void patty_ax25_sock_upgrade(patty_ax25_sock *sock) { - sock->version = PATTY_AX25_2_2; - sock->flags_hdlc = PATTY_AX25_SOCK_2_2_DEFAULT_HDLC; - sock->n_maxlen_tx = PATTY_AX25_SOCK_2_2_DEFAULT_MAXLEN; - sock->n_maxlen_rx = PATTY_AX25_SOCK_2_2_DEFAULT_MAXLEN; - sock->n_window_tx = PATTY_AX25_SOCK_2_2_DEFAULT_WINDOW; - sock->n_window_rx = PATTY_AX25_SOCK_2_2_DEFAULT_WINDOW; -} - void patty_ax25_sock_destroy(patty_ax25_sock *sock) { if (sock->fd) { if (sock->iface) { @@ -163,11 +162,23 @@ void patty_ax25_sock_destroy(patty_ax25_sock *sock) { close(sock->fd); } - free(sock->buf); free(sock->tx_slots); + free(sock->rx_buf); + free(sock->tx_buf); free(sock); } +int patty_ax25_sock_upgrade(patty_ax25_sock *sock) { + sock->version = PATTY_AX25_2_2; + sock->flags_hdlc = PATTY_AX25_SOCK_2_2_DEFAULT_HDLC; + sock->n_maxlen_tx = PATTY_AX25_SOCK_2_2_DEFAULT_MAXLEN; + sock->n_maxlen_rx = PATTY_AX25_SOCK_2_2_DEFAULT_MAXLEN; + sock->n_window_tx = PATTY_AX25_SOCK_2_2_DEFAULT_WINDOW; + sock->n_window_rx = PATTY_AX25_SOCK_2_2_DEFAULT_WINDOW; + + return init_bufs(sock); +} + int patty_ax25_sock_params_set(patty_ax25_sock *sock, patty_ax25_params *params) { if (params->flags & PATTY_AX25_PARAM_CLASSES) { @@ -350,13 +361,13 @@ ssize_t patty_ax25_sock_send(patty_ax25_sock *sock, ssize_t encoded; uint8_t *buf = PATTY_AX25_FRAME_CONTROL_U(control)? - sock->buf: tx_slot(sock, sock->seq_send); + sock->tx_buf: tx_slot(sock, sock->seq_send); if (toobig(sock, infolen)) { goto error_toobig; } - if ((encoded = encode_address(sock, cr, buf, tx_bufsz(sock))) < 0) { + if ((encoded = encode_address(sock, cr, buf, bufsz(sock))) < 0) { goto error_encode_address; } else { offset += encoded;