Permalink
Browse files

Portability pixie dust for ioctl macros

First attempt at supporting the ioctl request macros on bsd and linux.
The result seems to work but doesn't seem to be quite right yet.

On BSD systems, the top 3 bits indicate the direction of the request:

8 : ioctl parameter will be copied from user space into the kernel
4 : ioctl parameter will be copied into the user supplied buffer from
    the kernel
2 : no parameters are used

The remaining 13 bits are used to indicate the size of the parameter.

On Linux systems, only the top 2 bits are used. Setting both bits to 0
indicates a void parameter. The meaning of the bits is reversed:

8 : ioctl parameter will be copied into the user supplied buffer from
    the kernel
4 : ioctl parameter will be copied from user space into the kernel

The remaining 14 bits is used to hold the length of the parameter.
  • Loading branch information...
1 parent 0cd4e78 commit 1f30b9c1ff229a889325ab0ac18afcb973b552e2 @msantos committed May 9, 2011
Showing with 7 additions and 11 deletions.
  1. +2 −0 ebin/procket.app
  2. +0 −6 include/bpf.hrl
  3. +5 −5 src/bpf.erl
View
2 ebin/procket.app
@@ -4,8 +4,10 @@
{vsn, "0.03"},
{modules, [
procket,
+ procket_ioctl,
packet,
bpf,
+ tuntap,
mktmp,
icmp,
echo
View
6 include/bpf.hrl
@@ -79,12 +79,6 @@
%% #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
-define(BPF_WORDALIGN(X), bpf:align(X)).
--define(IOC_IN, 16#80000000).
--define(IOC_OUT, 16#40000000).
--define(IOC_INOUT, ?IOC_IN bor ?IOC_OUT).
--define(IOC_VOID, 16#20000000).
--define(IOCPARM_MASK, 16#1fff).
-
-define(BIOCGBLEN, bpf:ior($B, 102, ?SIZEOF_U_INT)).
-define(BIOCSBLEN, bpf:iowr($B, 102, ?SIZEOF_U_INT)).
-define(BIOCSETF, bpf:iow($B, 103, ?SIZEOF_STRUCT_BPF_PROGRAM)).
View
10 src/bpf.erl
@@ -340,19 +340,19 @@ offset(?BPF_B) -> byte.
%%-------------------------------------------------------------------------
%% BSD ioctl request calculation (taken from ioccom.h)
ioc(Inout, Group, Num, Len) ->
- Inout bor ((Len band ?IOCPARM_MASK) bsl 16) bor (Group bsl 8) bor Num.
+ procket_ioctl:ioc(Inout, Group, Num, Len).
io(G,N) ->
- ioc(?IOC_VOID, G, N, 0).
+ ioc(procket_ioctl:void(bsd), G, N, 0).
iow(G,N,T) ->
- ioc(?IOC_IN, G, N, T).
+ ioc(procket_ioctl:in(bsd), G, N, T).
ior(G,N,T) ->
- ioc(?IOC_OUT, G, N, T).
+ ioc(procket_ioctl:out(bsd), G, N, T).
iowr(G,N,T) ->
- ioc(?IOC_INOUT, G, N, T).
+ ioc(procket_ioctl:inout(bsd), G, N, T).
sizeof(timeval) ->
erlang:system_info({wordsize, external}) + ?SIZEOF_U_INT.

0 comments on commit 1f30b9c

Please sign in to comment.