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
This commit is contained in:
XANTRONIX Development 2020-07-22 19:45:20 -04:00 committed by XANTRONIX Industrial
parent 5fa29da680
commit fa53b8d49d
3 changed files with 37 additions and 24 deletions

View file

@ -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);

View file

@ -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;
}

View file

@ -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;