summaryrefslogtreecommitdiff
path: root/trafgen_proto.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-07-26 22:35:08 +0300
committerTobias Klauser <tklauser@distanz.ch>2016-08-02 17:24:43 +0200
commit820e85f9f29513860092a02599efa529c00a1089 (patch)
tree864ad7a68b1985ba4dd390170091458aa904ed73 /trafgen_proto.c
parent59e69bf9106e6f085f90cb33ac7241e3ded41767 (diff)
trafgen: proto: Move proto headers into packet
Until now headers were used only for packet creation at compile time, which does not allow to handle dynamic field updates at runtime. To support dynamic updates, it is necessary to keep the proto_hdr entries around after packet compilation so we can reference the header fields to dynamically update. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tk: Adjust patch description, simplify code] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_proto.c')
-rw-r--r--trafgen_proto.c30
1 files changed, 9 insertions, 21 deletions
diff --git a/trafgen_proto.c b/trafgen_proto.c
index e5b1ad3..e08c9c2 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -24,15 +24,12 @@
static struct proto_ctx ctx;
-#define PROTO_MAX_LAYERS 16
-
-static struct proto_hdr *headers[PROTO_MAX_LAYERS];
-static size_t headers_count;
-
static struct proto_hdr *registered;
struct proto_hdr *proto_lower_header(struct proto_hdr *hdr)
{
+ struct proto_hdr **headers = current_packet()->headers;
+ size_t headers_count = current_packet()->headers_count;
struct proto_hdr *lower = NULL;
size_t i;
@@ -128,10 +125,11 @@ bool proto_field_is_set(struct proto_hdr *hdr, uint32_t fid)
struct proto_hdr *proto_header_init(enum proto_id pid)
{
+ struct proto_hdr **headers = current_packet()->headers;
struct proto_hdr *hdr = proto_header_by_id(pid);
struct proto_hdr *new_hdr;
- bug_on(headers_count >= PROTO_MAX_LAYERS);
+ bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS);
new_hdr = xmalloc(sizeof(*new_hdr));
memcpy(new_hdr, hdr, sizeof(*new_hdr));
@@ -141,7 +139,7 @@ struct proto_hdr *proto_header_init(enum proto_id pid)
if (new_hdr->header_init)
new_hdr->header_init(new_hdr);
- headers[headers_count++] = new_hdr;
+ headers[current_packet()->headers_count++] = new_hdr;
return new_hdr;
}
@@ -155,9 +153,10 @@ struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
enum proto_id pid)
{
struct proto_hdr *current;
+ size_t headers_count = current_packet()->headers_count;
if (headers_count > 0) {
- current = headers[headers_count - 1];
+ current = current_packet()->headers[headers_count - 1];
if (current->layer >= proto_header_by_id(pid)->layer)
goto set_proto;
@@ -418,6 +417,8 @@ void protos_init(const char *dev)
void proto_packet_finish(void)
{
+ struct proto_hdr **headers = current_packet()->headers;
+ size_t headers_count = current_packet()->headers_count;
ssize_t i;
/* Go down from upper layers to do last calculations (checksum) */
@@ -427,17 +428,4 @@ void proto_packet_finish(void)
if (p->packet_finish)
p->packet_finish(p);
}
-
- for (i = 0; i < headers_count; i++) {
- struct proto_hdr *p = headers[i];
-
- if (p->fields) {
- xfree(p->fields);
- p->fields_count = 0;
- }
-
- xfree(headers[i]);
- }
-
- headers_count = 0;
}