Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Test for sniffing packets

Export pcap_lookup().
  • Loading branch information...
commit 9019c01fc9c89ed1a299adbbe373fe80a795a2f5 1 parent ac8b281
@msantos authored
Showing with 31 additions and 1 deletion.
  1. +4 −0 README.md
  2. +4 −1 src/ewpcap.erl
  3. +23 −0 test/ewpcap_tests.erl
View
4 README.md
@@ -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]}]
View
5 src/ewpcap.erl
@@ -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).
@@ -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} ->
View
23 test/ewpcap_tests.erl
@@ -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, _} ->
Please sign in to comment.
Something went wrong with that request. Please try again.