Loading Makefile +6 −6 Original line number Original line Diff line number Diff line Loading @@ -13,7 +13,7 @@ endif # environment where things likely will work -- as well as anything # environment where things likely will work -- as well as anything # works on the bajillion of different Linux environments # works on the bajillion of different Linux environments ifneq (, $(findstring linux, $(SYS))) ifneq (, $(findstring linux, $(SYS))) LIBS = -lpcap -lm -lrt -ldl -lpthread LIBS = -lm -lrt -ldl -lpthread INCLUDES = INCLUDES = FLAGS2 = FLAGS2 = endif endif Loading @@ -23,7 +23,7 @@ endif # my regularly regression-test environment. That means at any point # my regularly regression-test environment. That means at any point # in time, something might be minorly broken in Mac OS X. # in time, something might be minorly broken in Mac OS X. ifneq (, $(findstring darwin, $(SYS))) ifneq (, $(findstring darwin, $(SYS))) LIBS = -lpcap -lm LIBS = -lm INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = INSTALL_DATA = -pm755 INSTALL_DATA = -pm755 Loading @@ -37,7 +37,7 @@ endif # intended environment, so it make break in the future. # intended environment, so it make break in the future. ifneq (, $(findstring mingw, $(SYS))) ifneq (, $(findstring mingw, $(SYS))) INCLUDES = -Ivs10/include INCLUDES = -Ivs10/include LIBS = -L vs10/lib -lwpcap -lIPHLPAPI -lWs2_32 LIBS = -L vs10/lib -lIPHLPAPI -lWs2_32 FLAGS2 = -march=i686 FLAGS2 = -march=i686 endif endif Loading @@ -47,20 +47,20 @@ endif # head with a hammer and want to feel a different sort of pain. # head with a hammer and want to feel a different sort of pain. ifneq (, $(findstring cygwin, $(SYS))) ifneq (, $(findstring cygwin, $(SYS))) INCLUDES = -I. INCLUDES = -I. LIBS = -lwpcap LIBS = FLAGS2 = FLAGS2 = endif endif # OpenBSD # OpenBSD ifneq (, $(findstring openbsd, $(SYS))) ifneq (, $(findstring openbsd, $(SYS))) LIBS = -lpcap -lm -pthread LIBS = -lm -pthread INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = endif endif # FreeBSD # FreeBSD ifneq (, $(findstring freebsd, $(SYS))) ifneq (, $(findstring freebsd, $(SYS))) LIBS = -lpcap -lm -pthread LIBS = -lm -pthread INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = endif endif Loading src/main.c +3 −1 Original line number Original line Diff line number Diff line Loading @@ -1428,6 +1428,7 @@ main_scan(struct Masscan *masscan) void pcap_init(void); /*************************************************************************** /*************************************************************************** ***************************************************************************/ ***************************************************************************/ Loading Loading @@ -1506,6 +1507,7 @@ int main(int argc, char *argv[]) /* We need to do a separate "raw socket" initialization step. This is /* We need to do a separate "raw socket" initialization step. This is * for Windows and PF_RING. */ * for Windows and PF_RING. */ pcap_init(); rawsock_init(); rawsock_init(); /* Init some protocol parser data structures */ /* Init some protocol parser data structures */ Loading src/rawsock-pcap.c 0 → 100644 +416 −0 Original line number Original line Diff line number Diff line /* Copyright (c) 2007 by Errata Security, All Rights Reserved * Copyright (c) 2017 by Robert David Graham * Programer(s): Robert David Graham [rdg] */ /* PCAPLIVE This code links the 'libpcap' module at RUNTIME rather than LOADTIME. This allows us to: (a) run this program on capture files without needing libpcap installed. (b) give more user friendly diagnostic to the users who don't realize that they need to install libpcap separately. On Windows, this uses the LoadLibrary/GetProcAddress functions to load the library. On Linux, this uses the dlopen/dlsym functions to do essentially the same thing. */ #include "logger.h" #if _MSC_VER==1200 #pragma warning(disable:4115 4201) #include <winerror.h> #endif #include "rawsock-pcap.h" #ifdef WIN32 #include <windows.h> #else #include <dlfcn.h> #endif #include <stdio.h> #include <string.h> #include <errno.h> #ifndef UNUSEDPARM #ifdef __GNUC__ #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); } 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"; } #ifdef WIN32 static void *null_PCAP_GET_AIRPCAP_HANDLE(void *p) { UNUSEDPARM(p); return NULL; } #endif #ifdef WIN32 static unsigned null_AIRPCAP_SET_DEVICE_CHANNEL(void *p, unsigned channel) { UNUSEDPARM(p);UNUSEDPARM(channel); return 0; /*0=failure, 1=success*/ } #endif static unsigned null_CAN_TRANSMIT(const char *devicename) { #if WIN32 struct DeviceCapabilities { unsigned AdapterId; /* An Id that identifies the adapter model.*/ char AdapterModelName; /* String containing a printable adapter model.*/ unsigned AdapterBus; /* The type of bus the adapter is plugged to. */ unsigned CanTransmit; /* TRUE if the adapter is able to perform frame injection.*/ unsigned CanSetTransmitPower; /* TRUE if the adapter's transmit power is can be specified by the user application.*/ unsigned ExternalAntennaPlug; /* TRUE if the adapter supports plugging one or more external antennas.*/ unsigned SupportedMedia; unsigned SupportedBands; } caps; void * (*myopen)(const char *devicename, char *errbuf); void (*myclose)(void *h); unsigned (*mycapabilities)(void *h, struct DeviceCapabilities *caps); unsigned result = 0; void *hAirpcap; hAirpcap = LoadLibraryA("airpcap.dll"); if (hAirpcap == NULL) return 0; myopen = (void * (*)(const char *, char*))GetProcAddress(hAirpcap, "AirpcapOpen"); myclose = (void (*)(void*))GetProcAddress(hAirpcap, "AirpcapClose"); mycapabilities = (unsigned (*)(void*, struct DeviceCapabilities *))GetProcAddress(hAirpcap, "AirpcapGetDeviceCapabilities"); if (myopen && mycapabilities && myclose ) { void *h = myopen(devicename, NULL); if (h) { if (mycapabilities(h, &caps)) { result = caps.CanTransmit; } myclose(h); } } FreeLibrary(hAirpcap); return result; #elif defined(__linux__) return 1; #elif defined(__APPLE__) || defined(__FreeBSD__) return 1; #else #error unknown os #endif } struct PcapFunctions PCAP = { 0,0,0,0,0, null_PCAP_CLOSE, }; static void *my_null(int x, ...) { return 0; } static pcap_t *null_PCAP_OPEN_OFFLINE(const char *fname, char *errbuf) { return my_null(2, fname, errbuf); } static int null_PCAP_SENDPACKET(pcap_t *p, const unsigned char *buf, int size) { return (int)my_null(p, buf, size); } static const unsigned char *null_PCAP_NEXT(pcap_t *p, struct pcap_pkthdr *h) { return 0; } static int null_PCAP_SETDIRECTION(pcap_t *p, pcap_direction_t d) { return 0; } static const char *null_PCAP_DATALINK_VAL_TO_NAME(int dlt) { return 0; } static void null_PCAP_PERROR(pcap_t *p, char *prefix) { perror("pcap"); } 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; } /** * 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. */ void pcap_init(void) { struct PcapFunctions *pl = &PCAP; #ifdef WIN32 HMODULE hPacket; HMODULE hLibpcap; HMODULE hAirpcap; pl->is_available = 0; pl->is_printing_debug = 1; /* Look for the Packet.dll */ 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"); return; default: fprintf(stderr, "%s: couldn't load %d\n", "Packet.dll", (int)GetLastError()); return; } } /* Look for the Packet.dll */ 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; } /* Look for the Packet.dll */ hAirpcap = LoadLibraryA("airpcap.dll"); if (hLibpcap == NULL) { if (pl->is_printing_debug) fprintf(stderr, "%s: couldn't load %d\n", "airpcap.dll", (int)GetLastError()); return; } #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(2, "pcap: failed to load: %s\n", possible_names[i]); } } if (hLibpcap == NULL) fprintf(stderr, "pcap: failed to load libpcap\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 #ifdef WIN32 DOLINK(PCAP_GET_AIRPCAP_HANDLE, get_airpcap_handle); if (pl->func_err) { pl->func_err = 0; } if (hAirpcap) { pl->airpcap_set_device_channel = (AIRPCAP_SET_DEVICE_CHANNEL)GetProcAddress(hAirpcap, "AirpcapSetDeviceChannel"); if (pl->airpcap_set_device_channel == NULL) pl->airpcap_set_device_channel = null_AIRPCAP_SET_DEVICE_CHANNEL; } #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_DEV_NAME , dev_name); DOLINK(PCAP_DEV_DESCRIPTION , dev_description); DOLINK(PCAP_DEV_NEXT , dev_next); pl->can_transmit = null_CAN_TRANSMIT; if (!pl->func_err) pl->is_available = 1; else pl->is_available = 0; } src/rawsock-pcap.h 0 → 100644 +96 −0 Original line number Original line Diff line number Diff line #ifndef RAWSOCK_PCAP_H #define RAWSOCK_PCAP_H #include <sys/time.h> struct pcap; typedef struct pcap pcap_t; typedef struct pcap_if pcap_if_t; enum { PCAP_ERRBUF_SIZE=256, }; typedef enum { PCAP_D_INOUT = 0, PCAP_D_IN, PCAP_D_OUT } pcap_direction_t; struct pcap_pkthdr { struct timeval ts; unsigned caplen; unsigned len; #ifdef __APPLE__ char comment[256]; #endif }; 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 void * (*PCAP_GET_AIRPCAP_HANDLE)(void *p); typedef unsigned (*AIRPCAP_SET_DEVICE_CHANNEL)(void *p, unsigned channel); typedef unsigned (*CAN_TRANSMIT)(const char *devicename); 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_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); 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_GET_AIRPCAP_HANDLE get_airpcap_handle; AIRPCAP_SET_DEVICE_CHANNEL airpcap_set_device_channel; //AIRPCAP_SET_FCS_PRESENCE airpcap_set_fcs_presence; //BOOL AirpcapSetFcsPresence(PAirpcapHandle AdapterHandle, BOOL IsFcsPresent); CAN_TRANSMIT can_transmit; 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_DEV_NAME dev_name; PCAP_DEV_DESCRIPTION dev_description; PCAP_DEV_NEXT dev_next; }; extern struct PcapFunctions PCAP; void pcap_init(void); #endif src/rawsock.c +33 −31 Original line number Original line Diff line number Diff line Loading @@ -12,8 +12,8 @@ #include "rawsock-pfring.h" #include "rawsock-pfring.h" #include "pixie-timer.h" #include "pixie-timer.h" #include "main-globals.h" #include "main-globals.h" #include "rawsock-pcap.h" #include <pcap.h> //#include <pcap.h> #include <assert.h> #include <assert.h> #include <ctype.h> #include <ctype.h> Loading Loading @@ -205,6 +205,8 @@ rawsock_init(void) } } /*************************************************************************** /*************************************************************************** * This function prints to the command line a list of all the network * intefaces/devices. ***************************************************************************/ ***************************************************************************/ void void rawsock_list_adapters(void) rawsock_list_adapters(void) Loading @@ -212,23 +214,24 @@ rawsock_list_adapters(void) pcap_if_t *alldevs; pcap_if_t *alldevs; char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE]; if (pcap_findalldevs(&alldevs, errbuf) != -1) { if (PCAP.findalldevs(&alldevs, errbuf) != -1) { int i; int i; pcap_if_t *d; const pcap_if_t *d; i=0; i=0; if (alldevs == NULL) { if (alldevs == NULL) { fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } } /* Print the list */ /* Print the list */ for(d=alldevs; d; d=d->next) { for(d=alldevs; d; d=PCAP.dev_next(d)) { fprintf(stderr, " %d %s \t", i++, d->name); fprintf(stderr, " %d %s \t", i++, PCAP.dev_name(d)); if (d->description) if (PCAP.dev_description(d)) fprintf(stderr, "(%s)\n", d->description); fprintf(stderr, "(%s)\n", PCAP.dev_description(d)); else else fprintf(stderr, "(No description available)\n"); fprintf(stderr, "(No description available)\n"); } } fprintf(stderr,"\n"); fprintf(stderr,"\n"); PCAP.freealldevs(alldevs); } else { } else { fprintf(stderr, "%s\n", errbuf); fprintf(stderr, "%s\n", errbuf); } } Loading @@ -236,25 +239,24 @@ rawsock_list_adapters(void) /*************************************************************************** /*************************************************************************** ***************************************************************************/ ***************************************************************************/ static char * static const char * adapter_from_index(unsigned index) adapter_from_index(unsigned index) { { pcap_if_t *alldevs; pcap_if_t *alldevs; char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE]; int x; int x; x = pcap_findalldevs(&alldevs, errbuf); x = PCAP.findalldevs(&alldevs, errbuf); if (x != -1) { if (x != -1) { pcap_if_t *d; const pcap_if_t *d; if (alldevs == NULL) { if (alldevs == NULL) { fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } } /* Print the list */ /* Print the list */ for(d=alldevs; d; d=d->next) for(d=alldevs; d; d=PCAP.dev_next(d)) { { if (index-- == 0) if (index-- == 0) return d->name; return PCAP.dev_name(d); } } return 0; return 0; } else { } else { Loading Loading @@ -339,7 +341,7 @@ rawsock_send_packet( /* LIBPCAP */ /* LIBPCAP */ if (adapter->pcap) if (adapter->pcap) return pcap_sendpacket(adapter->pcap, packet, length); return PCAP.sendpacket(adapter->pcap, packet, length); return 0; return 0; } } Loading Loading @@ -377,13 +379,13 @@ int rawsock_recv_packet( return 1; return 1; *length = hdr.caplen; *length = hdr.caplen; *secs = hdr.ts.tv_sec; *secs = (unsigned)hdr.ts.tv_sec; *usecs = hdr.ts.tv_usec; *usecs = (unsigned)hdr.ts.tv_usec; } else if (adapter->pcap) { } else if (adapter->pcap) { struct pcap_pkthdr hdr; struct pcap_pkthdr hdr; *packet = pcap_next(adapter->pcap, &hdr); *packet = PCAP.next(adapter->pcap, &hdr); if (*packet == NULL) { if (*packet == NULL) { if (is_pcap_file) { if (is_pcap_file) { Loading @@ -395,7 +397,7 @@ int rawsock_recv_packet( } } *length = hdr.caplen; *length = hdr.caplen; *secs = hdr.ts.tv_sec; *secs = (unsigned)hdr.ts.tv_sec; *usecs = hdr.ts.tv_usec; *usecs = hdr.ts.tv_usec; } } Loading Loading @@ -506,9 +508,9 @@ rawsock_ignore_transmits(struct Adapter *adapter, const unsigned char *adapter_m if (adapter->pcap) { if (adapter->pcap) { int err; int err; err = pcap_setdirection(adapter->pcap, PCAP_D_IN); err = PCAP.setdirection(adapter->pcap, PCAP_D_IN); if (err) { if (err) { pcap_perror(adapter->pcap, "pcap_setdirection(IN)"); PCAP.perror(adapter->pcap, "pcap_setdirection(IN)"); } } } } #else #else Loading Loading @@ -554,7 +556,7 @@ rawsock_close_adapter(struct Adapter *adapter) PFRING.close(adapter->ring); PFRING.close(adapter->ring); } } if (adapter->pcap) { if (adapter->pcap) { pcap_close(adapter->pcap); PCAP.close(adapter->pcap); } } if (adapter->sendq) { if (adapter->sendq) { pcap_sendqueue_destroy(adapter->sendq); pcap_sendqueue_destroy(adapter->sendq); Loading Loading @@ -739,18 +741,18 @@ rawsock_init_adapter(const char *adapter_name, * This is the stanard that should work everywhere. * This is the stanard that should work everywhere. *----------------------------------------------------------------*/ *----------------------------------------------------------------*/ { { LOG(1, "pcap: %s\n", pcap_lib_version()); LOG(1, "pcap: %s\n", PCAP.lib_version()); LOG(2, "pcap:'%s': opening...\n", adapter_name); LOG(2, "pcap:'%s': opening...\n", adapter_name); if (memcmp(adapter_name, "file:", 5) == 0) { if (memcmp(adapter_name, "file:", 5) == 0) { LOG(1, "pcap: file: %s\n", adapter_name+5); LOG(1, "pcap: file: %s\n", adapter_name+5); is_pcap_file = 1; is_pcap_file = 1; adapter->pcap = pcap_open_offline( adapter->pcap = PCAP.open_offline( adapter_name+5, /* interface name */ adapter_name+5, /* interface name */ errbuf); errbuf); } else { } else { adapter->pcap = pcap_open_live( adapter->pcap = PCAP.open_live( adapter_name, /* interface name */ adapter_name, /* interface name */ 65536, /* max packet size */ 65536, /* max packet size */ 8, /* promiscuous mode */ 8, /* promiscuous mode */ Loading Loading @@ -778,7 +780,7 @@ rawsock_init_adapter(const char *adapter_name, /* Figure out the link-type. We suport Ethernet and IP */ /* Figure out the link-type. We suport Ethernet and IP */ { { int dl = pcap_datalink(adapter->pcap); int dl = PCAP.datalink(adapter->pcap); switch (dl) { switch (dl) { case 1: /* Ethernet */ case 1: /* Ethernet */ adapter->link_type = dl; adapter->link_type = dl; Loading @@ -788,13 +790,13 @@ rawsock_init_adapter(const char *adapter_name, break; break; default: default: LOG(0, "unknown data link type: %u(%s)\n", LOG(0, "unknown data link type: %u(%s)\n", dl, pcap_datalink_val_to_name(dl)); dl, PCAP.datalink_val_to_name(dl)); break; break; } } } } #if 0 /* Set any BPF filters the user might've set */ /* Set any BPF filters the user might've set */ if (bpf_filter) { if (bpf_filter) { int err; int err; Loading @@ -818,7 +820,7 @@ rawsock_init_adapter(const char *adapter_name, exit(1); exit(1); } } } } #endif } } Loading Loading
Makefile +6 −6 Original line number Original line Diff line number Diff line Loading @@ -13,7 +13,7 @@ endif # environment where things likely will work -- as well as anything # environment where things likely will work -- as well as anything # works on the bajillion of different Linux environments # works on the bajillion of different Linux environments ifneq (, $(findstring linux, $(SYS))) ifneq (, $(findstring linux, $(SYS))) LIBS = -lpcap -lm -lrt -ldl -lpthread LIBS = -lm -lrt -ldl -lpthread INCLUDES = INCLUDES = FLAGS2 = FLAGS2 = endif endif Loading @@ -23,7 +23,7 @@ endif # my regularly regression-test environment. That means at any point # my regularly regression-test environment. That means at any point # in time, something might be minorly broken in Mac OS X. # in time, something might be minorly broken in Mac OS X. ifneq (, $(findstring darwin, $(SYS))) ifneq (, $(findstring darwin, $(SYS))) LIBS = -lpcap -lm LIBS = -lm INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = INSTALL_DATA = -pm755 INSTALL_DATA = -pm755 Loading @@ -37,7 +37,7 @@ endif # intended environment, so it make break in the future. # intended environment, so it make break in the future. ifneq (, $(findstring mingw, $(SYS))) ifneq (, $(findstring mingw, $(SYS))) INCLUDES = -Ivs10/include INCLUDES = -Ivs10/include LIBS = -L vs10/lib -lwpcap -lIPHLPAPI -lWs2_32 LIBS = -L vs10/lib -lIPHLPAPI -lWs2_32 FLAGS2 = -march=i686 FLAGS2 = -march=i686 endif endif Loading @@ -47,20 +47,20 @@ endif # head with a hammer and want to feel a different sort of pain. # head with a hammer and want to feel a different sort of pain. ifneq (, $(findstring cygwin, $(SYS))) ifneq (, $(findstring cygwin, $(SYS))) INCLUDES = -I. INCLUDES = -I. LIBS = -lwpcap LIBS = FLAGS2 = FLAGS2 = endif endif # OpenBSD # OpenBSD ifneq (, $(findstring openbsd, $(SYS))) ifneq (, $(findstring openbsd, $(SYS))) LIBS = -lpcap -lm -pthread LIBS = -lm -pthread INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = endif endif # FreeBSD # FreeBSD ifneq (, $(findstring freebsd, $(SYS))) ifneq (, $(findstring freebsd, $(SYS))) LIBS = -lpcap -lm -pthread LIBS = -lm -pthread INCLUDES = -I. INCLUDES = -I. FLAGS2 = FLAGS2 = endif endif Loading
src/main.c +3 −1 Original line number Original line Diff line number Diff line Loading @@ -1428,6 +1428,7 @@ main_scan(struct Masscan *masscan) void pcap_init(void); /*************************************************************************** /*************************************************************************** ***************************************************************************/ ***************************************************************************/ Loading Loading @@ -1506,6 +1507,7 @@ int main(int argc, char *argv[]) /* We need to do a separate "raw socket" initialization step. This is /* We need to do a separate "raw socket" initialization step. This is * for Windows and PF_RING. */ * for Windows and PF_RING. */ pcap_init(); rawsock_init(); rawsock_init(); /* Init some protocol parser data structures */ /* Init some protocol parser data structures */ Loading
src/rawsock-pcap.c 0 → 100644 +416 −0 Original line number Original line Diff line number Diff line /* Copyright (c) 2007 by Errata Security, All Rights Reserved * Copyright (c) 2017 by Robert David Graham * Programer(s): Robert David Graham [rdg] */ /* PCAPLIVE This code links the 'libpcap' module at RUNTIME rather than LOADTIME. This allows us to: (a) run this program on capture files without needing libpcap installed. (b) give more user friendly diagnostic to the users who don't realize that they need to install libpcap separately. On Windows, this uses the LoadLibrary/GetProcAddress functions to load the library. On Linux, this uses the dlopen/dlsym functions to do essentially the same thing. */ #include "logger.h" #if _MSC_VER==1200 #pragma warning(disable:4115 4201) #include <winerror.h> #endif #include "rawsock-pcap.h" #ifdef WIN32 #include <windows.h> #else #include <dlfcn.h> #endif #include <stdio.h> #include <string.h> #include <errno.h> #ifndef UNUSEDPARM #ifdef __GNUC__ #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); } 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"; } #ifdef WIN32 static void *null_PCAP_GET_AIRPCAP_HANDLE(void *p) { UNUSEDPARM(p); return NULL; } #endif #ifdef WIN32 static unsigned null_AIRPCAP_SET_DEVICE_CHANNEL(void *p, unsigned channel) { UNUSEDPARM(p);UNUSEDPARM(channel); return 0; /*0=failure, 1=success*/ } #endif static unsigned null_CAN_TRANSMIT(const char *devicename) { #if WIN32 struct DeviceCapabilities { unsigned AdapterId; /* An Id that identifies the adapter model.*/ char AdapterModelName; /* String containing a printable adapter model.*/ unsigned AdapterBus; /* The type of bus the adapter is plugged to. */ unsigned CanTransmit; /* TRUE if the adapter is able to perform frame injection.*/ unsigned CanSetTransmitPower; /* TRUE if the adapter's transmit power is can be specified by the user application.*/ unsigned ExternalAntennaPlug; /* TRUE if the adapter supports plugging one or more external antennas.*/ unsigned SupportedMedia; unsigned SupportedBands; } caps; void * (*myopen)(const char *devicename, char *errbuf); void (*myclose)(void *h); unsigned (*mycapabilities)(void *h, struct DeviceCapabilities *caps); unsigned result = 0; void *hAirpcap; hAirpcap = LoadLibraryA("airpcap.dll"); if (hAirpcap == NULL) return 0; myopen = (void * (*)(const char *, char*))GetProcAddress(hAirpcap, "AirpcapOpen"); myclose = (void (*)(void*))GetProcAddress(hAirpcap, "AirpcapClose"); mycapabilities = (unsigned (*)(void*, struct DeviceCapabilities *))GetProcAddress(hAirpcap, "AirpcapGetDeviceCapabilities"); if (myopen && mycapabilities && myclose ) { void *h = myopen(devicename, NULL); if (h) { if (mycapabilities(h, &caps)) { result = caps.CanTransmit; } myclose(h); } } FreeLibrary(hAirpcap); return result; #elif defined(__linux__) return 1; #elif defined(__APPLE__) || defined(__FreeBSD__) return 1; #else #error unknown os #endif } struct PcapFunctions PCAP = { 0,0,0,0,0, null_PCAP_CLOSE, }; static void *my_null(int x, ...) { return 0; } static pcap_t *null_PCAP_OPEN_OFFLINE(const char *fname, char *errbuf) { return my_null(2, fname, errbuf); } static int null_PCAP_SENDPACKET(pcap_t *p, const unsigned char *buf, int size) { return (int)my_null(p, buf, size); } static const unsigned char *null_PCAP_NEXT(pcap_t *p, struct pcap_pkthdr *h) { return 0; } static int null_PCAP_SETDIRECTION(pcap_t *p, pcap_direction_t d) { return 0; } static const char *null_PCAP_DATALINK_VAL_TO_NAME(int dlt) { return 0; } static void null_PCAP_PERROR(pcap_t *p, char *prefix) { perror("pcap"); } 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; } /** * 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. */ void pcap_init(void) { struct PcapFunctions *pl = &PCAP; #ifdef WIN32 HMODULE hPacket; HMODULE hLibpcap; HMODULE hAirpcap; pl->is_available = 0; pl->is_printing_debug = 1; /* Look for the Packet.dll */ 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"); return; default: fprintf(stderr, "%s: couldn't load %d\n", "Packet.dll", (int)GetLastError()); return; } } /* Look for the Packet.dll */ 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; } /* Look for the Packet.dll */ hAirpcap = LoadLibraryA("airpcap.dll"); if (hLibpcap == NULL) { if (pl->is_printing_debug) fprintf(stderr, "%s: couldn't load %d\n", "airpcap.dll", (int)GetLastError()); return; } #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(2, "pcap: failed to load: %s\n", possible_names[i]); } } if (hLibpcap == NULL) fprintf(stderr, "pcap: failed to load libpcap\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 #ifdef WIN32 DOLINK(PCAP_GET_AIRPCAP_HANDLE, get_airpcap_handle); if (pl->func_err) { pl->func_err = 0; } if (hAirpcap) { pl->airpcap_set_device_channel = (AIRPCAP_SET_DEVICE_CHANNEL)GetProcAddress(hAirpcap, "AirpcapSetDeviceChannel"); if (pl->airpcap_set_device_channel == NULL) pl->airpcap_set_device_channel = null_AIRPCAP_SET_DEVICE_CHANNEL; } #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_DEV_NAME , dev_name); DOLINK(PCAP_DEV_DESCRIPTION , dev_description); DOLINK(PCAP_DEV_NEXT , dev_next); pl->can_transmit = null_CAN_TRANSMIT; if (!pl->func_err) pl->is_available = 1; else pl->is_available = 0; }
src/rawsock-pcap.h 0 → 100644 +96 −0 Original line number Original line Diff line number Diff line #ifndef RAWSOCK_PCAP_H #define RAWSOCK_PCAP_H #include <sys/time.h> struct pcap; typedef struct pcap pcap_t; typedef struct pcap_if pcap_if_t; enum { PCAP_ERRBUF_SIZE=256, }; typedef enum { PCAP_D_INOUT = 0, PCAP_D_IN, PCAP_D_OUT } pcap_direction_t; struct pcap_pkthdr { struct timeval ts; unsigned caplen; unsigned len; #ifdef __APPLE__ char comment[256]; #endif }; 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 void * (*PCAP_GET_AIRPCAP_HANDLE)(void *p); typedef unsigned (*AIRPCAP_SET_DEVICE_CHANNEL)(void *p, unsigned channel); typedef unsigned (*CAN_TRANSMIT)(const char *devicename); 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_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); 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_GET_AIRPCAP_HANDLE get_airpcap_handle; AIRPCAP_SET_DEVICE_CHANNEL airpcap_set_device_channel; //AIRPCAP_SET_FCS_PRESENCE airpcap_set_fcs_presence; //BOOL AirpcapSetFcsPresence(PAirpcapHandle AdapterHandle, BOOL IsFcsPresent); CAN_TRANSMIT can_transmit; 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_DEV_NAME dev_name; PCAP_DEV_DESCRIPTION dev_description; PCAP_DEV_NEXT dev_next; }; extern struct PcapFunctions PCAP; void pcap_init(void); #endif
src/rawsock.c +33 −31 Original line number Original line Diff line number Diff line Loading @@ -12,8 +12,8 @@ #include "rawsock-pfring.h" #include "rawsock-pfring.h" #include "pixie-timer.h" #include "pixie-timer.h" #include "main-globals.h" #include "main-globals.h" #include "rawsock-pcap.h" #include <pcap.h> //#include <pcap.h> #include <assert.h> #include <assert.h> #include <ctype.h> #include <ctype.h> Loading Loading @@ -205,6 +205,8 @@ rawsock_init(void) } } /*************************************************************************** /*************************************************************************** * This function prints to the command line a list of all the network * intefaces/devices. ***************************************************************************/ ***************************************************************************/ void void rawsock_list_adapters(void) rawsock_list_adapters(void) Loading @@ -212,23 +214,24 @@ rawsock_list_adapters(void) pcap_if_t *alldevs; pcap_if_t *alldevs; char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE]; if (pcap_findalldevs(&alldevs, errbuf) != -1) { if (PCAP.findalldevs(&alldevs, errbuf) != -1) { int i; int i; pcap_if_t *d; const pcap_if_t *d; i=0; i=0; if (alldevs == NULL) { if (alldevs == NULL) { fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } } /* Print the list */ /* Print the list */ for(d=alldevs; d; d=d->next) { for(d=alldevs; d; d=PCAP.dev_next(d)) { fprintf(stderr, " %d %s \t", i++, d->name); fprintf(stderr, " %d %s \t", i++, PCAP.dev_name(d)); if (d->description) if (PCAP.dev_description(d)) fprintf(stderr, "(%s)\n", d->description); fprintf(stderr, "(%s)\n", PCAP.dev_description(d)); else else fprintf(stderr, "(No description available)\n"); fprintf(stderr, "(No description available)\n"); } } fprintf(stderr,"\n"); fprintf(stderr,"\n"); PCAP.freealldevs(alldevs); } else { } else { fprintf(stderr, "%s\n", errbuf); fprintf(stderr, "%s\n", errbuf); } } Loading @@ -236,25 +239,24 @@ rawsock_list_adapters(void) /*************************************************************************** /*************************************************************************** ***************************************************************************/ ***************************************************************************/ static char * static const char * adapter_from_index(unsigned index) adapter_from_index(unsigned index) { { pcap_if_t *alldevs; pcap_if_t *alldevs; char errbuf[PCAP_ERRBUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE]; int x; int x; x = pcap_findalldevs(&alldevs, errbuf); x = PCAP.findalldevs(&alldevs, errbuf); if (x != -1) { if (x != -1) { pcap_if_t *d; const pcap_if_t *d; if (alldevs == NULL) { if (alldevs == NULL) { fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); fprintf(stderr, "ERR:libpcap: no adapters found, are you sure you are root?\n"); } } /* Print the list */ /* Print the list */ for(d=alldevs; d; d=d->next) for(d=alldevs; d; d=PCAP.dev_next(d)) { { if (index-- == 0) if (index-- == 0) return d->name; return PCAP.dev_name(d); } } return 0; return 0; } else { } else { Loading Loading @@ -339,7 +341,7 @@ rawsock_send_packet( /* LIBPCAP */ /* LIBPCAP */ if (adapter->pcap) if (adapter->pcap) return pcap_sendpacket(adapter->pcap, packet, length); return PCAP.sendpacket(adapter->pcap, packet, length); return 0; return 0; } } Loading Loading @@ -377,13 +379,13 @@ int rawsock_recv_packet( return 1; return 1; *length = hdr.caplen; *length = hdr.caplen; *secs = hdr.ts.tv_sec; *secs = (unsigned)hdr.ts.tv_sec; *usecs = hdr.ts.tv_usec; *usecs = (unsigned)hdr.ts.tv_usec; } else if (adapter->pcap) { } else if (adapter->pcap) { struct pcap_pkthdr hdr; struct pcap_pkthdr hdr; *packet = pcap_next(adapter->pcap, &hdr); *packet = PCAP.next(adapter->pcap, &hdr); if (*packet == NULL) { if (*packet == NULL) { if (is_pcap_file) { if (is_pcap_file) { Loading @@ -395,7 +397,7 @@ int rawsock_recv_packet( } } *length = hdr.caplen; *length = hdr.caplen; *secs = hdr.ts.tv_sec; *secs = (unsigned)hdr.ts.tv_sec; *usecs = hdr.ts.tv_usec; *usecs = hdr.ts.tv_usec; } } Loading Loading @@ -506,9 +508,9 @@ rawsock_ignore_transmits(struct Adapter *adapter, const unsigned char *adapter_m if (adapter->pcap) { if (adapter->pcap) { int err; int err; err = pcap_setdirection(adapter->pcap, PCAP_D_IN); err = PCAP.setdirection(adapter->pcap, PCAP_D_IN); if (err) { if (err) { pcap_perror(adapter->pcap, "pcap_setdirection(IN)"); PCAP.perror(adapter->pcap, "pcap_setdirection(IN)"); } } } } #else #else Loading Loading @@ -554,7 +556,7 @@ rawsock_close_adapter(struct Adapter *adapter) PFRING.close(adapter->ring); PFRING.close(adapter->ring); } } if (adapter->pcap) { if (adapter->pcap) { pcap_close(adapter->pcap); PCAP.close(adapter->pcap); } } if (adapter->sendq) { if (adapter->sendq) { pcap_sendqueue_destroy(adapter->sendq); pcap_sendqueue_destroy(adapter->sendq); Loading Loading @@ -739,18 +741,18 @@ rawsock_init_adapter(const char *adapter_name, * This is the stanard that should work everywhere. * This is the stanard that should work everywhere. *----------------------------------------------------------------*/ *----------------------------------------------------------------*/ { { LOG(1, "pcap: %s\n", pcap_lib_version()); LOG(1, "pcap: %s\n", PCAP.lib_version()); LOG(2, "pcap:'%s': opening...\n", adapter_name); LOG(2, "pcap:'%s': opening...\n", adapter_name); if (memcmp(adapter_name, "file:", 5) == 0) { if (memcmp(adapter_name, "file:", 5) == 0) { LOG(1, "pcap: file: %s\n", adapter_name+5); LOG(1, "pcap: file: %s\n", adapter_name+5); is_pcap_file = 1; is_pcap_file = 1; adapter->pcap = pcap_open_offline( adapter->pcap = PCAP.open_offline( adapter_name+5, /* interface name */ adapter_name+5, /* interface name */ errbuf); errbuf); } else { } else { adapter->pcap = pcap_open_live( adapter->pcap = PCAP.open_live( adapter_name, /* interface name */ adapter_name, /* interface name */ 65536, /* max packet size */ 65536, /* max packet size */ 8, /* promiscuous mode */ 8, /* promiscuous mode */ Loading Loading @@ -778,7 +780,7 @@ rawsock_init_adapter(const char *adapter_name, /* Figure out the link-type. We suport Ethernet and IP */ /* Figure out the link-type. We suport Ethernet and IP */ { { int dl = pcap_datalink(adapter->pcap); int dl = PCAP.datalink(adapter->pcap); switch (dl) { switch (dl) { case 1: /* Ethernet */ case 1: /* Ethernet */ adapter->link_type = dl; adapter->link_type = dl; Loading @@ -788,13 +790,13 @@ rawsock_init_adapter(const char *adapter_name, break; break; default: default: LOG(0, "unknown data link type: %u(%s)\n", LOG(0, "unknown data link type: %u(%s)\n", dl, pcap_datalink_val_to_name(dl)); dl, PCAP.datalink_val_to_name(dl)); break; break; } } } } #if 0 /* Set any BPF filters the user might've set */ /* Set any BPF filters the user might've set */ if (bpf_filter) { if (bpf_filter) { int err; int err; Loading @@ -818,7 +820,7 @@ rawsock_init_adapter(const char *adapter_name, exit(1); exit(1); } } } } #endif } } Loading