Skip to content

Commit 368171d

Browse files
committed
Support pcap.DLT, and cap:datalink().
Knowing the datalink is required for parsing the packet data.
1 parent 666ea41 commit 368171d

3 files changed

Lines changed: 97 additions & 11 deletions

File tree

README.txt

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,23 @@ Open a source device to read packets from.
4646

4747

4848

49+
-- pcap.DLT = { EN10MB=DLT_EN10MB, [DLT_EN10MB] = "EN10MB", ... }
50+
51+
DLT is a table of common DLT types. The DLT number and name are mapped to each other.
52+
53+
DLT.EN10MB is Ethernet (of all speeds, the name is historical).
54+
DLT.LINUX_SLL can occur when capturing on Linux with a device of "any".
55+
56+
See <http://www.tcpdump.org/linktypes.html> for more information.
57+
58+
The numeric values are returned by cap:datalink() and accepted as linktype values
59+
in pcap.open_dead().
60+
61+
4962
-- cap = pcap.open_dead([linktype, [caplen]])
5063

51-
linktype is one of the DLT_ numbers, and defaults to 1 ("DLT_EN10MB")
52-
caplen is the maximum size of packet, and defaults to ...
64+
- linktype is one of the DLT numbers, and defaults to pcap.DLT.EN10MB.
65+
- caplen is the maximum size of packet, and defaults to ...
5366

5467
caplen defaults to 0, meaning "no limit" (actually, its changed into
5568
65535 internally, which is what tcpdump does)
@@ -61,7 +74,7 @@ BPF program.
6174

6275
-- cap = pcap.open_offline([fname])
6376

64-
fname defaults to "-", stdin.
77+
- fname defaults to "-", stdin.
6578

6679
Open a savefile to read packets from.
6780

@@ -84,6 +97,14 @@ it's created.
8497
(the default is to optimize).
8598

8699

100+
-- num = cap:datalink()
101+
102+
Interpretation of the packet data requires knowing it's datalink type. This
103+
function returns that as a number.
104+
105+
See pcap.DLT for more information.
106+
107+
87108
-- capdata, timestamp, wirelen = cap:next()
88109

89110
Example:

pcap-test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,14 @@ assertnext(cap, eth, nil, #eth)
8585
assertnext(cap, "pcap closed", nil, #"pcap closed")
8686
assertnext(cap, nil, nil, nil)
8787

88+
print"dlt table test"
89+
90+
assert(pcap.DLT)
91+
assert(pcap.DLT.EN10MB == 1)
92+
assert(pcap.DLT.LINUX_SLL == 113)
93+
assert(pcap.DLT[1] == "EN10MB")
94+
assert(pcap.DLT[113] == "LINUX_SLL")
95+
8896
print"+ok"
8997

98+

pcap.c

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
181209
caplen defaults to 0, meaning "no limit" (actually, its changed into
182210
65535 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
185213
file. It can be used with cap:dump_open() to write a pcap file, or to compile a
186214
BPF 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-
*/
193216
static 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
214237
Open 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

Comments
 (0)