Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 90 lines (73 sloc) 3.272 kB
da30435 @msantos Populate the local ARP cache
authored
1 %% Copyright (c) 2012, 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(spood_pinger).
32
081ec7c @msantos Make the ping interval configurable
authored
33 -export([start/1, start/2]).
da30435 @msantos Populate the local ARP cache
authored
34 -export([range/2]).
35
081ec7c @msantos Make the ping interval configurable
authored
36 -define(INTERVAL, 15*60000). % 15 minutes
da30435 @msantos Populate the local ARP cache
authored
37
38 %% Populate the local ARP cache by periodically scanning the network
081ec7c @msantos Make the ping interval configurable
authored
39 start(Dev) ->
40 start(Dev, ?INTERVAL).
41
42 start(Dev, Interval) when is_binary(Dev) ->
43 start(binary_to_list(Dev), Interval);
44 start(Dev, Interval) when is_list(Dev), Interval > 0 ->
da30435 @msantos Populate the local ARP cache
authored
45 {ok, Ifs} = inet:getifaddrs(),
46 Cfg = proplists:get_value(Dev, Ifs),
47
48 [Addr] = [ {A,B,C,D} || {addr, {A,B,C,D}} <- Cfg ],
49 [Netmask] = [ {A,B,C,D} || {netmask, {A,B,C,D}} <- Cfg ],
50
51 {Network, Broadcast} = range(Addr, Netmask),
52
53 {ok, Socket} = gen_udp:open(0, [
54 {active, false}
55 ]),
56
081ec7c @msantos Make the ping interval configurable
authored
57 poll(Socket, Interval, ipv4_to_int(Network)+1, ipv4_to_int(Broadcast)).
da30435 @msantos Populate the local ARP cache
authored
58
59 range({A1,A2,A3,A4}, {M1,M2,M3,M4}) ->
60 Addr = (A1 bsl 24) bor (A2 bsl 16) bor (A3 bsl 8) bor A4,
61 Mask = (M1 bsl 24) bor (M2 bsl 16) bor (M3 bsl 8) bor M4,
62
63 {int_to_ipv4(Addr band Mask), int_to_ipv4(Addr bor (bnot Mask))}.
64
65 int_to_ipv4(N) ->
66 <<A, B, C, D>> = <<N:4/unsigned-integer-unit:8>>,
67 {A,B,C,D}.
68
69 ipv4_to_int({A,B,C,D}) ->
70 <<N:4/unsigned-integer-unit:8>> = <<A, B, C, D>>,
71 N.
72
081ec7c @msantos Make the ping interval configurable
authored
73 poll(Socket, Interval, Start, End) ->
da30435 @msantos Populate the local ARP cache
authored
74 scan(Socket, Start, End),
081ec7c @msantos Make the ping interval configurable
authored
75 timer:sleep(Interval),
76 poll(Socket, Interval, Start, End).
da30435 @msantos Populate the local ARP cache
authored
77
78 scan(Socket, Address, End) when Address < End ->
79 Port = crypto:rand_uniform(16#0FFF, 16#FFFF),
80
81 % error_logger:info_report([
82 % {address, int_to_ipv4(Address)},
83 % {port, Port}
84 % ]),
85
86 ok = gen_udp:send(Socket, int_to_ipv4(Address), Port, <<>>),
87 scan(Socket, Address+1, End);
88 scan(_Socket, _Address, _End) ->
89 ok.
Something went wrong with that request. Please try again.