Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (81 sloc) 3.255 kB
441fde8 @msantos 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
33 -export([init/0,open/2,poll/0,close/0,listen/1,listen/2]).
34 -export([make_args/2]).
35
36 -define(PROGNAME, "priv/procket").
37
38 -on_load(on_load/0).
39
40 init() ->
41 on_load().
42
43 on_load() ->
44 ok = erlang:load_nif("priv/procket", []),
45 true.
46
47 open(_,_) ->
48 erlang:error(not_implemented).
49
50 poll() ->
51 erlang:error(not_implemented).
52
53 close() ->
54 erlang:error(not_implemented).
55
56 listen(Port) ->
57 listen(Port, []).
58 listen(Port, Options) when is_integer(Port), is_list(Options) ->
59 Opt = case proplists:lookup(pipe, Options) of
60 none -> Options ++ [{pipe, mktmp:dir() ++ "/sock"}];
61 _ -> Options
62 end,
63 ok = open(proplists:get_value(pipe, Opt), Port),
64 Cmd = make_args(Port, Opt),
65 case os:cmd(Cmd) of
66 [] ->
67 poll();
68 Error ->
69 {error, procket_cmd, Error}
70 end.
71
72 make_args(Port, Options) ->
73 Bind = " " ++ case proplists:lookup(ip, Options) of
74 none ->
75 integer_to_list(Port);
76 IP ->
77 get_switch(IP) ++ ":" ++ integer_to_list(Port)
78 end,
79 proplists:get_value(progname, Options, ?PROGNAME) ++ " " ++
80 string:join([ get_switch(proplists:lookup(Arg, Options)) || Arg <- [
81 pipe,
82 protocol
83 ], proplists:lookup(Arg, Options) /= none ],
84 " ") ++ Bind.
85
86 get_switch({pipe, Arg}) -> "-p " ++ Arg;
87 get_switch({protocol, raw}) -> "-P 0";
88 get_switch({protocol, icmp}) -> "-P 1";
89 get_switch({protocol, tcp}) -> "-P 6";
90 get_switch({protocol, udp}) -> "-P 17";
91 get_switch({ip, Arg}) when is_tuple(Arg) -> inet_parse:ntoa(Arg);
92 get_switch({ip, Arg}) when is_list(Arg) -> Arg.
93
94
Something went wrong with that request. Please try again.