summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2015-02-16 11:32:46 +0100
committerTobias Klauser <tklauser@distanz.ch>2015-02-16 11:32:46 +0100
commit084939b1e5e692e73dbcb299c7d728d6c2928cbc (patch)
tree6ec32f5dbfa0db9d9afc24a7c8e3e110bb171409
parente5649bbb4e6555370f4c46b070616c1b215a6135 (diff)
pkt: Implement growing of packet on pkt_put
Also get rid of the unnecessary head member of struct pkt. For now we only append data at the end of a packet. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--llmnr.c2
-rw-r--r--pkt.h26
2 files changed, 19 insertions, 9 deletions
diff --git a/llmnr.c b/llmnr.c
index 8b5b387..e007de5 100644
--- a/llmnr.c
+++ b/llmnr.c
@@ -167,7 +167,7 @@ static void llmnr_respond(unsigned int ifindex, const struct llmnr_hdr *hdr,
log_info("Response packet length: %zu\n", pkt_len(p));
- if (sendto(sock, p->head, pkt_len(p), 0, sa, sizeof(struct sockaddr_in)) < 0) {
+ if (sendto(sock, p->data, pkt_len(p), 0, sa, sizeof(struct sockaddr_in)) < 0) {
log_err("Failed to send response: %s\n", strerror(errno));
}
diff --git a/pkt.h b/pkt.h
index 7a5d9be..30caae5 100644
--- a/pkt.h
+++ b/pkt.h
@@ -1,9 +1,10 @@
/*
* Packet buffer structure and utilities.
*
- * Based on pkt_buff.h from the netsniff-ng toolkit.
- *
* Copyright (C) 2015 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * Based on pkt_buff.h from the netsniff-ng toolkit which is:
+ *
* Copyright (C) 2012 Christoph Jaeger
*
* This file is part of llmnrd.
@@ -28,10 +29,10 @@
#include <stdbool.h>
#include <stdint.h>
+#include "log.h"
#include "util.h"
struct pkt {
- uint8_t *head;
uint8_t *data;
uint8_t *tail;
size_t size;
@@ -39,7 +40,7 @@ struct pkt {
static inline bool pkt_invariant(struct pkt *p)
{
- return p && (p->head <= p->data) && (p->data <= p->tail);
+ return p && (p->data <= p->tail);
}
static inline struct pkt *pkt_alloc(size_t size)
@@ -47,7 +48,7 @@ static inline struct pkt *pkt_alloc(size_t size)
struct pkt *p = xmalloc(sizeof(*p) + size);
uint8_t *data = (uint8_t *)p + sizeof(*p);
- p->head = p->data = p->tail = data;
+ p->data = p->tail = data;
return p;
}
@@ -66,15 +67,24 @@ static inline size_t pkt_len(struct pkt *p)
static inline uint8_t *pkt_put(struct pkt *p, size_t len)
{
- uint8_t *data = NULL;
+ uint8_t *data;
assert(pkt_invariant(p));
if (len <= pkt_len(p)) {
data = p->tail;
p->tail += len;
- } else
- assert(false); /* TODO */
+ } else {
+ /* grow packet */
+ size_t new_size = p->size + len - pkt_len(p);
+ struct pkt *np = xrealloc(p, sizeof(*np) + new_size);
+
+ log_dbg("Reallocating packet from %zu to %zu bytes\n", p->size, new_size);
+ data = (uint8_t *)np + sizeof(*np);
+
+ np->data = data;
+ np->tail = data + pkt_len(p);
+ }
return data;
}