28 end |
28 end |
29 |
29 |
30 function send_to_host(from_host, to_host, data) |
30 function send_to_host(from_host, to_host, data) |
31 if hosts[to_host] then |
31 if hosts[to_host] then |
32 -- Write to connection |
32 -- Write to connection |
33 hosts[to_host].send(data); |
33 hosts[to_host].sends2s(data); |
34 log("debug", "stanza sent over s2s"); |
34 log("debug", "stanza sent over s2s"); |
35 else |
35 else |
36 log("debug", "opening a new outgoing connection for this stanza"); |
36 log("debug", "opening a new outgoing connection for this stanza"); |
37 local host_session = new_outgoing(from_host, to_host); |
37 local host_session = new_outgoing(from_host, to_host); |
38 -- Store in buffer |
38 -- Store in buffer |
52 session.trace = newproxy(true); |
52 session.trace = newproxy(true); |
53 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end; |
53 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("s2s session got collected, now "..open_sessions.." s2s sessions are allocated") end; |
54 end |
54 end |
55 open_sessions = open_sessions + 1; |
55 open_sessions = open_sessions + 1; |
56 local w = conn.write; |
56 local w = conn.write; |
57 session.send = function (t) w(tostring(t)); end |
57 session.sends2s = function (t) w(tostring(t)); end |
58 return session; |
58 return session; |
59 end |
59 end |
60 |
60 |
61 function new_outgoing(from_host, to_host) |
61 function new_outgoing(from_host, to_host) |
62 local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; |
62 local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; |
79 local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); |
79 local conn_name = "s2sout"..tostring(conn):match("[a-f0-9]*$"); |
80 host_session.log = logger_init(conn_name); |
80 host_session.log = logger_init(conn_name); |
81 end |
81 end |
82 |
82 |
83 local w = conn.write; |
83 local w = conn.write; |
84 host_session.send = function (t) w(tostring(t)); end |
84 host_session.sends2s = function (t) w(tostring(t)); end |
85 |
85 |
86 conn.write(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host)); |
86 conn.write(format([[<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' from='%s' to='%s' version='1.0'>]], from_host, to_host)); |
87 |
87 |
88 return host_session; |
88 return host_session; |
89 end |
89 end |
90 |
90 |
91 function streamopened(session, attr) |
91 function streamopened(session, attr) |
92 session.log("debug", "s2s stream opened"); |
92 session.log("debug", "s2s stream opened"); |
93 local send = session.send; |
93 local send = session.sends2s; |
94 |
94 |
95 session.version = tonumber(attr.version) or 0; |
95 session.version = tonumber(attr.version) or 0; |
96 if session.version >= 1.0 and not (attr.to and attr.from) then |
96 if session.version >= 1.0 and not (attr.to and attr.from) then |
97 print("to: "..tostring(attr.to).." from: "..tostring(attr.from)); |
97 print("to: "..tostring(attr.to).." from: "..tostring(attr.from)); |
98 --error(session.to_host.." failed to specify 'to' or 'from' hostname as per RFC"); |
98 --error(session.to_host.." failed to specify 'to' or 'from' hostname as per RFC"); |
116 if not session.dialback_verifying then |
116 if not session.dialback_verifying then |
117 -- generate dialback key |
117 -- generate dialback key |
118 if not attr.id then error("stream response did not give us a streamid!!!"); end |
118 if not attr.id then error("stream response did not give us a streamid!!!"); end |
119 session.streamid = attr.id; |
119 session.streamid = attr.id; |
120 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host); |
120 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host); |
121 session.send(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key)); |
121 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key)); |
122 session.log("info", "sent dialback key on outgoing s2s stream"); |
122 session.log("info", "sent dialback key on outgoing s2s stream"); |
123 else |
123 else |
124 mark_connected(session); |
124 mark_connected(session); |
125 end |
125 end |
126 end |
126 end |
161 |
161 |
162 return true; |
162 return true; |
163 end |
163 end |
164 |
164 |
165 function mark_connected(session) |
165 function mark_connected(session) |
166 local sendq, send = session.sendq, session.send; |
166 local sendq, send = session.sendq, session.sends2s; |
|
167 |
|
168 local from, to = session.from_host, session.to_host; |
|
169 |
167 session.log("debug", session.direction.." s2s connection "..session.from_host.."->"..session.to_host.." is now complete"); |
170 session.log("debug", session.direction.." s2s connection "..session.from_host.."->"..session.to_host.." is now complete"); |
|
171 |
|
172 local send_to_host = send_to_host; |
|
173 function session.send(data) send_to_host(from, to, data); end |
|
174 |
168 if sendq then |
175 if sendq then |
169 session.log("debug", "sending queued stanzas across new outgoing connection"); |
176 session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host); |
170 for i, data in ipairs(sendq) do |
177 for i, data in ipairs(sendq) do |
171 send(data); |
178 send(data); |
172 sendq[i] = nil; |
179 sendq[i] = nil; |
173 end |
180 end |
174 session.sendq = nil; |
181 session.sendq = nil; |