gen_tcpd / src / gen_tcpd.erl

  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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
%%% ----------------------------------------------------------------------------
%%% Copyright 2008
%%% Martin Carlson, martin@martinc.eu
%%% Oscar Hellström, oscar@hellstrom.st
%%%
%%% 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.
%%%     * The names of its contributors may not be used to endorse or promote
%%%       products derived from this software without specific prior written
%%%       permission.
%%%
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
%%% ----------------------------------------------------------------------------
%%% @copyright 2008 Martin Carlson, Oscar Hellström
%%% @author Martin Carlson <martin@martinc.eu>
%%% @author Oscar Hellström <oscar@hellstrom.st> [http://oscar.hellstrom.st]
%%% @version {@version}, {@date}, {@time}
%%% @doc
%%% The gen_tcpd behaviour is a generic way of accepting TCP connections.
%%%
%%% As with all OTP behaviours it is using a set of callbacks for the
%%% specific parts of the code.
%%% <pre>
%%% gen_tcpd module            Callback module
%%% ---------------            ---------------
%%% gen_tcpd:start_link -----> Module:init/1
%%% -                   -----> Module:handle_connection/2
%%% -                   -----> Module:handle_info/2
%%% -                   -----> Module:terminate/2
%%% </pre>
%%% == Callbacks ==
%%% <pre>
%%% Module:init(Args) -> Result
%%%     Types Args = term()
%%%           Result = {ok, State} | {stop, Reason}
%%% </pre>
%%% After {@link start_link/5} has been called this function
%%% is called by the new to initialise a state. If the initialisation is
%%% successful the function should return <code>{ok, State}</code> where
%%% <code>State</code> is the state which will be passed to the client in
%%% in the next callback. <code>Args</code> is the <code>Args</code> passed
%%% to {@link start_link/5}.
%%%
%%% <pre>
%%% Module:handle_connection(Socket, State) -> void()
%%%     Types Socket = {@link socket()}
%%%           State = term()
%%% </pre>
%%% When a TCP / SSL connection is accepted,
%%% `Module:handle_connection(Socket, State)' will be called from a new
%%% process. This process should handle the TCP connection and do whatever
%%% it wants to. When this function returns, the process exits.
%%%
%%% The process which this is called from is linked to the `gen_tcpd'
%%% process. It is allowed to trap exits in the `gen_tcpd' process if
%%% this is wanted. It's also possible to pass the `gen_tcpd' process as
%%% part of the `State' argument to unlink from it.
%%%
%%% It might seem strange that the process is not under some individual
%%% supervisor, but it has been shown that starting children under
%%% supervisors in a vary rapid pace can overload a supervisor and become a
%%% bottleneck in accepting connections.
%%%
%%% <pre>
%%% Module:handle_info(Info, State) -> noreply
%%%     Types Info = term()
%%%           State = term()
%%% </pre>
%%% This function is called if the gen_tcpd process receives any messasge it
%%% dosen't recognise. E.g. <code>{'EXIT', Pid, Reason}</code> messages if
%%% the process is trapping exits.
%%%
%%% <pre>
%%% Module:terminate(Reason, State) -> void()
%%%     Types Reason = term()
%%%           State = term()
%%% </pre>
%%% This function will be called if any of the other callbacks return
%%% <code>{stop, Reason}</code>.
%%%
%%% @type socket()
%%% @end
%%% ----------------------------------------------------------------------------
-module(gen_tcpd).
-behaviour(gen_server).

-export([start_link/5]).
-export([
	send/2,
	recv/2,
	recv/3,
	close/1,
	peername/1,
	port/1,
	sockname/1,
	setopts/2,
	controlling_process/2,
	type/1,
	stop/1
]).
-export([
	init/1,
	handle_call/3,
	handle_cast/2,
	handle_info/2,
	code_change/3,
	terminate/2
]).
-export([init_acceptor/5]).
-export([behaviour_info/1]).

-include("gen_tcpd_types.hrl").

-record(state, {callback, acceptors, socket}).

%% @spec start_link(Callback, CallbackArg, Type, Port, Options) -> {ok, Pid}
%% Callback = atom()
%% CallbackArg = term()
%% Type = tcp | ssl
%% Port = integer()
%% Options = [Opt]
%% Opt = {socket_options, SocketOptions} | {acceptors, Acceptors} |
%%       {ssl_accept_timeout, Timeout}
%% SocketOptions = [SocketOpt]
%% Acceptors = integer()
%% Timeout = infinity | integer()
%% Pid = pid()
%% @doc Starts a gen_tcpd process and links to it.
%% @end
-spec start_link(atom(), term(), ssl | tcp, 0..65535, [{atom(), term()}]) ->
	{ok, pid()} | {error, term()} | ignore.
start_link(Callback, CallbackArg, Type, Port, Options) ->
	Args = [Type, Callback, CallbackArg, Port, Options],
	ok = check_options(Options),
	gen_server:start_link(?MODULE, Args, []).

%% @spec port(Ref) -> Port::integer()
%% Ref = Name | {Name, Node} | {global, GlobalName} | pid()
%% Name = atom()
%% Node = atom()
%% GlobalName = term()
%% @doc
%% Returns the listening port for the gen_tcpd.
%% This is useful if gen server was called with <code>Port</code> =
%% <code>0</code>.
%% @end
-spec port(server_ref()) -> 1..65535.
port(Ref) ->
	gen_server:call(Ref, port).

%% @spec stop(Ref) -> ok
%% Ref = Name | {Name, Node} | {global, GlobalName} | pid()
%% Name = atom()
%% Node = atom()
%% GlobalName = term()
%% @doc
%% Stops the gen_tcpd server and frees the listening port.
%% @end
-spec stop(server_ref()) -> ok.
stop(Ref) ->
	gen_server:cast(Ref, stop).

%% @spec recv(Socket::socket(), Size::integer()) -> Result
%% Result = {ok, Packet} | {error, Reason}
%% Reason = {error, timeout} | {error, posix()}
%% @doc Tries to read <code>Size</code> octets of data from
%% <code>Socket</code>. <code>Size</code> is only relevant if the socket is in
%% raw format.
-spec recv(gen_tcpd_socket(), non_neg_integer()) ->
	{ok, binary() | list()} | {error, atom()}.
recv({Mod, Socket}, Size) ->
	Mod:recv(Socket, Size).

%% @spec recv(Socket::socket(), Size::integer(), Timeout::integer()) -> Result
%% Result = {ok, Packet} | {error, Reason}
%% Reason = {error, timeout} | {error, posix()}
%% @doc Tries to read <code>Size</code> octets of data from
%% <code>Socket</code>. <code>Size</code> is only relevant if the socket is in
%% raw format. The recv request will return <code>{error, Timeout}</code> if
%% <code>Size</code> octets of data is not available within
%% <code>Timeout</code> milliseconds.
-spec recv(gen_tcpd_socket(), non_neg_integer(), timeout()) ->
	{ok, binary() | list()} | {error, atom()}.
recv({Mod, Socket}, Size, Timeout) ->
	Mod:recv(Socket, Size, Timeout).

%% @spec send(Socket::socket(), Packet) -> ok | {error, Reason}
%% Packet = [char()] | binary()
%% Reason = posix()
%% @doc Sends <code>Packet</code> on <code>Socket</code>.
-spec send(gen_tcpd_socket(), iolist() | binary()) -> ok | {error, atom()}.
send({Mod, Socket}, Packet) ->
	Mod:send(Socket, Packet).

%% @spec close(Socket::socket()) -> ok | {error, Reason}
%% Reason = posix()
%% @doc Closes <code>Socket</code>.
-spec close(gen_tcpd_socket()) -> ok | {error, atom()}.
close({Mod, Socket}) ->
	Mod:close(Socket).

%% @spec peername(Socket::socket()) -> {ok, {Address, Port}} | {error, Reason}
%% Address = ipaddress()
%% Port = integer()
%% Reason = posix()
%% @doc Returns the remote address and port of <code>Socket</code>.
-spec peername(gen_tcpd_socket()) -> {ok, {ip_address(), 1..65535}} | {error, atom()}.
peername({gen_tcp, Socket}) ->
	inet:peername(Socket);
peername({Mod, Socket}) ->
	Mod:peername(Socket).

%% @spec sockname(Socket::socket()) -> {ok, {Address, Port}} | {error, Reason}
%% Address = ipaddress()
%% Port = integer()
%% Reason = posix()
%% @doc Returns the local address and port of <code>Socket</code>.
-spec sockname(gen_tcpd_socket()) -> {ok, {ip_address(), 1..65535}} | {error, atom()}.
sockname({gen_tcp, Socket}) ->
	inet:sockname(Socket);
sockname({Mod, Socket}) ->
	Mod:sockname(Socket).

%% @spec type(Socket::socket()) -> tcp | ssl
%% @doc Returns the type of <code>Socket</code>. 
-spec type(gen_tcpd_socket()) -> tcp | ssl.
type({gen_tcp, _}) ->
	tcp;
type({ssl, _}) ->
	ssl;
type(A) ->
	exit({badarg, A}).

%% @spec controlling_process(Socket::socket(), Pid::pid()) ->
%%                                   ok | {error, Reason}
%% Reason = closed | not_owner | posix()
%% @doc Assigns a new controlling process <code>Pid</code> to
%% <code>Socket</code>.
-spec controlling_process(gen_tcpd_socket(), pid()) ->
	ok | {error, atom()}.
controlling_process({Mod, Socket}, Pid) ->
	Mod:controlling_process(Socket, Pid).

%% @spec setopts(Socket::socket(), Options) -> ok | {error, Reason}
%% Options = [{Option, Value} | Option]
%% Option = atom()
%% Value = term()
%% Reason = posix()
%% @doc Sets options for a socket.
%% See backend modules for more info.
-spec setopts(gen_tcpd_socket(), [{atom(), term()} | atom()]) ->
	ok | {error, atom()}.
setopts({gen_tcp, Socket}, Options) ->
	inet:setopts(Socket, Options);
setopts({Mod, Socket}, Options) ->
	Mod:setopts(Socket, Options).

%% @hidden
-spec init(any()) ->
	{ok, any()} | {ok, any(), timeout() | hibernate} | {stop, any()} | ignore.
init([Type, Mod, Args, Port, Options]) ->
	Acceptors = proplists:get_value(acceptors, Options, 1),
	Timeout = proplists:get_value(ssl_accept_timeout, Options, infinity),
	SocketOptions = proplists:get_value(socket_options, Options, []),
	case Mod:init(Args) of
		{ok, CState} ->
			case listen(module(Type), Port, SocketOptions) of
				{ok, Socket} ->
					start_acceptors(Acceptors, Mod, CState, Socket, Timeout),
					{ok, #state{
						callback = {Mod, CState},
						socket = Socket
					}};
				{error, Reason} ->
					{stop, Reason}
			end;
		{stop, Reason} ->
			Mod:terminate(Reason, undefined),
			{stop, Reason};
		Other ->
			{stop, {invalid_return_value, Other}}
	end.

%% @hidden
-spec handle_call(any(), {pid(), any()}, any()) ->
	{reply, any(), any()} | {reply, any(), any(), timeout() | hibernate} |
	{noreply, any()} | {noreply, any(), timeout() | hibernate} |
	{stop, any(), any(), any()} | {stop, any(), any()}.
handle_call(port, _, #state{socket = Socket} = State) ->
	{reply, sock_port(Socket), State};
handle_call(Request, _, State) ->
	{reply, {error, {bad_request, Request}}, State}.


%% @hidden
-spec handle_cast(any(), any()) ->
	{noreply, any()} | {noreply, any(), timeout() | hibernate} |
	{stop, any(), any()}.
handle_cast(stop, State) ->
	{stop, normal, State};
handle_cast(_, State) ->
	{noreply, State}.

%% @hidden
-spec handle_info(any(), any()) ->
	{noreply, any()} | {noreply, any(), timeout() | hibernate} |
	{stop, any(), any()}.
handle_info(Info, State) ->
	{CMod, CState} = State#state.callback,
	case CMod:handle_info(Info, CState) of
		noreply ->
			{noreply, State};
		{stop, Reason} ->
			{stop, Reason, State};
		Other ->
			exit({invalid_return_value, Other})
	end.

%% @hidden
-spec terminate(any(), any()) -> any().
terminate(Reason, #state{callback = {CMod, CState}} = State) ->
	close(State#state.socket),
	CMod:terminate(Reason, CState).

%% @hidden
-spec code_change(any(), any(), any()) ->
	{ok, any()}.
code_change(_, _, State) ->
	{ok, State}.

%% @private
start_acceptors(0, _, _, _, _) ->
	ok;
start_acceptors(Acceptors, Callback, CState, Socket, SSLTimeout) ->
	Args = [self(), Callback, CState, Socket, SSLTimeout],
	proc_lib:spawn(?MODULE, init_acceptor, Args),
	start_acceptors(Acceptors - 1, Callback, CState, Socket, SSLTimeout).

%% @hidden
-spec init_acceptor(pid(), atom(), term(), any(), timeout()) -> _.
init_acceptor(Parent, Callback, CState, Socket, SSLTimeout) ->
	try link(Parent)
		catch exit:noproc -> exit(normal)
	end,
	accept(Parent, Callback, CState, Socket, SSLTimeout).

accept(Parent, Callback, CState, Socket, SSLTimeout) ->
	case do_accept(Socket, SSLTimeout) of
		{ok, Client} ->
			Args = [Parent, Callback, CState, Socket, SSLTimeout],
			proc_lib:spawn(?MODULE, init_acceptor, Args),
			Callback:handle_connection(Client, CState);
		{error, {{ssl, ssl_accept}, timeout}} -> % SSL negotiation timed out
			accept(Parent, Callback, CState, Socket, SSLTimeout);
		{error, {_, closed}} ->
			unlink(Parent), % no need to send exit signals here
			exit(normal);
		Other ->
			erlang:error(Other)
	end.

listen(Mod, Port, Options) ->
	case Mod:listen(Port, Options) of
		{ok, Socket}    -> {ok, {Mod, Socket}};
		{error, Reason} -> {error, {{Mod, listen}, Reason}}
	end.

do_accept({ssl, Socket}, SSLTimeout) ->
	case ssl:transport_accept(Socket) of
		{ok, Client} ->
			case ssl:ssl_accept(Client, SSLTimeout) of
				ok ->
					{ok, {ssl, Client}};
				{error, Reason} ->
					{error, {{ssl, ssl_accept}, Reason}}
			end;
		{error, Reason} ->
			{error, {{ssl, trasport_accept}, Reason}}
	end;
do_accept({Mod, Socket}, _) ->
	case Mod:accept(Socket) of
		{ok, Client}    -> {ok, {Mod, Client}};
		{error, Reason} -> {error, {{Mod, accept}, Reason}}
	end.

sock_port({gen_tcp, Socket}) ->
	element(2, inet:port(Socket));
sock_port({Mod, Socket}) ->
	element(2, Mod:port(Socket)).

check_options([{socket_options, List} | T]) when is_list(List) ->
	check_options(T);
check_options([{acceptors, N} | T]) when is_integer(N), N > 0 ->
	check_options(T);
check_options([{ssl_accept_timeout, infinity} | T]) ->
	check_options(T);
check_options([{ssl_accept_timeout, TO} | T]) when is_integer(TO), TO >= 0 ->
	check_options(T);
check_options([Option | _]) ->
	erlang:error({bad_option, Option});
check_options([]) ->
	ok.

module(tcp)  -> gen_tcp;
module(Type) -> Type.

%% @hidden
-spec behaviour_info(any()) -> [{atom(), non_neg_integer()}] | ok.
behaviour_info(callbacks) ->
	[{init, 1}, {handle_connection, 2}, {handle_info, 2}, {terminate, 2}];
behaviour_info(_) ->
	ok.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.