Skip to content

Commit 2799018

Browse files
committed
Reordered functions into more logical groups.
1 parent 8ebba26 commit 2799018

File tree

2 files changed

+140
-120
lines changed

2 files changed

+140
-120
lines changed

README.txt

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,18 @@ See below, extracted from in-source comments.
3131

3232
** pcap - a binding to libpcap
3333

34-
pcap._LIB_VERSION is the libpcap version string, as returned from pcap_lib_version().
3534

35+
-- pcap.DLT = { EN10MB=DLT_EN10MB, [DLT_EN10MB] = "EN10MB", ... }
36+
37+
DLT is a table of common DLT types. The DLT number and name are mapped to each other.
38+
39+
DLT.EN10MB is Ethernet (of all speeds, the name is historical).
40+
DLT.LINUX_SLL can occur when capturing on Linux with a device of "any".
41+
42+
See <http://www.tcpdump.org/linktypes.html> for more information.
43+
44+
The numeric values are returned by cap:datalink() and accepted as linktype values
45+
in pcap.open_dead().
3646

3747

3848
-- cap = pcap.open_live(device, snaplen, promisc, timeout)
@@ -50,48 +60,24 @@ Open a source device to read packets from.
5060

5161

5262

53-
-- dumper:close()
54-
55-
Manually close a dumper object, freeing it's resources (this will happen on
56-
garbage collection if not done explicitly).
57-
58-
59-
-- pcap.DLT = { EN10MB=DLT_EN10MB, [DLT_EN10MB] = "EN10MB", ... }
60-
61-
DLT is a table of common DLT types. The DLT number and name are mapped to each other.
62-
63-
DLT.EN10MB is Ethernet (of all speeds, the name is historical).
64-
DLT.LINUX_SLL can occur when capturing on Linux with a device of "any".
65-
66-
See <http://www.tcpdump.org/linktypes.html> for more information.
67-
68-
The numeric values are returned by cap:datalink() and accepted as linktype values
69-
in pcap.open_dead().
70-
71-
72-
-- cap = pcap.open_dead([linktype, [caplen]])
63+
-- cap = pcap.open_dead([linktype, [snaplen]])
7364

7465

7566
- linktype is one of the DLT numbers, and defaults to pcap.DLT.EN10MB.
7667

77-
- caplen is the maximum size of packet, and defaults to ...
78-
79-
caplen defaults to 0, meaning "no limit" (actually, its changed into
80-
65535 internally, which is what tcpdump does)
68+
- snaplen is the maximum size of packet, and defaults to 65535 (also,
69+
a value of 0 is changed into 65535 internally, as tcpdump does).
8170

8271
Open a pcap that doesn't read from either a live interface, or an offline pcap
8372
file. It can be used with cap:dump_open() to write a pcap file, or to compile a
8473
BPF program.
8574

8675

87-
-- cap = pcap.open_offline([fname])
88-
89-
fname defaults to "-", stdin.
76+
-- cap = pcap.open_offline(fname)
9077

9178
Open a savefile to read packets from.
9279

93-
Warning, fname defaulting to stdin causes unsuspecting users to
94-
think this API is hanging, when they don't actually have a pcap on stdin.
80+
An fname of "-" is a synonym for stdin.
9581

9682

9783
-- cap:close()
@@ -100,14 +86,6 @@ Manually close a cap object, freeing it's resources (this will happen on
10086
garbage collection if not done explicitly).
10187

10288

103-
-- dumper = cap:dump_open([fname])
104-
105-
fname defaults to "-", stdout.
106-
107-
Note that the dumper object is independent of the cap object, once
108-
it's created.
109-
110-
11189
-- cap = cap:set_filter(filter, nooptimize)
11290

11391

@@ -125,6 +103,15 @@ function returns that as a number.
125103
See pcap.DLT for more information.
126104

127105

106+
-- snaplen = cap:snapshot()
107+
108+
The snapshot length.
109+
110+
For a live capture, snapshot is the maximum amount of the packet that will be
111+
captured, for writing of captures, it is the maximum size of a packet that can
112+
be written.
113+
114+
128115
-- fd = cap:getfd()
129116

130117
Get a selectable file descriptor number which can be used to wait for packets.
@@ -167,12 +154,37 @@ Injects packet.
167154
Return is bytes sent on success, or nil,emsg on failure.
168155

169156

157+
-- dumper = cap:dump_open(fname)
158+
159+
Open a dump file to write packets to.
160+
161+
An fname of "-" is a synonym for stdout.
162+
163+
Note that the dumper object is independent of the cap object, once
164+
it's created (so the cap object can be closed if its not going to
165+
be used).
166+
167+
168+
-- dumper:close()
169+
170+
Manually close a dumper object, freeing it's resources (this will happen on
171+
garbage collection if not done explicitly).
172+
173+
170174
-- dumper = dumper:dump(pkt, [timestamp, [wirelen]])
171175

172-
pkt to dump
176+
pkt is the packet to write to the dumpfile.
177+
178+
timestamp of packet, defaults to 0, meaning the current time.
179+
180+
wirelen was the original length of the packet before being truncated to header
181+
(defaults to length of header, the correct value if it was not truncated).
173182

174-
timestamp of packet, defaults to 0, meaning the current time
175-
wire length of packet, defaults to pkt's length
183+
If only the header of the packet is available, wirelen should be set to the
184+
original packet length before it was truncated. Also, be very careful to not
185+
write a header that is longer than the caplen (which will 65535 unless a
186+
different value was specified in open_live or open_dead), the pcap file
187+
will not be valid.
176188

177189
Returns self on sucess.
178190
Returns nil and an error msg on failure.
@@ -198,3 +210,8 @@ Combine seperate seconds and microseconds into one numeric seconds.
198210
-- seci, useci = pcap.secs2tv(secs)
199211

200212
Split one numeric seconds into seperate seconds and microseconds.
213+
214+
215+
-- pcap._LIB_VERSION = ...
216+
217+
The libpcap version string, as returned from pcap_lib_version().

pcap.c

Lines changed: 82 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
*/
648651
static const luaL_reg pcap_module[] =
649652
{
650653
{"open_live", lpcap_open_live},

0 commit comments

Comments
 (0)