Add trafgen_l2.c module for generating L2 related headers.

Add Ethernet header generating. By default source MAC address
is used from the specified output device.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen/Makefile |  1 +
 trafgen_l2.c     | 32 ++++++++++++++++++++++++++++++++
 trafgen_l2.h     | 12 ++++++++++++
 trafgen_proto.c  |  3 +++
 4 files changed, 48 insertions(+)
 create mode 100644 trafgen_l2.c
 create mode 100644 trafgen_l2.h

diff --git a/trafgen/Makefile b/trafgen/Makefile
index 2ea684f..1c95118 100644
--- a/trafgen/Makefile
+++ b/trafgen/Makefile
@@ -20,6 +20,7 @@ trafgen-objs =        xmalloc.o \
                sysctl.o \
                cpp.o \
                trafgen_proto.o \
+               trafgen_l2.o \
                trafgen_lexer.yy.o \
                trafgen_parser.tab.o \
                trafgen.o
diff --git a/trafgen_l2.c b/trafgen_l2.c
new file mode 100644
index 0000000..1082049
--- /dev/null
+++ b/trafgen_l2.c
@@ -0,0 +1,32 @@
+/*
+ * netsniff-ng - the packet sniffing beast
+ * Subject to the GPL, version 2.
+ */
+
+#include "built_in.h"
+#include "trafgen_l2.h"
+#include "trafgen_proto.h"
+
+struct proto_field eth_fields[] = {
+       { .id = ETH_DST_ADDR, .len = 6, },
+       { .id = ETH_SRC_ADDR, .len = 6, .offset = 6 },
+       { .id = ETH_PROTO_ID, .len = 2, .offset = 12 },
+};
+
+static void eth_header_init(struct proto_hdr *hdr)
+{
+       proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
+
+       proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
+}
+
+static struct proto_hdr eth_hdr = {
+       .id             = PROTO_ETH,
+       .layer          = PROTO_L2,
+       .header_init    = eth_header_init,
+};
+
+void protos_l2_init(void)
+{
+       proto_header_register(&eth_hdr);
+}
diff --git a/trafgen_l2.h b/trafgen_l2.h
new file mode 100644
index 0000000..75d3d33
--- /dev/null
+++ b/trafgen_l2.h
@@ -0,0 +1,12 @@
+#ifndef TRAFGEN_L2_I_H
+#define TRAFGEN_L2_I_H
+
+enum eth_field {
+       ETH_DST_ADDR,
+       ETH_SRC_ADDR,
+       ETH_PROTO_ID,
+};
+
+extern void protos_l2_init(void);
+
+#endif /* TRAFGEN_L2_I_H */
diff --git a/trafgen_proto.c b/trafgen_proto.c
index 69579fa..efa6803 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -10,6 +10,7 @@
 #include "dev.h"
 #include "xmalloc.h"
 #include "trafgen_conf.h"
+#include "trafgen_l2.h"
 #include "trafgen_proto.h"
 
 #define field_shift_and_mask(f, v) (((v) << (f)->shift) & \
@@ -331,6 +332,8 @@ void protos_init(char *dev)
 
        ctx.dev = dev;
 
+       protos_l2_init();
+
        for (p = registered; p; p = p->next)
                p->ctx = &ctx;
 }
-- 
2.6.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to