2003-11-10 22:25:36 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_receiver.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose : Socket receiver for C2S and S2S connections
|
|
|
|
%%% Created : 10 Nov 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_receiver).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
|
|
|
-export([start/3,
|
|
|
|
receiver/4,
|
|
|
|
change_shaper/2,
|
2004-08-01 22:12:20 +02:00
|
|
|
reset_stream/1,
|
|
|
|
starttls/2]).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
|
|
|
|
start(Socket, SockMod, Shaper) ->
|
|
|
|
proc_lib:spawn(?MODULE, receiver, [Socket, SockMod, Shaper, self()]).
|
|
|
|
|
|
|
|
|
|
|
|
receiver(Socket, SockMod, Shaper, C2SPid) ->
|
2004-12-03 23:54:02 +01:00
|
|
|
XMLStreamState = xml_stream:new(C2SPid),
|
2003-11-10 22:25:36 +01:00
|
|
|
ShaperState = shaper:new(Shaper),
|
|
|
|
Timeout = case SockMod of
|
|
|
|
ssl ->
|
|
|
|
20;
|
|
|
|
_ ->
|
|
|
|
infinity
|
|
|
|
end,
|
2004-12-03 23:54:02 +01:00
|
|
|
receiver(Socket, SockMod, ShaperState, C2SPid, XMLStreamState, Timeout).
|
2003-11-10 22:25:36 +01:00
|
|
|
|
2004-12-03 23:54:02 +01:00
|
|
|
receiver(Socket, SockMod, ShaperState, C2SPid, XMLStreamState, Timeout) ->
|
2004-08-01 22:12:20 +02:00
|
|
|
Res = (catch SockMod:recv(Socket, 0, Timeout)),
|
|
|
|
case Res of
|
|
|
|
{ok, Data} ->
|
|
|
|
receive
|
|
|
|
{starttls, TLSSocket} ->
|
2004-12-03 23:54:02 +01:00
|
|
|
xml_stream:close(XMLStreamState),
|
|
|
|
XMLStreamState1 = xml_stream:new(C2SPid),
|
2004-08-01 22:12:20 +02:00
|
|
|
TLSRes = tls:recv_data(TLSSocket, Data),
|
|
|
|
receiver1(TLSSocket, tls,
|
2004-12-03 23:54:02 +01:00
|
|
|
ShaperState, C2SPid, XMLStreamState1, Timeout,
|
2004-08-01 22:12:20 +02:00
|
|
|
TLSRes)
|
|
|
|
after 0 ->
|
|
|
|
receiver1(Socket, SockMod,
|
2004-12-03 23:54:02 +01:00
|
|
|
ShaperState, C2SPid, XMLStreamState, Timeout,
|
2004-08-01 22:12:20 +02:00
|
|
|
Res)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
receiver1(Socket, SockMod,
|
2004-12-03 23:54:02 +01:00
|
|
|
ShaperState, C2SPid, XMLStreamState, Timeout, Res)
|
2004-08-01 22:12:20 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2004-12-03 23:54:02 +01:00
|
|
|
receiver1(Socket, SockMod, ShaperState, C2SPid, XMLStreamState, Timeout, Res) ->
|
2004-08-01 22:12:20 +02:00
|
|
|
case Res of
|
2003-11-10 22:25:36 +01:00
|
|
|
{ok, Text} ->
|
|
|
|
ShaperSt1 = receive
|
|
|
|
{change_shaper, Shaper} ->
|
|
|
|
shaper:new(Shaper)
|
|
|
|
after 0 ->
|
|
|
|
ShaperState
|
|
|
|
end,
|
|
|
|
NewShaperState = shaper:update(ShaperSt1, size(Text)),
|
2004-12-03 23:54:02 +01:00
|
|
|
XMLStreamState1 = receive
|
|
|
|
reset_stream ->
|
|
|
|
xml_stream:close(XMLStreamState),
|
|
|
|
xml_stream:new(C2SPid)
|
|
|
|
after 0 ->
|
|
|
|
XMLStreamState
|
|
|
|
end,
|
|
|
|
XMLStreamState2 = xml_stream:parse(XMLStreamState1, Text),
|
|
|
|
receiver(Socket, SockMod, NewShaperState, C2SPid, XMLStreamState2,
|
2003-11-10 22:25:36 +01:00
|
|
|
Timeout);
|
|
|
|
{error, timeout} ->
|
2004-12-03 23:54:02 +01:00
|
|
|
receiver(Socket, SockMod, ShaperState, C2SPid, XMLStreamState,
|
2003-11-10 22:25:36 +01:00
|
|
|
Timeout);
|
|
|
|
{error, Reason} ->
|
2004-12-03 23:54:02 +01:00
|
|
|
xml_stream:close(XMLStreamState),
|
2003-11-10 22:25:36 +01:00
|
|
|
gen_fsm:send_event(C2SPid, closed),
|
|
|
|
ok;
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("(~w) abnormal ~w:recv termination:~n\t~p~n",
|
|
|
|
[Socket, SockMod, Reason]),
|
2004-12-03 23:54:02 +01:00
|
|
|
xml_stream:close(XMLStreamState),
|
2003-11-10 22:25:36 +01:00
|
|
|
gen_fsm:send_event(C2SPid, closed),
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
change_shaper(Pid, Shaper) ->
|
|
|
|
Pid ! {change_shaper, Shaper}.
|
|
|
|
|
|
|
|
reset_stream(Pid) ->
|
|
|
|
Pid ! reset_stream.
|
|
|
|
|
2004-08-01 22:12:20 +02:00
|
|
|
starttls(Pid, TLSSocket) ->
|
|
|
|
Pid ! {starttls, TLSSocket}.
|
|
|
|
|
2003-11-10 22:25:36 +01:00
|
|
|
|