diff --git a/Makefile b/Makefile index 9c94cfe..1094c09 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ ifneq (, $(findstring linux, $(SYS))) ifneq (, $(findstring musl, $(SYS))) LIBS = else -LIBS = -lm -lrt -ldl -lpthread +LIBS = -lm -lrt -ldl -lpthread -lpcap endif INCLUDES = FLAGS2 = @@ -91,7 +91,7 @@ endif DEFINES = -CFLAGS = -g -ggdb $(FLAGS2) $(INCLUDES) $(DEFINES) -Wall -O2 +CFLAGS = -g -ggdb $(FLAGS2) $(INCLUDES) $(DEFINES) -Wall -O2 -DSTATICPCAP -static -L../libpcap .SUFFIXES: .c .cpp all: bin/masscan diff --git a/src/main.c b/src/main.c index 3a9e789..cd8e75a 100644 --- a/src/main.c +++ b/src/main.c @@ -40,7 +40,7 @@ #include "output.h" /* for outputting results */ #include "rte-ring.h" /* producer/consumer ring buffer */ #include "rawsock-pcapfile.h" /* for saving pcap files w/ raw packets */ -#include "stub-pcap.h" /* dynamically load libpcap library */ +#include "pcap/pcap.h" /* dynamically load libpcap library */ #include "smack.h" /* Aho-corasick state-machine pattern-matcher */ #include "pixie-timer.h" /* portable time functions */ #include "pixie-threads.h" /* portable threads */ @@ -1648,8 +1648,6 @@ int main(int argc, char *argv[]) /* We need to do a separate "raw socket" initialization step. This is * for Windows and PF_RING. */ - if (pcap_init() != 0) - LOG(2, "libpcap: failed to load\n"); rawsock_init(); /* Init some protocol parser data structures */ diff --git a/src/rawsock.c b/src/rawsock.c index 158271a..ae74546 100755 --- a/src/rawsock.c +++ b/src/rawsock.c @@ -9,7 +9,7 @@ #include "logger.h" #include "main-ptrace.h" #include "string_s.h" -#include "stub-pcap.h" +#include "pcap/pcap.h" #include "stub-pfring.h" #include "pixie-timer.h" #include "main-globals.h" @@ -202,7 +202,7 @@ rawsock_list_adapters(void) pcap_if_t *alldevs; char errbuf[PCAP_ERRBUF_SIZE]; - if (PCAP.findalldevs(&alldevs, errbuf) != -1) { + if (pcap_findalldevs(&alldevs, errbuf) != -1) { int i; const pcap_if_t *d; i=0; @@ -211,15 +211,15 @@ rawsock_list_adapters(void) fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } /* Print the list */ - for(d=alldevs; d; d=PCAP.dev_next(d)) { - fprintf(stderr, " %d %s \t", i++, PCAP.dev_name(d)); - if (PCAP.dev_description(d)) - fprintf(stderr, "(%s)\n", PCAP.dev_description(d)); + for(d=alldevs; d; d=d->next) { + fprintf(stderr, " %d %s \t", i++, d->name); + if (d->description) + fprintf(stderr, "(%s)\n", d->description); else fprintf(stderr, "(No description available)\n"); } fprintf(stderr,"\n"); - PCAP.freealldevs(alldevs); + pcap_freealldevs(alldevs); } else { fprintf(stderr, "%s\n", errbuf); } @@ -234,7 +234,7 @@ adapter_from_index(unsigned index) char errbuf[PCAP_ERRBUF_SIZE]; int x; - x = PCAP.findalldevs(&alldevs, errbuf); + x = pcap_findalldevs(&alldevs, errbuf); if (x != -1) { const pcap_if_t *d; @@ -242,9 +242,9 @@ adapter_from_index(unsigned index) fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } /* Print the list */ - for(d=alldevs; d; d=PCAP.dev_next(d)) { + for(d=alldevs; d; d=d->next) { if (index-- == 0) - return PCAP.dev_name(d); + return d->name; } return 0; } else { @@ -262,12 +262,8 @@ void rawsock_flush(struct Adapter *adapter) { if (adapter->sendq) { - PCAP.sendqueue_transmit(adapter->pcap, adapter->sendq, 0); - /* Dude, I totally forget why this step is necessary. I vaguely * remember there's a good reason for it though */ - PCAP.sendqueue_destroy(adapter->sendq); - adapter->sendq = PCAP.sendqueue_alloc(SENDQ_SIZE); } } @@ -312,27 +308,11 @@ rawsock_send_packet( /* WINDOWS PCAP */ if (adapter->sendq) { - int err; - struct pcap_pkthdr hdr; - hdr.len = length; - hdr.caplen = length; - - err = PCAP.sendqueue_queue(adapter->sendq, &hdr, packet); - if (err) { - rawsock_flush(adapter); - PCAP.sendqueue_queue(adapter->sendq, &hdr, packet); - } - - if (flush) { - rawsock_flush(adapter); - } - - return 0; } /* LIBPCAP */ if (adapter->pcap) - return PCAP.sendpacket(adapter->pcap, packet, length); + return pcap_sendpacket(adapter->pcap, packet, length); return 0; } @@ -376,7 +356,7 @@ int rawsock_recv_packet( } else if (adapter->pcap) { struct pcap_pkthdr hdr; - *packet = PCAP.next(adapter->pcap, &hdr); + *packet = pcap_next(adapter->pcap, &hdr); if (*packet == NULL) { if (is_pcap_file) { @@ -516,9 +496,9 @@ rawsock_ignore_transmits(struct Adapter *adapter, const char *ifname) if (adapter->pcap) { int err; - err = PCAP.setdirection(adapter->pcap, PCAP_D_IN); + err = pcap_setdirection(adapter->pcap, PCAP_D_IN); if (err) { - ; //PCAP.perror(adapter->pcap, "if: pcap_setdirection(IN)"); + ; //pcap_perror(adapter->pcap, "if: pcap_setdirection(IN)"); } else { LOG(2, "if:%s: not receiving transmits\n", ifname); } @@ -534,10 +514,9 @@ rawsock_close_adapter(struct Adapter *adapter) PFRING.close(adapter->ring); } if (adapter->pcap) { - PCAP.close(adapter->pcap); + pcap_close(adapter->pcap); } if (adapter->sendq) { - PCAP.sendqueue_destroy(adapter->sendq); } free(adapter); @@ -714,8 +693,8 @@ rawsock_init_adapter(const char *adapter_name, LOG(1, "pcap: file: %s\n", adapter_name+5); is_pcap_file = 1; - adapter->pcap = PCAP.open_offline(adapter_name+5, errbuf); - adapter->link_type = PCAP.datalink(adapter->pcap); + adapter->pcap = pcap_open_offline(adapter_name+5, errbuf); + adapter->link_type = pcap_datalink(adapter->pcap); } /*---------------------------------------------------------------- * PORTABILITY: LIBPCAP @@ -724,14 +703,14 @@ rawsock_init_adapter(const char *adapter_name, *----------------------------------------------------------------*/ { int err; - LOG(1, "[+] if(%s): pcap: %s\n", adapter_name, PCAP.lib_version()); + LOG(1, "[+] if(%s): pcap: %s\n", adapter_name, pcap_lib_version()); LOG(2, "[+] if(%s): opening...\n", adapter_name); /* This reserves resources, but doesn't actually open the * adapter until we call pcap_activate */ - adapter->pcap = PCAP.create(adapter_name, errbuf); + adapter->pcap = pcap_create(adapter_name, errbuf); if (adapter->pcap == NULL) { - adapter->pcap = PCAP.open_live( + adapter->pcap = pcap_open_live( adapter_name, /* interface name */ 65536, /* max packet size */ 8, /* promiscuous mode */ @@ -746,33 +725,33 @@ rawsock_init_adapter(const char *adapter_name, return 0; } } else { - err = PCAP.set_snaplen(adapter->pcap, 65536); + err = pcap_set_snaplen(adapter->pcap, 65536); if (err) { - PCAP.perror(adapter->pcap, "if: set_snaplen"); + pcap_perror(adapter->pcap, "if: set_snaplen"); goto pcap_error; } - err = PCAP.set_promisc(adapter->pcap, 8); + err = pcap_set_promisc(adapter->pcap, 8); if (err) { - PCAP.perror(adapter->pcap, "if: set_promisc"); + pcap_perror(adapter->pcap, "if: set_promisc"); goto pcap_error; } - err = PCAP.set_timeout(adapter->pcap, 1000); + err = pcap_set_timeout(adapter->pcap, 1000); if (err) { - PCAP.perror(adapter->pcap, "if: set_timeout"); + pcap_perror(adapter->pcap, "if: set_timeout"); goto pcap_error; } - err = PCAP.set_immediate_mode(adapter->pcap, 1); + err = pcap_set_immediate_mode(adapter->pcap, 1); if (err) { - PCAP.perror(adapter->pcap, "if: set_immediate_mode"); + pcap_perror(adapter->pcap, "if: set_immediate_mode"); goto pcap_error; } /* If errors happen, they aren't likely to happen above, but will * happen where when they are applied */ - err = PCAP.activate(adapter->pcap); + err = pcap_activate(adapter->pcap); switch (err) { case 0: /* drop down below */ @@ -782,7 +761,7 @@ rawsock_init_adapter(const char *adapter_name, LOG(0, " [hint] need to sudo or run as root or something\n"); goto pcap_error; default: - LOG(0, "[-] if(%s): activate:%d: %s\n", adapter_name, err, PCAP.geterr(adapter->pcap)); + LOG(0, "[-] if(%s): activate:%d: %s\n", adapter_name, err, pcap_geterr(adapter->pcap)); if (err < 0) goto pcap_error; } @@ -793,10 +772,10 @@ rawsock_init_adapter(const char *adapter_name, /* Figure out the link-type. We suport Ethernet and IP */ - adapter->link_type = PCAP.datalink(adapter->pcap); + adapter->link_type = pcap_datalink(adapter->pcap); switch (adapter->link_type) { case -1: - PCAP.perror(adapter->pcap, "if: datalink"); + pcap_perror(adapter->pcap, "if: datalink"); goto pcap_error; case 0: /* Null/Loopback [VPN tunnel] */ LOG(1, "[+] if(%s): VPN tunnel interface found\n", adapter_name); @@ -808,7 +787,7 @@ rawsock_init_adapter(const char *adapter_name, LOG(0, "[-] if(%s): unknown data link type: %u(%s)\n", adapter_name, adapter->link_type, - PCAP.datalink_val_to_name(adapter->link_type)); + pcap_datalink_val_to_name(adapter->link_type)); break; } @@ -824,14 +803,14 @@ rawsock_init_adapter(const char *adapter_name, adapter->sendq = 0; #if defined(WIN32) if (is_sendq) - adapter->sendq = PCAP.sendqueue_alloc(SENDQ_SIZE); + adapter->sendq = pcap_sendqueue_alloc(SENDQ_SIZE); #endif return adapter; pcap_error: if (adapter->pcap) { - PCAP.close(adapter->pcap); + pcap_close(adapter->pcap); adapter->pcap = NULL; } if (adapter->pcap == NULL) { diff --git a/src/stub-pcap.c b/src/stub-pcap.c deleted file mode 100644 index d93554f..0000000 --- a/src/stub-pcap.c +++ /dev/null @@ -1,457 +0,0 @@ -/* Copyright (c) 2007 by Errata Security, All Rights Reserved - * Copyright (c) 2017 by Robert David Graham - * Programmer(s): Robert David Graham [rdg] - */ -/* - LIBPCAP INTERFACE - - This VERY MESSY code is a hack to load the 'libpcap' library - at runtime rather than compile time. - - This reason for this mess is that it gets rid of a dependency - when compiling this project. Otherwise, developers would have - to download the 'libpcap-dev' dependency in order to build - this project. - - Almost every platform these days (OpenBSD, FreeBSD, macOS, - Debian, RedHat) comes with a "libpcap.so" library already - installed by default with a known BINARY interface. Thus, - we can include the data structures definitions directly - in this project, then load the library dynamically. - - For those systems without libpcap.so already installed, the - user can either install those on the system, or compile - this project in "STATIC" mode, which will link to the - static libpcap.a library. - -*/ -#include "logger.h" - -#if defined(_MSC_VER) -#pragma warning(disable:4115 4201) -#pragma warning(disable:4100) /* unreferenced formal parameter */ -//#include -#endif - -#include "stub-pcap.h" - - -#ifdef WIN32 -#define WIN32_LEAN_AND_MEAN -#include -#else -#include -#endif - -#include -#include -#include - -#ifndef UNUSEDPARM -#ifdef __GNUC__ -#define UNUSEDPARM(x) -#else -#define UNUSEDPARM(x) x=(x) -#endif -#endif - -struct pcap_if { - struct pcap_if *next; - char *name; /* name to hand to "pcap_open_live()" */ - char *description; /* textual description of interface, or NULL */ - void *addresses; - unsigned flags; /* PCAP_IF_ interface flags */ -}; - -static void seterr(char *errbuf, const char *msg) -{ - size_t length = strlen(msg); - - if (length > PCAP_ERRBUF_SIZE-1) - length = PCAP_ERRBUF_SIZE-1; - memcpy(errbuf, msg, length); - errbuf[length] = '\0'; -} - -static void null_PCAP_CLOSE(void *hPcap) -{ -#ifdef STATICPCAP - pcap_close(hPcap); - return; -#endif - UNUSEDPARM(hPcap); -} - -#ifdef STATICPCAP -static pcap_t *(*null_PCAP_CREATE)(const char *source, char *errbuf); -static int (*null_PCAP_SET_SNAPLEN)(pcap_t *p, int snaplen); -static int (*null_PCAP_SET_PROMISC)(pcap_t *p, int promisc); -static int (*null_PCAP_SET_TIMEOUT)(pcap_t *p, int to_ms); -static int (*null_PCAP_SET_IMMEDIATE_MODE)(pcap_t *p, int immediate_mode); -static int (*null_PCAP_SET_BUFFER_SIZE)(pcap_t *p, int buffer_size); -static int (*null_PCAP_SET_RFMON)(pcap_t *p, int rfmon); -static int (*null_PCAP_CAN_SET_RFMON)(pcap_t *p); -static int (*null_PCAP_ACTIVATE)(pcap_t *p); -#else -static pcap_t *null_PCAP_CREATE(const char *source, char *errbuf) {return 0;} -static int null_PCAP_SET_SNAPLEN(pcap_t *p, int snaplen) {return 0;} -static int null_PCAP_SET_PROMISC(pcap_t *p, int promisc) {return 0;} -static int null_PCAP_SET_TIMEOUT(pcap_t *p, int to_ms) {return 0;} -static int null_PCAP_SET_IMMEDIATE_MODE(pcap_t *p, int immediate_mode) {return 0;} -static int null_PCAP_SET_BUFFER_SIZE(pcap_t *p, int buffer_size) {return 0;} -static int null_PCAP_SET_RFMON(pcap_t *p, int rfmon) {return 0;} -static int null_PCAP_CAN_SET_RFMON(pcap_t *p) {return 0;} -static int null_PCAP_ACTIVATE(pcap_t *p) {return 0;} -#endif - -static unsigned null_PCAP_DATALINK(void *hPcap) -{ -#ifdef STATICPCAP - return pcap_datalink(hPcap); -#endif - UNUSEDPARM(hPcap); - return 0; -} - - -static unsigned null_PCAP_DISPATCH(void *hPcap, unsigned how_many_packets, PCAP_HANDLE_PACKET handler, void *handle_data) -{ -#ifdef STATICPCAP - return pcap_dispatch(hPcap, how_many_packets, handler, handle_data); -#endif - UNUSEDPARM(hPcap);UNUSEDPARM(how_many_packets);UNUSEDPARM(handler);UNUSEDPARM(handle_data); - return 0; -} - - -static int null_PCAP_FINDALLDEVS(pcap_if_t **alldevs, char *errbuf) -{ -#ifdef STATICPCAP - return pcap_findalldevs(alldevs, errbuf); -#endif - *alldevs = 0; - seterr(errbuf, "libpcap not loaded"); - return -1; -} - - -static void null_PCAP_FREEALLDEVS(pcap_if_t *alldevs) -{ -#ifdef STATICPCAP - return pcap_freealldevs(alldevs); -#endif - UNUSEDPARM(alldevs); - return; -} - - -static char *null_PCAP_LOOKUPDEV(char *errbuf) -{ -#ifdef STATICPCAP - return pcap_lookupdev(errbuf); -#endif - seterr(errbuf, "libpcap not loaded"); - return ""; -} - - -static void * null_PCAP_OPEN_LIVE(const char *devicename, unsigned snap_length, unsigned is_promiscuous, unsigned read_timeout, char *errbuf) -{ -#ifdef STATICPCAP - return pcap_open_live(devicename, snap_length, is_promiscuous, read_timeout, errbuf); -#endif - seterr(errbuf, "libpcap not loaded"); - UNUSEDPARM(devicename);UNUSEDPARM(snap_length);UNUSEDPARM(is_promiscuous);UNUSEDPARM(read_timeout); - return NULL; -} - -static int null_PCAP_MAJOR_VERSION(void *p) -{ -#ifdef STATICPCAP - return pcap_major_version(p); -#endif - UNUSEDPARM(p); - return 0; -} - - -static int null_PCAP_MINOR_VERSION(void *p) -{ -#ifdef STATICPCAP - return pcap_minor_version(p); -#endif - UNUSEDPARM(p); - return 0; -} - -static const char *null_PCAP_LIB_VERSION(void) -{ -#ifdef STATICPCAP - return pcap_lib_version(); -#endif - - return "stub/0.0"; -} - - - - - -struct PcapFunctions PCAP = { - 0,0,0,0,0, - null_PCAP_CLOSE, -}; - - -static void *my_null(int x, ...) -{ - UNUSEDPARM(x); - printf("%.*s", 0, "a"); /* Remove warnings about no effects */ - return 0; -} -static pcap_t *null_PCAP_OPEN_OFFLINE(const char *fname, char *errbuf) -{ -#ifdef STATICPCAP - return pcap_open_offline(fname, errbuf); -#endif - return my_null(2, fname, errbuf); -} -static int null_PCAP_SENDPACKET(pcap_t *p, const unsigned char *buf, int size) -{ -#ifdef STATICPCAP - return pcap_sendpacket(p, buf, size); -#endif - my_null(3, p, buf, size); - return 0; -} - -static const unsigned char *null_PCAP_NEXT(pcap_t *p, struct pcap_pkthdr *h) -{ -#ifdef STATICPCAP - return pcap_next(p, h); -#endif - my_null(3, p, h); - return 0; -} -static int null_PCAP_SETDIRECTION(pcap_t *p, pcap_direction_t d) -{ -#ifdef STATICPCAP - return pcap_setdirection(p, d); -#endif - my_null(2, p, d); - return 0; -} -static const char *null_PCAP_DATALINK_VAL_TO_NAME(int dlt) -{ -#ifdef STATICPCAP - return pcap_datalink_val_to_name(dlt); -#endif - my_null(1, dlt); - return 0; -} -static void null_PCAP_PERROR(pcap_t *p, char *prefix) -{ -#ifdef STATICPCAP - pcap_perror(p, prefix); - return; -#endif - UNUSEDPARM(p); - fprintf(stderr, "%s\n", prefix); - perror("pcap"); -} -static const char*null_PCAP_GETERR(pcap_t *p) -{ -#ifdef STATICPCAP - return pcap_geterr(p); -#endif - UNUSEDPARM(p); - return "(unknown)"; -} -static const char *null_PCAP_DEV_NAME(const pcap_if_t *dev) -{ - return dev->name; -} -static const char *null_PCAP_DEV_DESCRIPTION(const pcap_if_t *dev) -{ - return dev->description; -} -static const pcap_if_t *null_PCAP_DEV_NEXT(const pcap_if_t *dev) -{ - return dev->next; -} - -/* - * Some Windows-specific functions to improve speed - */ -#if defined(WIN32) -static pcap_send_queue *null_PCAP_SENDQUEUE_ALLOC(size_t size) -{ - UNUSEDPARM(size); - return 0; -} -static unsigned null_PCAP_SENDQUEUE_TRANSMIT(pcap_t *p, pcap_send_queue *queue, int sync) -{ - my_null(3, p, queue, sync); - return 0; -} -static void null_PCAP_SENDQUEUE_DESTROY(pcap_send_queue *queue) -{ - my_null(1, queue); - UNUSEDPARM(queue); -} -static int null_PCAP_SENDQUEUE_QUEUE(pcap_send_queue *queue, - const struct pcap_pkthdr *pkt_header, - const unsigned char *pkt_data) -{ - my_null(4, queue, pkt_header, pkt_data); - return 0; -} -#endif /*WIN32*/ - - -/** - * Runtime-load the libpcap shared-object or the winpcap DLL. We - * load at runtime rather than loadtime to allow this program to - * be used to process offline content, and to provide more helpful - * messages to people who don't realize they need to install PCAP. - */ -int pcap_init(void) -{ - struct PcapFunctions *pl = &PCAP; -#ifdef WIN32 - void * hPacket; - void * hLibpcap; - - pl->is_available = 0; - pl->is_printing_debug = 1; - - /* Look for the Packet.dll */ - hPacket = LoadLibraryA("NPcap\\Packet.dll"); - if (hPacket == NULL) - hPacket = LoadLibraryA("Packet.dll"); - if (hPacket == NULL) { - if (pl->is_printing_debug) - switch (GetLastError()) { - case ERROR_MOD_NOT_FOUND: - fprintf(stderr, "%s: not found\n", "Packet.dll"); - fprintf(stderr, " HINT: you must install either WinPcap or Npcap\n"); - return -1; - default: - fprintf(stderr, "%s: couldn't load %d\n", "Packet.dll", (int)GetLastError()); - return -1; - } - } - - /* Look for the winpcap.dll */ - hLibpcap = LoadLibraryA("Npcap\\wpcap.dll"); - if (hLibpcap == NULL) - hLibpcap = LoadLibraryA("wpcap.dll"); - if (hLibpcap == NULL) { - if (pl->is_printing_debug) - fprintf(stderr, "%s: couldn't load %d\n", "wpcap.dll", (int)GetLastError()); - return -1; - } - - -#define DOLINK(PCAP_DATALINK, datalink) \ -pl->datalink = (PCAP_DATALINK)GetProcAddress(hLibpcap, "pcap_"#datalink); \ -if (pl->datalink == NULL) pl->func_err=1, pl->datalink = null_##PCAP_DATALINK; -#endif - - -#ifndef WIN32 -#ifndef STATICPCAP - void *hLibpcap; - - pl->is_available = 0; - pl->is_printing_debug = 1; - - { - static const char *possible_names[] = { - "libpcap.so", - "libpcap.A.dylib", - "libpcap.dylib", - "libpcap.so.0.9.5", - "libpcap.so.0.9.4", - "libpcap.so.0.8", - 0 - }; - unsigned i; - for (i=0; possible_names[i]; i++) { - hLibpcap = dlopen(possible_names[i], RTLD_LAZY); - if (hLibpcap) { - LOG(1, "[+] pcap: found library: %s\n", possible_names[i]); - break; - } else { - LOG(1, "[-] pcap: failed to load: %s\n", possible_names[i]); - } - } - - if (hLibpcap == NULL) { - LOG(0, "[-] FAIL: failed to load libpcap shared library\n"); - LOG(0, " [hint]: you must install libpcap or WinPcap\n"); - } - } - -#define DOLINK(PCAP_DATALINK, datalink) \ -pl->datalink = (PCAP_DATALINK)dlsym(hLibpcap, "pcap_"#datalink); \ - if (pl->datalink == NULL) LOG(1, "pcap: pcap_%s: failed\n", #datalink); \ - if (pl->datalink == NULL) pl->func_err=1, pl->datalink = null_##PCAP_DATALINK; -#else -#define DOLINK(PCAP_DATALINK, datalink) \ -pl->func_err=0, pl->datalink = null_##PCAP_DATALINK; -#endif -#endif - - DOLINK(PCAP_CLOSE , close); - DOLINK(PCAP_DATALINK , datalink); - DOLINK(PCAP_DISPATCH , dispatch); - DOLINK(PCAP_FINDALLDEVS , findalldevs); - DOLINK(PCAP_FREEALLDEVS , freealldevs); - DOLINK(PCAP_LIB_VERSION , lib_version); - DOLINK(PCAP_LOOKUPDEV , lookupdev); - DOLINK(PCAP_MAJOR_VERSION , major_version); - DOLINK(PCAP_MINOR_VERSION , minor_version); - DOLINK(PCAP_OPEN_LIVE , open_live); - - DOLINK(PCAP_OPEN_OFFLINE , open_offline); - DOLINK(PCAP_SENDPACKET , sendpacket); - DOLINK(PCAP_NEXT , next); - DOLINK(PCAP_SETDIRECTION , setdirection); - DOLINK(PCAP_DATALINK_VAL_TO_NAME , datalink_val_to_name); - DOLINK(PCAP_PERROR , perror); - DOLINK(PCAP_GETERR , geterr); - - - /* pseudo functions that don't exist in the libpcap interface */ - pl->dev_name = null_PCAP_DEV_NAME; - pl->dev_description = null_PCAP_DEV_DESCRIPTION; - pl->dev_next = null_PCAP_DEV_NEXT; - - /* windows-only functions that might improve speed */ -#if defined(WIN32) - DOLINK(PCAP_SENDQUEUE_ALLOC , sendqueue_alloc); - DOLINK(PCAP_SENDQUEUE_TRANSMIT , sendqueue_transmit); - DOLINK(PCAP_SENDQUEUE_DESTROY , sendqueue_destroy); - DOLINK(PCAP_SENDQUEUE_QUEUE , sendqueue_queue); -#endif - - DOLINK(PCAP_CREATE , create); - DOLINK(PCAP_SET_SNAPLEN , set_snaplen); - DOLINK(PCAP_SET_PROMISC , set_promisc); - DOLINK(PCAP_SET_TIMEOUT , set_timeout); - DOLINK(PCAP_SET_IMMEDIATE_MODE , set_immediate_mode); - DOLINK(PCAP_SET_BUFFER_SIZE , set_buffer_size); - DOLINK(PCAP_SET_RFMON , set_rfmon); - DOLINK(PCAP_CAN_SET_RFMON , can_set_rfmon); - DOLINK(PCAP_ACTIVATE , activate); - - - if (!pl->func_err) - pl->is_available = 1; - else - pl->is_available = 0; - - return 0; -} - diff --git a/src/stub-pcap.h b/src/stub-pcap.h deleted file mode 100755 index 1bac175..0000000 --- a/src/stub-pcap.h +++ /dev/null @@ -1,222 +0,0 @@ -/* - Dynamically load libpcap at runtime - - This library optionally loads the 'libpcap' library at runtime, rather - than statically linked at compile time. The advantage of this is that - the user can build this project with no dependencies -- although they - may require this dependency in order to run the program. - - As of 2017, libpcap shared libraries are standard on major Linux - distributions (Debian, Readhat), FreeBSD, OpenBSD, and macOS. On - Windows, "winpcap" must be downloaded. -*/ -#ifndef STUB_PCAP_H -#define STUB_PCAP_H -#include - - - -/* Including the right ".h" file to define "timeval" is difficult, so instead - * so instead we are simply going to define our own structure. This should - * match the binary definition within the operating system - */ -#if __NetBSD__ -#include -#define pcap_timeval timeval -#elif __OpenBSD__ -#include -#define pcap_timeval bpf_timeval -#else -struct pcap_timeval { - long tv_sec; /* seconds */ - long tv_usec; /* and microseconds */ -}; -#endif - -/* Forward reference of opaque 'pcap_t' structure */ -struct pcap; -typedef struct pcap pcap_t; - -/* Forward reference of opaque 'pcap_if_t' structure */ -struct pcap_if; -typedef struct pcap_if pcap_if_t; - -/* How many bytes to reserve for error messages. This is the number specified - * in libpcap, smaller numbers can crash */ -enum { - PCAP_ERRBUF_SIZE=256, -}; - -/* used in pcap_setdirection() */ -typedef enum { - PCAP_D_INOUT = 0, - PCAP_D_IN = 1, - PCAP_D_OUT = 2, -} pcap_direction_t; - -enum { - PCAP_ERROR = -1, - PCAP_ERROR_BREAK = -2, - PCAP_ERROR_NOT_ACTIVATED = -3, - PCAP_ERROR_ACTIVATED = -4, - PCAP_ERROR_NO_SUCH_DEVICE = -5, - PCAP_ERROR_RFMON_NOTSUP = -6, - PCAP_ERROR_NOT_RFMON = -7, - PCAP_ERROR_PERM_DENIED = -8, - PCAP_ERROR_IFACE_NOT_UP = -9, - PCAP_ERROR_CANTSET_TSTAMP_TYPE = -10, - PCAP_ERROR_PROMISC_PERM_DENIED = -11, - PCAP_ERROR_TSTAMP_PRECISION_NOTSUP = -12, - - /* warnings, not errors */ - PCAP_WARNING = 1, - PCAP_WARNING_PROMISC_NOTSUP = 2, - PCAP_WARNING_TSTAMP_TYPE_NOTSUP = 3, -}; - -/* The packet header for capturing packets. Apple macOS inexplicably adds - * an extra comment-field onto the end of this, so the definition needs - * to be careful to match the real definition */ -struct pcap_pkthdr { - struct pcap_timeval ts; - unsigned caplen; - unsigned len; -#ifdef __APPLE__ - char comment[256]; -#endif -}; - - -/* - * This block is for function declarations. Consult the libpcap - * documentation for what these functions really mean - */ -typedef void (*PCAP_HANDLE_PACKET)(unsigned char *v_seap, const struct pcap_pkthdr *framehdr, const unsigned char *buf); -typedef void (*PCAP_CLOSE)(void *hPcap); -typedef unsigned (*PCAP_DATALINK)(void *hPcap); -typedef unsigned (*PCAP_DISPATCH)(void *hPcap, unsigned how_many_packets, PCAP_HANDLE_PACKET handler, void *handle_data); -typedef int (*PCAP_FINDALLDEVS)(pcap_if_t **alldevs, char *errbuf); -typedef const char *(*PCAP_LIB_VERSION)(void); -typedef char * (*PCAP_LOOKUPDEV)(char *errbuf); -typedef int (*PCAP_MAJOR_VERSION)(void *p); -typedef int (*PCAP_MINOR_VERSION)(void *p); -typedef void * (*PCAP_OPEN_LIVE)(const char *devicename, unsigned snap_length, unsigned is_promiscuous, unsigned read_timeout, char *errbuf); -typedef void (*PCAP_FREEALLDEVS)(pcap_if_t *alldevs); -typedef pcap_t * (*PCAP_OPEN_OFFLINE)(const char *fname, char *errbuf); -typedef int (*PCAP_SENDPACKET)(pcap_t *p, const unsigned char *buf, int size); -typedef const unsigned char *(*PCAP_NEXT)(pcap_t *p, struct pcap_pkthdr *h); -typedef int (*PCAP_SETDIRECTION)(pcap_t *, pcap_direction_t); -typedef const char *(*PCAP_DATALINK_VAL_TO_NAME)(int dlt); -typedef void (*PCAP_PERROR)(pcap_t *p, char *prefix); -typedef const char *(*PCAP_GETERR)(pcap_t *p); -typedef const char *(*PCAP_DEV_NAME)(const pcap_if_t *dev); -typedef const char *(*PCAP_DEV_DESCRIPTION)(const pcap_if_t *dev); -typedef const pcap_if_t *(*PCAP_DEV_NEXT)(const pcap_if_t *dev); - -/* - pcap_open() replaced with a series of calls to: - p = pcap_create(device, errbuf); - pcap_set_snaplen(p, snaplen); - pcap_set_promisc(p, promisc); - pcap_set_timeout(p, to_ms); - pcap_activate(p); - */ -typedef pcap_t *(*PCAP_CREATE)(const char *source, char *errbuf); -typedef int (*PCAP_SET_SNAPLEN)(pcap_t *p, int snaplen); -typedef int (*PCAP_SET_PROMISC)(pcap_t *p, int promisc); -typedef int (*PCAP_SET_TIMEOUT)(pcap_t *p, int to_ms); -typedef int (*PCAP_SET_IMMEDIATE_MODE)(pcap_t *p, int immediate_mode); -typedef int (*PCAP_SET_BUFFER_SIZE)(pcap_t *p, int buffer_size); -typedef int (*PCAP_SET_RFMON)(pcap_t *p, int rfmon); -typedef int (*PCAP_CAN_SET_RFMON)(pcap_t *p); -typedef int (*PCAP_ACTIVATE)(pcap_t *p); - - - -/* - * PORTABILITY: Windows supports the "sendq" feature, and is really slow - * without this feature. It's not needed on Linux, so we just create - * equivalent functions that do nothing - */ -struct pcap_send_queue; -typedef struct pcap_send_queue pcap_send_queue; - -typedef pcap_send_queue *(*PCAP_SENDQUEUE_ALLOC)(size_t size); -typedef unsigned (*PCAP_SENDQUEUE_TRANSMIT)(pcap_t *p, pcap_send_queue *queue, int sync); -typedef void (*PCAP_SENDQUEUE_DESTROY)(pcap_send_queue *queue); -typedef int (*PCAP_SENDQUEUE_QUEUE)(pcap_send_queue *queue, const struct pcap_pkthdr *pkt_header, const unsigned char *pkt_data); - - - - - -struct PcapFunctions { - unsigned func_err:1; - unsigned is_available:1; - unsigned is_printing_debug:1; - unsigned status; - unsigned errcode; - - PCAP_CLOSE close; - PCAP_DATALINK datalink; - PCAP_DISPATCH dispatch; - PCAP_FINDALLDEVS findalldevs; - PCAP_FREEALLDEVS freealldevs; - PCAP_LOOKUPDEV lookupdev; - PCAP_LIB_VERSION lib_version; - PCAP_MAJOR_VERSION major_version; - PCAP_MINOR_VERSION minor_version; - PCAP_OPEN_LIVE open_live; - - - PCAP_OPEN_OFFLINE open_offline; - PCAP_SENDPACKET sendpacket; - PCAP_NEXT next; - PCAP_SETDIRECTION setdirection; - PCAP_DATALINK_VAL_TO_NAME datalink_val_to_name; - PCAP_PERROR perror; - PCAP_GETERR geterr; - - /* Accessor functions for opaque data structure, don't really - * exist in libpcap */ - PCAP_DEV_NAME dev_name; - PCAP_DEV_DESCRIPTION dev_description; - PCAP_DEV_NEXT dev_next; - - /* Windows-only functions */ - PCAP_SENDQUEUE_ALLOC sendqueue_alloc; - PCAP_SENDQUEUE_TRANSMIT sendqueue_transmit; - PCAP_SENDQUEUE_DESTROY sendqueue_destroy; - PCAP_SENDQUEUE_QUEUE sendqueue_queue; - - PCAP_CREATE create; - PCAP_SET_SNAPLEN set_snaplen; - PCAP_SET_PROMISC set_promisc; - PCAP_SET_TIMEOUT set_timeout; - PCAP_SET_IMMEDIATE_MODE set_immediate_mode; - PCAP_SET_BUFFER_SIZE set_buffer_size; - PCAP_SET_RFMON set_rfmon; - PCAP_CAN_SET_RFMON can_set_rfmon; - PCAP_ACTIVATE activate; - -}; - -/** - * This is global structure containing all the libpcap function pointers. - * use in the form "PCAP.functionname()" rather than "pcap_functioname()". - */ -extern struct PcapFunctions PCAP; - -/** - * Dynamically loads the shared library (libpcap.so, libpcap.dynlib, - * or libpcap.dll. Call this during program startup like main() in order - * to load the libraries. Not thread safe, so call from the startup - * thread, but not within threads. - * @return - * 0 on success or - * -1 on failure - */ -int pcap_init(void); - - -#endif