Skip to content

Commit abc04f3

Browse files
committed
Clarify handling and meaning of snapshot length.
Add cap:snapshot(), which returns the snapshot length, and expanded docs on the meaning of the snapshot length.
1 parent cb35f89 commit abc04f3

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

pcap.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,11 @@ static void pcap_dlt_set(lua_State* L, const char* name, int number)
220220
}
221221

222222
/*-
223-
-- cap = pcap.open_dead([linktype, [caplen]])
223+
-- cap = pcap.open_dead([linktype, [snaplen]])
224224
225225
- linktype is one of the DLT numbers, and defaults to pcap.DLT.EN10MB.
226-
- caplen is the maximum size of packet, and defaults to ...
227-
228-
caplen defaults to 0, meaning "no limit" (actually, its changed into
229-
65535 internally, which is what tcpdump does)
226+
- snaplen is the maximum size of packet, and defaults to 65535 (also,
227+
a value of 0 is changed into 65535 internally, as tcpdump does).
230228
231229
Open a pcap that doesn't read from either a live interface, or an offline pcap
232230
file. It can be used with cap:dump_open() to write a pcap file, or to compile a
@@ -293,7 +291,7 @@ static int lpcap_close (lua_State *L)
293291
fname defaults to "-", stdout.
294292
295293
Note that the dumper object is independent of the cap object, once
296-
it's created.
294+
it's created (the cap object can be closed).
297295
*/
298296
static int lpcap_dump_open(lua_State *L)
299297
{
@@ -372,6 +370,22 @@ static int lpcap_datalink(lua_State* L)
372370
return 1;
373371
}
374372

373+
/*-
374+
-- snaplen = cap:snapshot()
375+
376+
The snapshot length.
377+
378+
For a live capture, snapshot is the maximum amount of the packet that will be
379+
captured, for writing of captures, it is the maximum size of a packet that can
380+
be written.
381+
*/
382+
static int lpcap_snapshot(lua_State* L)
383+
{
384+
pcap_t* cap = checkpcap(L);
385+
lua_pushnumber(L, pcap_snapshot(cap));
386+
return 1;
387+
}
388+
375389
/*-
376390
-- fd = cap:getfd()
377391
@@ -490,10 +504,18 @@ static pcap_dumper_t* checkdumper(lua_State* L)
490504
/*-
491505
-- dumper = dumper:dump(pkt, [timestamp, [wirelen]])
492506
493-
pkt to dump
507+
pkt is the packet to write to the dumpfile.
494508
495-
timestamp of packet, defaults to 0, meaning the current time
496-
wire length of packet, defaults to pkt's length
509+
timestamp of packet, defaults to 0, meaning the current time.
510+
511+
wirelen was the original length of the packet before being truncated to header
512+
(defaults to length of header, the correct value if it was not truncated).
513+
514+
If only the header of the packet is available, wirelen should be set to the
515+
original packet length before it was truncated. Also, be very careful to not
516+
write a header that is longer than the caplen (which will 65535 unless a
517+
different value was specified in open_live or open_dead), the pcap file
518+
will not be valid.
497519
498520
Returns self on sucess.
499521
Returns nil and an error msg on failure.
@@ -502,6 +524,7 @@ Note that arguments are compatible with cap:next(), and that since
502524
pcap_dump() doesn't return error indicators only the failure
503525
values from cap:next() will ever be returned.
504526
*/
527+
/* TODO store the snaplen in dumper's environment, so we can check it here */
505528
static int lpcap_dump(lua_State* L)
506529
{
507530
pcap_dumper_t* dumper = checkdumper(L);
@@ -521,6 +544,8 @@ static int lpcap_dump(lua_State* L)
521544
opttimeval(L, 3, &hdr.ts);
522545
wirelen = luaL_optint(L, 4, caplen);
523546

547+
luaL_argcheck(L, wirelen >= caplen, 4, "original wirelen cannot be less than current pkt length");
548+
524549
hdr.caplen = caplen;
525550
hdr.len = wirelen;
526551

@@ -609,6 +634,7 @@ static const luaL_reg pcap_methods[] =
609634
{"dump_open", lpcap_dump_open},
610635
{"set_filter", lpcap_set_filter},
611636
{"datalink", lpcap_datalink},
637+
{"snapshot", lpcap_snapshot},
612638
{"getfd", lpcap_getfd},
613639
{"next", lpcap_next},
614640
/* TODO - wt_pcap.c also had a next_nonblocking(), I'm not sure why a setnonblocking() wasn't sufficient */

0 commit comments

Comments
 (0)