diff --git a/pcap_wrapper.cpp b/pcap_wrapper.cpp new file mode 100644 index 0000000..0288328 --- /dev/null +++ b/pcap_wrapper.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +int (*pcap_loop )(pcap_t *, int, pcap_handler, u_char *); +int (*pcap_breakloop )(pcap_t *); + +pcap_t* (*pcap_create)(const char *, char *); + +int (*pcap_set_snaplen) (pcap_t *, int)=0; +int (*pcap_set_promisc) (pcap_t *, int)=0; +int (*pcap_can_set_rfmon) (pcap_t *)=0; +int (*pcap_set_rfmon )(pcap_t *, int)=0; +int (*pcap_set_timeout)(pcap_t *, int)=0; +int (*pcap_set_buffer_size)(pcap_t *, int)=0; +int (*pcap_activate)(pcap_t *)=0; + +int (*pcap_setfilter)(pcap_t *, struct bpf_program *)=0; +int (*pcap_setdirection)(pcap_t *, pcap_direction_t)=0; + +int (*pcap_datalink)(pcap_t *)=0; + +void (*pcap_freecode)(struct bpf_program *)=0; + +int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int, + bpf_u_int32)=0; + +char* (*pcap_geterr)(pcap_t *)=0; +int (*pcap_sendpacket)(pcap_t *, const u_char *, int)=0; + +char* (*pcap_lookupdev)(char *)=0; + +int (*pcap_findalldevs)(pcap_if_t **, char *)=0; + +struct init_pcap_t +{ + init_pcap_t() + { + init_pcap(); + } + +}do_it; + +static void init_npcap_dll_path() +{ + BOOL(WINAPI *SetDllDirectory)(LPCTSTR); + char sysdir_name[512]; + int len; + + SetDllDirectory = (BOOL(WINAPI *)(LPCTSTR)) GetProcAddress(GetModuleHandle("kernel32.dll"), "SetDllDirectoryA"); + if (SetDllDirectory == NULL) { + printf("Error in SetDllDirectory\n"); + } + else { + len = GetSystemDirectory(sysdir_name, 480); // be safe + if (!len) + printf("Error in GetSystemDirectory (%d)\n", (int)GetLastError()); + strcat(sysdir_name, "\\Npcap"); + if (SetDllDirectory(sysdir_name) == 0) + printf("Error in SetDllDirectory(\"System32\\Npcap\")\n"); + } +} + +#define EXPORT_FUN(XXX) do{ XXX= (__typeof__(XXX)) GetProcAddress(wpcap, #XXX); }while(0) +int init_pcap() +{ + HMODULE wpcap=LoadLibrary("wpcap.dll"); + if(wpcap!=0) + { + printf("using system32/wpcap.dll\n"); + } + else + { + init_npcap_dll_path(); + //SetDllDirectory("C:\\Windows\\System32\\Npcap\\"); + wpcap=LoadLibrary("wpcap.dll"); + if(wpcap!=0) + printf("using system32/npcap/wpcap.dll\n"); + } + if(wpcap==0) + { + printf("cant not open wpcap.dll, make sure winpcap/npcap is installed\n"); + exit(-1); + } + assert(wpcap!=0); + + EXPORT_FUN(pcap_loop); + EXPORT_FUN(pcap_breakloop); + EXPORT_FUN(pcap_create); + EXPORT_FUN(pcap_set_snaplen); + EXPORT_FUN(pcap_set_promisc); + EXPORT_FUN(pcap_set_timeout); + EXPORT_FUN(pcap_activate); + EXPORT_FUN(pcap_setfilter); + EXPORT_FUN(pcap_setdirection); + EXPORT_FUN(pcap_datalink); + EXPORT_FUN(pcap_freecode); + EXPORT_FUN(pcap_compile); + EXPORT_FUN(pcap_geterr); + EXPORT_FUN(pcap_sendpacket); + EXPORT_FUN(pcap_lookupdev); + EXPORT_FUN(pcap_findalldevs); + /* + pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop"); + pcap_create = (__typeof__(pcap_create))GetProcAddress(wpcap, "pcap_create"); + pcap_set_snaplen = (__typeof__(pcap_set_snaplen))GetProcAddress(wpcap, "pcap_set_snaplen"); + pcap_set_promisc = (__typeof__(pcap_set_promisc))GetProcAddress(wpcap, "pcap_set_promisc"); + pcap_set_timeout = (__typeof__(pcap_set_timeout))GetProcAddress(wpcap, "pcap_set_timeout"); + pcap_activate = (__typeof__(pcap_activate))GetProcAddress(wpcap, "pcap_activate"); + pcap_setfilter = (__typeof__(pcap_setfilter))GetProcAddress(wpcap, "pcap_setfilter"); + pcap_setdirection = (__typeof__(pcap_setdirection))GetProcAddress(wpcap, "pcap_setdirection"); + pcap_datalink = (__typeof__(pcap_datalink))GetProcAddress(wpcap, "pcap_datalink"); + pcap_freecode = (__typeof__(pcap_freecode))GetProcAddress(wpcap, "pcap_freecode"); + pcap_compile = (__typeof__(pcap_compile))GetProcAddress(wpcap, "pcap_compile"); + pcap_geterr = (__typeof__(pcap_geterr))GetProcAddress(wpcap, "pcap_geterr"); + pcap_sendpacket = (__typeof__(pcap_sendpacket))GetProcAddress(wpcap, "pcap_sendpacket"); + pcap_lookupdev = (__typeof__(pcap_lookupdev))GetProcAddress(wpcap, "pcap_lookupdev"); + pcap_findalldevs = (__typeof__(pcap_findalldevs))GetProcAddress(wpcap, "pcap_findalldevs"); + //pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop"); + //pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop"); + //pcap_loop = (__typeof__(pcap_loop))GetProcAddress(wpcap, "pcap_loop"); + */ + return 0; + +} diff --git a/pcap_wrapper.h b/pcap_wrapper.h new file mode 100644 index 0000000..355fd69 --- /dev/null +++ b/pcap_wrapper.h @@ -0,0 +1,127 @@ +#pragma once + +//#ifdef __cplusplus +//extern "C" { +//#endif + +//#include +//#include + +struct bpf_program +{ + char a[2000]; +}; + +struct pcap_t +{ + char a[2000]; +}; + +typedef unsigned int bpf_u_int32; + +typedef struct my_timeval { + int tv_sec; + int tv_usec; +} my_timeval; + +struct pcap_pkthdr { + struct my_timeval ts; /* time stamp */ + bpf_u_int32 caplen; /* length of portion present */ + bpf_u_int32 len; /* length this packet (off wire) */ +}; + +typedef enum { + PCAP_D_INOUT = 0, + PCAP_D_IN, + PCAP_D_OUT +} pcap_direction_t; + + +struct pcap_addr { + struct pcap_addr *next; + struct sockaddr *addr; /* address */ + struct sockaddr *netmask; /* netmask for that address */ + struct sockaddr *broadaddr; /* broadcast address for that address */ + struct sockaddr *dstaddr; /* P2P destination address for that address */ +}; + +struct pcap_if { + struct pcap_if *next; + char *name; /* name to hand to "pcap_open_live()" */ + char *description; /* textual description of interface, or NULL */ + struct pcap_addr *addresses; + bpf_u_int32 flags; /* PCAP_IF_ interface flags */ +}; + +typedef struct pcap_if pcap_if_t; +typedef struct pcap_addr pcap_addr_t; + + + +typedef unsigned char u_char; + + +#define PCAP_ERRBUF_SIZE 256 + +#define DLT_NULL 0 /* BSD loopback encapsulation */ +#define DLT_EN10MB 1 /* Ethernet (10Mb) */ +#define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */ +#define DLT_AX25 3 /* Amateur Radio AX.25 */ +#define DLT_PRONET 4 /* Proteon ProNET Token Ring */ +#define DLT_CHAOS 5 /* Chaos */ +#define DLT_IEEE802 6 /* 802.5 Token Ring */ +#define DLT_ARCNET 7 /* ARCNET, with BSD-style header */ +#define DLT_SLIP 8 /* Serial Line IP */ +#define DLT_PPP 9 /* Point-to-point Protocol */ +#define DLT_FDDI 10 /* FDDI */ +#define DLT_LINUX_SLL 113 + +#define PCAP_NETMASK_UNKNOWN 0xffffffff + +typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *, + const u_char *); + +extern int (*pcap_loop )(pcap_t *, int, pcap_handler, u_char *); + +extern int (*pcap_breakloop )(pcap_t *); + +extern pcap_t* (*pcap_create)(const char *, char *); + +extern int (*pcap_set_snaplen) (pcap_t *, int); +extern int (*pcap_set_promisc) (pcap_t *, int); +extern int (*pcap_can_set_rfmon) (pcap_t *); +extern int (*pcap_set_rfmon )(pcap_t *, int); +extern int (*pcap_set_timeout)(pcap_t *, int); +extern int (*pcap_set_buffer_size)(pcap_t *, int); +extern int (*pcap_activate)(pcap_t *); + +extern int (*pcap_setfilter)(pcap_t *, struct bpf_program *); +extern int (*pcap_setdirection)(pcap_t *, pcap_direction_t); + +extern int (*pcap_datalink)(pcap_t *); + +extern void (*pcap_freecode)(struct bpf_program *); + +extern int (*pcap_compile)(pcap_t *, struct bpf_program *, const char *, int, + bpf_u_int32); + +extern char* (*pcap_geterr)(pcap_t *); +extern int (*pcap_sendpacket)(pcap_t *, const u_char *, int); + +extern char* (*pcap_lookupdev)(char *); + +extern int (*pcap_findalldevs)(pcap_if_t **, char *); + +inline int pcap_set_immediate_mode(pcap_t *,int) +{ + return 0; +} + + + +//#ifdef __cplusplus +//} +//#endif + +int init_pcap(); +