Add helper which fill device's ipv4 addr to the specified proto field.

It will be used by such protos like ARP, IPv4, etc.

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 efa6803..dac806e 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -5,6 +5,7 @@
 
 #include <stddef.h>
 #include <string.h>
+#include <netinet/in.h>
 #include <linux/if_ether.h>
 
 #include "dev.h"
@@ -326,6 +327,37 @@ void proto_field_set_default_dev_mac(struct proto_hdr 
*hdr, uint32_t fid)
        __proto_field_set_dev_mac(hdr, fid, true);
 }
 
+static void __proto_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid,
+                                      bool is_default)
+{
+       struct sockaddr_storage ss = { };
+       struct sockaddr_in *ss4;
+       uint32_t ip_addr;
+       int ret;
+
+       if (proto_field_is_set(hdr, fid))
+               return;
+
+       ret = device_address(hdr->ctx->dev, AF_INET, &ss);
+       if (ret < 0)
+               panic("Could not get device IPv4 address\n");
+
+       ss4 = (struct sockaddr_in *) &ss;
+       ip_addr = ss4->sin_addr.s_addr;
+
+       __proto_field_set_bytes(hdr, fid, (uint8_t *)&ip_addr, is_default, 
false);
+}
+
+void proto_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid)
+{
+       __proto_field_set_dev_ipv4(hdr, fid, false);
+}
+
+void proto_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid)
+{
+       __proto_field_set_dev_ipv4(hdr, fid, true);
+}
+
 void protos_init(char *dev)
 {
        struct proto_hdr *p;
diff --git a/trafgen_proto.h b/trafgen_proto.h
index 4041c27..b0578e3 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -96,4 +96,7 @@ extern void proto_field_set_default_be32(struct proto_hdr 
*hdr, uint32_t fid,
 extern void proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
 extern void proto_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t 
fid);
 
+extern void proto_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
+extern void proto_field_set_default_dev_ipv4(struct proto_hdr *hdr, 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