Add helper function for easy set device's MAC address to proto
field which may be used by Ethernet & ARP protos generation.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_proto.c | 32 ++++++++++++++++++++++++++++++++
 trafgen_proto.h |  3 +++
 2 files changed, 35 insertions(+)

diff --git a/trafgen_proto.c b/trafgen_proto.c
index caf2685..e48a13e 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -5,7 +5,9 @@
 
 #include <stddef.h>
 #include <string.h>
+#include <linux/if_ether.h>
 
+#include "dev.h"
 #include "xmalloc.h"
 #include "trafgen_conf.h"
 #include "trafgen_proto.h"
@@ -251,6 +253,36 @@ void proto_field_set_default_be32(enum proto_id pid, 
uint32_t fid, uint32_t val)
        __proto_field_set_bytes(pid, fid, (uint8_t *)&val, true, true);
 }
 
+static void __proto_field_set_dev_mac(enum proto_id pid, uint32_t fid,
+                                     bool is_default)
+{
+       struct proto_gen *prot = proto_get_by_id(pid);
+       uint8_t mac[ETH_ALEN];
+       int ret;
+
+       if (proto_field_is_set(pid, fid))
+               return;
+
+       if (!prot->ctx->dev)
+               panic("Device is not specified\n");
+
+       ret = device_hw_address(prot->ctx->dev, mac);
+       if (ret < 0)
+               panic("Could not get device hw adress\n");
+
+       __proto_field_set_bytes(prot->id, fid, mac, is_default, false);
+}
+
+void proto_field_set_dev_mac(enum proto_id pid, uint32_t fid)
+{
+       __proto_field_set_dev_mac(pid, fid, false);
+}
+
+void proto_field_set_default_dev_mac(enum proto_id pid, uint32_t fid)
+{
+       __proto_field_set_dev_mac(pid, fid, true);
+}
+
 void protos_init(char *dev)
 {
        struct proto_gen *p;
diff --git a/trafgen_proto.h b/trafgen_proto.h
index 76f3376..821872a 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -79,4 +79,7 @@ extern void proto_field_set_default_be16(enum proto_id pid, 
uint32_t fid,
 extern void proto_field_set_default_be32(enum proto_id pid, uint32_t fid,
                                         uint32_t val);
 
+extern void proto_field_set_dev_mac(enum proto_id pid, uint32_t fid);
+extern void proto_field_set_default_dev_mac(enum proto_id pid, uint32_t fid);
+
 #endif /* TRAFGEN_PROTO_I_H */
-- 
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