summaryrefslogtreecommitdiff
path: root/trafgen_proto.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2017-06-07 22:24:52 +0300
committerTobias Klauser <tklauser@distanz.ch>2017-06-09 15:50:06 +0200
commit78c13b71e196a107eaa4ec00bb40b062929a6a88 (patch)
tree39598c8a9bd49e1a3d13247c288c04ea0778d69d /trafgen_proto.c
parent322deaf9bae5dc87b174c4369e7d00b6bb0bf641 (diff)
trafgen: Allow to generate packets to output pcap file
Add trafgen_dev.c module which provides generic way of reading and writing packets to/from networking device or a pcap file. Also allow to handle output pcap file via '-o, --out, --dev' option. It might be useful in future for testing some link protocols which is not easy to capture (e.g. wlan packets) w/o having some special setup. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> [tklauser: fix whitespace issues] Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_proto.c')
-rw-r--r--trafgen_proto.c50
1 files changed, 29 insertions, 21 deletions
diff --git a/trafgen_proto.c b/trafgen_proto.c
index 0353d6b..c2cbffb 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -24,7 +24,7 @@
((f)->mask ? (f)->mask : (0xffffffff))) >> (f)->shift)
struct ctx {
- const char *dev;
+ struct dev_io *dev;
};
static struct ctx ctx;
@@ -508,11 +508,13 @@ static void __proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid,
if (proto_hdr_field_is_set(hdr, fid))
return;
- ret = device_hw_address(ctx.dev, mac, sizeof(mac));
- if (ret < 0)
- panic("Could not get device hw address\n");
+ if (dev_io_is_netdev(ctx.dev)) {
+ ret = device_hw_address(dev_io_name_get(ctx.dev), mac, sizeof(mac));
+ if (ret < 0)
+ panic("Could not get device hw address\n");
- __proto_field_set_bytes(field, mac, 6, is_default, false);
+ __proto_field_set_bytes(field, mac, 6, is_default, false);
+ }
}
void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid)
@@ -536,14 +538,17 @@ static void __proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid,
if (proto_hdr_field_is_set(hdr, fid))
return;
- ret = device_address(ctx.dev, AF_INET, &ss);
- if (ret < 0) {
- fprintf(stderr, "Warning: Could not get device IPv4 address for %s\n", ctx.dev);
- return;
- }
+ if (dev_io_is_netdev(ctx.dev)) {
+ ret = device_address(dev_io_name_get(ctx.dev), AF_INET, &ss);
+ if (ret < 0) {
+ fprintf(stderr, "Warning: Could not get device IPv4 address for %s\n",
+ dev_io_name_get(ctx.dev));
+ return;
+ }
- ss4 = (struct sockaddr_in *) &ss;
- __proto_field_set_bytes(field, (uint8_t *)&ss4->sin_addr.s_addr, 4, is_default, false);
+ ss4 = (struct sockaddr_in *) &ss;
+ __proto_field_set_bytes(field, (uint8_t *)&ss4->sin_addr.s_addr, 4, is_default, false);
+ }
}
void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid)
@@ -567,14 +572,17 @@ static void __proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid,
if (proto_hdr_field_is_set(hdr, fid))
return;
- ret = device_address(ctx.dev, AF_INET6, &ss);
- if (ret < 0) {
- fprintf(stderr, "Warning: Could not get device IPv6 address for %s\n", ctx.dev);
- return;
- }
+ if (dev_io_is_netdev(ctx.dev)) {
+ ret = device_address(dev_io_name_get(ctx.dev), AF_INET6, &ss);
+ if (ret < 0) {
+ fprintf(stderr, "Warning: Could not get device IPv6 address for %s\n",
+ dev_io_name_get(ctx.dev));
+ return;
+ }
- ss6 = (struct sockaddr_in6 *) &ss;
- __proto_field_set_bytes(field, (uint8_t *)&ss6->sin6_addr.s6_addr, 16, is_default, false);
+ ss6 = (struct sockaddr_in6 *) &ss;
+ __proto_field_set_bytes(field, (uint8_t *)&ss6->sin6_addr.s6_addr, 16, is_default, false);
+ }
}
void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid)
@@ -658,7 +666,7 @@ void proto_field_set_default_string(struct proto_field *field, const char *str)
__proto_field_set_bytes(field, (uint8_t *)str, strlen(str) + 1, true, false);
}
-void protos_init(const char *dev)
+void protos_init(struct dev_io *dev)
{
ctx.dev = dev;
@@ -792,7 +800,7 @@ void proto_field_dyn_apply(struct proto_field *field)
field->hdr->ops->field_changed(field);
}
-const char *proto_dev_get(void)
+struct dev_io *proto_dev_get(void)
{
return ctx.dev;
}