From 6b908f22e27165686aaf62e86fd8f9b7efbc52c9 Mon Sep 17 00:00:00 2001 From: Heikki Hannikainen Date: Sat, 15 Apr 2017 22:09:26 +0300 Subject: [PATCH] IS2 UDP: Send sequence numbers, remove malloc()+free() from transmit --- src/aprsis2.c | 34 ++++++++++++++++++++++------------ src/worker.h | 4 ++-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/aprsis2.c b/src/aprsis2.c index ce04847..a75f227 100644 --- a/src/aprsis2.c +++ b/src/aprsis2.c @@ -27,18 +27,24 @@ * Allocate a buffer for a message, fill with head an tail */ +static void is2_setup_buffer(char *buf, int len) +{ + uint32_t *len_p = (uint32_t *)buf; + + *len_p = htonl(len); + + buf[0] = STX; + buf[IS2_HEAD_LEN + len] = ETX; +} + static void *is2_allocate_buffer(int len) { /* total length of outgoing buffer */ int nlen = len + IS2_HEAD_LEN + IS2_TAIL_LEN; char *buf = hmalloc(nlen); - uint32_t *len_p = (uint32_t *)buf; - *len_p = htonl(len); - - buf[0] = STX; - buf[nlen-1] = ETX; + is2_setup_buffer(buf, len); return (void *)buf; } @@ -69,15 +75,19 @@ static int is2_write_message(struct worker_t *self, struct client_t *c, IS2Messa static int is2_corepeer_write_message(struct worker_t *self, struct client_t *c, IS2Message *m) { - /* Could optimize by writing directly on client obuf... - * if it doesn't fit there, we're going to disconnect anyway. - */ + m->sequence = c->corepeer_is2_sequence++; + m->has_sequence = 1; int len = is2_message__get_packed_size(m); - void *buf = is2_allocate_buffer(len); - is2_message__pack(m, buf + IS2_HEAD_LEN); - int r = udp_client_write(self, c, buf, len + IS2_HEAD_LEN + IS2_TAIL_LEN); // TODO: return value check! - hfree(buf); + int blen = len + IS2_HEAD_LEN + IS2_TAIL_LEN; + if (blen > c->obuf_size) { + hlog(LOG_DEBUG, "%s/%s: IS2 UDP: serialized IS2 frame length %d does not fit in obuf", c->addr_rem, c->username, blen); + return -1; + } + is2_setup_buffer(c->obuf, len); + + is2_message__pack(m, (void *)c->obuf + IS2_HEAD_LEN); + int r = udp_client_write(self, c, c->obuf, blen); hlog(LOG_DEBUG, "%s/%s: IS2 UDP: serialized length %d, frame %d, wrote %d", c->addr_rem, c->username, len, len + IS2_HEAD_LEN + IS2_TAIL_LEN, r); return r; diff --git a/src/worker.h b/src/worker.h index c556246..bb16c1c 100644 --- a/src/worker.h +++ b/src/worker.h @@ -421,8 +421,8 @@ struct client_t { #endif char filter_s[FILTER_S_SIZE]; - /* IS2 handshake random token */ - char *corepeer_is2_challenge; + char *corepeer_is2_challenge; /* IS2 handshake random token */ + uint32_t corepeer_is2_sequence; /* IS2 UDP peer sequence number */ }; extern struct client_t *client_alloc(void);