Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (113 sloc) 4.015 kb
0cd4e78 @msantos Support tuntap interfaces
authored
1 %% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
2 %% All rights reserved.
3 %%
4 %% Redistribution and use in source and binary forms, with or without
5 %% modification, are permitted provided that the following conditions
6 %% are met:
7 %%
8 %% Redistributions of source code must retain the above copyright
9 %% notice, this list of conditions and the following disclaimer.
10 %%
11 %% Redistributions in binary form must reproduce the above copyright
12 %% notice, this list of conditions and the following disclaimer in the
13 %% documentation and/or other materials provided with the distribution.
14 %%
15 %% Neither the name of the author nor the names of its contributors
16 %% may be used to endorse or promote products derived from this software
17 %% without specific prior written permission.
18 %%
19 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 %% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 %% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 %% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 %% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 %% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 %% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 %% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 %% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 %% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 %% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 %% POSSIBILITY OF SUCH DAMAGE.
31 -module(procket_ioctl).
32
33 -export([
34 in/0, out/0, void/0,
35 in/1, out/1, void/1,
0c32f66 @msantos README and whitespace cleanup
authored
36 ioc/4,
0cd4e78 @msantos Support tuntap interfaces
authored
37 io/2, ior/3,
38 iow/3, iowr/3
39 ]).
40
41
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
42 %%
43 %% Calculate the values of the ioctl request macros at runtime.
44 %%
45
46 %% On BSD systems, the top 3 bits indicate the direction of the request:
47 %%
48 %% 8 : ioctl parameter will be copied from user space into the kernel
49 %% 4 : ioctl parameter will be copied into the user supplied buffer from
50 %% the kernel
51 %% 2 : no parameters are used
52 %%
53 %% The remaining 13 bits are used to indicate the size of the parameter.
54 %%
55 %% On Linux systems, only the top 2 bits are used. Setting both bits to 0
56 %% indicates a void parameter. The meaning of the bits is reversed:
57 %%
58 %% 8 : ioctl parameter will be copied into the user supplied buffer from
59 %% the kernel
60 %% 4 : ioctl parameter will be copied from user space into the kernel
61 %%
62 %% The remaining 14 bits is used to hold the length of the parameter.
63 %%
0cd4e78 @msantos Support tuntap interfaces
authored
64 %% BSD: <<IN:1, OUT:1, VOID:1, Length:13, Group:8, Command:8>>
65 %%
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
66 %% _IOC_OUT = 0x40000000
67 %% _IOC_IN = 0x80000000
0cd4e78 @msantos Support tuntap interfaces
authored
68 %%
69 %% #define _IOR(g,n,t) _IOC(IOC_OUT, (g), (n), sizeof(t))
70 %% #define _IOW(g,n,t) _IOC(IOC_IN, (g), (n), sizeof(t))
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
71 %%
0cd4e78 @msantos Support tuntap interfaces
authored
72 %% Linux: <<OUT:1, IN:1, Length:14, Group:8, Command:8>>
73 %%
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
74 %% _IOC_OUT = 0x80000000
75 %% _IOC_IN = 0x40000000
0cd4e78 @msantos Support tuntap interfaces
authored
76 %%
77 %% #define _IOR(g,n,t) _IOC(_IOC_READ,(g),(n),(t))
78 %% #define _IOW(g,n,t) _IOC(_IOC_WRITE,(g),(n),(t))
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
79 %%
0cd4e78 @msantos Support tuntap interfaces
authored
80
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
81 %% BSD: _IOC_IN
82 %% Linux: _IOC_WRITE << _IOC_DIRSHIFT (16)
0cd4e78 @msantos Support tuntap interfaces
authored
83 in() -> in(os()).
84 in(bsd) -> 16#80000000;
85 in(linux) -> 16#40000000.
86
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
87 %% BSD: _IOC_OUT
88 %% Linux: _IOC_READ << _IOC_DIRSHIFT (16)
0cd4e78 @msantos Support tuntap interfaces
authored
89 out() -> out(os()).
90 out(bsd) -> 16#40000000;
91 out(linux) -> 16#80000000.
92
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
93 %% BSD: _IOC_VOID
94 %% Linux: _IOC_NONE
0cd4e78 @msantos Support tuntap interfaces
authored
95 void() -> void(os()).
96 void(bsd) -> 16#20000000;
97 void(linux) -> 0.
98
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
99 -define(IOCPARM_MASK, 16#1fff).
100
0cd4e78 @msantos Support tuntap interfaces
authored
101 ioc(Inout,Group,Num,Len) ->
b84cdf3 @msantos Mask the top 3 bits for BSD ioctl requests
authored
102 Size = case os() of
103 bsd -> Len band ?IOCPARM_MASK;
104 linux -> Len
105 end,
106 Inout bor (Size bsl 16) bor (Group bsl 8) bor Num.
0cd4e78 @msantos Support tuntap interfaces
authored
107
108 io(Type,NR) ->
109 ioc(void(), Type, NR, 0).
110
111 ior(Type,NR,Size) ->
112 ioc(out(), Type, NR, Size).
113
114 iow(Type,NR,Size) ->
115 ioc(in(), Type, NR, Size).
116
117 iowr(Type,NR,Size) ->
118 ioc(in() bor out(), Type, NR, Size).
119
120
121 os() ->
122 case os:type() of
123 {unix, linux} -> linux;
124 {unix,BSD} when BSD == darwin;
125 BSD == openbsd;
126 BSD == netbsd;
127 BSD == freebsd -> bsd
128 end.
Something went wrong with that request. Please try again.