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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:11:32 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2006-09-05 06:26:28 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_socket).
|
|
|
|
-author('alexey@process-one.net').
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start/4,
|
|
|
|
connect/3,
|
2008-12-26 12:45:08 +01:00
|
|
|
connect/4,
|
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,
|
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").
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
-record(socket_state, {sockmod, socket, receiver}).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
2009-10-19 11:36:23 +02:00
|
|
|
%% Function:
|
2006-09-05 06:26:28 +02:00
|
|
|
%% Description:
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start(Module, SockMod, Socket, Opts) ->
|
2006-09-25 05:51:11 +02:00
|
|
|
case Module:socket_type() of
|
|
|
|
xml_stream ->
|
|
|
|
MaxStanzaSize =
|
|
|
|
case lists:keysearch(max_stanza_size, 1, Opts) of
|
|
|
|
{value, {_, Size}} -> Size;
|
|
|
|
_ -> infinity
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
{ReceiverMod, Receiver, RecRef} =
|
|
|
|
case catch SockMod:custom_receiver(Socket) of
|
|
|
|
{receiver, RecMod, RecPid} ->
|
|
|
|
{RecMod, RecPid, RecMod};
|
2009-10-19 11:36:23 +02:00
|
|
|
_ ->
|
2009-08-31 20:37:52 +02:00
|
|
|
RecPid = ejabberd_receiver:start(
|
|
|
|
Socket, SockMod, none, MaxStanzaSize),
|
|
|
|
{ejabberd_receiver, RecPid, RecPid}
|
|
|
|
end,
|
2006-09-25 05:51:11 +02:00
|
|
|
SocketData = #socket_state{sockmod = SockMod,
|
|
|
|
socket = Socket,
|
2009-08-31 20:37:52 +02:00
|
|
|
receiver = RecRef},
|
2008-05-05 18:22:06 +02:00
|
|
|
case Module:start({?MODULE, SocketData}, Opts) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
case SockMod:controlling_process(Socket, Receiver) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, _Reason} ->
|
|
|
|
SockMod:close(Socket)
|
|
|
|
end,
|
2009-08-31 20:37:52 +02:00
|
|
|
ReceiverMod:become_controller(Receiver, Pid);
|
2006-09-25 05:51:11 +02:00
|
|
|
{error, _Reason} ->
|
|
|
|
SockMod:close(Socket)
|
2008-05-05 18:22:06 +02:00
|
|
|
end;
|
2008-10-12 13:11:29 +02:00
|
|
|
independent ->
|
|
|
|
ok;
|
2006-09-25 05:51:11 +02:00
|
|
|
raw ->
|
2008-05-05 18:22:06 +02:00
|
|
|
case Module:start({SockMod, Socket}, Opts) of
|
|
|
|
{ok, Pid} ->
|
|
|
|
case SockMod:controlling_process(Socket, Pid) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, _Reason} ->
|
|
|
|
SockMod:close(Socket)
|
|
|
|
end;
|
2006-09-25 05:51:11 +02:00
|
|
|
{error, _Reason} ->
|
|
|
|
SockMod:close(Socket)
|
2006-10-01 03:53:37 +02:00
|
|
|
end
|
2006-09-25 05:51:11 +02:00
|
|
|
end.
|
2006-09-05 06:26:28 +02:00
|
|
|
|
|
|
|
connect(Addr, Port, Opts) ->
|
2008-12-26 12:45:08 +01:00
|
|
|
connect(Addr, Port, Opts, infinity).
|
|
|
|
|
|
|
|
connect(Addr, Port, Opts, Timeout) ->
|
|
|
|
case gen_tcp:connect(Addr, Port, Opts, Timeout) of
|
2006-09-05 06:26:28 +02:00
|
|
|
{ok, Socket} ->
|
|
|
|
Receiver = ejabberd_receiver:start(Socket, gen_tcp, none),
|
|
|
|
SocketData = #socket_state{sockmod = gen_tcp,
|
|
|
|
socket = Socket,
|
|
|
|
receiver = Receiver},
|
|
|
|
Pid = self(),
|
|
|
|
case gen_tcp:controlling_process(Socket, Receiver) of
|
|
|
|
ok ->
|
|
|
|
ejabberd_receiver:become_controller(Receiver, Pid),
|
|
|
|
{ok, SocketData};
|
|
|
|
{error, _Reason} = Error ->
|
|
|
|
gen_tcp:close(Socket),
|
|
|
|
Error
|
|
|
|
end;
|
2006-09-25 05:51:11 +02:00
|
|
|
{error, _Reason} = Error ->
|
2006-09-05 06:26:28 +02:00
|
|
|
Error
|
|
|
|
end.
|
|
|
|
|
|
|
|
starttls(SocketData, TLSOpts) ->
|
|
|
|
{ok, TLSSocket} = tls:tcp_to_tls(SocketData#socket_state.socket, TLSOpts),
|
|
|
|
ejabberd_receiver:starttls(SocketData#socket_state.receiver, TLSSocket),
|
|
|
|
SocketData#socket_state{socket = TLSSocket, sockmod = tls}.
|
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
starttls(SocketData, TLSOpts, Data) ->
|
|
|
|
{ok, TLSSocket} = tls:tcp_to_tls(SocketData#socket_state.socket, TLSOpts),
|
|
|
|
ejabberd_receiver:starttls(SocketData#socket_state.receiver, TLSSocket),
|
|
|
|
send(SocketData, Data),
|
|
|
|
SocketData#socket_state{socket = TLSSocket, sockmod = tls}.
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
compress(SocketData) ->
|
|
|
|
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
|
|
|
|
SocketData#socket_state.sockmod,
|
|
|
|
SocketData#socket_state.socket),
|
|
|
|
ejabberd_receiver:compress(SocketData#socket_state.receiver, ZlibSocket),
|
|
|
|
SocketData#socket_state{socket = ZlibSocket, sockmod = ejabberd_zlib}.
|
|
|
|
|
2006-10-01 03:53:37 +02:00
|
|
|
compress(SocketData, Data) ->
|
|
|
|
{ok, ZlibSocket} = ejabberd_zlib:enable_zlib(
|
|
|
|
SocketData#socket_state.sockmod,
|
|
|
|
SocketData#socket_state.socket),
|
|
|
|
ejabberd_receiver:compress(SocketData#socket_state.receiver, ZlibSocket),
|
|
|
|
send(SocketData, Data),
|
|
|
|
SocketData#socket_state{socket = ZlibSocket, sockmod = ejabberd_zlib}.
|
|
|
|
|
2009-08-31 20:37:52 +02:00
|
|
|
reset_stream(SocketData) when is_pid(SocketData#socket_state.receiver) ->
|
|
|
|
ejabberd_receiver:reset_stream(SocketData#socket_state.receiver);
|
|
|
|
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
|
|
|
|
2009-10-19 11:36:23 +02:00
|
|
|
%% sockmod=gen_tcp|tls|ejabberd_zlib
|
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
|
|
|
|
%% sockmod=ejabberd_http_poll|ejabberd_http_bind or any custom module
|
2009-08-31 20:37:52 +02:00
|
|
|
send_xml(SocketData, Data) ->
|
|
|
|
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)
|
|
|
|
when is_pid(SocketData#socket_state.receiver) ->
|
|
|
|
ejabberd_receiver:change_shaper(SocketData#socket_state.receiver, Shaper);
|
|
|
|
change_shaper(SocketData, Shaper)
|
|
|
|
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.
|
|
|
|
|
2006-09-05 06:36:01 +02:00
|
|
|
get_peer_certificate(SocketData) ->
|
|
|
|
tls:get_peer_certificate(SocketData#socket_state.socket).
|
|
|
|
|
|
|
|
get_verify_result(SocketData) ->
|
|
|
|
tls:get_verify_result(SocketData#socket_state.socket).
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
close(SocketData) ->
|
|
|
|
ejabberd_receiver:close(SocketData#socket_state.receiver).
|
|
|
|
|
2006-10-19 06:46:24 +02:00
|
|
|
sockname(#socket_state{sockmod = SockMod, socket = Socket}) ->
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:sockname(Socket);
|
|
|
|
_ ->
|
|
|
|
SockMod:sockname(Socket)
|
|
|
|
end.
|
|
|
|
|
|
|
|
peername(#socket_state{sockmod = SockMod, socket = Socket}) ->
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:peername(Socket);
|
|
|
|
_ ->
|
|
|
|
SockMod:peername(Socket)
|
|
|
|
end.
|
|
|
|
|
2006-09-05 06:26:28 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%%====================================================================
|