From 7fd05f607a34527adaece34b6ff1b5228b841022 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 29 Jun 2020 00:10:48 -0400 Subject: [PATCH] Huh, I managed to make something nice --- include/patty/ax25/frame.h | 7 ++--- src/frame.c | 29 +++++++++---------- src/server.c | 58 ++++++++++++++++++-------------------- 3 files changed, 43 insertions(+), 51 deletions(-) diff --git a/include/patty/ax25/frame.h b/include/patty/ax25/frame.h index fcb1131..913af67 100644 --- a/include/patty/ax25/frame.h +++ b/include/patty/ax25/frame.h @@ -71,12 +71,9 @@ ssize_t patty_ax25_frame_encode(patty_ax25_frame *frame, size_t len); ssize_t patty_ax25_frame_encode_reply_to(patty_ax25_frame *frame, - void *buf, + patty_ax25_frame *reply, enum patty_ax25_frame_format format, - uint16_t control, - uint8_t proto, - void *info, - size_t infolen, + void *buf, size_t len); enum patty_ax25_version patty_ax25_frame_version(patty_ax25_frame *frame); diff --git a/src/frame.c b/src/frame.c index d6ec769..2fd2b72 100644 --- a/src/frame.c +++ b/src/frame.c @@ -335,40 +335,39 @@ error_toobig: } ssize_t patty_ax25_frame_encode_reply_to(patty_ax25_frame *frame, - void *buf, + patty_ax25_frame *reply, enum patty_ax25_frame_format format, - uint16_t control, - uint8_t proto, - void *info, - size_t infolen, + void *buf, size_t len) { - size_t offset = 0; + size_t offset; - offset = encode_reply_address(frame, buf, len); + if ((offset = encode_reply_address(frame, buf, len)) < 0) { + goto error_toobig; + } switch (format) { case PATTY_AX25_FRAME_NORMAL: - ((uint8_t *)buf)[offset++] = control; + ((uint8_t *)buf)[offset++] = reply->control; break; case PATTY_AX25_FRAME_EXTENDED: - ((uint8_t *)buf)[offset++] = (control & 0xff00) >> 8; - ((uint8_t *)buf)[offset++] = control & 0xf00ff; + ((uint8_t *)buf)[offset++] = (reply->control & 0xff00) >> 8; + ((uint8_t *)buf)[offset++] = reply->control & 0xf00ff; break; } - if (PATTY_AX25_CONTROL_INFO(control)) { - if (1 + offset + infolen > len) { + if (PATTY_AX25_CONTROL_INFO(reply->control)) { + if (len < 1 + offset + reply->infolen) { goto error_toobig; } - ((uint8_t *)buf)[offset++] = proto; + ((uint8_t *)buf)[offset++] = reply->proto; - memcpy((uint8_t *)buf + offset, info, infolen); + memcpy((uint8_t *)buf + offset, reply->info, reply->infolen); - offset += infolen; + offset += reply->infolen; } return offset; diff --git a/src/server.c b/src/server.c index 64a4092..b8893a1 100644 --- a/src/server.c +++ b/src/server.c @@ -1069,22 +1069,16 @@ static void save_reply_addr(patty_ax25_sock *sock, sock->hops = hops; } -static int reply_to(patty_ax25_frame *frame, - patty_ax25_if *iface, - enum patty_ax25_frame_format format, - uint16_t control, - uint8_t proto, - void *info, - size_t infolen) { +static int reply_to(patty_ax25_if *iface, + patty_ax25_frame *frame, + patty_ax25_frame *reply, + enum patty_ax25_frame_format format) { ssize_t len; if ((len = patty_ax25_frame_encode_reply_to(frame, - iface->tx_buf, + reply, PATTY_AX25_FRAME_NORMAL, - control, - proto, - NULL, - 0, + iface->tx_buf, iface->tx_bufsz)) < 0) { goto error_toobig; } @@ -1095,28 +1089,30 @@ error_toobig: return -1; } -static int reply_dm(patty_ax25_frame *frame, - patty_ax25_if *iface, +static int reply_dm(patty_ax25_if *iface, + patty_ax25_frame *frame, enum patty_ax25_frame_u_flags flags) { - return reply_to(frame, - iface, - PATTY_AX25_FRAME_NORMAL, - PATTY_AX25_FRAME_U_DM | flags, - PATTY_AX25_PROTO_NONE, - NULL, - 0); + patty_ax25_frame reply = { + .control = PATTY_AX25_FRAME_U_DM | flags, + .proto = PATTY_AX25_PROTO_NONE, + .info = NULL, + .infolen = 0 + }; + + return reply_to(iface, frame, &reply, PATTY_AX25_FRAME_NORMAL); } -static int reply_ua(patty_ax25_frame *frame, - patty_ax25_if *iface, +static int reply_ua(patty_ax25_if *iface, + patty_ax25_frame *frame, enum patty_ax25_frame_u_flags flags) { - return reply_to(frame, - iface, - PATTY_AX25_FRAME_NORMAL, - PATTY_AX25_FRAME_U_UA | flags, - PATTY_AX25_PROTO_NONE, - NULL, - 0); + patty_ax25_frame reply = { + .control = PATTY_AX25_FRAME_U_UA | flags, + .proto = PATTY_AX25_PROTO_NONE, + .info = NULL, + .infolen = 0 + }; + + return reply_to(iface, frame, &reply, PATTY_AX25_FRAME_NORMAL); } static int handle_frame(patty_ax25_server *server, @@ -1137,7 +1133,7 @@ static int handle_frame(patty_ax25_server *server, if ((sock = sock_by_addr(server->socks_pending_accept, &frame.dest)) == NULL) { - if (reply_dm(&frame, iface, PATTY_AX25_FRAME_U_FINAL) < 0) { + if (reply_dm(iface, &frame, PATTY_AX25_FRAME_U_FINAL) < 0) { goto error_io; } }