1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 "../transport.h"
18 #include "socketengine.h"
22 #include "treeserver.h"
24 #include "treesocket.h"
25 #include "resolvers.h"
26 #include "handshaketimer.h"
28 /* $ModDep: m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_spanningtree/handshaketimer.h */
30 /* Handle ERROR command */
31 bool TreeSocket::Error(parameterlist ¶ms)
33 if (params.size() < 1)
35 ServerInstance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(!InboundServerName.empty() ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
36 /* we will return false to cause the socket to close. */
40 void TreeSocket::Split(const std::string& line, std::string& prefix, std::string& command, parameterlist& params)
42 irc::tokenstream tokens(line);
44 if (!tokens.GetToken(prefix))
49 prefix = prefix.substr(1);
53 this->SendError("BUG (?) Empty prefix received: " + line);
56 if (!tokens.GetToken(command))
58 this->SendError("BUG (?) Empty command received: " + line);
68 this->SendError("BUG (?) Empty command received: " + line);
71 while (tokens.GetToken(param))
73 params.push_back(param);
77 void TreeSocket::ProcessLine(std::string &line)
83 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "S[%d] I %s", this->GetFd(), line.c_str());
85 Split(line, prefix, command, params);
90 switch (this->LinkState)
97 * Waiting for SERVER command from remote server. Server initiating
98 * the connection sends the first SERVER command, listening server
99 * replies with theirs if its happy, then if the initiator is happy,
100 * it starts to send its net sync, which starts the merge, otherwise
103 if (command == "PASS")
106 * Ignore this silently. Some services packages insist on sending PASS, even
107 * when it is not required (i.e. by us). We have to ignore this here, otherwise
108 * as it's an unknown command (effectively), it will cause the connection to be
109 * closed, which probably isn't what people want. -- w00t
112 else if (command == "SERVER")
114 this->Inbound_Server(params);
116 else if (command == "ERROR")
120 else if (command == "USER")
122 this->SendError("Client connections to this port are prohibited.");
124 else if (command == "CAPAB")
130 this->SendError(std::string("Invalid command in negotiation phase: ") + command.c_str());
136 * We have sent SERVER to the other side of the connection. Now we're waiting for them to start BURST.
137 * The other option at this stage of things, of course, is for them to close our connection thanks
138 * to invalid credentials.. -- w
140 if (command == "SERVER")
143 * Connection is either attempting to re-auth itself (stupid) or sending netburst without sending BURST.
144 * Both of these aren't allowable, so block them here. -- w
146 this->SendError("You may not re-authenticate or commence netburst without sending BURST.");
148 else if (command == "BURST")
152 time_t them = atoi(params[0].c_str());
153 time_t delta = them - ServerInstance->Time();
154 if ((delta < -600) || (delta > 600))
156 ServerInstance->SNO->WriteToSnoMask('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));
157 SendError("Your clocks are out by "+ConvToStr(abs((long)delta))+" seconds (this is more than five minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
160 else if ((delta < -30) || (delta > 30))
162 ServerInstance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs((long)delta));
165 this->LinkState = CONNECTED;
167 Utils->timeoutlist.erase(this);
169 Link* lnk = Utils->FindLink(InboundServerName);
171 Node = new TreeServer(this->Utils, InboundServerName, InboundDescription, InboundSID, Utils->TreeRoot, this, lnk ? lnk->Hidden : false);
173 Utils->TreeRoot->AddChild(Node);
174 parameterlist sparams;
175 sparams.push_back(InboundServerName);
176 sparams.push_back("*");
177 sparams.push_back("1");
178 sparams.push_back(InboundSID);
179 sparams.push_back(":"+InboundDescription);
180 Utils->DoOneToAllButSender(ServerInstance->Config->GetSID(),"SERVER",sparams,InboundServerName);
181 Utils->DoOneToAllButSender(prefix, "BURST", params, InboundServerName);
182 Node->bursting = true;
185 else if (command == "ERROR")
189 else if (command == "CAPAB")
198 * We're connecting (OUTGOING) to another server. They are in state WAIT_AUTH_1 until they verify
199 * our credentials, when they proceed into WAIT_AUTH_2 and send SERVER to us. We then send BURST
200 * + our netburst, which will put them into CONNECTED state. -- w
202 if (command == "SERVER")
204 // Our credentials have been accepted, send netburst. (this puts US into the CONNECTED state)
205 this->Outbound_Reply_Server(params);
207 else if (command == "ERROR")
211 else if (command == "CAPAB")
219 * Credentials have been exchanged, we've gotten their 'BURST' (or sent ours).
220 * Anything from here on should be accepted a little more reasonably.
222 this->ProcessConnectedLine(prefix, command, params);
227 void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params)
229 User* who = ServerInstance->FindUUID(prefix);
230 std::string direction;
234 direction = who->server;
238 TreeServer* ServerSource = Utils->FindServer(prefix);
240 ServerSource = Utils->FindServer(GetName());
244 who = Utils->ServerUser;
245 Utils->ServerUser->SetFakeServer(ServerSource->GetName());
246 Utils->ServerUser->uuid = ServerSource->GetID();
251 /* It is important that we don't close the link here, unknown prefix can occur
252 * due to various race conditions such as the KILL message for a user somehow
253 * crossing the users QUIT further upstream from the server. Thanks jilles!
255 ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
256 command.c_str(), prefix.c_str());
261 // Make sure prefix is still good
265 * Check for fake direction here, and drop any instances that are found.
266 * What is fake direction? Imagine the following server setup:
267 * 0AA <-> 0AB <-> 0AC
268 * Fake direction would be 0AC sending a message to 0AB claiming to be from
269 * 0AA, or something similar. Basically, a message taking a path that *cannot*
272 * When would this be seen?
273 * Well, hopefully never. It could be caused by race conditions, bugs, or
274 * "miscreant" servers, though, so let's check anyway. -- w
276 * We also check here for totally invalid prefixes (prefixes that are neither
277 * a valid SID or a valid UUID, so that invalid UUID or SID never makes it
278 * to the higher level functions. -- B
280 TreeServer* route_back_again = Utils->BestRouteTo(direction);
281 if ((!route_back_again) || (route_back_again->GetSocket() != this))
283 if (route_back_again)
284 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'",
285 prefix.c_str(),this->GetName().c_str());
290 * First up, check for any malformed commands (e.g. MODE without a timestamp)
291 * and rewrite commands where necessary (SVSMODE -> MODE for services). -- w
293 if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels.
296 // TODO move all this into Commands
297 if (command == "UID")
299 this->ParseUID(prefix, params);
301 else if (command == "FJOIN")
303 this->ForceJoin(prefix,params);
305 else if (command == "STATS")
307 this->Stats(prefix, params);
309 else if (command == "MOTD")
311 this->Motd(prefix, params);
313 else if (command == "ADMIN")
315 this->Admin(prefix, params);
317 else if (command == "MAP")
319 Utils->Creator->HandleMap(params, who);
321 else if (command == "SERVER")
323 this->RemoteServer(prefix,params);
325 else if (command == "ERROR")
329 else if (command == "OPERTYPE")
331 this->OperType(prefix,params);
333 else if (command == "AWAY")
335 this->Away(prefix,params);
337 else if (command == "FMODE")
339 this->ForceMode(prefix,params);
341 else if (command == "FTOPIC")
343 this->ForceTopic(prefix,params);
345 else if (command == "METADATA")
347 this->MetaData(prefix,params);
349 else if (command == "PING")
351 this->LocalPing(prefix,params);
353 else if (command == "PONG")
355 TreeServer *s = Utils->FindServer(prefix);
356 if (s && s->bursting)
358 ServerInstance->SNO->WriteToSnoMask('l',"Server \002%s\002 has not finished burst, forcing end of burst (send ENDBURST!)", prefix.c_str());
361 this->LocalPong(prefix,params);
363 else if (command == "VERSION")
365 this->ServerVersion(prefix,params);
367 else if (command == "FHOST")
369 this->ChangeHost(prefix,params);
371 else if (command == "FNAME")
373 this->ChangeName(prefix,params);
375 else if (command == "FIDENT")
377 this->ChangeIdent(prefix,params);
379 else if (command == "ADDLINE")
381 this->AddLine(prefix,params);
383 else if (command == "DELLINE")
385 this->DelLine(prefix,params);
387 else if (command == "SVSNICK")
389 this->SVSNick(prefix,params);
391 else if (command == "SAVE")
393 this->ForceNick(prefix,params);
395 else if (command == "OPERQUIT")
397 this->OperQuit(prefix,params);
399 else if (command == "IDLE")
401 this->Whois(prefix,params);
403 else if (command == "PUSH")
405 this->Push(prefix,params);
407 else if (command == "TIME")
409 this->Time(prefix,params);
411 else if (command == "SVSJOIN")
413 this->ServiceJoin(prefix,params);
415 else if (command == "SVSPART")
417 this->ServicePart(prefix,params);
419 else if (command == "SQUIT")
421 if (params.size() == 2)
423 this->Squit(Utils->FindServer(params[0]),params[1]);
426 else if (command == "MODENOTICE")
428 if (params.size() >= 2)
430 ServerInstance->Users->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s",
431 who->nick.c_str(), params[1].c_str());
433 Utils->DoOneToAllButSender(prefix, command, params, prefix);
435 else if (command == "SNONOTICE")
437 if (params.size() >= 2)
439 ServerInstance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + who->nick + ": "+ params[1]);
440 Utils->DoOneToAllButSender(prefix, command, params, prefix);
443 else if (command == "BURST")
445 // Set prefix server as bursting
448 ServerInstance->SNO->WriteToSnoMask('l', "WTF: Got BURST from a non-server(?): %s", prefix.c_str());
452 route_back_again->bursting = true;
453 Utils->DoOneToAllButSender(prefix, command, params, prefix);
455 else if (command == "ENDBURST")
459 ServerInstance->SNO->WriteToSnoMask('l', "WTF: Got ENDBURST from a non-server(?): %s", prefix.c_str());
463 route_back_again->FinishBurst();
464 Utils->DoOneToAllButSender(prefix, command, params, prefix);
466 else if (command == "ENCAP")
468 this->Encap(prefix, params);
470 else if (command == "NICK")
472 if (params.size() != 2)
474 SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]);
480 SendError("Protocol violation: Server changing nick");
484 /* Update timestamp on user when they change nicks */
485 who->age = atoi(params[1].c_str());
488 * On nick messages, check that the nick doesnt already exist here.
489 * If it does, perform collision logic.
491 User* x = ServerInstance->FindNickOnly(params[0]);
492 if ((x) && (x != who))
495 /* x is local, who is remote */
496 collideret = this->DoCollision(x, who->age, who->ident, who->GetIPString(), who->uuid);
500 * Remote client lost, or both lost, parsing or passing on this
501 * nickchange would be pointless, as the incoming client's server will
502 * soon recieve SVSNICK to change its nick to its UID. :) -- w00t
507 who->ForceNickChange(params[0].c_str());
508 Utils->RouteCommand(route_back_again, command, params, who);
512 Command* cmd = ServerInstance->Parser->GetHandler(command);
513 CmdResult res = CMD_INVALID;
514 if (cmd && params.size() >= cmd->min_params)
516 res = cmd->Handle(params, who);
519 if (res == CMD_INVALID)
520 SendError("Unrecognised or malformed command '" + command + "' -- possibly loaded mismatched modules");
521 if (res == CMD_SUCCESS)
522 Utils->RouteCommand(route_back_again, command, params, who);
526 std::string TreeSocket::GetName()
528 std::string sourceserv = this->myhost;
529 if (!this->InboundServerName.empty())
531 sourceserv = this->InboundServerName;
536 void TreeSocket::OnTimeout()
538 if (this->LinkState == CONNECTING)
540 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Connection to \002%s\002 timed out.", myhost.c_str());
541 Utils->Creator->ConnectServer(myautoconnect);
545 void TreeSocket::Close()
547 this->BufferedSocket::Close();
549 // Test fix for big fuckup
550 if (this->LinkState != CONNECTED)
553 // Connection closed.
554 // If the connection is fully up (state CONNECTED)
555 // then propogate a netsplit to all peers.
556 std::string quitserver = this->myhost;
557 if (!this->InboundServerName.empty())
559 quitserver = this->InboundServerName;
561 TreeServer* s = Utils->FindServer(quitserver);
564 Squit(s,"Remote host closed the connection");
567 if (!quitserver.empty())
569 ServerInstance->SNO->WriteToSnoMask('l', "Connection to '\2%s\2' failed.",quitserver.c_str());
571 time_t server_uptime = ServerInstance->Time() - this->age;
573 ServerInstance->SNO->WriteToSnoMask('l', "Connection to '\2%s\2' was established for %s", quitserver.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str());