EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
Macros | Functions
pcap_service.c File Reference

File containing the implementation of the pcap service utilities. More...

#include <pcap.h>
#include "pcap_service.h"
#include "../utils/allocs.h"
#include "../utils/log.h"
#include "../utils/net.h"
#include "../utils/os.h"
Include dependency graph for pcap_service.c:

Macros

#define PCAP_SNAPSHOT_LENGTH   65535
 
#define PCAP_BUFFER_SIZE   64 * 1024
 

Functions

int find_device (char *ifname, bpf_u_int32 *net, bpf_u_int32 *mask)
 
void receive_pcap_packet (u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
 
int capture_pcap_packet (struct pcap_context *ctx)
 Captures a pcap packet. More...
 
void close_pcap (struct pcap_context *ctx)
 Closes the pcap service. More...
 
int capture_pcap_start (struct pcap_context *ctx)
 Starts the blocking pcap loop. More...
 
void capture_pcap_stop (struct pcap_context *ctx)
 Stops the blocking pcap loop. More...
 
int get_pcap_datalink (struct pcap_context *ctx)
 Get the pcap config datalink value. More...
 
int run_pcap (char *interface, bool immediate, bool promiscuous, int timeout, char *filter, bool nonblock, capture_callback_fn pcap_fn, void *fn_ctx, struct pcap_context **pctx)
 Executes the libpcap service. More...
 
int dump_file_pcap (struct pcap_context *ctx, char *file_path, struct pcap_pkthdr *header, uint8_t *packet)
 Saves a packet packet into file. More...
 
int get_pcap_stats (const struct pcap_context *ctx, struct pcap_stat *ps)
 Return pcap capture statistics. More...
 
int inject_pcap (struct pcap_context *ctx, uint8_t *packet, size_t size)
 Injects a packets. More...
 
void free_pcap_list (UT_array *ctx_list)
 Frees a pcap list. More...
 
UT_array * create_pcap_list (void)
 Creates a pcap list. More...
 

Detailed Description

File containing the implementation of the pcap service utilities.

Author
Alexandru Mereacre
Date
2021

Macro Definition Documentation

◆ PCAP_BUFFER_SIZE

#define PCAP_BUFFER_SIZE   64 * 1024

◆ PCAP_SNAPSHOT_LENGTH

#define PCAP_SNAPSHOT_LENGTH   65535

Function Documentation

◆ capture_pcap_packet()

int capture_pcap_packet ( struct pcap_context ctx)

Captures a pcap packet.

Parameters
ctxThe pcap context structure
Returns
int 0 on success, -1 otherwise

◆ capture_pcap_start()

int capture_pcap_start ( struct pcap_context ctx)

Starts the blocking pcap loop.

Parameters
ctxThe pcap context
Returns
int 0 on success, -1 on error, -2 if the loop terminated

◆ capture_pcap_stop()

void capture_pcap_stop ( struct pcap_context ctx)

Stops the blocking pcap loop.

Parameters
ctxThe pcap context

◆ close_pcap()

void close_pcap ( struct pcap_context ctx)

Closes the pcap service.

Parameters
ctxThe pcap context

◆ create_pcap_list()

UT_array* create_pcap_list ( void  )

Creates a pcap list.

Returns
UT_array* The pcap list, NULL on failure

◆ dump_file_pcap()

int dump_file_pcap ( struct pcap_context ctx,
char *  file_path,
struct pcap_pkthdr *  header,
uint8_t *  packet 
)

Saves a packet packet into file.

Parameters
ctxThe pcap context
file_pathThe file path to save the packet
headerThe packet header
packetThe packet data
Returns
int 0 on success, -1 on failure

◆ find_device()

int find_device ( char *  ifname,
bpf_u_int32 *  net,
bpf_u_int32 *  mask 
)

◆ free_pcap_list()

void free_pcap_list ( UT_array *  ctx_list)

Frees a pcap list.

Parameters
ctx_listThe pcap list

◆ get_pcap_datalink()

int get_pcap_datalink ( struct pcap_context ctx)

Get the pcap config datalink value.

Parameters
ctxThe pcap context
Returns
int the config value

◆ get_pcap_stats()

int get_pcap_stats ( const struct pcap_context ctx,
struct pcap_stat *  ps 
)

Return pcap capture statistics.

Parameters
ctx[in]The pcap context
ps[out]The pcap_stat structure
Returns
0 on success, -1 on failure

◆ inject_pcap()

int inject_pcap ( struct pcap_context ctx,
uint8_t *  packet,
size_t  size 
)

Injects a packets.

Parameters
ctxThe pcap context
packetThe packet data
sizeThe packet size
Returns
int number of bytes injected on success, -1 on failure

◆ receive_pcap_packet()

void receive_pcap_packet ( u_char *  args,
const struct pcap_pkthdr *  header,
const u_char *  packet 
)

◆ run_pcap()

int run_pcap ( char *  interface,
bool  immediate,
bool  promiscuous,
int  timeout,
char *  filter,
bool  nonblock,
capture_callback_fn  pcap_fn,
void *  fn_ctx,
struct pcap_context **  pctx 
)

Executes the libpcap service.

Parameters
interfaceThe capture interface
immediateThe immediate mode flag
promiscuousThe promiscuous mode flag
timeoutThe timeout (in milliseconds)
filterThe capture filter string
nonblockSets the capture to nonblocking mode
pcap_fnThe pcap capture callback
fn_ctxThe context for callback function
pctxThe returned pcap context
Returns
0 on success, -1 on failure