@@ -172,11 +172,39 @@ static int lpcap_open_live(lua_State *L)
172172}
173173
174174
175+ /*-
176+ -- pcap.DLT = { EN10MB=DLT_EN10MB, [DLT_EN10MB] = "EN10MB", ... }
177+
178+ DLT is a table of common DLT types. The DLT number and name are mapped to each other.
179+
180+ DLT.EN10MB is Ethernet (of all speeds, the name is historical).
181+ DLT.LINUX_SLL can occur when capturing on Linux with a device of "any".
182+
183+ See <http://www.tcpdump.org/linktypes.html> for more information.
184+
185+ The numeric values are returned by cap:datalink() and accepted as linktype values
186+ in pcap.open_dead().
187+ */
188+ /* In the table at the top of the stack, dlt, do:
189+ * dlt[name] = number
190+ * dlt[number] = name
191+ */
192+ static void pcap_dlt_set (lua_State * L , const char * name , int number )
193+ {
194+ lua_pushstring (L , name );
195+ lua_pushinteger (L , number );
196+ lua_settable (L , -3 );
197+
198+ lua_pushinteger (L , number );
199+ lua_pushstring (L , name );
200+ lua_settable (L , -3 );
201+ }
202+
175203/*-
176204-- cap = pcap.open_dead([linktype, [caplen]])
177205
178- linktype is one of the DLT_ numbers, and defaults to 1 ("DLT_EN10MB")
179- caplen is the maximum size of packet, and defaults to ...
206+ - linktype is one of the DLT numbers, and defaults to pcap.DLT.EN10MB.
207+ - caplen is the maximum size of packet, and defaults to ...
180208
181209caplen defaults to 0, meaning "no limit" (actually, its changed into
18221065535 internally, which is what tcpdump does)
@@ -185,11 +213,6 @@ Open a pcap that doesn't read from either a live interface, or an offline pcap
185213file. It can be used with cap:dump_open() to write a pcap file, or to compile a
186214BPF program.
187215*/
188- /*
189- TODO should accept strings as the link type, or have a table of the link
190- types:
191- pcap.DLT = { NULL = 0, EN10MB = 1, ... }
192- */
193216static int lpcap_open_dead (lua_State * L )
194217{
195218 int linktype = luaL_optint (L , 1 , DLT_EN10MB );
@@ -209,7 +232,7 @@ static int lpcap_open_dead(lua_State *L)
209232/*-
210233-- cap = pcap.open_offline([fname])
211234
212- fname defaults to "-", stdin.
235+ - fname defaults to "-", stdin.
213236
214237Open a savefile to read packets from.
215238
@@ -290,6 +313,22 @@ static int lpcap_set_filter(lua_State* L)
290313 return 1 ;
291314}
292315
316+ /*-
317+ -- num = cap:datalink()
318+
319+ Interpretation of the packet data requires knowing it's datalink type. This
320+ function returns that as a number.
321+
322+ See pcap.DLT for more information.
323+ */
324+ static int lpcap_datalink (lua_State * L )
325+ {
326+ pcap_t * cap = checkpcap (L );
327+ lua_pushnumber (L , pcap_datalink (cap ));
328+
329+ return 1 ;
330+ }
331+
293332
294333/*-
295334-- capdata, timestamp, wirelen = cap:next()
@@ -515,6 +554,7 @@ static const luaL_reg pcap_methods[] =
515554{
516555 {"dump_open" , lpcap_dump_open },
517556 {"set_filter" , lpcap_set_filter },
557+ {"datalink" , lpcap_datalink },
518558 {"next" , lpcap_next },
519559 {"__gc" , lpcap_destroy },
520560 {"close" , lpcap_destroy },
@@ -538,6 +578,22 @@ LUALIB_API int luaopen_pcap (lua_State *L)
538578 luaL_register (L , "pcap" , pcap_module );
539579 lua_pushstring (L , pcap_lib_version ());
540580 lua_setfield (L , -2 , "_LIB_VERSION" );
581+
582+ /* Create DLT table */
583+ /* TODO - add all the DLT values... */
584+ lua_newtable (L );
585+ #ifdef DLT_EN10MB
586+ pcap_dlt_set (L , "EN10MB" , DLT_EN10MB );
587+ #endif
588+ #ifdef DLT_RAW
589+ pcap_dlt_set (L , "RAW" , DLT_RAW );
590+ #endif
591+ #ifdef DLT_LINUX_SLL
592+ pcap_dlt_set (L , "LINUX_SLL" , DLT_LINUX_SLL );
593+ #endif
594+
595+ lua_setfield (L , -2 , "DLT" );
596+
541597 return 1 ;
542598}
543599
0 commit comments