Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

make bpf work on NetBSD

  • Loading branch information...
commit 1d04b94d365f4a7268d1d271c5305e5a01092e94 1 parent 76eeb83
@yamt yamt authored
Showing with 46 additions and 8 deletions.
  1. +4 −4 include/bpf.hrl
  2. +42 −4 src/bpf.erl
View
8 include/bpf.hrl
@@ -33,7 +33,7 @@
%%% BPF ioctl structures and commands
%%-------------------------------------------------------------------------
--define(SIZEOF_STRUCT_IFREQ, 32).
+-define(SIZEOF_STRUCT_IFREQ, bpf:sizeof(ifreq)).
-define(SIZEOF_INT32_T, 4).
-define(SIZEOF_U_INT, ?SIZEOF_INT32_T).
@@ -75,7 +75,7 @@
(?SIZEOF_U_INT + ?SIZEOF_U_INT + 4)).
%% #define BPF_ALIGNMENT sizeof(int32_t)
--define(BPF_ALIGNMENT, ?SIZEOF_INT32_T).
+-define(BPF_ALIGNMENT, bpf:alignment()).
%% #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
-define(BPF_WORDALIGN(X), bpf:align(X)).
@@ -96,8 +96,8 @@
-define(BIOCSRSIG, bpf:iow($B, 115, ?SIZEOF_U_INT)).
-define(BIOCGHDRCMPLT, bpf:ior($B, 116, ?SIZEOF_U_INT)).
-define(BIOCSHDRCMPLT, bpf:iow($B, 117, ?SIZEOF_U_INT)).
--define(BIOCGSEESENT, bpf:ior($B, 118, ?SIZEOF_U_INT)).
--define(BIOCSSEESENT, bpf:iow($B, 119, ?SIZEOF_U_INT)).
+-define(BIOCGSEESENT, bpf:ior($B, gseesent, ?SIZEOF_U_INT)).
+-define(BIOCSSEESENT, bpf:iow($B, sseesent, ?SIZEOF_U_INT)).
-define(BIOCSDLT, bpf:iow($B, 120, ?SIZEOF_U_INT)).
-define(BIOCGDLTLIST, bpf:iowr($B, 121, ?SIZEOF_STRUCT_BPF_DLTLIST)).
View
46 src/bpf.erl
@@ -48,7 +48,7 @@
-export([
pad/1, align/1,
io/2, iow/3, ior/3, iowr/3, ioc/4,
- sizeof/1
+ sizeof/1, alignment/0
]).
@@ -229,6 +229,15 @@ bool(<<0:32>>) -> false.
%%
%% On 32-bit, struct timeval32: 4 bytes tv_sec, 4 bytes tv_usec
%% On 64-bit, struct timeval: 8 bytes tv_sec, 4 bytes tv_usec
+%%
+%% On NetBSD, the bh_tstamp is like the following.
+%% struct bpf_timeval {
+%% long tv_sec;
+%% long tv_usec;
+%% };
+%% So,
+%% On 32-bit, struct bpf_timeval: 4 bytes tv_sec, 4 bytes tv_usec
+%% On 64-bit, struct bpf_timeval: 8 bytes tv_sec, 8 bytes tv_usec
pad(Len) ->
align(Len) - Len.
@@ -253,10 +262,15 @@ buf_2(_Time, _Datalen, Error) ->
hdr(Data) ->
Size = erlang:system_info({wordsize, external}),
-
+ UsecSize = case os:type() of
+ {_, netbsd} ->
+ Size;
+ _ -> % OSX
+ 4
+ end,
case Data of
<<Sec:Size/native-unsigned-integer-unit:8,
- Usec:4/native-unsigned-integer-unit:8,
+ Usec:UsecSize/native-unsigned-integer-unit:8,
Caplen:4/native-unsigned-integer-unit:8,
Datalen:4/native-unsigned-integer-unit:8,
Hdrlen:2/native-unsigned-integer-unit:8,
@@ -339,6 +353,16 @@ offset(?BPF_B) -> byte.
%%% Internal functions
%%-------------------------------------------------------------------------
%% BSD ioctl request calculation (taken from ioccom.h)
+ioc(Inout, Group, Name, Len) when is_atom(Name) ->
+ List = case os:type() of
+ {_, netbsd} ->
+ [{gseesent, 120},
+ {sseesent, 121}];
+ _ -> % OSX
+ [{gseesent, 118},
+ {sseesent, 119}]
+ end,
+ ioc(Inout, Group, proplists:get_value(Name, List), Len);
ioc(Inout, Group, Num, Len) ->
procket_ioctl:ioc(Inout, Group, Num, Len).
@@ -355,8 +379,22 @@ iowr(G,N,T) ->
ioc(procket_ioctl:inout(bsd), G, N, T).
sizeof(timeval) ->
- erlang:system_info({wordsize, external}) + ?SIZEOF_U_INT.
+ erlang:system_info({wordsize, external}) + ?SIZEOF_U_INT;
+sizeof(ifreq) ->
+ case os:type() of
+ {_, netbsd} ->
+ 144;
+ _ -> % OSX
+ 32
+ end.
+alignment() ->
+ case os:type() of
+ {_, netbsd} ->
+ erlang:system_info({wordsize, external});
+ _ -> % OSX
+ ?SIZEOF_INT32_T
+ end.
init(Socket, Dev) ->
% Set the interface for the bpf
Please sign in to comment.
Something went wrong with that request. Please try again.