2006-09-05 06:26:28 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_socket.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Socket with zlib and TLS support library
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 23 Aug 2006 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2006-09-05 06:26:28 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_socket).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start/4,
|
|
|
|
connect/3,
|
2008-12-26 12:45:08 +01:00
|
|
|
connect/4,
|
2016-10-22 12:01:45 +02:00
|
|
|
connect/5,
|
2006-09-05 06:26:28 +02:00
|
|
|
starttls/2,
|
2006-10-01 03:53:37 +02:00
|
|
|
starttls/3,
|
2006-09-05 06:26:28 +02:00
|
|
|
compress/1,
|
2006-10-01 03:53:37 +02:00
|
|
|
compress/2,
|
2006-09-05 06:26:28 +02:00
|
|
|
reset_stream/1,
|
|
|
|
send/2,
|
2009-08-31 20:37:52 +02:00
|
|
|
send_xml/2,
|
2006-09-05 06:26:28 +02:00
|
|
|
change_shaper/2,
|
2007-04-12 08:08:32 +02:00
|
|
|
monitor/1,
|
2006-09-05 06:26:28 +02:00
|
|
|
get_sockmod/1,
|
2016-07-18 14:01:32 +02:00
|
|
|
get_transport/1,
|
2006-09-05 06:36:01 +02:00
|
|
|
get_peer_certificate/1,
|
|
|
|
get_verify_result/1,
|
2006-10-19 06:46:24 +02:00
|
|
|
close/1,
|
|
|
|
sockname/1, peername/1]).
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2009-10-19 11:36:23 +02:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2017-01-02 15:53:25 +01:00
|
|
|
-type sockmod() :: ejabberd_bosh |
|
2015-03-12 13:14:30 +01:00
|
|
|
ejabberd_http_ws |
|
2016-02-03 16:13:16 +01:00
|
|
|
gen_tcp | fast_tls | ezlib.
|
2013-03-14 10:33:02 +01:00
|
|
|
-type receiver() :: pid () | atom().
|
|
|
|
-type socket() :: pid() | inet:socket() |
|
2016-02-03 16:13:16 +01:00
|
|
|
fast_tls:tls_socket() |
|
2016-11-22 13:17:05 +01:00
|
|
|
ezlib:zlib_socket() |
|
2017-01-02 15:53:25 +01:00
|
|
|
ejabberd_bosh:bind_socket().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-record(socket_state, {sockmod = gen_tcp :: sockmod(),
|
|
|
|
socket = self() :: socket(),
|
|
|
|
receiver = self() :: receiver()}).
|
|
|
|
|
|
|
|
-type socket_state() :: #socket_state{}.
|
2009-10-19 11:36:23 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-export_type([socket/0, socket_state/0, sockmod/0]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec start(atom(), sockmod(), socket(), [{atom(), any()}]) -> any().
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
start(Module, SockMod, Socket, Opts) ->
|
2006-09-25 05:51:11 +02:00
|
|
|
case Module:socket_type() of
|
2013-03-14 10:33:02 +01:00
|
|
|
xml_stream ->
|
|
|
|
MaxStanzaSize = case lists:keysearch(max_stanza_size, 1,
|
|
|
|
Opts)
|
|
|
|
of
|
|
|
|
{value, {_, Size}} -> Size;
|
|
|
|
_ -> infinity
|
|
|
|
end,
|
|
|
|
{ReceiverMod, Receiver, RecRef} = case catch
|
|
|
|
SockMod:custom_receiver(Socket)
|
|
|
|
of
|
|
|
|
{receiver, RecMod, RecPid} ->
|
|
|
|
{RecMod, RecPid, RecMod};
|
|
|
|
_ ->
|
|
|
|
RecPid =
|
|
|
|
ejabberd_receiver:start(Socket,
|
|
|
|
SockMod,
|
|
|
|
none,
|
|
|
|
MaxStanzaSize),
|
|
|
|
{ejabberd_receiver, RecPid,
|
|
|
|
RecPid}
|
|
|
|
end,
|
|
|
|
SocketData = #socket_state{sockmod = SockMod,
|
|
|
|
socket = Socket, receiver = RecRef},
|
|
|
|
case Module:start({?MODULE, SocketData}, Opts) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
case SockMod:controlling_process(Socket, Receiver) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, _Reason} -> SockMod:close(Socket)
|
2009-08-31 20:37:52 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
ReceiverMod:become_controller(Receiver, Pid);
|
|
|
|
{error, _Reason} ->
|
|
|
|
SockMod:close(Socket),
|
|
|
|
case ReceiverMod of
|
|
|
|
ejabberd_receiver -> ReceiverMod:close(Receiver);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
independent -> ok;
|
|
|
|
raw ->
|
|
|
|
case Module:start({SockMod, Socket}, Opts) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
case SockMod:controlling_process(Socket, Pid) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, _Reason} -> SockMod:close(Socket)
|
|
|
|
end;
|
|
|
|
{error, _Reason} -> SockMod:close(Socket)
|
|
|
|
end
|
2006-09-25 05:51:11 +02:00
|
|
|
end.
|
2006-09-05 06:26:28 +02:00
|
|
|
|
|
|
|
connect(Addr, Port, Opts) ->
|
2016-10-22 12:01:45 +02:00
|
|
|
connect(Addr, Port, Opts, infinity, self()).
|
2008-12-26 12:45:08 +01:00
|
|
|
|
|
|
|
connect(Addr, Port, Opts, Timeout) ->
|
2016-10-22 12:01:45 +02:00
|
|
|
connect(Addr, Port, Opts, Timeout, self()).
|
|
|
|
|
|
|
|
connect(Addr, Port, Opts, Timeout, Owner) ->
|
2008-12-26 12:45:08 +01:00
|
|
|
case gen_tcp:connect(Addr, Port, Opts, Timeout) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, Socket} ->
|
|
|
|
Receiver = ejabberd_receiver:start(Socket, gen_tcp,
|
|
|
|
none),
|
|
|
|
SocketData = #socket_state{sockmod = gen_tcp,
|
|
|
|
socket = Socket, receiver = Receiver},
|
|
|
|
case gen_tcp:controlling_process(Socket, Receiver) of
|
|
|
|
ok ->
|
2016-10-22 12:01:45 +02:00
|
|
|
ejabberd_receiver:become_controller(Receiver, Owner),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, SocketData};
|
|
|
|
{error, _Reason} = Error -> gen_tcp:close(Socket), Error
|
|
|
|
end;
|
|
|
|
{error, _Reason} = Error -> Error
|
2006-09-05 06:26:28 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
starttls(SocketData, TLSOpts) ->
|
2016-02-03 16:13:16 +01:00
|
|
|
{ok, TLSSocket} = fast_tls:tcp_to_tls(SocketData#socket_state.socket, TLSOpts),
|
2006-09-05 06:26:28 +02:00
|
|
|
ejabberd_receiver:starttls(SocketData#socket_state.receiver, TLSSocket),
|
2016-02-03 16:13:16 +01:00
|
|
|
SocketData#socket_state{socket = TLSSocket, sockmod = fast_tls}.
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
starttls(SocketData, TLSOpts, Data) ->
|
2016-02-03 16:13:16 +01:00
|
|
|
{ok, TLSSocket} = fast_tls:tcp_to_tls(SocketData#socket_state.socket, TLSOpts),
|
2006-10-01 03:53:37 +02:00
|
|
|
ejabberd_receiver:starttls(SocketData#socket_state.receiver, TLSSocket),
|
|
|
|
send(SocketData, Data),
|
2016-02-03 16:13:16 +01:00
|
|
|
SocketData#socket_state{socket = TLSSocket, sockmod = fast_tls}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
2013-06-17 15:33:25 +02:00
|
|
|
compress(SocketData) -> compress(SocketData, undefined).
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
compress(SocketData, Data) ->
|
2013-04-08 11:12:54 +02:00
|
|
|
{ok, ZlibSocket} =
|
|
|
|
ejabberd_receiver:compress(SocketData#socket_state.receiver,
|
|
|
|
Data),
|
|
|
|
SocketData#socket_state{socket = ZlibSocket,
|
|
|
|
sockmod = ezlib}.
|
2006-10-01 03:53:37 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
reset_stream(SocketData)
|
|
|
|
when is_pid(SocketData#socket_state.receiver) ->
|
2009-08-31 20:37:52 +02:00
|
|
|
ejabberd_receiver:reset_stream(SocketData#socket_state.receiver);
|
2013-03-14 10:33:02 +01:00
|
|
|
reset_stream(SocketData)
|
|
|
|
when is_atom(SocketData#socket_state.receiver) ->
|
|
|
|
(SocketData#socket_state.receiver):reset_stream(SocketData#socket_state.socket).
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2013-06-17 15:33:25 +02:00
|
|
|
-spec send(socket_state(), iodata()) -> ok.
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
send(SocketData, Data) ->
|
2009-10-19 11:36:23 +02:00
|
|
|
case catch (SocketData#socket_state.sockmod):send(
|
|
|
|
SocketData#socket_state.socket, Data) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, timeout} ->
|
|
|
|
?INFO_MSG("Timeout on ~p:send",[SocketData#socket_state.sockmod]),
|
|
|
|
exit(normal);
|
|
|
|
Error ->
|
|
|
|
?DEBUG("Error in ~p:send: ~p",[SocketData#socket_state.sockmod, Error]),
|
|
|
|
exit(normal)
|
|
|
|
end.
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2009-10-19 11:36:23 +02:00
|
|
|
%% Can only be called when in c2s StateData#state.xml_socket is true
|
|
|
|
%% This function is used for HTTP bind
|
2015-04-15 10:47:10 +02:00
|
|
|
%% sockmod=ejabberd_http_ws|ejabberd_http_bind or any custom module
|
2016-07-26 10:29:17 +02:00
|
|
|
-spec send_xml(socket_state(), fxml:xmlel()) -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
send_xml(SocketData, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch
|
|
|
|
(SocketData#socket_state.sockmod):send_xml(SocketData#socket_state.socket,
|
|
|
|
Data).
|
2006-09-05 06:26:28 +02:00
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
change_shaper(SocketData, Shaper)
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_pid(SocketData#socket_state.receiver) ->
|
|
|
|
ejabberd_receiver:change_shaper(SocketData#socket_state.receiver,
|
|
|
|
Shaper);
|
2009-08-31 20:37:52 +02:00
|
|
|
change_shaper(SocketData, Shaper)
|
2013-03-14 10:33:02 +01:00
|
|
|
when is_atom(SocketData#socket_state.receiver) ->
|
|
|
|
(SocketData#socket_state.receiver):change_shaper(SocketData#socket_state.socket,
|
|
|
|
Shaper).
|
|
|
|
|
|
|
|
monitor(SocketData)
|
|
|
|
when is_pid(SocketData#socket_state.receiver) ->
|
|
|
|
erlang:monitor(process,
|
|
|
|
SocketData#socket_state.receiver);
|
|
|
|
monitor(SocketData)
|
|
|
|
when is_atom(SocketData#socket_state.receiver) ->
|
|
|
|
(SocketData#socket_state.receiver):monitor(SocketData#socket_state.socket).
|
2007-04-12 08:08:32 +02:00
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
get_sockmod(SocketData) ->
|
|
|
|
SocketData#socket_state.sockmod.
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
get_transport(#socket_state{sockmod = SockMod,
|
|
|
|
socket = Socket}) ->
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp -> tcp;
|
|
|
|
fast_tls -> tls;
|
|
|
|
ezlib ->
|
|
|
|
case ezlib:get_sockmod(Socket) of
|
2016-12-29 15:18:42 +01:00
|
|
|
gen_tcp -> tcp_zlib;
|
|
|
|
fast_tls -> tls_zlib
|
2016-07-18 14:01:32 +02:00
|
|
|
end;
|
2016-11-22 13:17:05 +01:00
|
|
|
ejabberd_bosh -> http_bind;
|
2016-07-18 14:01:32 +02:00
|
|
|
ejabberd_http_ws -> websocket
|
|
|
|
end.
|
|
|
|
|
2006-09-05 06:36:01 +02:00
|
|
|
get_peer_certificate(SocketData) ->
|
2016-02-03 16:13:16 +01:00
|
|
|
fast_tls:get_peer_certificate(SocketData#socket_state.socket).
|
2006-09-05 06:36:01 +02:00
|
|
|
|
|
|
|
get_verify_result(SocketData) ->
|
2016-02-03 16:13:16 +01:00
|
|
|
fast_tls:get_verify_result(SocketData#socket_state.socket).
|
2006-09-05 06:36:01 +02:00
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
close(SocketData) ->
|
|
|
|
ejabberd_receiver:close(SocketData#socket_state.receiver).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
sockname(#socket_state{sockmod = SockMod,
|
|
|
|
socket = Socket}) ->
|
2006-10-19 06:46:24 +02:00
|
|
|
case SockMod of
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_tcp -> inet:sockname(Socket);
|
|
|
|
_ -> SockMod:sockname(Socket)
|
2006-10-19 06:46:24 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
peername(#socket_state{sockmod = SockMod,
|
|
|
|
socket = Socket}) ->
|
2006-10-19 06:46:24 +02:00
|
|
|
case SockMod of
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_tcp -> inet:peername(Socket);
|
|
|
|
_ -> SockMod:peername(Socket)
|
2006-10-19 06:46:24 +02:00
|
|
|
end.
|