Browse Source

Call pcap_set_immediate_mode if available

pull/115/head
emanuele-f 5 years ago
parent
commit
252ee3e82f
  1. 7
      CMakeLists.txt
  2. 6
      configure.seed
  3. 42
      tools/n2n_decode.c

7
CMakeLists.txt

@ -1,5 +1,6 @@
project(n2n) project(n2n)
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
include(CheckFunctionExists)
# N2n information # N2n information
set(N2N_VERSION 2.5.1) set(N2N_VERSION 2.5.1)
@ -98,6 +99,12 @@ if(PCAP_LIB)
add_executable(n2n-decode tools/n2n_decode.c) add_executable(n2n-decode tools/n2n_decode.c)
target_link_libraries(n2n-decode n2n pcap) target_link_libraries(n2n-decode n2n pcap)
install(TARGETS n2n-decode RUNTIME DESTINATION bin) install(TARGETS n2n-decode RUNTIME DESTINATION bin)
set(CMAKE_REQUIRED_LIBRARIES ${PCAP_LIB})
check_function_exists(pcap_set_immediate_mode HAVE_PCAP_IMMEDIATE_MODE)
IF(HAVE_PCAP_IMMEDIATE_MODE)
ADD_DEFINITIONS("-DHAVE_PCAP_IMMEDIATE_MODE")
ENDIF()
endif() endif()
install(TARGETS n2n-benchmark RUNTIME DESTINATION bin) install(TARGETS n2n-benchmark RUNTIME DESTINATION bin)

6
configure.seed

@ -31,6 +31,12 @@ if test x$pcap != x; then
ADDITIONAL_TOOLS="$ADDITIONAL_TOOLS n2n-decode" ADDITIONAL_TOOLS="$ADDITIONAL_TOOLS n2n-decode"
fi fi
AC_CHECK_LIB([pcap], [pcap_set_immediate_mode], pcap_immediate_mode=true)
if test x$pcap_immediate_mode != x; then
AC_DEFINE([HAVE_PCAP_IMMEDIATE_MODE], [], [Have pcap_immediate_mode])
fi
MACHINE=`uname -m` MACHINE=`uname -m`
SYSTEM=`uname -s` SYSTEM=`uname -s`

42
tools/n2n_decode.c

@ -20,11 +20,13 @@
#include "n2n.h" #include "n2n.h"
#define SNAPLEN 1500 #define SNAPLEN 1500
#define TIMEOUT 200
/* *************************************************** */ /* *************************************************** */
static int aes_mode = 0; static int aes_mode = 0;
static int running = 1; static int running = 1;
static char *ifname = NULL;
static n2n_edge_conf_t conf; static n2n_edge_conf_t conf;
static n2n_trans_op_t transop; static n2n_trans_op_t transop;
static pcap_t *handle; static pcap_t *handle;
@ -146,17 +148,22 @@ static int run_packet_loop() {
struct pcap_pkthdr header; struct pcap_pkthdr header;
const u_char *packet; const u_char *packet;
traceEvent(TRACE_NORMAL, "Running loop"); traceEvent(TRACE_NORMAL, "Capturing packets on %s...", ifname);
// TODO handle timeout
while(running) { while(running) {
n2n_common_t common = {0}; n2n_common_t common;
n2n_PACKET_t pkt = {0}; n2n_PACKET_t pkt;
uint ipsize, common_offset; uint ipsize, common_offset;
size_t idx, rem; size_t idx, rem;
memset(&common, 0, sizeof(common));
memset(&pkt, 0, sizeof(pkt));
packet = pcap_next(handle, &header); packet = pcap_next(handle, &header);
if(!packet)
continue;
if(header.caplen < MIN_LEN) { if(header.caplen < MIN_LEN) {
traceEvent(TRACE_INFO, "Skipping packet too small: size=%d", header.caplen); traceEvent(TRACE_INFO, "Skipping packet too small: size=%d", header.caplen);
continue; continue;
@ -219,7 +226,7 @@ static int run_packet_loop() {
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
u_char c; u_char c;
struct bpf_program fcode; struct bpf_program fcode;
char *bpf_filter = NULL, *ifname = NULL, *out_fname = NULL; char *bpf_filter = NULL, *out_fname = NULL;
char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE];
int rv = 0; int rv = 0;
FILE *outf = stdout; FILE *outf = stdout;
@ -240,7 +247,7 @@ int main(int argc, char* argv[]) {
switch(c) { switch(c) {
case 'c': case 'c':
strncpy((char*)conf.community_name, optarg, sizeof(conf.community_name)); strncpy((char*)conf.community_name, optarg, sizeof(conf.community_name)-1);
break; break;
case 'i': case 'i':
ifname = strdup(optarg); ifname = strdup(optarg);
@ -278,13 +285,30 @@ int main(int argc, char* argv[]) {
#endif #endif
n2n_transop_twofish_init(&conf, &transop); n2n_transop_twofish_init(&conf, &transop);
handle = pcap_open_live(ifname, SNAPLEN, 1, 1000, errbuf); if((handle = pcap_create(ifname, errbuf)) == NULL) {
if(handle == NULL) {
traceEvent(TRACE_ERROR, "Cannot open device %s: %s", ifname, errbuf); traceEvent(TRACE_ERROR, "Cannot open device %s: %s", ifname, errbuf);
return(1); return(1);
} }
if((pcap_set_timeout(handle, TIMEOUT) != 0) ||
(pcap_set_snaplen(handle, SNAPLEN) != 0)) {
traceEvent(TRACE_ERROR, "Error while setting timeout/snaplen");
return(1);
}
#ifdef HAVE_PCAP_IMMEDIATE_MODE
/* The timeout is not honored unless immediate mode is set.
* See https://github.com/mfontanini/libtins/issues/180 */
if(pcap_set_immediate_mode(handle, 1) != 0) {
traceEvent(TRACE_ERROR, "Could not set PCAP immediate mode");
return(1);
}
#endif
if(pcap_activate(handle) != 0) {
traceEvent(TRACE_ERROR, "pcap_activate failed: %s", pcap_geterr(handle));
}
if(pcap_datalink(handle) != DLT_EN10MB) { if(pcap_datalink(handle) != DLT_EN10MB) {
traceEvent(TRACE_ERROR, "Device %s doesn't provide Ethernet headers - not supported", ifname); traceEvent(TRACE_ERROR, "Device %s doesn't provide Ethernet headers - not supported", ifname);
return(2); return(2);

Loading…
Cancel
Save