From 8fe8ed1942b6d7ff029a5a02e3352009d31f2ee2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 21 Mar 2013 20:34:51 +0100 Subject: Save maximum length in packet buffers --- src/packet.h | 14 ++++++++++---- src/send.c | 22 +++++++++++----------- src/tlv.c | 10 +++++----- src/tlv.h | 2 +- src/types.h | 1 + 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/packet.h b/src/packet.h index df22dd6..09b6dc0 100644 --- a/src/packet.h +++ b/src/packet.h @@ -43,11 +43,17 @@ struct __attribute__((packed)) gp_babel_packet { uint8_t tlv[]; }; +struct gp_babel_packet_buf { + size_t max_len; + gp_babel_packet_t packet; +}; + #define gp_babel_packet_alloca(size) ({ \ - gp_babel_packet_t *__packet = alloca(sizeof(gp_babel_packet_t)+size); \ - __packet->version = htons(GP_BABEL_VERSION); \ - __packet->len = 0; \ - __packet; \ + gp_babel_packet_buf_t *__buf = alloca(sizeof(gp_babel_packet_buf_t) + size - sizeof(gp_babel_packet_t)); \ + __buf->max_len = size - sizeof(gp_babel_packet_t); \ + __buf->packet.version = htons(GP_BABEL_VERSION); \ + __buf->packet.len = 0; \ + __buf; \ }) static inline size_t gp_babel_packet_size(const gp_babel_packet_t *packet) { diff --git a/src/send.c b/src/send.c index cdeb07a..56685c3 100644 --- a/src/send.c +++ b/src/send.c @@ -44,22 +44,22 @@ static inline bool send_neigh(gmrf_t *gmrf, const gp_babel_neigh_t *neigh, const } void gp_babel_send_ack(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_neigh_t *neigh, uint16_t nonce) { - gp_babel_packet_t *packet = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); + gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); - gp_babel_tlv_ack_t *ack = gp_babel_tlv_add(packet, GP_BABEL_PACKET_MAX, TLV_ACK, sizeof(gp_babel_tlv_ack_t)); + gp_babel_tlv_ack_t *ack = gp_babel_tlv_add(buf, TLV_ACK, sizeof(gp_babel_tlv_ack_t)); if (!ack) return; ack->nonce = htons(nonce); - send_neigh(gmrf, neigh, packet); + send_neigh(gmrf, neigh, &buf->packet); } -static void add_ihus(gmrf_t *gmrf, gp_babel_packet_t *packet, size_t max_len, const gp_babel_iface_t *iface) { +static void add_ihus(gmrf_t *gmrf, gp_babel_packet_buf_t *buf, const gp_babel_iface_t *iface) { const gp_babel_neigh_t *neigh; for (neigh = iface->neighbours; neigh; neigh = neigh->next) { - gp_babel_tlv_ihu_t *ihu = gp_babel_tlv_add(packet, GP_BABEL_PACKET_MAX, TLV_IHU, sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)); + gp_babel_tlv_ihu_t *ihu = gp_babel_tlv_add(buf, TLV_IHU, sizeof(gp_babel_tlv_ihu_t)+sizeof(gmrf_addr_t)); if (!ihu) return; @@ -74,25 +74,25 @@ static void add_ihus(gmrf_t *gmrf, gp_babel_packet_t *packet, size_t max_len, co void gp_babel_send_hellos(gmrf_t *gmrf, gmrf_context_t *ctx) { gmrf_logf(gmrf, LOG_DEBUG, "sending hellos..."); - gp_babel_packet_t *packet = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); + gp_babel_packet_buf_t *buf = gp_babel_packet_alloca(GP_BABEL_PACKET_MAX); - gp_babel_tlv_hello_t *hello = gp_babel_tlv_add(packet, GP_BABEL_PACKET_MAX, TLV_HELLO, sizeof(gp_babel_tlv_hello_t)); + gp_babel_tlv_hello_t *hello = gp_babel_tlv_add(buf, TLV_HELLO, sizeof(gp_babel_tlv_hello_t)); if (!hello) return; hello->reserved = 0; hello->interval = htons(GP_BABEL_HELLO_INTERVAL); - uint16_t len = packet->len; + uint16_t len = buf->packet.len; gp_babel_iface_t *iface; for (iface = ctx->interfaces; iface; iface = iface->next) { hello->seqno = htons(iface->seqno++); - packet->len = len; + buf->packet.len = len; - add_ihus(gmrf, packet, GP_BABEL_PACKET_MAX, iface); + add_ihus(gmrf, buf, iface); - gmrf_iface_send_bc(gmrf, iface->gmrf_iface, packet, gp_babel_packet_size(packet)); + gmrf_iface_send_bc(gmrf, iface->gmrf_iface, &buf->packet, gp_babel_packet_size(&buf->packet)); } } diff --git a/src/tlv.c b/src/tlv.c index 488f775..38232d1 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -57,18 +57,18 @@ bool gp_babel_tlv_parse(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet return true; } -void* gp_babel_tlv_add(gp_babel_packet_t *packet, size_t max_len, gp_babel_tlv_type_t type, size_t len) { - size_t pktlen = ntohs(packet->len); +void* gp_babel_tlv_add(gp_babel_packet_buf_t *buf, gp_babel_tlv_type_t type, size_t len) { + size_t pktlen = ntohs(buf->packet.len); - if (pktlen+len+2 > max_len) + if (pktlen+len+2 > buf->max_len) return NULL; - uint8_t *data = packet->tlv+pktlen+2; + uint8_t *data = buf->packet.tlv+pktlen+2; data[-2] = type; data[-1] = len; - packet->len = htons(pktlen+len+2); + buf->packet.len = htons(pktlen+len+2); return data; } diff --git a/src/tlv.h b/src/tlv.h index d05a4a8..6c35129 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -57,7 +57,7 @@ typedef void (*gp_babel_tlv_cb)(gmrf_t *gmrf, gmrf_context_t *ctx, gp_babel_tlv_ bool gp_babel_tlv_parse(gmrf_t *gmrf, gmrf_context_t *ctx, const gp_babel_packet_t *packet, gp_babel_tlv_cb cb, void *arg); -void* gp_babel_tlv_add(gp_babel_packet_t *packet, size_t max_len, gp_babel_tlv_type_t type, size_t len); +void* gp_babel_tlv_add(gp_babel_packet_buf_t *buf, gp_babel_tlv_type_t type, size_t len); #endif /* _GMRF_PROTO_BABEL_TLV_H_ */ diff --git a/src/types.h b/src/types.h index ce3d99f..1d05c03 100644 --- a/src/types.h +++ b/src/types.h @@ -35,6 +35,7 @@ typedef struct __attribute__((packed)) gp_gabel_node_id { } gp_babel_node_id_t; typedef struct gp_babel_packet gp_babel_packet_t; +typedef struct gp_babel_packet_buf gp_babel_packet_buf_t; typedef struct gp_babel_iface gp_babel_iface_t; typedef struct gp_babel_neigh gp_babel_neigh_t; -- cgit v1.2.3