Flowgrind
Advanced TCP traffic generator
fg_pcap.h File Reference

Packet capture support for the Flowgrind daemon. More...

#include "config.h"
#include "daemon.h"

Go to the source code of this file.

Functions

void fg_pcap_go (struct flow *flow)
 Start a tcpdump to capture traffic of the provided flow. More...
 
int fg_pcap_init (void)
 Initialize flowgrind's pcap library. More...
 

Detailed Description

Packet capture support for the Flowgrind daemon.

Definition in file fg_pcap.h.

Function Documentation

◆ fg_pcap_go()

void fg_pcap_go ( struct flow flow)

Start a tcpdump to capture traffic of the provided flow.

If the flow was not configured for tcp dumping or dumping is already in progress the method will do nothing and return immediately. Otherwise the method blocks until the actual capturing starts. In case an error occurs a log message is created.

Parameters
[in]flowthe flow whose traffic should be captured

Definition at line 314 of file fg_pcap.c.

315 {
316  if (!flow->settings.traffic_dump)
317  return;
318 
319  if (dumping) {
320  logging(LOG_WARNING, "pcap: dumping already in progress on "
321  "this host");
322  return;
323  }
324 
325  DEBUG_MSG(LOG_DEBUG, "called fg_pcap_go() for flow %d", flow->id);
326  dumping = true;
327 
328  int rc = pthread_create(&flow->pcap_thread, NULL, fg_pcap_work,
329  (void*)flow);
330 
331  /* barrier: dump thread is ready (or aborted) */
333 
334  if (rc)
335  logging(LOG_WARNING, "could not start pcap thread: %s",
336  strerror(rc) );
337 }

◆ fg_pcap_init()

int fg_pcap_init ( void  )

Initialize flowgrind's pcap library.

This method fills internal structures on which other methods of this library depend. It is therefore crucial to call it before any call to other methods of this library.

Returns
return 0 for success, or -1 for failure

Definition at line 84 of file fg_pcap.c.

85 {
86  /* initalize *alldevs for later use */
87  if (pcap_findalldevs(&alldevs, errbuf) == -1) {
88  logging(LOG_WARNING,"error in pcap_findalldevs: %s\n", errbuf);
89  return -1;
90  }
91 
92 #ifdef DEBUG
93  for (pcap_if_t *d = alldevs; d; d = d->next) {
94  char *devdes = NULL;
95  if (asprintf(&devdes, "%s: ", d->name) == -1)
96  return -1;
97 
98  for (pcap_addr_t *a = d->addresses; a; a = a->next) {
99  if (!a->addr)
100  continue;
101 
102  asprintf_append(&devdes, "a=%s",
103  fg_nameinfo(a->addr,
104  sizeof(struct sockaddr)));
105  if (a->next)
106  asprintf_append(&devdes, ", ");
107  }
108  DEBUG_MSG(LOG_ERR, "pcap: found pcapable device (%s)", devdes);
109  free(devdes);
110  }
111 #endif /* DEBUG*/
112 
114  return 0;
115 }
DEBUG_MSG
#define DEBUG_MSG(LVL, MSG,...)
Print debug message to standard error.
Definition: debug.h:49
pthread_barrier_wait
int pthread_barrier_wait(pthread_barrier_t *barrier)
Synchronizes participating threads at the barrier referenced by barrier.
Definition: fg_barrier.c:80
alldevs
static pcap_if_t * alldevs
Definition: fg_pcap.c:79
fg_pcap_work
static void * fg_pcap_work(void *arg)
Worker method performing actual packet capturing for the provided flow.
Definition: fg_pcap.c:155
flow
Definition: daemon.h:73
pthread_barrier_init
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count)
Allocates resources required to use the barrier referenced by barrier.
Definition: fg_barrier.c:37
pcap_barrier
static pthread_barrier_t pcap_barrier
Definition: fg_pcap.c:76
logging
void logging(int priority, const char *fmt,...)
Definition: fg_log.c:69
flow::settings
struct flow_settings settings
Definition: daemon.h:83
errbuf
static char errbuf[PCAP_ERRBUF_SIZE]
Definition: fg_pcap.c:73
flow_settings::traffic_dump
int traffic_dump
Dump traffic using libpcap (option -M).
Definition: common.h:204
dumping
static bool dumping
Definition: fg_pcap.c:82
asprintf_append
int asprintf_append(char **strp, const char *fmt,...)
Definition: fg_string.c:98
flow::id
int id
Definition: daemon.h:75
fg_nameinfo
const char * fg_nameinfo(const struct sockaddr *sa, socklen_t salen)
Definition: fg_socket.c:374
flow::pcap_thread
pthread_t pcap_thread
Definition: daemon.h:161