buf_new(XMPPDomain)),
Opts2}
end,
- case ejabberd_c2s:start({?MODULE, Socket}, [{receiver, self()}|Opts]) of
+ case ejabberd_c2s:start(?MODULE, Socket, [{receiver, self()}|Opts]) of
{ok, C2SPid} ->
ejabberd_c2s:accept(C2SPid),
Inactivity = gen_mod:get_module_opt(XMPPDomain,
-protocol({rfc, 6121}).
%% ejabberd_listener callbacks
--export([start/2, start_link/2, accept/1, listen_opt_type/1, listen_options/0]).
+-export([start/3, start_link/3, accept/1, listen_opt_type/1, listen_options/0]).
%% ejabberd_config callbacks
-export([opt_type/1, transform_listen_option/2]).
%% xmpp_stream_in callbacks
%%%===================================================================
%%% ejabberd_listener API
%%%===================================================================
-start(SockData, Opts) ->
- xmpp_stream_in:start(?MODULE, [SockData, Opts],
+start(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
-start_link(SockData, Opts) ->
- xmpp_stream_in:start_link(?MODULE, [SockData, Opts],
+start_link(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start_link(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
accept(Ref) ->
-author('alexey@process-one.net').
%% External exports
--export([start/2, start_link/2,
+-export([start/3, start_link/3,
accept/1, receive_headers/1, recv_file/2,
transform_listen_option/2, listen_opt_type/1,
listen_options/0]).
--export([init/2, opt_type/1]).
+-export([init/3, opt_type/1]).
-include("logger.hrl").
-include("xmpp.hrl").
-define(SEND_BUF, 65536).
-define(MAX_POST_SIZE, 20971520). %% 20Mb
-start(SockData, Opts) ->
+start(SockMod, Socket, Opts) ->
{ok,
proc_lib:spawn(ejabberd_http, init,
- [SockData, Opts])}.
+ [SockMod, Socket, Opts])}.
-start_link(SockData, Opts) ->
+start_link(SockMod, Socket, Opts) ->
{ok,
proc_lib:spawn_link(ejabberd_http, init,
- [SockData, Opts])}.
+ [SockMod, Socket, Opts])}.
-init({SockMod, Socket}, Opts) ->
+init(SockMod, Socket, Opts) ->
TLSEnabled = proplists:get_bool(tls, Opts),
TLSOpts1 = lists:filter(fun ({ciphers, _}) -> true;
({dhfile, _}) -> true;
Socket = {http_ws, self(), IP},
?DEBUG("Client connected through websocket ~p",
[Socket]),
- case ejabberd_c2s:start({?MODULE, Socket}, [{receiver, self()}|Opts]) of
+ case ejabberd_c2s:start(?MODULE, Socket, [{receiver, self()}|Opts]) of
{ok, C2SPid} ->
ejabberd_c2s:accept(C2SPid),
Timer = erlang:start_timer(WSTimeout, self(), []),
-type endpoint() :: {inet:port_number(), inet:ip_address(), transport()}.
-type listen_opts() :: [proplists:property()].
-type listener() :: {endpoint(), module(), listen_opts()}.
+-type sockmod() :: gen_tcp.
+-type socket() :: inet:socket().
--callback start({gen_tcp, inet:socket()}, listen_opts()) ->
+-callback start(sockmod(), socket(), listen_opts()) ->
{ok, pid()} | {error, any()} | ignore.
--callback start_link({gen_tcp, inet:socket()}, listen_opts()) ->
+-callback start_link(sockmod(), socket(), listen_opts()) ->
{ok, pid()} | {error, any()} | ignore.
-callback accept(pid()) -> any().
-callback listen_opt_type(atom()) -> fun((term()) -> term()).
-spec accept(inet:socket(), module(), listen_opts(), atom()) -> no_return().
accept(ListenSocket, Module, Opts, Sup) ->
Interval = proplists:get_value(accept_interval, Opts, 0),
- accept(ListenSocket, Module, Opts, Sup, Interval).
+ Arity = case erlang:function_exported(Module, start, 3) of
+ true -> 3;
+ false -> 2
+ end,
+ accept(ListenSocket, Module, Opts, Sup, Interval, Arity).
--spec accept(inet:socket(), module(), listen_opts(), atom(), non_neg_integer()) -> no_return().
-accept(ListenSocket, Module, Opts, Sup, Interval) ->
+-spec accept(inet:socket(), module(), listen_opts(), atom(),
+ non_neg_integer(), 2|3) -> no_return().
+accept(ListenSocket, Module, Opts, Sup, Interval, Arity) ->
NewInterval = check_rate_limit(Interval),
case gen_tcp:accept(ListenSocket) of
{ok, Socket} ->
gen_tcp:close(Socket);
{{Addr, Port}, {PAddr, PPort}} = SP ->
Opts2 = [{sock_peer_name, SP} | Opts],
- Receiver = case start_connection(Module, Socket, Opts2, Sup) of
+ Receiver = case start_connection(Module, Arity, Socket, Opts2, Sup) of
{ok, RecvPid} ->
RecvPid;
_ ->
_ ->
case {inet:sockname(Socket), inet:peername(Socket)} of
{{ok, {Addr, Port}}, {ok, {PAddr, PPort}}} ->
- Receiver = case start_connection(Module, Socket, Opts, Sup) of
+ Receiver = case start_connection(Module, Arity, Socket, Opts, Sup) of
{ok, RecvPid} ->
RecvPid;
_ ->
gen_tcp:close(Socket)
end
end,
- accept(ListenSocket, Module, Opts, Sup, NewInterval);
+ accept(ListenSocket, Module, Opts, Sup, NewInterval, Arity);
{error, Reason} ->
?ERROR_MSG("(~w) Failed TCP accept: ~s",
[ListenSocket, inet:format_error(Reason)]),
- accept(ListenSocket, Module, Opts, Sup, NewInterval)
+ accept(ListenSocket, Module, Opts, Sup, NewInterval, Arity)
end.
-spec udp_recv(inet:socket(), module(), listen_opts()) -> no_return().
throw({error, Reason})
end.
--spec start_connection(module(), inet:socket(), listen_opts(), atom()) ->
+-spec start_connection(module(), 2|3, inet:socket(), listen_opts(), atom()) ->
{ok, pid()} | {error, any()} | ignore.
-start_connection(Module, Socket, Opts, Sup) ->
+start_connection(Module, Arity, Socket, Opts, Sup) ->
Res = case Sup of
- undefined -> Module:start({gen_tcp, Socket}, Opts);
+ undefined ->
+ case Arity of
+ 3 -> Module:start(gen_tcp, Socket, Opts);
+ 2 -> Module:start({gen_tcp, Socket}, Opts)
+ end;
_ -> supervisor:start_child(Sup, [{gen_tcp, Socket}, Opts])
end,
case Res of
case code:ensure_loaded(Mod) of
{module, Mod} ->
lists:foreach(
- fun({Fun, Arity}) ->
- case erlang:function_exported(Mod, Fun, Arity) of
+ fun({Fun, Arities}) ->
+ case lists:any(
+ fun(Arity) ->
+ erlang:function_exported(Mod, Fun, Arity)
+ end, Arities) of
true -> ok;
false ->
?ERROR_MSG("Failed to load listening module ~s, "
"The module is either not a listening module "
"or it is a third-party module which "
"requires update",
- [Mod, Fun, Arity]),
+ [Mod, Fun, hd(Arities)]),
erlang:error(badarg)
end
- end, [{start, 2}, {start_link, 2},
- {accept, 1}, {listen_options, 0}]);
+ end, [{start, [3,2]}, {start_link, [3,2]},
+ {accept, [1]}, {listen_options, [0]}]);
_ ->
?ERROR_MSG("Failed to load unknown listening module ~s: "
"make sure there is no typo and ~s.beam "
-behaviour(ejabberd_listener).
%% ejabberd_listener callbacks
--export([start/2, start_link/2, accept/1, listen_opt_type/1, listen_options/0]).
+-export([start/3, start_link/3, accept/1, listen_opt_type/1, listen_options/0]).
%% xmpp_stream_in callbacks
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3]).
%%%===================================================================
%%% API
%%%===================================================================
-start(SockData, Opts) ->
- xmpp_stream_in:start(?MODULE, [SockData, Opts],
+start(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
-start_link(SockData, Opts) ->
- xmpp_stream_in:start_link(?MODULE, [SockData, Opts],
+start_link(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start_link(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
close(Ref) ->
-protocol({xep, 114, '1.6'}).
%% ejabberd_listener callbacks
--export([start/2, start_link/2, accept/1]).
+-export([start/3, start_link/3, accept/1]).
-export([listen_opt_type/1, listen_options/0, transform_listen_option/2]).
%% xmpp_stream_in callbacks
-export([init/1, handle_info/2, terminate/2, code_change/3]).
%%%===================================================================
%%% API
%%%===================================================================
-start(SockData, Opts) ->
- xmpp_stream_in:start(?MODULE, [SockData, Opts],
+start(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
-start_link(SockData, Opts) ->
- xmpp_stream_in:start_link(?MODULE, [SockData, Opts],
+start_link(SockMod, Socket, Opts) ->
+ xmpp_stream_in:start_link(?MODULE, [{SockMod, Socket}, Opts],
ejabberd_config:fsm_limit_opts(Opts)).
accept(Ref) ->
%%%-------------------------------------------------------------------
%%% File : ejabberd_sip.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
-%%% Purpose :
+%%% Purpose :
%%% Created : 30 Apr 2017 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%
%%%
-ifndef(SIP).
-include("logger.hrl").
--export([accept/1, start/2, start_link/2, listen_options/0]).
+-export([accept/1, start/3, start_link/3, listen_options/0]).
fail() ->
?CRITICAL_MSG("Listening module ~s is not available: "
"ejabberd is not compiled with SIP support",
fail().
listen_options() ->
fail().
-start(_, _) ->
+start(_, _, _) ->
fail().
-start_link(_, _) ->
+start_link(_, _, _) ->
fail().
-else.
%% API
--export([tcp_init/2, udp_init/2, udp_recv/5, start/2,
- start_link/2, accept/1, listen_options/0]).
+-export([tcp_init/2, udp_init/2, udp_recv/5, start/3,
+ start_link/3, accept/1, listen_options/0]).
%%%===================================================================
udp_recv(Sock, Addr, Port, Data, Opts) ->
esip_socket:udp_recv(Sock, Addr, Port, Data, Opts).
-start(Opaque, Opts) ->
- esip_socket:start(Opaque, Opts).
+start(SockMod, Socket, Opts) ->
+ esip_socket:start({SockMod, Socket}, Opts).
-start_link({gen_tcp, Sock}, Opts) ->
+start_link(gen_tcp, Sock, Opts) ->
esip_socket:start_link(Sock, Opts).
accept(_) ->
-ifndef(STUN).
-include("logger.hrl").
--export([accept/1, start/2, start_link/2, listen_options/0]).
+-export([accept/1, start/3, start_link/3, listen_options/0]).
fail() ->
?CRITICAL_MSG("Listening module ~s is not available: "
"ejabberd is not compiled with STUN/TURN support",
fail().
listen_options() ->
fail().
-start(_, _) ->
+start(_, _, _) ->
fail().
-start_link(_, _) ->
+start_link(_, _, _) ->
fail().
-else.
--export([tcp_init/2, udp_init/2, udp_recv/5, start/2,
- start_link/2, accept/1, listen_opt_type/1, listen_options/0]).
+-export([tcp_init/2, udp_init/2, udp_recv/5, start/3,
+ start_link/3, accept/1, listen_opt_type/1, listen_options/0]).
-include("logger.hrl").
udp_recv(Socket, Addr, Port, Packet, Opts) ->
stun:udp_recv(Socket, Addr, Port, Packet, Opts).
-start(Opaque, Opts) ->
- stun:start(Opaque, Opts).
+start(SockMod, Socket, Opts) ->
+ stun:start({SockMod, Socket}, Opts).
-start_link({gen_tcp, Sock}, Opts) ->
- stun:start_link(Sock, Opts).
+start_link(_SockMod, Socket, Opts) ->
+ stun:start_link(Socket, Opts).
accept(_Pid) ->
ok.
-author('badlop@process-one.net').
--export([start/2, start_link/2, handler/2, process/2, accept/1,
+-export([start/3, start_link/3, handler/2, process/2, accept/1,
transform_listen_option/2, listen_opt_type/1, listen_options/0]).
-include("logger.hrl").
%% Listener interface
%% -----------------------------
-start({gen_tcp = _SockMod, Socket}, Opts) ->
- ejabberd_http:start({gen_tcp, Socket}, [{xmlrpc, true}|Opts]).
+start(gen_tcp = _SockMod, Socket, Opts) ->
+ ejabberd_http:start(gen_tcp, Socket, [{xmlrpc, true}|Opts]).
-start_link({gen_tcp = _SockMod, Socket}, Opts) ->
- ejabberd_http:start_link({gen_tcp, Socket}, [{xmlrpc, true}|Opts]).
+start_link(gen_tcp = _SockMod, Socket, Opts) ->
+ ejabberd_http:start_link(gen_tcp, Socket, [{xmlrpc, true}|Opts]).
accept(Pid) ->
ejabberd_http:accept(Pid).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% ejabberd_listener API
--export([start_link/2, listen_opt_type/1, listen_options/0, accept/1]).
+-export([start/3, start_link/3, listen_opt_type/1, listen_options/0, accept/1]).
%% Legacy ejabberd_listener API
-export([become_controller/2, socket_type/0]).
%% API
%%%===================================================================
%%% API
%%%===================================================================
-start({SockMod, Sock}, ListenOpts) ->
- mod_mqtt_session:start(SockMod, Sock, ListenOpts);
+start(SockMod, Sock, ListenOpts) ->
+ mod_mqtt_session:start(SockMod, Sock, ListenOpts).
+
start(Host, Opts) ->
gen_mod:start_child(?MODULE, Host, Opts).
-start_link({SockMod, Sock}, ListenOpts) ->
+start_link(SockMod, Sock, ListenOpts) ->
mod_mqtt_session:start_link(SockMod, Sock, ListenOpts).
stop(Host) ->
wait_for_request/2, wait_for_activation/2,
stream_established/2]).
--export([start/2, stop/1, start_link/2, start_link/3, activate/2,
+-export([start/3, stop/1, start_link/3, activate/2,
relay/3, accept/1, listen_opt_type/1,
listen_options/0]).
%%-------------------------------
-start({gen_tcp, Socket}, Opts1) ->
+start(gen_tcp, Socket, Opts1) ->
{[{server_host, Host}], Opts} = lists:partition(
fun({server_host, _}) -> true;
(_) -> false
end, Opts1),
p1_fsm:start(?MODULE, [Socket, Host, Opts], []).
-start_link({gen_tcp, Socket}, Opts1) ->
+start_link(gen_tcp, Socket, Opts1) ->
{[{server_host, Host}], Opts} = lists:partition(
fun({server_host, _}) -> true;
(_) -> false
end, Opts1),
- start_link(Socket, Host, Opts).
-
+ start_link(Socket, Host, Opts);
start_link(Socket, Host, Opts) ->
p1_fsm:start_link(?MODULE, [Socket, Host, Opts], []).