Permalink
Browse files

Clean up examples, README

  • Loading branch information...
1 parent 1a5cca4 commit f168c08477188fffab4266e3cfb67d32743fb18f @msantos committed Aug 5, 2012
Showing with 92 additions and 15 deletions.
  1. +6 −9 README.md
  2. +71 −0 examples/bpf_ex.erl
  3. +7 −2 examples/lsf.erl
  4. +8 −4 examples/lsf_inet.erl
View
@@ -73,9 +73,9 @@ These libraries are not required but can be used with epcap\_compile:
* datalinktype set to ethernet (DLT_EN10MB)
- * a packet length of 65535 byte
+ * a packet length of 65535 bytes
- See pcap_compile(7) for information about each of these options.
+ See pcap_compile(3PCAP) for information about each of these options.
## EXAMPLES
@@ -176,12 +176,12 @@ The same BPF program can be generated from Erlang by using the bpf module in pro
-module(bpf_ex).
- -export([f/0, f/1]).
+ -export([f/1, f/2]).
- f() ->
- f("ip and ( src host 192.168.10.1 or dst host 192.168.10.1 )").
+ f(Dev) ->
+ f(Dev, "ip and ( src host 192.168.10.1 or dst host 192.168.10.1 )").
- f(Filter) ->
+ f(Dev, Filter) ->
{ok, Socket, Length} = bpf:open(Dev),
{ok, Fcode} = epcap_compile:compile(Filter),
{ok, _} = bpf:ctl(Socket, setf, Fcode),
@@ -205,6 +205,3 @@ The same BPF program can be generated from Erlang by using the bpf module in pro
timer:sleep(10),
loop(Socket, Length)
end.
-
-
-## TODO
View
@@ -0,0 +1,71 @@
+%% Copyright (c) 2012, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(bpf_ex).
+-export([f/0, f/1, f/2]).
+
+f() ->
+ f("en1").
+
+f(Dev) ->
+ case os:type() of
+ {unix, BSD} when BSD == darwin;
+ BSD == freebsd;
+ BSD == netbsd;
+ BSD == openbsd ->
+ f(Dev, "ip and ( src host 192.168.10.1 or dst host 192.168.10.1 )");
+ _ ->
+ {error, unsupported}
+ end.
+
+f(Dev, Filter) ->
+ {ok, Socket, Length} = bpf:open(Dev),
+ {ok, Fcode} = epcap_compile:compile(Filter),
+ {ok, _} = bpf:ctl(Socket, setf, Fcode),
+ loop(Socket, Length).
+
+loop(Socket, Length) ->
+ case procket:read(Socket, Length) of
+ {ok, <<>>} ->
+ loop(Socket, Length);
+ {ok, Data} ->
+ {bpf_buf, Time, Datalen, Packet, Rest} = bpf:buf(Data),
+ error_logger:info_report([
+ {time, Time},
+ {packet_is_truncated, Datalen /= byte_size(Packet)},
+ {packet, Packet},
+ {packet_size, byte_size(Packet)},
+ {remaining, byte_size(Rest)}
+ ]),
+ loop(Socket, Length);
+ {error, eagain} ->
+ timer:sleep(10),
+ loop(Socket, Length)
+ end.
View
@@ -32,8 +32,13 @@
-export([f/0, f/1]).
f() ->
- {ok, Fcode} = epcap_compile:compile("tcp and ( port 80 or port 443 )"),
- f(Fcode).
+ case os:type() of
+ {unix, linux} ->
+ {ok, Fcode} = epcap_compile:compile("tcp and ( port 80 or port 443 )"),
+ f(Fcode);
+ _ ->
+ {error, unsupported}
+ end.
f(Fcode) when is_list(Fcode) ->
{ok, S} = packet:socket(),
View
@@ -32,10 +32,14 @@
-export([f/0]).
f() ->
- {ok, Fcode} = epcap_compile:compile("tcp and port 443"),
- unfiltered(Fcode),
- filtered(Fcode).
-
+ case os:type() of
+ {unix, linux} ->
+ {ok, Fcode} = epcap_compile:compile("tcp and port 443"),
+ unfiltered(Fcode),
+ filtered(Fcode);
+ _ ->
+ {error, unsupported}
+ end.
unfiltered(Fcode) when is_list(Fcode) ->
{ok, S} = gen_tcp:connect("www.google.com", 80, [

0 comments on commit f168c08

Please sign in to comment.