Skip to content

Commit

Permalink
Test for sniffing packets
Browse files Browse the repository at this point in the history
Export pcap_lookup().
  • Loading branch information
msantos committed Aug 23, 2012
1 parent ac8b281 commit 9019c01
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -138,6 +138,10 @@ SMP erlang must be enabled (erl -smp -pa ebin).

Write the packet to the network. See pcap_sendpacket(3PCAP).

dev() -> {ok, string()} | {error, pcap_error_string()}

Returns the default device used by PCAP.

getifaddrs() -> {ok, Iflist} | {error, posix()}

Types Iflist = [{Ifname, [Ifopt]}]
Expand Down
5 changes: 4 additions & 1 deletion src/ewpcap.erl
Expand Up @@ -37,7 +37,7 @@
filter/2, filter/3,
read/1, read/2,
write/2,
getifaddrs/0
getifaddrs/0, dev/0
]).

-define(PCAP_NETMASK_UNKNOWN, 16#ffffffff).
Expand Down Expand Up @@ -148,6 +148,9 @@ read(#ewpcap_resource{ref = Ref}, Timeout) ->
write(#ewpcap_resource{res = Res}, Data) when is_list(Data); is_binary(Data) ->
pcap_sendpacket(Res, Data).

dev() ->
pcap_lookupdev().

getifaddrs() ->
case pcap_findalldevs() of
{ok, Iflist} ->
Expand Down
23 changes: 23 additions & 0 deletions test/ewpcap_tests.erl
Expand Up @@ -35,6 +35,29 @@
-include_lib("eunit/include/eunit.hrl").
-include("ewpcap.hrl").

open_test() ->
case ewpcap:getifaddrs() of
{ok, []} ->
error_logger:info_report([
{skipping, "beam does not have privileges to run test"}
]),
ok;
{ok, _Iflist} ->
open_test_1()
end.

open_test_1() ->
{ok, Ifname} = ewpcap:dev(),
{ok, Socket} = ewpcap:open(Ifname, [{filter, "tcp and port 29"}]),

{error, eagain} = ewpcap:read(Socket, 100),
gen_tcp:connect({8,8,8,8}, 29, [binary], 100),
{ok, Packet} = ewpcap:read(Socket),

error_logger:info_report([{got, Packet}]),

ok.

getifaddrs_test() ->
case os:type() of
{unix, _} ->
Expand Down

0 comments on commit 9019c01

Please sign in to comment.