Skip to content

Add support for opening 802.11 devices in monitor mode #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,44 @@ static int lpcap_open_live(lua_State *L)
int snaplen = luaL_optint(L, 2, 0);
int promisc = lua_toboolean(L, 3);
int to_ms = 1000 * luaL_optint(L, 4, 0); /* convert to milliseconds */
int monitor = lua_toboolean(L, 5);
pcap_t** cap = pushpcapopen(L);
char errbuf[PCAP_ERRBUF_SIZE];
if(snaplen == 0)
snaplen = 0xffff;
*cap = pcap_open_live(device, snaplen, promisc, to_ms, errbuf);
pcap_set_rfmon(*cap, monitor);
return checkpcapopen(L, cap, errbuf);
}

/*-
-- cap = pcap.open_monitor(device, snaplen, timeout)

Open a 802.11 source device to read packets in monitor mode.

- device is the physical device (defaults to "any")
- snaplen is the size to capture, where 0 means max possible (defaults to 0)
- timeout is the timeout for reads in seconds (default is 0, return if no packets available)

*/
static int lpcap_open_monitor(lua_State *L)
{
const char *device = luaL_optstring(L, 1, "any");
int snaplen = luaL_optint(L, 2, 0);
int to_ms = 1000 * luaL_optint(L, 3, 0); /* convert to milliseconds */

char errbuf[PCAP_ERRBUF_SIZE];
if(snaplen == 0)
snaplen = 0xffff;

pcap_t** cap = pushpcapopen(L);
*cap = pcap_create(device, errbuf);
pcap_set_rfmon(*cap, 1);
pcap_set_snaplen(*cap, snaplen);
pcap_set_promisc(*cap, 0);
pcap_set_timeout(*cap, to_ms);
pcap_activate(*cap);

return checkpcapopen(L, cap, errbuf);
}

Expand Down Expand Up @@ -658,6 +691,7 @@ The libpcap version string, as returned from pcap_lib_version().
static const luaL_reg pcap_module[] =
{
{"open_live", lpcap_open_live},
{"open_monitor", lpcap_open_monitor},
{"open_offline", lpcap_open_offline},
{"open_dead", lpcap_open_dead},
{"tv2secs", lpcap_tv2secs},
Expand Down