summaryrefslogtreecommitdiff
path: root/trafgen_dev.c
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2017-07-29 12:46:09 +0300
committerTobias Klauser <tklauser@distanz.ch>2017-08-10 09:03:46 +0200
commit439af62bca4794d78d53fb4634f560d6a75f0adb (patch)
tree346cc168c42bbe1d7f0eb6bd493d244016172a01 /trafgen_dev.c
parent19348cec323373d84674c1d2cf34315cbf47c80d (diff)
trafgen: Dump proto headers in *.cfg format
Added trafgen_dump.c module which dumps headers from packet in .cfg format. Packet is dumped if -o <file>.cfg was specified, it might be useful to specify *.pcap file as input and convert it into .cfg file to edit proto fields in human readable format. To make it possible several main changes were added: 1) packet id is embedded into struct packet.id, and it is updated on each realloc_packet() 2) Added new struct proto_hdr.get_next_proto callback to make possible apply fields of next header. 3) Added new dev_io ops for writting packets into .cfg file, to re-use common dev_io mechsnism for packets dumping. Before dump the default ETH_PROTO fields are applied as first header and then next proto_hdr is identified via .get_next_proto(...) callback. Meanwhile only eth, arp, vlan, ip4, udp, & tcp protos can be dissected into *.cfg format. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_dev.c')
-rw-r--r--trafgen_dev.c36
1 files changed, 33 insertions, 3 deletions
diff --git a/trafgen_dev.c b/trafgen_dev.c
index d613cce..f65442f 100644
--- a/trafgen_dev.c
+++ b/trafgen_dev.c
@@ -20,6 +20,7 @@
#include "linktype.h"
#include "trafgen_dev.h"
#include "trafgen_conf.h"
+#include "trafgen_dump.h"
static int dev_pcap_open(struct dev_io *dev, const char *name, enum dev_io_mode_t mode)
{
@@ -90,6 +91,7 @@ static struct packet *dev_pcap_read(struct dev_io *dev)
pkt = realloc_packet();
pkt->len = pkt_len;
+ pkt->is_created = true;
pkt->payload = xzmalloc(pkt_len);
memcpy(pkt->payload, buf, pkt_len);
pcap_get_tstamp(&phdr, dev->pcap_magic, &pkt->tstamp);
@@ -97,7 +99,7 @@ static struct packet *dev_pcap_read(struct dev_io *dev)
return pkt;
}
-static int dev_pcap_write(struct dev_io *dev, const struct packet *pkt)
+static int dev_pcap_write(struct dev_io *dev, struct packet *pkt)
{
uint8_t *buf = pkt->payload;
size_t len = pkt->len;
@@ -172,7 +174,7 @@ static int dev_net_open(struct dev_io *dev, const char *name, enum dev_io_mode_t
return 0;
}
-static int dev_net_write(struct dev_io *dev, const struct packet *pkt)
+static int dev_net_write(struct dev_io *dev, struct packet *pkt)
{
struct sockaddr_ll saddr = {
.sll_family = PF_PACKET,
@@ -215,6 +217,31 @@ static const struct dev_io_ops dev_net_ops = {
.close = dev_net_close,
};
+static int dev_cfg_open(struct dev_io *dev, const char *name, enum dev_io_mode_t mode)
+{
+ dev->fd = open_or_die_m(name, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE, DEFFILEMODE);
+ return 0;
+}
+
+static int dev_cfg_write(struct dev_io *dev, struct packet *pkt)
+{
+ if (packet_dump_fd(pkt, dev->fd))
+ return -1;
+
+ return pkt->len;
+}
+
+static void dev_cfg_close(struct dev_io *dev)
+{
+ close(dev->fd);
+}
+
+static const struct dev_io_ops dev_cfg_ops = {
+ .open = dev_cfg_open,
+ .write = dev_cfg_write,
+ .close = dev_cfg_close,
+};
+
struct dev_io *dev_io_open(const char *name, enum dev_io_mode_t mode)
{
struct dev_io *dev = xzmalloc(sizeof(struct dev_io));
@@ -222,6 +249,9 @@ struct dev_io *dev_io_open(const char *name, enum dev_io_mode_t mode)
if (strstr(name, ".pcap")) {
dev->name = xstrdup(name);
dev->ops = &dev_pcap_ops;
+ } else if (strstr(name, ".cfg")) {
+ dev->name = xstrdup(name);
+ dev->ops = &dev_cfg_ops;
} else if (device_mtu(name) > 0) {
dev->name = xstrndup(name, IFNAMSIZ);
dev->ops = &dev_net_ops;
@@ -240,7 +270,7 @@ struct dev_io *dev_io_open(const char *name, enum dev_io_mode_t mode)
return dev;
};
-int dev_io_write(struct dev_io *dev, const struct packet *pkt)
+int dev_io_write(struct dev_io *dev, struct packet *pkt)
{
bug_on(!dev);
bug_on(!dev->ops);