Permalink
Browse files

cleanup, rename to gen_netlink

  • Loading branch information...
1 parent ebe1395 commit 660bce10f8778aa4075f8de371c69a42fc9d23a3 Felix Lange committed Jan 4, 2011
Showing with 13 additions and 313 deletions.
  1. +1 −0 .gitignore
  2. +12 −0 ebin/gen_netlink.app
  3. +0 −20 ebin/gen_socket.app
  4. +0 −77 src/echo.erl
  5. +0 −128 src/icmp.erl
  6. +0 −88 src/mktmp.erl
View
@@ -1,2 +1,3 @@
*.[oa]
*.beam
+/priv/*.so
View
@@ -0,0 +1,12 @@
+{application, gen_netlink, [
+ {description, "Netlink socket toolkit"},
+ {vsn, "0.02"},
+ {modules, [gen_socket,
+ packet,
+ netlink
+ ]},
+ {registered, []},
+ {applications, [kernel, stdlib]},
+ {env, []}
+]}.
+
View
@@ -1,20 +0,0 @@
-{application, gen_socket,
- [
- {description, "Low level socket operations"},
- {vsn, "0.02"},
- {modules, [
- gen_socket,
- packet,
- mktmp,
- icmp,
- echo,
- netlink
- ]},
- {registered, []},
- {applications, [
- kernel,
- stdlib
- ]},
- {env, []}
- ]}.
-
View
@@ -1,77 +0,0 @@
-%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
-%% All rights reserved.
-%%
-%% Redistribution and use in source and binary forms, with or without
-%% modification, are permitted provided that the following conditions
-%% are met:
-%%
-%% Redistributions of source code must retain the above copyright
-%% notice, this list of conditions and the following disclaimer.
-%%
-%% Redistributions in binary form must reproduce the above copyright
-%% notice, this list of conditions and the following disclaimer in the
-%% documentation and/or other materials provided with the distribution.
-%%
-%% Neither the name of the author nor the names of its contributors
-%% may be used to endorse or promote products derived from this software
-%% without specific prior written permission.
-%%
-%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-%% POSSIBILITY OF SUCH DAMAGE.
-
-%% procket test client
--module(echo).
-
--export([start/0,start/1,start/2]).
-
--define(PORT, 54).
-
-start() ->
- start(tcp).
-start(tcp) ->
- start(?PORT, [{protocol, tcp}, {family, inet}, {type, stream}]);
-start(udp) ->
- start(?PORT, [{protocol, udp}, {family, inet}, {type, dgram}]).
-start(Port, Options) ->
- Protocol = proplists:get_value(protocol, Options, tcp),
- {ok, Fd} = procket:open(Port, Options),
- io:format("Listening on: ~p/~p~n", [Port, Protocol]),
- listen(Protocol, Fd, Port).
-
-listen(tcp, Fd, Port) ->
- {ok, S} = gen_tcp:listen(Port, [binary, {fd, Fd}]),
- accept(S);
-listen(udp, Fd, Port) ->
- {ok, S} = gen_udp:open(Port, [binary, {fd, Fd}]),
- recv(S).
-
-accept(LS) ->
- {ok, S} = gen_tcp:accept(LS),
- spawn(fun() -> accept(LS) end),
- recv(S).
-
-recv(S) ->
- receive
- {tcp, S, Data} ->
- gen_tcp:send(S, Data),
- recv(S);
- {tcp_closed, S} ->
- ok;
- {udp, S, IP, Port, Data} ->
- gen_udp:send(S, IP, Port, Data),
- recv(S);
- Error ->
- error_logger:error_report({recv, Error})
- end.
-
-
View
@@ -1,128 +0,0 @@
-%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
-%% All rights reserved.
-%%
-%% Redistribution and use in source and binary forms, with or without
-%% modification, are permitted provided that the following conditions
-%% are met:
-%%
-%% Redistributions of source code must retain the above copyright
-%% notice, this list of conditions and the following disclaimer.
-%%
-%% Redistributions in binary form must reproduce the above copyright
-%% notice, this list of conditions and the following disclaimer in the
-%% documentation and/or other materials provided with the distribution.
-%%
-%% Neither the name of the author nor the names of its contributors
-%% may be used to endorse or promote products derived from this software
-%% without specific prior written permission.
-%%
-%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-%% POSSIBILITY OF SUCH DAMAGE.
--module(icmp).
--export([ping/1, ping/2]).
-
--record(icmp, {
- valid,
- type, code, checksum,
- id, sequence,
- gateway,
- un,
- mtu
- }).
-
--record(state, {
- s, % socket
- id, % ping ID
- seq = 0, % ping sequence number
- ip, % IP Address
- n % Number of pings
- }).
-
--define(ICMP_ECHO_REPLY, 0).
--define(ICMP_ECHO, 8).
-
-ping(IP) ->
- ping(IP, 1).
-ping(IP, N) ->
- crypto:start(),
- Id = crypto:rand_uniform(0, 16#FFFF),
- {ok, FD} = procket:open(0, [{protocol, icmp}, {type, raw}, {family, inet}]),
- {ok, S} = gen_udp:open(0, [binary, {fd, FD}]),
- loop(#state{
- s = S,
- id = Id,
- ip = IP,
- n = N
- }).
-
-loop(#state{n = N, seq = Seq}) when Seq >= N ->
- ok;
-loop(#state{s = S, id = Id, seq = Seq, ip = IP, n = N} = State) ->
- Packet = make_packet(Id, Seq),
- ok = gen_udp:send(S, IP, 0, Packet),
- receive
- {udp, S, _IP, _Port, <<_:20/bytes, Data/binary>>} ->
- {ICMP, <<Mega:32/integer, Sec:32/integer, Micro:32/integer, Payload/binary>>} = icmp(Data),
- error_logger:info_report([
- {type, ICMP#icmp.type},
- {code, ICMP#icmp.code},
- {checksum, ICMP#icmp.checksum},
- {id, ICMP#icmp.id},
- {sequence, ICMP#icmp.sequence},
- {payload, Payload},
- {time, timer:now_diff(erlang:now(), {Mega, Sec, Micro})}
- ]),
- sleep(N, Seq)
- after
- 5000 ->
- error_logger:error_report([{noresponse, Packet}])
- end,
- loop(State#state{seq = Seq + 1}).
-
-make_packet(Id, Seq) ->
- {Mega,Sec,USec} = erlang:now(),
-
- % Pad packet to 64 bytes
- Payload = list_to_binary(lists:seq($\s, $K)),
-
- CS = makesum(<<?ICMP_ECHO:8, 0:8, 0:16, Id:16, Seq:16, Mega:32, Sec:32, USec:32, Payload/binary>>),
- <<
- 8:8, % Type
- 0:8, % Code
- CS:16, % Checksum
- Id:16, % Id
- Seq:16, % Sequence
-
- Mega:32, Sec:32, USec:32, % Payload: time
- Payload/binary
- >>.
-
-
-makesum(Hdr) -> 16#FFFF - checksum(Hdr).
-
-checksum(Hdr) ->
- lists:foldl(fun compl/2, 0, [ W || <<W:16>> <= Hdr ]).
-
-compl(N) when N =< 16#FFFF -> N;
-compl(N) -> (N band 16#FFFF) + (N bsr 16).
-compl(N,S) -> compl(N+S).
-
-icmp(<<?ICMP_ECHO_REPLY:8, 0:8, Checksum:16, Id:16, Sequence:16, Payload/binary>>) ->
- {#icmp{
- type = ?ICMP_ECHO_REPLY, code = 0, checksum = Checksum, id = Id,
- sequence = Sequence
- }, Payload}.
-
-sleep(N,S) when N =:= S + 1 -> ok;
-sleep(_,_) -> timer:sleep(1000).
-
View
@@ -1,88 +0,0 @@
-%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
-%% All rights reserved.
-%%
-%% Redistribution and use in source and binary forms, with or without
-%% modification, are permitted provided that the following conditions
-%% are met:
-%%
-%% Redistributions of source code must retain the above copyright
-%% notice, this list of conditions and the following disclaimer.
-%%
-%% Redistributions in binary form must reproduce the above copyright
-%% notice, this list of conditions and the following disclaimer in the
-%% documentation and/or other materials provided with the distribution.
-%%
-%% Neither the name of the author nor the names of its contributors
-%% may be used to endorse or promote products derived from this software
-%% without specific prior written permission.
-%%
-%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-%% POSSIBILITY OF SUCH DAMAGE.
-
-%% Atomically create a temporary file.
-%%
-%% This module just creates a directory, put whatever temp files
-%% you need inside the directory.
-%%
-%% Problems:
-%% * On some platforms, Unix sockets will be created with
-%% modes 777.
-%% * The module does not test if the directory exists before
-%% creation
-%% * If the directory creation fails, the module will just
-%% return an error.
-
--module(mktmp).
-
--export([dirname/0,name/1,template/2,make_dir/1,close/1]).
-
--include_lib("kernel/include/file.hrl").
-
--define(TEMPLATE, "erlang-XXXXXXXXXXXX").
--define(S_IRWXU, 8#00400 bor 8#00200 bor 8#00100).
--define(ALPHANUM, "0123456789"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- "abcdefghijklmnopqrstuvwxyz").
-
-dirname() ->
- TMP = case os:getenv("TMPDIR") of
- false -> "/tmp";
- Dir -> Dir
- end,
- name(TMP ++ "/" ++ ?TEMPLATE).
-
-name(Template) ->
- template(Template, ?ALPHANUM).
-
-template(Name, Chars) ->
- crypto:start(),
- template(lists:reverse(Name), [], Chars).
-template([$X|Rest], Acc, Chars) ->
- template(Rest,
- [lists:nth(crypto:rand_uniform(1,length(Chars)+1), Chars)|Acc],
- Chars);
-template(Name, Rand, _) when length(Rand) >= 6 ->
- lists:reverse(Name) ++ Rand.
-
-make_dir(Path) ->
- case file:make_dir(Path) of
- ok ->
- file:write_file_info(Path, #file_info{mode = ?S_IRWXU});
- Error ->
- Error
- end.
-
-close(Path) ->
- file:del_dir(Path).
-
-

0 comments on commit 660bce1

Please sign in to comment.