25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Rolled back the previous IP getting patch, and Report connection's IP address in ejabberd_http_poll

SVN Revision: 1450
This commit is contained in:
Christophe Romain 2008-07-16 12:55:29 +00:00
parent ece37b1dad
commit e0e7797e99
5 changed files with 44 additions and 65 deletions

View File

@ -11,6 +11,15 @@
ejabberd_service
* doc/guide.html: Likewise
2008-07-15 Alexey Shchepin <alexey@process-one.net>
* src/web/ejabberd_http_poll.erl: Report connection's IP address
* src/ejabberd_c2s.erl: Rolled back the previous IP getting patch
* src/ejabberd_sm.erl: Likewise
* src/ejabberd_receiver.erl: Likewise
* src/web/ejabberd_http_poll.erl: Likewise
2008-07-14 Badlop <badlop@process-one.net>
* doc/guide.tex: Update what permissions does enable-user grant

View File

@ -1267,9 +1267,6 @@ handle_info({route, From, To, Packet}, StateName, StateData) ->
handle_info({'DOWN', Monitor, _Type, _Object, _Info}, _StateName, StateData)
when Monitor == StateData#state.socket_monitor ->
{stop, normal, StateData};
handle_info({peername, IP}, StateName, StateData) ->
ejabberd_sm:set_session_ip(StateData#state.sid, IP),
fsm_next_state(StateName, StateData#state{ip = IP});
handle_info(Info, StateName, StateData) ->
?ERROR_MSG("Unexpected info: ~p", [Info]),
fsm_next_state(StateName, StateData).

View File

@ -269,8 +269,7 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
activate_socket(#state{socket = Socket,
sock_mod = SockMod,
c2s_pid = C2SPid}) ->
sock_mod = SockMod}) ->
PeerName =
case SockMod of
gen_tcp ->
@ -283,8 +282,7 @@ activate_socket(#state{socket = Socket,
case PeerName of
{error, _Reason} ->
self() ! {tcp_closed, Socket};
{ok, IP} ->
C2SPid ! {peername, IP},
{ok, _} ->
ok
end.

View File

@ -49,8 +49,7 @@
ctl_process/2,
get_session_pid/3,
get_user_info/3,
get_user_ip/3,
set_session_ip/2
get_user_ip/3
]).
%% gen_server callbacks
@ -165,18 +164,6 @@ get_user_info(User, Server, Resource) ->
[{node, Node}, {conn, Conn}, {ip, IP}]
end.
set_session_ip(SID, IP) ->
case mnesia:dirty_read(session, SID) of
[#session{info = Info} = Session] ->
NewInfo = case lists:keymember(ip, 1, Info) of
true -> lists:keyreplace(ip, 1, Info, {ip, IP});
false -> [{ip, IP}|Info]
end,
mnesia:dirty_write(Session#session{info = NewInfo});
_ ->
ok
end.
set_presence(SID, User, Server, Resource, Priority, Presence, Info) ->
set_session(SID, User, Server, Resource, Priority, Info),
ejabberd_hooks:run(set_presence_hook, jlib:nameprep(Server),

View File

@ -30,7 +30,7 @@
-behaviour(gen_fsm).
%% External exports
-export([start_link/2,
-export([start_link/3,
init/1,
handle_event/3,
handle_sync_event/4,
@ -50,16 +50,14 @@
-record(http_poll, {id, pid}).
-define(NULL_PEER, {{0, 0, 0, 0}, 0}).
-record(state, {id,
key,
socket,
output = "",
input = "",
waiting_input = false, %% {ReceiverPid, Tag}
last_receiver,
timer,
ip = ?NULL_PEER }).
timer}).
%-define(DBGFSM, true).
@ -77,19 +75,19 @@
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start(ID, Key) ->
start(ID, Key, IP) ->
mnesia:create_table(http_poll,
[{ram_copies, [node()]},
{attributes, record_info(fields, http_poll)}]),
supervisor:start_child(ejabberd_http_poll_sup, [ID, Key]).
supervisor:start_child(ejabberd_http_poll_sup, [ID, Key, IP]).
start_link(ID, Key) ->
gen_fsm:start_link(?MODULE, [ID, Key], ?FSMOPTS).
start_link(ID, Key, IP) ->
gen_fsm:start_link(?MODULE, [ID, Key, IP], ?FSMOPTS).
send({http_poll, FsmRef}, Packet) ->
send({http_poll, FsmRef, _IP}, Packet) ->
gen_fsm:sync_send_all_state_event(FsmRef, {send, Packet}).
setopts({http_poll, FsmRef}, Opts) ->
setopts({http_poll, FsmRef, _IP}, Opts) ->
case lists:member({active, once}, Opts) of
true ->
gen_fsm:send_all_state_event(FsmRef, {activate, self()});
@ -97,31 +95,27 @@ setopts({http_poll, FsmRef}, Opts) ->
ok
end.
sockname(_) ->
{ok, ?NULL_PEER}.
sockname(_Socket) ->
{ok, {{0, 0, 0, 0}, 0}}.
peername({http_poll, FsmRef}) ->
case catch gen_fsm:sync_send_all_state_event(FsmRef, peername, 1000) of
{ok, IP} -> {ok, IP};
_ -> {ok, ?NULL_PEER}
end;
peername(_) ->
{ok, ?NULL_PEER}.
peername({http_poll, _FsmRef, IP}) ->
{ok, IP}.
controlling_process(_Socket, _Pid) ->
ok.
close({http_poll, FsmRef}) ->
close({http_poll, FsmRef, _IP}) ->
catch gen_fsm:sync_send_all_state_event(FsmRef, close).
process([], #request{data = Data, ip = IP} = _Request) ->
process([], #request{data = Data,
ip = IP} = _Request) ->
case catch parse_request(Data) of
{ok, ID1, Key, NewKey, Packet} ->
ID = if
(ID1 == "0") or (ID1 == "mobile") ->
NewID = sha:sha(term_to_binary({now(), make_ref()})),
{ok, Pid} = start(NewID, ""),
{ok, Pid} = start(NewID, "", IP),
mnesia:transaction(
fun() ->
mnesia:write(#http_poll{id = NewID,
@ -131,7 +125,7 @@ process([], #request{data = Data, ip = IP} = _Request) ->
true ->
ID1
end,
case http_put(ID, Key, NewKey, Packet, IP) of
case http_put(ID, Key, NewKey, Packet) of
{error, not_exists} ->
{200, ?BAD_REQUEST, ""};
{error, bad_key} ->
@ -176,8 +170,8 @@ process(_, _Request) ->
%% ignore |
%% {stop, StopReason}
%%----------------------------------------------------------------------
init([ID, Key]) ->
?INFO_MSG("started: ~p", [{ID, Key}]),
init([ID, Key, IP]) ->
?INFO_MSG("started: ~p", [{ID, Key, IP}]),
%% Read c2s options from the first ejabberd_c2s configuration in
%% the config file listen section
@ -187,12 +181,12 @@ init([ID, Key]) ->
%% connector.
Opts = ejabberd_c2s_config:get_c2s_limits(),
ejabberd_socket:start(ejabberd_c2s, ?MODULE, {http_poll, self()}, Opts),
%{ok, C2SPid} = ejabberd_c2s:start({?MODULE, {http_poll, self()}}, Opts),
%ejabberd_c2s:become_controller(C2SPid),
Socket = {http_poll, self(), IP},
ejabberd_socket:start(ejabberd_c2s, ?MODULE, Socket, Opts),
Timer = erlang:start_timer(?HTTP_POLL_TIMEOUT, self(), []),
{ok, loop, #state{id = ID,
key = Key,
socket = Socket,
timer = Timer}}.
%%----------------------------------------------------------------------
@ -229,7 +223,7 @@ handle_event({activate, From}, StateName, StateData) ->
StateData#state{waiting_input = {From, ok}}};
Input ->
Receiver = From,
Receiver ! {tcp, {http_poll, self()}, list_to_binary(Input)},
Receiver ! {tcp, StateData#state.socket, list_to_binary(Input)},
{next_state, StateName, StateData#state{input = "",
waiting_input = false,
last_receiver = Receiver
@ -257,7 +251,7 @@ handle_sync_event(stop, _From, _StateName, StateData) ->
Reply = ok,
{stop, normal, Reply, StateData};
handle_sync_event({http_put, Key, NewKey, Packet, IP},
handle_sync_event({http_put, Key, NewKey, Packet},
_From, StateName, StateData) ->
Allow = case StateData#state.key of
"" ->
@ -279,10 +273,9 @@ handle_sync_event({http_put, Key, NewKey, Packet, IP},
Input = [StateData#state.input|Packet],
Reply = ok,
{reply, Reply, StateName, StateData#state{input = Input,
key = NewKey,
ip = IP}};
key = NewKey}};
{Receiver, _Tag} ->
Receiver ! {tcp, {http_poll, self()},
Receiver ! {tcp, StateData#state.socket,
list_to_binary(Packet)},
cancel_timer(StateData#state.timer),
Timer = erlang:start_timer(?HTTP_POLL_TIMEOUT, self(), []),
@ -291,8 +284,7 @@ handle_sync_event({http_put, Key, NewKey, Packet, IP},
StateData#state{waiting_input = false,
last_receiver = Receiver,
key = NewKey,
timer = Timer,
ip = IP}}
timer = Timer}}
end;
true ->
Reply = {error, bad_key},
@ -303,10 +295,6 @@ handle_sync_event(http_get, _From, StateName, StateData) ->
Reply = {ok, StateData#state.output},
{reply, Reply, StateName, StateData#state{output = ""}};
handle_sync_event(peername, _From, StateName, StateData) ->
Reply = {ok, StateData#state.ip},
{reply, Reply, StateName, StateData};
handle_sync_event(_Event, _From, StateName, StateData) ->
Reply = ok,
{reply, Reply, StateName, StateData}.
@ -345,10 +333,10 @@ terminate(_Reason, _StateName, StateData) ->
case StateData#state.last_receiver of
undefined -> ok;
Receiver ->
Receiver ! {tcp_closed, {http_poll, self()}}
Receiver ! {tcp_closed, StateData#state.socket}
end;
{Receiver, _Tag} ->
Receiver ! {tcp_closed, {http_poll, self()}}
Receiver ! {tcp_closed, StateData#state.socket}
end,
catch resend_messages(StateData#state.output),
ok.
@ -357,13 +345,13 @@ terminate(_Reason, _StateName, StateData) ->
%%% Internal functions
%%%----------------------------------------------------------------------
http_put(ID, Key, NewKey, Packet, IP) ->
http_put(ID, Key, NewKey, Packet) ->
case mnesia:dirty_read({http_poll, ID}) of
[] ->
{error, not_exists};
[#http_poll{pid = FsmRef}] ->
gen_fsm:sync_send_all_state_event(
FsmRef, {http_put, Key, NewKey, Packet, IP})
FsmRef, {http_put, Key, NewKey, Packet})
end.
http_get(ID) ->