summaryrefslogtreecommitdiff
path: root/trafgen_proto.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-01-26 22:25:05 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-01-28 16:15:23 +0100
commitd053535a66ff864d309dec2303927e410b27e5b6 (patch)
tree548dd875b5ad9a14ac61d18ffb005186af8249c6 /trafgen_proto.c
parentf3c3677c5dd67d7edda5bee5b316e014be231337 (diff)
trafgen: proto: Add functon to fill field with device ipv4 address
Add helper which fills a device's ipv4 addr to the specified protocol field. It will be used by protocols like ARP, IPv4, etc. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_proto.c')
-rw-r--r--trafgen_proto.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/trafgen_proto.c b/trafgen_proto.c
index 4976e54..2c090b2 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"
@@ -321,6 +322,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(const char *dev)
{
struct proto_hdr *p;