Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 137 lines (113 sloc) 4.372 kb
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
1 %% Copyright (c) 2010, 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).
32
194feb8 Michael Santos Add NIF versions of recvfrom/sendto
authored
33 -export([
34 init/0,open/1,poll/1,close/2,listen/1,listen/2,
65a61dc Michael Santos Add an NIF interface to ioctl()
authored
35 recvfrom/2,sendto/4,bind/2,
3649b5c Michael Santos Add an NIF setsockopt for promiscous mode
authored
36 ioctl/3,setsockopt/4
194feb8 Michael Santos Add NIF versions of recvfrom/sendto
authored
37 ]).
5bbc1bb Michael Santos Dynamically create path to procket executable too.
authored
38 -export([make_args/2,progname/0]).
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
39
40 -on_load(on_load/0).
41
5d8600f Michael Santos Dynamically create path to NIF relative to module.
authored
42
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
43 init() ->
44 on_load().
45
46 on_load() ->
5bbc1bb Michael Santos Dynamically create path to procket executable too.
authored
47 erlang:load_nif(progname(), []).
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
48
00214e3 Michael Santos Make the NIF stateless!
authored
49 open(_) ->
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
50 erlang:error(not_implemented).
51
00214e3 Michael Santos Make the NIF stateless!
authored
52 poll(_) ->
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
53 erlang:error(not_implemented).
54
00214e3 Michael Santos Make the NIF stateless!
authored
55 close(_,_) ->
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
56 erlang:error(not_implemented).
57
74a854e Michael Santos Add sendto/4 and bind/2
authored
58 bind(_,_) ->
59 erlang:error(not_implemented).
60
194feb8 Michael Santos Add NIF versions of recvfrom/sendto
authored
61 recvfrom(_,_) ->
62 erlang:error(not_implemented).
63
65a61dc Michael Santos Add an NIF interface to ioctl()
authored
64 ioctl(_,_,_) ->
65 erlang:error(not_implemented).
66
74a854e Michael Santos Add sendto/4 and bind/2
authored
67 sendto(_,_,_,_) ->
194feb8 Michael Santos Add NIF versions of recvfrom/sendto
authored
68 erlang:error(not_implemented).
69
3649b5c Michael Santos Add an NIF setsockopt for promiscous mode
authored
70 setsockopt(_,_,_,_) ->
71 erlang:error(not_implemented).
72
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
73 listen(Port) ->
74 listen(Port, []).
75 listen(Port, Options) when is_integer(Port), is_list(Options) ->
76 Opt = case proplists:lookup(pipe, Options) of
77 none -> Options ++ [{pipe, mktmp:dir() ++ "/sock"}];
78 _ -> Options
79 end,
00214e3 Michael Santos Make the NIF stateless!
authored
80 {ok, Sockfd} = open(proplists:get_value(pipe, Opt)),
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
81 Cmd = make_args(Port, Opt),
82 case os:cmd(Cmd) of
83 [] ->
00214e3 Michael Santos Make the NIF stateless!
authored
84 FD = poll(Sockfd),
85 close(proplists:get_value(pipe, Opt), Sockfd),
86 FD;
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
87 Error ->
00214e3 Michael Santos Make the NIF stateless!
authored
88 {error, {procket_cmd, Error}}
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
89 end.
90
91 make_args(Port, Options) ->
92 Bind = " " ++ case proplists:lookup(ip, Options) of
93 none ->
94 integer_to_list(Port);
95 IP ->
96 get_switch(IP) ++ ":" ++ integer_to_list(Port)
97 end,
5bbc1bb Michael Santos Dynamically create path to procket executable too.
authored
98 proplists:get_value(progname, Options, "sudo " ++ progname()) ++ " " ++
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
99 string:join([ get_switch(proplists:lookup(Arg, Options)) || Arg <- [
100 pipe,
96f8a1e Michael Santos Allow arbitrary socket family, type and protocol
authored
101 protocol,
102 family,
103 type
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
104 ], proplists:lookup(Arg, Options) /= none ],
105 " ") ++ Bind.
106
107 get_switch({pipe, Arg}) -> "-p " ++ Arg;
96f8a1e Michael Santos Allow arbitrary socket family, type and protocol
authored
108
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
109 get_switch({protocol, raw}) -> "-P 0";
110 get_switch({protocol, icmp}) -> "-P 1";
111 get_switch({protocol, tcp}) -> "-P 6";
112 get_switch({protocol, udp}) -> "-P 17";
73d8247 Michael Santos Type conversion is wardsback
authored
113 get_switch({protocol, Proto}) when is_integer(Proto) -> "-P " ++ integer_to_list(Proto);
96f8a1e Michael Santos Allow arbitrary socket family, type and protocol
authored
114
115 get_switch({type, stream}) -> "-T 1";
116 get_switch({type, dgram}) -> "-T 2";
117 get_switch({type, raw}) -> "-T 3";
73d8247 Michael Santos Type conversion is wardsback
authored
118 get_switch({type, Type}) when is_integer(Type) -> "-T " ++ integer_to_list(Type);
96f8a1e Michael Santos Allow arbitrary socket family, type and protocol
authored
119
120 get_switch({family, unspec}) -> "-F 0";
121 get_switch({family, inet}) -> "-F 2";
122 get_switch({family, packet}) -> "-F 17";
73d8247 Michael Santos Type conversion is wardsback
authored
123 get_switch({family, Family}) when is_integer(Family) -> "-F " ++ integer_to_list(Family);
96f8a1e Michael Santos Allow arbitrary socket family, type and protocol
authored
124
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
125 get_switch({ip, Arg}) when is_tuple(Arg) -> inet_parse:ntoa(Arg);
126 get_switch({ip, Arg}) when is_list(Arg) -> Arg.
127
5bbc1bb Michael Santos Dynamically create path to procket executable too.
authored
128 progname() ->
129 filename:join([
130 filename:dirname(code:which(?MODULE)),
131 "..",
132 "priv",
133 ?MODULE
134 ]).
135
441fde8 Michael Santos Interface for requesting privileged sockets in Erlang.
authored
136
Something went wrong with that request. Please try again.