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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2010-2011. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%% This module implements a transport_module that uses Erlang message
%% passing for transport.
%%
-module(diameter_etcp).
-behaviour(gen_server).
%% transport_module interface.
-export([start/3]).
%% gen_tcp-ish interface used by diameter_tcp.
-export([listen/2,
accept/1,
connect/3,
send/2,
close/1,
setopts/2,
port/1]).
%% child start
-export([start_link/1]).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
code_change/3,
terminate/2]).
%% Server states.
-record(listener,
{acceptors = [] :: [pid()]}).
-record(connection,
{parent :: pid(),
peer :: {connect, reference()} %% {connect, MRef}
| accept
| pid()}).
%% start/3
%% 'module' option makes diameter_tcp call here instead of gen_tcp/inet.
start(T, Svc, Opts)
when is_list(Opts) ->
diameter_etcp_sup:start(),
diameter_tcp:start(T, Svc, [{module, ?MODULE} | Opts]).
%% listen/2
%%
%% Spawn a process that represents the listening socket. The local
%% port number can be any term, not just an integer. The listener
%% process registers its host/port with diameter_reg and this is the
%% handle with which connect/3 finds the appropriate listening
%% process.
listen(LPort, Opts) ->
Parent = self(),
diameter_etcp_sup:start_child({listen, Parent, LPort, Opts}).
%% accept/1
%%
%% Output: pid()
accept(LPid) ->
start(fun(Ref, Parent) -> acceptor(LPid, Ref, Parent) end).
%% connect/3
%%
%% Output: pid()
%% RAddr here can either be a 4/8-tuple address or {Node, Addr}.
connect(RAddr, RPort, _Opts) ->
start(fun(Ref, Parent) -> connector(RAddr, RPort, Ref, Parent) end).
%% send/2
send(Pid, Bin) ->
Pid ! {send, Bin},
ok.
%% close/1
close(Pid) ->
Pid ! close,
monitor(Pid),
receive {'DOWN', _, process, Pid, _} -> ok end.
%% setopts/2
setopts(_, _) ->
ok.
%% port/1
port(_) ->
3868. %% We have no local port: fake it.
%% start_link/1
start_link(T) ->
gen_server:start_link(?MODULE, T, []).
%% ---------------------------------------------------------------------------
%% # init/1
%% ---------------------------------------------------------------------------
%% Maintain a list of acceptor pids as the process state. Each accept
%% adds a pid to the list, each connect removes one.
init({listen, Parent, LPort, Opts}) ->
monitor(Parent),
{ip, LAddr} = lists:keyfind(ip, 1, Opts),
true = diameter_reg:add_new({?MODULE, listener, LAddr, LPort}),
{ok, #listener{}};
init({connect, Fun, Ref, Parent}) ->
{ok, #connection{parent = Parent,
peer = Fun(Ref, Parent)}}.
%% ---------------------------------------------------------------------------
%% # handle_call/3
%% ---------------------------------------------------------------------------
handle_call(_, _, State) ->
{reply, nok, State}.
%% ---------------------------------------------------------------------------
%% # handle_cast/2
%% ---------------------------------------------------------------------------
handle_cast(_, State) ->
{noreply, State}.
%% ---------------------------------------------------------------------------
%% # handle_info/2
%% ---------------------------------------------------------------------------
handle_info(T, #listener{acceptors = L} = S) ->
{noreply, S#listener{acceptors = l(T,L)}};
handle_info(T, State) ->
{noreply, transition(T, State)}.
%% ---------------------------------------------------------------------------
%% # code_change/3
%% ---------------------------------------------------------------------------
code_change(_, State, _) ->
{ok, State}.
%% ---------------------------------------------------------------------------
%% # terminate/2
%% ---------------------------------------------------------------------------
terminate(_, _) ->
ok.
%% ---------------------------------------------------------------------------
monitor(Pid) ->
erlang:monitor(process, Pid).
putr(Key, Val) ->
put({?MODULE, Key}, Val).
eraser(Key) ->
erase({?MODULE, Key}).
%% l/2
l({'DOWN', _, process, _, _} = T, _) ->
x(T);
%% New accepting process.
l({accept, APid}, As) ->
As ++ [APid];
%% Peer wants to connect but we have no acceptor ...
l({connect, Peer}, [] = As) ->
Peer ! {refused, self()},
As;
%% ... or we do.
l({connect, Peer}, [APid | Rest]) ->
Peer ! {accepted, APid},
Rest.
x(T) ->
exit({shutdown, T}).
%% start/1
start(Fun) ->
Ref = make_ref(),
{ok, Pid}
= T
= diameter_etcp_sup:start_child({connect, Fun, Ref, self()}),
MRef = monitor(Pid),
receive
{ok, Ref} ->
T;
{'DOWN', MRef, process, _, Reason} ->
{error, Reason}
end.
%% acceptor/3
acceptor(LPid, Ref, Parent) ->
LPid ! {accept, self()}, %% announce that we're accepting
putr(ref, {ok, Ref}),
monitor(Parent),
monitor(LPid),
accept.
%% connector/4
connector(RAddr, RPort, Ref, Parent) ->
c(match(RAddr, RPort), Ref, Parent).
c([], _, _) ->
x(refused);
c([{_,LPid}], Ref, Parent) ->
LPid ! {connect, self()},
putr(ref, {ok, Ref}),
monitor(Parent),
{connect, monitor(LPid)}.
match({Node, RAddr}, RPort) ->
rpc:call(Node, diameter_reg, match, [{?MODULE, listener, RAddr, RPort}]);
match(RAddr, RPort) ->
match({node(), RAddr}, RPort).
%% transition/2
%% Unexpected parent or peer death.
transition({'DOWN', _, process, _, _} = T, S) ->
element(2,S) ! {tcp_error, self(), T},
x(T);
%% Connector is receiving acceptor pid from listener.
transition({accepted, Peer}, #connection{parent = Parent,
peer = {connect, MRef}}) ->
monitor(Peer),
erlang:demonitor(MRef, [flush]),
Peer ! {connect, self()},
Parent ! {ok, _} = eraser(ref),
#connection{parent = Parent,
peer = Peer};
%% Connector is receiving connection refusal from listener.
transition({refused, _} = T, #connection{peer = {connect, _}}) ->
x(T);
%% Acceptor is receiving peer connect.
transition({connect, Peer}, #connection{parent = Parent,
peer = accept}) ->
monitor(Peer),
Parent ! {ok, _} = eraser(ref),
#connection{parent = Parent,
peer = Peer};
%% Incoming message.
transition({recv, Bin}, #connection{parent = Parent} = S) ->
Parent ! {tcp, self(), Bin},
S;
%% Outgoing message.
transition({send, Bin}, #connection{peer = Peer} = S) ->
Peer ! {recv, Bin},
S;
%% diameter_etcp:close/1 call when a peer is connected ...
transition(close = T, #connection{peer = Peer})
when is_pid(Peer) ->
Peer ! {close, self()},
x(T);
%% ... or not.
transition(close = T, #connection{}) ->
x(T);
%% Peer is closing the connection.
transition({close, Peer} = T, #connection{parent = Parent,
peer = Peer})
when is_pid(Peer) ->
Parent ! {tcp_closed, self()},
x(T).
|