[netsniff-ng] Re: [PATCH 2/3] trafgen: Allow send packets from pcap file

2017-02-06 Thread Tobias Klauser
On 2017-01-31 at 23:31:35 +0100, Vadim Kochan  wrote:
> Add ability to send packets from pcap file if it has
> ".pcap" extension via "-i,--in" option.
> 
> By default packet sending is delayed considering original
> packets timestamps if no rate or delay is specified via -b/-t options.
> 
> Signed-off-by: Vadim Kochan 
> ---
>  trafgen.c| 155 
> +--
>  trafgen/Makefile |   4 ++
>  trafgen_conf.h   |   3 ++
>  trafgen_parser.y |   2 +-
>  4 files changed, 137 insertions(+), 27 deletions(-)
> 
> diff --git a/trafgen.c b/trafgen.c
> index 524b260..c0d77a3 100644
> --- a/trafgen.c
> +++ b/trafgen.c
> @@ -5,6 +5,8 @@
>   * Subject to the GPL, version 2.
>   */
>  
> +#define _GNU_SOURCE
> +
>  #include 
>  #include 
>  #include 
> @@ -16,7 +18,6 @@
>  #include 
>  #include 
>  #include 
> -#include 
>  #include 
>  #include 
>  #include 
> @@ -55,6 +56,7 @@
>  #include "ring_tx.h"
>  #include "csum.h"
>  #include "trafgen_proto.h"
> +#include "pcap_io.h"
>  
>  #ifndef timeval_to_timespec
>  #define timeval_to_timespec(tv, ts) { \
> @@ -65,14 +67,17 @@
>  
>  enum shaper_type {
>   SHAPER_NONE,
> + SHAPER_DELAY,
>   SHAPER_PKTS,
>   SHAPER_BYTES,
> + SHAPER_TSTAMP,
>  };
>  
>  struct shaper {
>   enum shaper_type type;
>   unsigned long long sent;
>   unsigned long long rate;
> + struct timeval tstamp;
>   struct timeval start;
>   struct timeval end;
>   struct timespec delay;
> @@ -88,6 +93,7 @@ struct ctx {
>   struct sockaddr_in dest;
>   struct shaper sh;
>   char *packet_str;
> + char *pcap_in;
>  };
>  
>  struct cpu_stats {
> @@ -556,15 +562,12 @@ static int xmit_smoke_probe(int icmp_sock, struct ctx 
> *ctx)
>  
>  static bool shaper_is_set(struct shaper *sh)
>  {
> - if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0)
> - return true;
> -
> - return sh->type != SHAPER_NONE;
> +   return sh->type != SHAPER_NONE;
>  }
>  
>  static void shaper_init(struct shaper *sh)
>  {
> - if (sh->type == SHAPER_NONE)
> + if (sh->type == SHAPER_NONE || sh->type == SHAPER_DELAY)
>   return;
>  
>   memset(>delay, 0, sizeof(struct timespec));
> @@ -574,6 +577,12 @@ static void shaper_init(struct shaper *sh)
>  
>  static void shaper_set_delay(struct shaper *sh, time_t sec, long int ns)
>  {
> + if (!(sec | ns)) {
> + sh->type = SHAPER_NONE;
> + return;
> + }
> +
> + sh->type = SHAPER_DELAY;
>   sh->delay.tv_sec = sec;
>   sh->delay.tv_nsec = ns;
>  }
> @@ -586,23 +595,48 @@ static void shaper_set_rate(struct shaper *sh, unsigned 
> long long rate,
>   sh->type = type;
>  }
>  
> -static void shaper_delay(struct shaper *sh, unsigned long pkt_len)
> +static void shaper_set_tstamp(struct shaper *sh, struct timeval *tv)
>  {
> - if (sh->type != SHAPER_NONE)
> + sh->tstamp.tv_sec = tv->tv_sec;
> + sh->tstamp.tv_usec = tv->tv_usec;
> +}
> +
> +static void shaper_delay(struct shaper *sh, struct packet *pkt)
> +{
> + if (sh->type == SHAPER_BYTES || sh->type == SHAPER_PKTS) {
> + unsigned long pkt_len = pkt->len;
> +
>   sh->sent += sh->type == SHAPER_BYTES ? pkt_len : 1;
>  
> - if (sh->sent >= sh->rate && sh->rate > 0) {
> - struct timeval delay_us;
> - struct timeval time_sent;
> - struct timeval time_1s = { .tv_sec = 1 };
> + if (sh->sent >= sh->rate && sh->rate > 0) {
> + struct timeval delay_us;
> + struct timeval time_sent;
> + struct timeval time_1s = { .tv_sec = 1 };
> +
> + bug_on(gettimeofday(>end, NULL));
> + timersub(>end, >start, _sent);
> +
> + if (timercmp(_1s, _sent, > )) {
> + timersub(_1s, _sent, _us);
> + timeval_to_timespec(_us, >delay);
> + }
> + }
> + } else if (sh->type == SHAPER_TSTAMP) {
> + struct timeval pkt_diff;
> + struct timeval diff;
>  
>   bug_on(gettimeofday(>end, NULL));
> - timersub(>end, >start, _sent);
> + timersub(>end, >start, );
> + timersub(>tstamp, >tstamp, _diff);
> +
> + if (timercmp(, _diff, <)) {
> + struct timeval delay;
>  
> - if (timercmp(_1s, _sent, > )) {
> - timersub(_1s, _sent, _us);
> - timeval_to_timespec(_us, >delay);
> + timersub(_diff, , );
> + timeval_to_timespec(, >delay);
>   }
> +
> + memcpy(>tstamp, >tstamp, sizeof(sh->tstamp));
>   }
>  
>   if ((sh->delay.tv_sec | sh->delay.tv_nsec) > 0) {
> @@ -698,7 +732,7 @@ retry:
>   num--;
>  
>   if 

[netsniff-ng] Re: [PATCH 1/3] pcap_io: Add function to get packet timestamp

2017-02-06 Thread Tobias Klauser
On 2017-01-31 at 23:31:34 +0100, Vadim Kochan  wrote:
> Add pcap_get_tstamp(...) function to get packet's timestamp considering
> different packet types & bytes order.
> 
> Signed-off-by: Vadim Kochan 
> ---
>  pcap_io.h | 53 +
>  1 file changed, 53 insertions(+)
> 
> diff --git a/pcap_io.h b/pcap_io.h
> index 4e41362..7bf5fe6 100644
> --- a/pcap_io.h
> +++ b/pcap_io.h
> @@ -373,6 +373,59 @@ static inline void pcap_set_length(pcap_pkthdr_t *phdr, 
> enum pcap_type type, u32
>   }
>  }
>  
> +static inline void pcap_get_tstamp(pcap_pkthdr_t *phdr, enum pcap_type type,
> +struct timeval *tv)

This should take a struct timespec in order to retain nanosecond
precision.

> +{
> + switch (type) {
> + case DEFAULT:
> + case DEFAULT_LL:
> + tv->tv_sec = phdr->ppo.ts.tv_sec;
> + tv->tv_usec = phdr->ppo.ts.tv_usec;
> + break;
> +
> + case DEFAULT_SWAPPED:
> + case DEFAULT_LL_SWAPPED:
> + tv->tv_sec = ___constant_swab32(phdr->ppo.ts.tv_sec);
> + tv->tv_usec = ___constant_swab32(phdr->ppo.ts.tv_usec);
> + break;
> +
> + case NSEC:
> + case NSEC_LL:
> + tv->tv_sec = phdr->ppn.ts.tv_sec;
> + tv->tv_usec = phdr->ppn.ts.tv_nsec / 1000;
> + break;
> +
> + case NSEC_SWAPPED:
> + case NSEC_LL_SWAPPED:
> + tv->tv_sec = ___constant_swab32(phdr->ppn.ts.tv_sec);
> + tv->tv_usec = ___constant_swab32(phdr->ppn.ts.tv_nsec) / 1000;
> + break;
> +
> + case KUZNETZOV:
> + tv->tv_sec = phdr->ppk.ts.tv_sec;
> + tv->tv_usec = phdr->ppk.ts.tv_usec;
> + break;
> +
> + case KUZNETZOV_SWAPPED:
> + tv->tv_sec = ___constant_swab32(phdr->ppk.ts.tv_sec);
> + tv->tv_usec = ___constant_swab32(phdr->ppk.ts.tv_usec);
> + break;
> +
> + case BORKMANN:
> + tv->tv_sec = phdr->ppb.ts.tv_sec;
> + tv->tv_usec = phdr->ppb.ts.tv_nsec / 1000;
> + break;
> +
> + case BORKMANN_SWAPPED:
> + tv->tv_sec = ___constant_swab32(phdr->ppb.ts.tv_sec);
> + tv->tv_usec = ___constant_swab32(phdr->ppb.ts.tv_nsec) / 1000;
> + break;
> +
> + default:
> + bug();
> + }
> +}
> +
>  static inline u32 pcap_get_hdr_length(pcap_pkthdr_t *phdr, enum pcap_type 
> type)
>  {
>   switch (type) {
> -- 
> 2.11.0
> 

-- 
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.


[netsniff-ng] [PATCH v2] trafgen: l3: Make possible to send frames via tun device

2017-02-06 Thread Vadim Kochan
tun interface does not have Ethernet header so lets push Ethernet
header only if device supports this.

Signed-off-by: Vadim Kochan 
---
 trafgen_l3.c| 15 ---
 trafgen_proto.c |  5 +
 trafgen_proto.h |  2 ++
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/trafgen_l3.c b/trafgen_l3.c
index 70aefb9..7c8a786 100644
--- a/trafgen_l3.c
+++ b/trafgen_l3.c
@@ -3,9 +3,10 @@
  * Subject to the GPL, version 2.
  */
 
-#include 
+#include 
 
 #include "die.h"
+#include "dev.h"
 #include "csum.h"
 #include "built_in.h"
 #include "trafgen_l2.h"
@@ -34,7 +35,11 @@ static struct proto_field ipv4_fields[] = {
 
 static void ipv4_header_init(struct proto_hdr *hdr)
 {
-   proto_lower_default_add(hdr, PROTO_ETH);
+   const char *dev = proto_dev_get();
+
+   /* In case of tun interface we do not need to create Ethernet header */
+   if (dev && device_mtu(dev) && device_type(dev) != ARPHRD_NONE)
+   proto_lower_default_add(hdr, PROTO_ETH);
 
proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_fields));
 
@@ -135,7 +140,11 @@ static struct proto_field ipv6_fields[] = {
 
 static void ipv6_header_init(struct proto_hdr *hdr)
 {
-   proto_lower_default_add(hdr, PROTO_ETH);
+   const char *dev = proto_dev_get();
+
+   /* In case of tun interface we do not need to create Ethernet header */
+   if (dev && device_mtu(dev) && device_type(dev) != ARPHRD_NONE)
+   proto_lower_default_add(hdr, PROTO_ETH);
 
proto_header_fields_add(hdr, ipv6_fields, array_size(ipv6_fields));
 
diff --git a/trafgen_proto.c b/trafgen_proto.c
index 88e0846..5fd9e1c 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -609,3 +609,8 @@ void proto_field_dyn_apply(struct proto_field *field)
if (field->hdr->ops->field_changed)
field->hdr->ops->field_changed(field);
 }
+
+const char *proto_dev_get(void)
+{
+   return ctx.dev;
+}
diff --git a/trafgen_proto.h b/trafgen_proto.h
index 29d68db..d863287 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -159,4 +159,6 @@ extern void proto_field_set_be32(struct proto_field *field, 
uint32_t val);
 extern void proto_field_func_add(struct proto_field *field,
 struct proto_field_func *func);
 
+extern const char *proto_dev_get(void);
+
 #endif /* TRAFGEN_PROTO_H */
-- 
2.11.0

-- 
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.


[netsniff-ng] Re: [PATCH v2] trafgen: l3: Make possible to send frames via tun device

2017-02-06 Thread Tobias Klauser
On 2017-02-06 at 17:23:11 +0100, Vadim Kochan  wrote:
> tun interface does not have Ethernet header so lets push Ethernet
> header only if device supports this.
> 
> Signed-off-by: Vadim Kochan 

Applied, thank you.

-- 
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.


[netsniff-ng] Re: [PATCH] geoip: Fix memory leak when using GeoIPRecord

2017-02-06 Thread Tobias Klauser
On 2017-02-04 at 10:56:14 +0100, Vadim Kochan  wrote:
> GeoIP_record_by_ipnum{,_v6} returns allocated pointer to
> GeoIPRecord with allocated city, region & postal_code which is
> not freed after the call.
> 
> Fixed by xstrdup-ing required GeoIPRecord member (city/region) and
> after calling GeoIPRecord_delete to free the geoip record.
> 
> Ofcourse it is needed to also free obtained city/region in netsniff-ng,
> astraceroute & flowtop tools.
> 
> Fixes: #169
> Signed-off-by: Vadim Kochan 

Applied, thanks.

-- 
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.