@@ -28,9 +28,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
2828
2929/*-
3030** pcap - a binding to libpcap
31-
32- pcap._LIB_VERSION is the libpcap version string, as returned from pcap_lib_version().
33-
3431*/
3532
3633#include <assert.h>
@@ -146,51 +143,6 @@ static int checkpcapopen(lua_State* L, pcap_t** cap, const char* errbuf)
146143
147144/* Wrap pcap_t */
148145
149- /*-
150- -- cap = pcap.open_live(device, snaplen, promisc, timeout)
151-
152- Open a source device to read packets from.
153-
154- - device is the physical device (defaults to "any")
155- - snaplen is the size to capture, where 0 means max possible (defaults to 0)
156- - promisc is whether to set the device into promiscuous mode (default is false)
157- - timeout is the timeout for reads in seconds (default is 0, return if no packets available)
158-
159- */
160- static int lpcap_open_live (lua_State * L )
161- {
162- const char * device = luaL_optstring (L , 1 , "any" );
163- int snaplen = luaL_optint (L , 2 , 0 );
164- int promisc = lua_toboolean (L , 3 );
165- int to_ms = 1000 * luaL_optint (L , 4 , 0 ); /* convert to milliseconds */
166- pcap_t * * cap = pushpcapopen (L );
167- char errbuf [PCAP_ERRBUF_SIZE ];
168- if (snaplen == 0 )
169- snaplen = 0xffff ;
170- * cap = pcap_open_live (device , snaplen , promisc , to_ms , errbuf );
171- return checkpcapopen (L , cap , errbuf );
172- }
173-
174-
175- /*-
176- -- dumper:close()
177-
178- Manually close a dumper object, freeing it's resources (this will happen on
179- garbage collection if not done explicitly).
180- */
181- static int lpcap_dump_close (lua_State * L )
182- {
183- pcap_dumper_t * * dumper = luaL_checkudata (L , 1 , L_PCAP_DUMPER_REGID );
184-
185- if (* dumper )
186- pcap_dump_close (* dumper );
187-
188- * dumper = NULL ;
189-
190- return 0 ;
191- }
192-
193-
194146/*-
195147-- pcap.DLT = { EN10MB=DLT_EN10MB, [DLT_EN10MB] = "EN10MB", ... }
196148
@@ -247,6 +199,32 @@ static void pcap_make_dlt(lua_State* L)
247199}
248200
249201
202+ /*-
203+ -- cap = pcap.open_live(device, snaplen, promisc, timeout)
204+
205+ Open a source device to read packets from.
206+
207+ - device is the physical device (defaults to "any")
208+ - snaplen is the size to capture, where 0 means max possible (defaults to 0)
209+ - promisc is whether to set the device into promiscuous mode (default is false)
210+ - timeout is the timeout for reads in seconds (default is 0, return if no packets available)
211+
212+ */
213+ static int lpcap_open_live (lua_State * L )
214+ {
215+ const char * device = luaL_optstring (L , 1 , "any" );
216+ int snaplen = luaL_optint (L , 2 , 0 );
217+ int promisc = lua_toboolean (L , 3 );
218+ int to_ms = 1000 * luaL_optint (L , 4 , 0 ); /* convert to milliseconds */
219+ pcap_t * * cap = pushpcapopen (L );
220+ char errbuf [PCAP_ERRBUF_SIZE ];
221+ if (snaplen == 0 )
222+ snaplen = 0xffff ;
223+ * cap = pcap_open_live (device , snaplen , promisc , to_ms , errbuf );
224+ return checkpcapopen (L , cap , errbuf );
225+ }
226+
227+
250228/*-
251229-- cap = pcap.open_dead([linktype, [snaplen]])
252230
@@ -310,37 +288,6 @@ static int lpcap_close (lua_State *L)
310288}
311289
312290
313- /*-
314- -- dumper = cap:dump_open(fname)
315-
316- Open a dump file to write packets to.
317-
318- An fname of "-" is a synonym for stdout.
319-
320- Note that the dumper object is independent of the cap object, once
321- it's created (so the cap object can be closed if its not going to
322- be used).
323- */
324- static int lpcap_dump_open (lua_State * L )
325- {
326- pcap_t * cap = checkpcap (L );
327- const char * fname = luaL_checkstring (L , 2 );
328- pcap_dumper_t * * dumper = lua_newuserdata (L , sizeof (* dumper ));
329-
330- * dumper = NULL ;
331-
332- luaL_getmetatable (L , L_PCAP_DUMPER_REGID );
333- lua_setmetatable (L , -2 );
334-
335- * dumper = pcap_dump_open (cap , fname );
336-
337- if (!* dumper ) {
338- return pusherr (L , cap );
339- }
340-
341- return 1 ;
342- }
343-
344291/* Current libpcap says to use PCAP_NETMASK_UNKNOWN if you don't know the
345292 netmask, older libpcaps says to use 0, so we do one or the other
346293 depending on whether the macro exists.
@@ -529,6 +476,57 @@ static pcap_dumper_t* checkdumper(lua_State* L)
529476 return * dumper ;
530477}
531478
479+ /*-
480+ -- dumper = cap:dump_open(fname)
481+
482+ Open a dump file to write packets to.
483+
484+ An fname of "-" is a synonym for stdout.
485+
486+ Note that the dumper object is independent of the cap object, once
487+ it's created (so the cap object can be closed if its not going to
488+ be used).
489+ */
490+ static int lpcap_dump_open (lua_State * L )
491+ {
492+ pcap_t * cap = checkpcap (L );
493+ const char * fname = luaL_checkstring (L , 2 );
494+ pcap_dumper_t * * dumper = lua_newuserdata (L , sizeof (* dumper ));
495+
496+ * dumper = NULL ;
497+
498+ luaL_getmetatable (L , L_PCAP_DUMPER_REGID );
499+ lua_setmetatable (L , -2 );
500+
501+ * dumper = pcap_dump_open (cap , fname );
502+
503+ if (!* dumper ) {
504+ return pusherr (L , cap );
505+ }
506+
507+ return 1 ;
508+ }
509+
510+
511+ /*-
512+ -- dumper:close()
513+
514+ Manually close a dumper object, freeing it's resources (this will happen on
515+ garbage collection if not done explicitly).
516+ */
517+ static int lpcap_dump_close (lua_State * L )
518+ {
519+ pcap_dumper_t * * dumper = luaL_checkudata (L , 1 , L_PCAP_DUMPER_REGID );
520+
521+ if (* dumper )
522+ pcap_dump_close (* dumper );
523+
524+ * dumper = NULL ;
525+
526+ return 0 ;
527+ }
528+
529+
532530/*-
533531-- dumper = dumper:dump(pkt, [timestamp, [wirelen]])
534532
@@ -645,6 +643,11 @@ static int lpcap_secs2tv(lua_State* L)
645643 return 2 ;
646644}
647645
646+ /*-
647+ -- pcap._LIB_VERSION = ...
648+
649+ The libpcap version string, as returned from pcap_lib_version().
650+ */
648651static const luaL_reg pcap_module [] =
649652{
650653 {"open_live" , lpcap_open_live },
0 commit comments