1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
%%%-------------------------------------------------------------------
%%% @author Dan Gudmundsson <[email protected]>
%%% @copyright (C) 2010, Dan Gudmundsson
%%% @doc
%%%
%%% @end
%%% Created : 22 Jun 2010 by Dan Gudmundsson <[email protected]>
%%%-------------------------------------------------------------------
-module(inet_proxy_dist).
-export([childspecs/0, listen/1, accept/1, accept_connection/5,
setup/5, close/1, select/1, is_node_name/1, tick/1]).
-include_lib("kernel/src/net_address.hrl").
-include_lib("kernel/src/dist.hrl").
-include_lib("kernel/src/dist_util.hrl").
-import(error_logger,[error_msg/2]).
childspecs() ->
io:format("childspecs called~n",[]),
{ok, [{proxy_server,{proxy_server, start_link, []},
permanent, 2000, worker, [proxy_server]}]}.
select(Node) ->
io:format("Select called~n",[]),
inet_ssl_dist:select(Node).
is_node_name(Name) ->
io:format("is_node_name~n",[]),
inet_ssl_dist:is_node_name(Name).
listen(Name) ->
io:format("listen called~n",[]),
gen_server:call(proxy_server, {listen, Name}, infinity).
accept(Listen) ->
io:format("accept called~n",[]),
gen_server:call(proxy_server, {accept, Listen}, infinity).
accept_connection(AcceptPid, Socket, MyNode, Allowed, SetupTime) ->
io:format("accept_connection called ~n",[]),
Kernel = self(),
spawn_link(fun() -> do_accept(Kernel, AcceptPid, Socket,
MyNode, Allowed, SetupTime) end).
setup(Node, Type, MyNode, LongOrShortNames,SetupTime) ->
io:format("setup called~n",[]),
Kernel = self(),
spawn(fun() -> do_setup(Kernel, Node, Type, MyNode, LongOrShortNames, SetupTime) end).
do_setup(Kernel, Node, Type, MyNode, LongOrShortNames, SetupTime) ->
[Name, Address] = splitnode(Node, LongOrShortNames),
case inet:getaddr(Address, inet) of
{ok, Ip} ->
Timer = dist_util:start_timer(SetupTime),
case erl_epmd:port_please(Name, Ip) of
{port, TcpPort, Version} ->
?trace("port_please(~p) -> version ~p~n",
[Node,Version]),
dist_util:reset_timer(Timer),
case gen_server:call(proxy_server, {connect, Ip, TcpPort}, infinity) of
{ok, Socket} ->
HSData = #hs_data{
kernel_pid = Kernel,
other_node = Node,
this_node = MyNode,
socket = Socket,
timer = Timer,
this_flags = 0,
other_version = Version,
f_send = fun(S,D) ->
io:format("Kernel call send~n",[]),
gen_tcp:send(S,D)
end,
f_recv = fun(S,N,T) ->
io:format("Kernel call receive~n",[]),
gen_tcp:recv(S,N,T)
end,
f_setopts_pre_nodeup =
fun(S) ->
io:format("Kernel pre nodeup~n",[]),
inet:setopts(S, [{active, false}, {packet, 4}])
end,
f_setopts_post_nodeup =
fun(S) ->
io:format("Kernel post nodeup~n",[]),
inet:setopts(S, [{deliver, port},{active, true}])
end,
f_getll = fun(S) -> inet:getll(S) end,
f_address =
fun(_,_) ->
#net_address{address = {Ip,TcpPort},
host = Address,
protocol = proxy,
family = inet}
end,
mf_tick = fun(S) -> gen_tcp:send(S, <<>>) end,
mf_getstat = fun(S) ->
{ok, Stats} = inet:getstat(S, [recv_cnt, send_cnt, send_pend]),
R = proplists:get_value(recv_cnt, Stats, 0),
W = proplists:get_value(send_cnt, Stats, 0),
P = proplists:get_value(send_pend, Stats, 0),
{ok, R,W,P}
end,
request_type = Type
},
dist_util:handshake_we_started(HSData);
_ ->
%% Other Node may have closed since
%% port_please !
?trace("other node (~p) "
"closed since port_please.~n",
[Node]),
?shutdown(Node)
end;
_ ->
?trace("port_please (~p) "
"failed.~n", [Node]),
?shutdown(Node)
end;
_Other ->
?trace("inet_getaddr(~p) "
"failed (~p).~n", [Node,Other]),
?shutdown(Node)
end.
close(Socket) ->
io:format("close called~n",[]),
gen_tcp:close(Socket),
ok.
do_accept(Kernel, AcceptPid, Socket, MyNode, Allowed, SetupTime) ->
process_flag(priority, max),
io:format("~p: in do_accept~n", [self()]),
receive
{AcceptPid, controller} ->
io:format("~p: do_accept controller~n", [self()]),
Timer = dist_util:start_timer(SetupTime),
case check_ip(Socket) of
true ->
HSData = #hs_data{
kernel_pid = Kernel,
this_node = MyNode,
socket = Socket,
timer = Timer,
this_flags = 0,
allowed = Allowed,
f_send = fun(S,D) ->
io:format("Kernel call send~n",[]),
gen_tcp:send(S,D) end,
f_recv = fun(S,N,T) ->
io:format("Kernel call receive~n",[]),
gen_tcp:recv(S,N,T) end,
f_setopts_pre_nodeup =
fun(S) ->
io:format("Kernel pre nodeup~n",[]),
inet:setopts(S, [{active, false}, {packet, 4}])
end,
f_setopts_post_nodeup =
fun(S) ->
io:format("Kernel post nodeup~n",[]),
inet:setopts(S, [{deliver, port},{active, true}])
end,
f_getll = fun(S) -> inet:getll(S) end,
f_address = fun get_remote_id/2,
mf_tick = fun(S) -> gen_tcp:send(S, <<>>) end,
mf_getstat = fun(S) ->
{ok, Stats} = inet:getstat(S, [recv_cnt, send_cnt, send_pend]),
R = proplists:get_value(recv_cnt, Stats, 0),
W = proplists:get_value(send_cnt, Stats, 0),
P = proplists:get_value(send_pend, Stats, 0),
{ok, R,W,P}
end
},
dist_util:handshake_other_started(HSData);
{false,IP} ->
error_logger:error_msg("** Connection attempt from "
"disallowed IP ~w ** ~n", [IP]),
?shutdown(no_node)
end
end.
get_remote_id(Socket, Node) ->
gen_server:call(proxy_server, {get_remote_id, {Socket,Node}}, infinity).
tick(Socket) ->
gen_tcp:send(Socket, <<>>).
check_ip(_) ->
true.
%% If Node is illegal terminate the connection setup!!
splitnode(Node, LongOrShortNames) ->
case split_node(atom_to_list(Node), $@, []) of
[Name|Tail] when Tail =/= [] ->
Host = lists:append(Tail),
case split_node(Host, $., []) of
[_] when LongOrShortNames == longnames ->
error_msg("** System running to use "
"fully qualified "
"hostnames **~n"
"** Hostname ~s is illegal **~n",
[Host]),
?shutdown(Node);
[_, _ | _] when LongOrShortNames == shortnames ->
error_msg("** System NOT running to use fully qualified "
"hostnames **~n"
"** Hostname ~s is illegal **~n",
[Host]),
?shutdown(Node);
_ ->
[Name, Host]
end;
[_] ->
error_msg("** Nodename ~p illegal, no '@' character **~n",
[Node]),
?shutdown(Node);
_ ->
error_msg("** Nodename ~p illegal **~n", [Node]),
?shutdown(Node)
end.
split_node([Chr|T], Chr, Ack) -> [lists:reverse(Ack)|split_node(T, Chr, [])];
split_node([H|T], Chr, Ack) -> split_node(T, Chr, [H|Ack]);
split_node([], _, Ack) -> [lists:reverse(Ack)].
|