Skip to content

Commit c8683b0

Browse files
author
bin huang
committed
Modify pcap.c and build for WinPCAP
1 parent 4788c91 commit c8683b0

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

msvcbuild.bat

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@rem Script to build luapcap
2+
3+
@setlocal
4+
@set MYCOMPILE=cl /nologo /MD /O2 /W3 /TC /c /D_CRT_SECURE_NO_DEPRECATE /D "WIN32" /I"..\wpdpack\Include" /I"..\lua\src" /I"..\wt-win-common" /D "LUA_BUILD_AS_DLL"
5+
@set MYLINK=link /nologo /DLL /LIBPATH:"..\wpdpack\Lib" /LIBPATH:"..\wt-win-common" /LIBPATH:"..\lua\src" /TLBID:1 /DLL "lua51.lib" "wpcap.lib" "wt-win-common.lib" "Packet.lib" "ws2_32.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
6+
@set MYMT=mt /nologo
7+
8+
if "%PLATFORM%"=="X64" (
9+
@set MACHINE=X64
10+
) else (
11+
@set MACHINE=X86
12+
)
13+
14+
del pcap.lib pcap.dll
15+
%MYCOMPILE% /D "_WINDLL" *.c
16+
%MYLINK% /export:luaopen_pcap /out:pcap.dll /implib:pcap.lib /MACHINE:%MACHINE% *.obj
17+
del /Q *.obj *.exp

pcap.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
3535
#include <stdlib.h>
3636
#include <string.h>
3737
#include <stdio.h>
38-
#include <sys/time.h>
3938
#include <time.h>
4039
#include <math.h>
4140
#include <pcap.h>
@@ -44,6 +43,10 @@ THE POSSIBILITY OF SUCH DAMAGE.
4443
#include "lauxlib.h"
4544
#include "lualib.h"
4645

46+
#ifdef WIN32
47+
#include <wt-win-common.h>
48+
#endif
49+
4750
static double tv2secs(struct timeval* tv)
4851
{
4952
double secs = tv->tv_sec;
@@ -58,11 +61,11 @@ static struct timeval* secs2tv(double secs, struct timeval* tv)
5861

5962
fpart = modf(secs, &ipart);
6063

61-
tv->tv_sec = (time_t) ipart;
64+
tv->tv_sec = (long) ipart;
6265

6366
fpart = modf(fpart * 1000000.0, &ipart);
6467

65-
tv->tv_usec = (suseconds_t) ipart;
68+
tv->tv_usec = (long) ipart;
6669

6770
if(fpart > 0.5)
6871
tv->tv_usec += 1;
@@ -369,6 +372,7 @@ Get a selectable file descriptor number which can be used to wait for packets.
369372
Returns the descriptor number on success, or nil if no such descriptor is
370373
available (see pcap_get_selectable_fd).
371374
*/
375+
#ifndef WIN32
372376
static int lpcap_getfd(lua_State* L)
373377
{
374378
pcap_t* cap = checkpcap(L);
@@ -381,6 +385,7 @@ static int lpcap_getfd(lua_State* L)
381385
lua_pushnumber(L, fd);
382386
return 1;
383387
}
388+
#endif
384389

385390
/*-
386391
-- capdata, timestamp, wirelen = cap:next()
@@ -447,6 +452,7 @@ Injects packet.
447452
448453
Return is bytes sent on success, or nil,emsg on failure.
449454
*/
455+
#ifndef WIN32
450456
static int lpcap_inject(lua_State* L)
451457
{
452458
pcap_t* cap = checkpcap(L);
@@ -464,6 +470,7 @@ static int lpcap_inject(lua_State* L)
464470
return 1;
465471
}
466472

473+
#endif
467474

468475
/* Wrap pcap_dumper_t */
469476

@@ -620,8 +627,8 @@ Combine seperate seconds and microseconds into one numeric seconds.
620627
static int lpcap_tv2secs(lua_State* L)
621628
{
622629
struct timeval tv;
623-
tv.tv_sec = luaL_checknumber(L, 1);
624-
tv.tv_usec = luaL_checknumber(L, 2);
630+
tv.tv_sec = (long)luaL_checknumber(L, 1);
631+
tv.tv_usec = (long)luaL_checknumber(L, 2);
625632

626633
lua_pushnumber(L, tv2secs(&tv));
627634
return 1;
@@ -666,10 +673,14 @@ static const luaL_reg pcap_methods[] =
666673
{"set_filter", lpcap_set_filter},
667674
{"datalink", lpcap_datalink},
668675
{"snapshot", lpcap_snapshot},
676+
#ifndef WIN32
669677
{"getfd", lpcap_getfd},
678+
#endif
670679
{"next", lpcap_next},
671680
/* TODO - wt_pcap.c also had a next_nonblocking(), I'm not sure why a setnonblocking() wasn't sufficient */
681+
#ifndef WIN32
672682
{"inject", lpcap_inject},
683+
#endif
673684
{NULL, NULL}
674685
};
675686

@@ -683,7 +694,7 @@ static const luaL_reg dumper_methods[] =
683694
};
684695

685696

686-
LUALIB_API int luaopen_pcap (lua_State *L)
697+
int luaopen_pcap (lua_State *L)
687698
{
688699
v_obj_metatable(L, L_PCAP_DUMPER_REGID, dumper_methods);
689700
v_obj_metatable(L, L_PCAP_REGID, pcap_methods);

0 commit comments

Comments
 (0)