Permalink
Browse files

bpf: retrieve and decode datalink type

  • Loading branch information...
msantos committed Apr 20, 2011
1 parent e8008bd commit 687f8aa8af2ed2f515c6710ef97f79ee779d981f
Showing with 115 additions and 5 deletions.
  1. +29 −1 include/bpf.hrl
  2. +86 −4 src/bpf.erl
View
@@ -71,7 +71,7 @@
%% } bfl_u;
%% };
-define(SIZEOF_STRUCT_BPF_DLTLIST,
- ?SIZEOF_U_INT + ?SIZEOF_U_INT + 8).
+ ?SIZEOF_U_INT + ?SIZEOF_U_INT + 4).
-define(BPF_ALIGNMENT, ?SIZEOF_INT32_T).
@@ -103,6 +103,34 @@
-define(BIOCSDLT, bpf:iow($B, 120, ?SIZEOF_U_INT)).
-define(BIOCGDLTLIST, bpf:iowr($B, 121, ?SIZEOF_STRUCT_BPF_DLTLIST)).
+-define(DLT_NULL, 0).
+-define(DLT_EN10MB, 1).
+-define(DLT_EN3MB, 2).
+-define(DLT_AX25, 3).
+-define(DLT_PRONET, 4).
+-define(DLT_CHAOS, 5).
+-define(DLT_IEEE802, 6).
+-define(DLT_ARCNET, 7).
+-define(DLT_SLIP, 8).
+-define(DLT_PPP, 9).
+-define(DLT_FDDI, 10).
+-define(DLT_ATM_RFC1483, 11).
+-define(DLT_RAW, 12).
+-define(DLT_SLIP_BSDOS, 15).
+-define(DLT_PPP_BSDOS, 16).
+-define(DLT_PFSYNC, 18).
+-define(DLT_ATM_CLIP, 19).
+-define(DLT_PPP_SERIAL, 50).
+-define(DLT_C_HDLC, 104).
+-define(DLT_CHDLC, ?DLT_C_HDLC).
+-define(DLT_IEEE802_11, 105).
+-define(DLT_LOOP, 108).
+-define(DLT_LINUX_SLL, 113).
+-define(DLT_PFLOG, 117).
+-define(DLT_IEEE802_11_RADIO, 127).
+-define(DLT_APPLE_IP_OVER_IEEE1394, 138).
+-define(DLT_IEEE802_11_RADIO_AVS, 163).
+
%%-------------------------------------------------------------------------
%%% BPF Filter
View
@@ -49,7 +49,8 @@
-export([
pad/1, align/1,
io/2, iow/3, ior/3, iowr/3, ioc/4,
- sizeof/1
+ sizeof/1,
+ dlt/1
]).
@@ -82,8 +83,20 @@ ctl(Socket, dlt) ->
end;
% struct bpf_dtlist
-%ctl(Socket, dltlist) ->
-% procket:ioctl(Socket, ?BIOCGDLTLIST, <<1:32/native>>);
+ctl(Socket, dltlist) ->
+ {ok, DLTs, [Res]} = procket:alloc([
+ <<32:4/native-unsigned-integer-unit:8>>,
+ {ptr, 32*4},
+ <<0:32>> % pad, needed?
+ ]),
+ case procket:ioctl(Socket, ?BIOCGDLTLIST, DLTs) of
+ {ok, _} ->
+ {ok, List} = procket:buf(Res),
+ Endian = erlang:system_info(endian),
+ {ok, [ binary:decode_unsigned(<<N:32>>, Endian) || <<N:32>> <= List, N /= 0 ]};
+ Error ->
+ Error
+ end;
ctl(Socket, flush) ->
procket:ioctl(Socket, ?BIOCFLUSH, <<0:32/native>>);
@@ -97,6 +110,14 @@ ctl(Socket, getif) ->
Error
end;
+ctl(Socket, hdrcmplt) ->
+ case procket:ioctl(Socket, ?BIOCGHDRCMPLT, <<1:32/native>>) of
+ {ok, Bool} ->
+ {ok, bool(Bool)};
+ Error ->
+ Error
+ end;
+
%ctl(Socket, timeout) ->
% Size = sizeof(timeval),
% procket:ioctl(Socket, ?BIOCGRTIMEOUT, <<0:Size/bytes>>);
@@ -163,7 +184,10 @@ ctl(Socket, setf, Insn) when is_list(Insn) ->
bool(true) -> <<1:32/native>>;
-bool(false) -> <<0:32>>.
+bool(false) -> <<0:32>>;
+
+bool(<<1:32/native>>) -> true;
+bool(<<0:32>>) -> false.
%% struct bpf_hdr {
%% struct BPF_TIMEVAL bh_tstamp; /* time stamp */
@@ -282,6 +306,64 @@ iowr(G,N,T) ->
sizeof(timeval) ->
erlang:system_info({wordsize, external}) + ?SIZEOF_U_INT.
+
+dlt(?DLT_NULL) -> null;
+dlt(?DLT_EN10MB) -> en10mb;
+dlt(?DLT_EN3MB) -> en3mb;
+dlt(?DLT_AX25) -> ax25;
+dlt(?DLT_PRONET) -> pronet;
+dlt(?DLT_CHAOS) -> chaos;
+dlt(?DLT_IEEE802) -> ieee802;
+dlt(?DLT_ARCNET) -> arcnet;
+dlt(?DLT_SLIP) -> slip;
+dlt(?DLT_PPP) -> ppp;
+dlt(?DLT_FDDI) -> fddi;
+dlt(?DLT_ATM_RFC1483) -> atm_rfc1483;
+dlt(?DLT_RAW) -> raw;
+dlt(?DLT_SLIP_BSDOS) -> slip_bsdos;
+dlt(?DLT_PPP_BSDOS) -> ppp_bsdos;
+dlt(?DLT_PFSYNC) -> pfsync;
+dlt(?DLT_ATM_CLIP) -> atm_clip;
+dlt(?DLT_PPP_SERIAL) -> ppp_serial;
+dlt(?DLT_C_HDLC) -> c_hdlc;
+dlt(?DLT_CHDLC) -> chdlc;
+dlt(?DLT_IEEE802_11) -> ieee802_11;
+dlt(?DLT_LOOP) -> loop;
+dlt(?DLT_LINUX_SLL) -> linux_sll;
+dlt(?DLT_PFLOG) -> pflog;
+dlt(?DLT_IEEE802_11_RADIO) -> ieee802_11_radio;
+dlt(?DLT_APPLE_IP_OVER_IEEE1394) -> apple_ip_over_ieee1394;
+dlt(?DLT_IEEE802_11_RADIO_AVS) -> ieee802_11_radio_avs;
+
+dlt(null) -> ?DLT_NULL;
+dlt(en10mb) -> ?DLT_EN10MB;
+dlt(en3mb) -> ?DLT_EN3MB;
+dlt(ax25) -> ?DLT_AX25;
+dlt(pronet) -> ?DLT_PRONET;
+dlt(chaos) -> ?DLT_CHAOS;
+dlt(ieee802) -> ?DLT_IEEE802;
+dlt(arcnet) -> ?DLT_ARCNET;
+dlt(slip) -> ?DLT_SLIP;
+dlt(ppp) -> ?DLT_PPP;
+dlt(fddi) -> ?DLT_FDDI;
+dlt(atm_rfc1483) -> ?DLT_ATM_RFC1483;
+dlt(raw) -> ?DLT_RAW;
+dlt(slip_bsdos) -> ?DLT_SLIP_BSDOS;
+dlt(ppp_bsdos) -> ?DLT_PPP_BSDOS;
+dlt(pfsync) -> ?DLT_PFSYNC;
+dlt(atm_clip) -> ?DLT_ATM_CLIP;
+dlt(ppp_serial) -> ?DLT_PPP_SERIAL;
+dlt(c_hdlc) -> ?DLT_C_HDLC;
+dlt(chdlc) -> ?DLT_CHDLC;
+dlt(ieee802_11) -> ?DLT_IEEE802_11;
+dlt(loop) -> ?DLT_LOOP;
+dlt(linux_sll) -> ?DLT_LINUX_SLL;
+dlt(pflog) -> ?DLT_PFLOG;
+dlt(ieee802_11_radio) -> ?DLT_IEEE802_11_RADIO;
+dlt(apple_ip_over_ieee1394) -> ?DLT_APPLE_IP_OVER_IEEE1394;
+dlt(ieee802_22_radio_avs) -> ?DLT_IEEE802_11_RADIO_AVS.
+
+
init(Socket, Dev) ->
% Set the interface for the bpf
{ok, _} = ctl(Socket, setif, Dev),

0 comments on commit 687f8aa

Please sign in to comment.