1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "socketengine.h"
21 #include "treeserver.h"
23 #include "treesocket.h"
24 #include "resolvers.h"
26 /* Handle ERROR command */
27 void TreeSocket::Error(parameterlist ¶ms)
29 std::string msg = params.size() ? params[0] : "";
30 SetError("received ERROR " + msg);
33 void TreeSocket::Split(const std::string& line, std::string& prefix, std::string& command, parameterlist& params)
35 irc::tokenstream tokens(line);
37 if (!tokens.GetToken(prefix))
42 prefix = prefix.substr(1);
46 this->SendError("BUG (?) Empty prefix received: " + line);
49 if (!tokens.GetToken(command))
51 this->SendError("BUG (?) Empty command received: " + line);
61 this->SendError("BUG (?) Empty command received: " + line);
64 while (tokens.GetToken(param))
66 params.push_back(param);
70 void TreeSocket::ProcessLine(std::string &line)
76 ServerInstance->Logs->Log("m_spanningtree", RAWIO, "S[%d] I %s", this->GetFd(), line.c_str());
78 Split(line, prefix, command, params);
83 switch (this->LinkState)
88 * Waiting for SERVER command from remote server. Server initiating
89 * the connection sends the first SERVER command, listening server
90 * replies with theirs if its happy, then if the initiator is happy,
91 * it starts to send its net sync, which starts the merge, otherwise
94 if (command == "PASS")
97 * Ignore this silently. Some services packages insist on sending PASS, even
98 * when it is not required (i.e. by us). We have to ignore this here, otherwise
99 * as it's an unknown command (effectively), it will cause the connection to be
100 * closed, which probably isn't what people want. -- w00t
103 else if (command == "SERVER")
105 this->Inbound_Server(params);
107 else if (command == "ERROR")
111 else if (command == "USER")
113 this->SendError("Client connections to this port are prohibited.");
115 else if (command == "CAPAB")
121 this->SendError(std::string("Invalid command in negotiation phase: ") + command.c_str());
127 * We have sent SERVER to the other side of the connection. Now we're waiting for them to start BURST.
128 * The other option at this stage of things, of course, is for them to close our connection thanks
129 * to invalid credentials.. -- w
131 if (command == "SERVER")
134 * Connection is either attempting to re-auth itself (stupid) or sending netburst without sending BURST.
135 * Both of these aren't allowable, so block them here. -- w
137 this->SendError("You may not re-authenticate or commence netburst without sending BURST.");
139 else if (command == "BURST")
143 time_t them = atoi(params[0].c_str());
144 time_t delta = them - ServerInstance->Time();
145 if ((delta < -600) || (delta > 600))
147 ServerInstance->SNO->WriteGlobalSno('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than five minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs((long)delta));
148 SendError("Your clocks are out by "+ConvToStr(abs((long)delta))+" seconds (this is more than five minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
151 else if ((delta < -30) || (delta > 30))
153 ServerInstance->SNO->WriteGlobalSno('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs((long)delta));
156 this->LinkState = CONNECTED;
158 Utils->timeoutlist.erase(this);
159 parameterlist sparams;
160 Utils->DoOneToAllButSender(MyRoot->GetID(), "BURST", params, MyRoot->GetName());
161 MyRoot->bursting = true;
162 this->DoBurst(MyRoot);
164 else if (command == "ERROR")
168 else if (command == "CAPAB")
177 * We're connecting (OUTGOING) to another server. They are in state WAIT_AUTH_1 until they verify
178 * our credentials, when they proceed into WAIT_AUTH_2 and send SERVER to us. We then send BURST
179 * + our netburst, which will put them into CONNECTED state. -- w
181 if (command == "SERVER")
183 // Our credentials have been accepted, send netburst. (this puts US into the CONNECTED state)
184 this->Outbound_Reply_Server(params);
186 else if (command == "ERROR")
190 else if (command == "CAPAB")
198 * Credentials have been exchanged, we've gotten their 'BURST' (or sent ours).
199 * Anything from here on should be accepted a little more reasonably.
201 this->ProcessConnectedLine(prefix, command, params);
208 void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params)
210 User* who = ServerInstance->FindUUID(prefix);
211 std::string direction;
215 TreeServer* ServerSource = Utils->FindServer(prefix);
217 ServerSource = MyRoot;
221 who = ServerSource->ServerUser;
225 /* It is important that we don't close the link here, unknown prefix can occur
226 * due to various race conditions such as the KILL message for a user somehow
227 * crossing the users QUIT further upstream from the server. Thanks jilles!
229 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
230 command.c_str(), prefix.c_str());
235 // Make sure prefix is still good
236 direction = who->server;
240 * Check for fake direction here, and drop any instances that are found.
241 * What is fake direction? Imagine the following server setup:
242 * 0AA <-> 0AB <-> 0AC
243 * Fake direction would be 0AC sending a message to 0AB claiming to be from
244 * 0AA, or something similar. Basically, a message taking a path that *cannot*
247 * When would this be seen?
248 * Well, hopefully never. It could be caused by race conditions, bugs, or
249 * "miscreant" servers, though, so let's check anyway. -- w
251 * We also check here for totally invalid prefixes (prefixes that are neither
252 * a valid SID or a valid UUID, so that invalid UUID or SID never makes it
253 * to the higher level functions. -- B
255 TreeServer* route_back_again = Utils->BestRouteTo(direction);
256 if ((!route_back_again) || (route_back_again->GetSocket() != this))
258 if (route_back_again)
259 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'",
260 prefix.c_str(),linkID.c_str());
265 * First up, check for any malformed commands (e.g. MODE without a timestamp)
266 * and rewrite commands where necessary (SVSMODE -> MODE for services). -- w
268 if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels.
271 // TODO move all this into Commands
272 if (command == "MAP")
274 Utils->Creator->HandleMap(params, who);
276 else if (command == "SERVER")
278 this->RemoteServer(prefix,params);
280 else if (command == "ERROR")
284 else if (command == "AWAY")
286 this->Away(prefix,params);
288 else if (command == "PING")
290 this->LocalPing(prefix,params);
292 else if (command == "PONG")
294 TreeServer *s = Utils->FindServer(prefix);
295 if (s && s->bursting)
297 ServerInstance->SNO->WriteGlobalSno('l',"Server \002%s\002 has not finished burst, forcing end of burst (send ENDBURST!)", prefix.c_str());
300 this->LocalPong(prefix,params);
302 else if (command == "VERSION")
304 this->ServerVersion(prefix,params);
306 else if (command == "ADDLINE")
308 this->AddLine(prefix,params);
310 else if (command == "DELLINE")
312 this->DelLine(prefix,params);
314 else if (command == "SAVE")
316 this->ForceNick(prefix,params);
318 else if (command == "OPERQUIT")
320 this->OperQuit(prefix,params);
322 else if (command == "IDLE")
324 this->Whois(prefix,params);
326 else if (command == "PUSH")
328 this->Push(prefix,params);
330 else if (command == "SQUIT")
332 if (params.size() == 2)
334 this->Squit(Utils->FindServer(params[0]),params[1]);
337 else if (command == "SNONOTICE")
339 if (params.size() >= 2)
341 ServerInstance->SNO->WriteToSnoMask(params[0][0], "From " + who->nick + ": "+ params[1]);
342 params[1] = ":" + params[1];
343 Utils->DoOneToAllButSender(prefix, command, params, prefix);
346 else if (command == "BURST")
348 // Set prefix server as bursting
349 TreeServer* ServerSource = Utils->FindServer(prefix);
352 ServerInstance->SNO->WriteGlobalSno('l', "WTF: Got BURST from a non-server(?): %s", prefix.c_str());
356 ServerSource->bursting = true;
357 Utils->DoOneToAllButSender(prefix, command, params, prefix);
359 else if (command == "ENDBURST")
361 TreeServer* ServerSource = Utils->FindServer(prefix);
364 ServerInstance->SNO->WriteGlobalSno('l', "WTF: Got ENDBURST from a non-server(?): %s", prefix.c_str());
368 ServerSource->FinishBurst();
369 Utils->DoOneToAllButSender(prefix, command, params, prefix);
371 else if (command == "ENCAP")
373 this->Encap(who, params);
375 else if (command == "NICK")
377 if (params.size() != 2)
379 SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]);
385 SendError("Protocol violation: Server changing nick");
389 /* Update timestamp on user when they change nicks */
390 who->age = atoi(params[1].c_str());
393 * On nick messages, check that the nick doesnt already exist here.
394 * If it does, perform collision logic.
396 User* x = ServerInstance->FindNickOnly(params[0]);
397 if ((x) && (x != who))
400 /* x is local, who is remote */
401 collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid);
405 * Remote client lost, or both lost, parsing or passing on this
406 * nickchange would be pointless, as the incoming client's server will
407 * soon recieve SVSNICK to change its nick to its UID. :) -- w00t
412 who->ForceNickChange(params[0].c_str());
413 Utils->RouteCommand(route_back_again, command, params, who);
417 Command* cmd = ServerInstance->Parser->GetHandler(command);
421 irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
422 ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Unrecognised S2S command :%s %s %s",
423 who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
424 SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules");
427 if (params.size() < cmd->min_params)
429 irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
430 ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Insufficient parameters for S2S command :%s %s %s",
431 who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
432 SendError("Insufficient parameters for command '" + command + "'");
435 CmdResult res = cmd->Handle(params, who);
437 if (res == CMD_INVALID)
439 irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
440 ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Error handing S2S command :%s %s %s",
441 who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
442 SendError("Error handling '" + command + "' -- possibly loaded mismatched modules");
444 if (res == CMD_SUCCESS)
445 Utils->RouteCommand(route_back_again, command, params, who);
449 void TreeSocket::OnTimeout()
451 ServerInstance->SNO->WriteGlobalSno('l', "CONNECT: Connection to \002%s\002 timed out.", linkID.c_str());
454 void TreeSocket::Close()
457 ServerInstance->GlobalCulls.AddItem(this);
458 this->BufferedSocket::Close();
459 SetError("Remote host closed connection");
461 // Connection closed.
462 // If the connection is fully up (state CONNECTED)
463 // then propogate a netsplit to all peers.
465 Squit(MyRoot,getError());
469 ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' failed.",linkID.c_str());
471 time_t server_uptime = ServerInstance->Time() - this->age;
473 ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", linkID.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str());