]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/treesocket2.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket2.cpp
index 0a0c22e39cf1cc1d95c685b8d069564e886c7d05..fdd28a73425b0fd32709b2b5230ada19b45b4555 100644 (file)
 
 
 #include "inspircd.h"
-#include "socket.h"
-#include "xline.h"
-#include "socketengine.h"
 
 #include "main.h"
 #include "utils.h"
 #include "treeserver.h"
-#include "link.h"
 #include "treesocket.h"
 #include "resolvers.h"
 
@@ -47,7 +43,7 @@ void TreeSocket::Split(const std::string& line, std::string& prefix, std::string
 
        if (!tokens.GetToken(prefix))
                return;
-       
+
        if (prefix[0] == ':')
        {
                prefix = prefix.substr(1);
@@ -84,7 +80,7 @@ void TreeSocket::ProcessLine(std::string &line)
        std::string command;
        parameterlist params;
 
-       ServerInstance->Logs->Log("m_spanningtree", RAWIO, "S[%d] I %s", this->GetFd(), line.c_str());
+       ServerInstance->Logs->Log("m_spanningtree", LOG_RAWIO, "S[%d] I %s", this->GetFd(), line.c_str());
 
        Split(line, prefix, command, params);
 
@@ -129,7 +125,7 @@ void TreeSocket::ProcessLine(std::string &line)
                        }
                        else
                        {
-                               this->SendError(std::string("Invalid command in negotiation phase: ") + command.c_str());
+                               this->SendError("Invalid command in negotiation phase: " + command);
                        }
                break;
                case WAIT_AUTH_2:
@@ -151,7 +147,7 @@ void TreeSocket::ProcessLine(std::string &line)
                        {
                                if (params.size())
                                {
-                                       time_t them = atoi(params[0].c_str());
+                                       time_t them = ConvToInt(params[0]);
                                        time_t delta = them - ServerInstance->Time();
                                        if ((delta < -600) || (delta > 600))
                                        {
@@ -164,9 +160,21 @@ void TreeSocket::ProcessLine(std::string &line)
                                                ServerInstance->SNO->WriteGlobalSno('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs((long)delta));
                                        }
                                }
+
+                               // Check for duplicate server name/sid again, it's possible that a new
+                               // server was introduced while we were waiting for them to send BURST.
+                               // (we do not reserve their server name/sid when they send SERVER, we do it now)
+                               if (!CheckDuplicate(capab->name, capab->sid))
+                                       return;
+
                                this->LinkState = CONNECTED;
                                Utils->timeoutlist.erase(this);
 
+                               linkID = capab->name;
+
+                               MyRoot = new TreeServer(Utils, capab->name, capab->description, capab->sid, Utils->TreeRoot, this, capab->hidden);
+                               Utils->TreeRoot->AddChild(MyRoot);
+
                                MyRoot->bursting = true;
                                this->DoBurst(MyRoot);
 
@@ -244,9 +252,25 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
                         * due to various race conditions such as the KILL message for a user somehow
                         * crossing the users QUIT further upstream from the server. Thanks jilles!
                         */
-                       ServerInstance->Logs->Log("m_spanningtree", DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
-                               command.c_str(), prefix.c_str());
-                       return;
+
+                       if ((prefix.length() == UIDGenerator::UUID_LENGTH) && (isdigit(prefix[0])) &&
+                               ((command == "FMODE") || (command == "MODE") || (command == "KICK") || (command == "TOPIC") || (command == "KILL") || (command == "ADDLINE") || (command == "DELLINE")))
+                       {
+                               /* Special case, we cannot drop these commands as they've been committed already on a
+                                * part of the network by the time we receive them, so in this scenario pretend the
+                                * command came from a server to avoid desync.
+                                */
+
+                               who = ServerInstance->FindUUID(prefix.substr(0, 3));
+                               if (!who)
+                                       who = this->MyRoot->ServerUser;
+                       }
+                       else
+                       {
+                               ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "Command '%s' from unknown prefix '%s'! Dropping entire command.",
+                                       command.c_str(), prefix.c_str());
+                               return;
+                       }
                }
        }
 
@@ -274,7 +298,7 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
        if ((!route_back_again) || (route_back_again->GetSocket() != this))
        {
                if (route_back_again)
-                       ServerInstance->Logs->Log("m_spanningtree",DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'",
+                       ServerInstance->Logs->Log("m_spanningtree",LOG_DEBUG,"Protocol violation: Fake direction '%s' from connection '%s'",
                                prefix.c_str(),linkID.c_str());
                return;
        }
@@ -286,6 +310,12 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
        if (command == "SVSMODE") // This isn't in an "else if" so we still force FMODE for changes on channels.
                command = "MODE";
 
+       if (proto_version < ProtocolVersion)
+       {
+               if (!PreProcessOldProtocolMessage(who, command, params))
+                       return;
+       }
+
        // TODO move all this into Commands
        if (command == "MAP")
        {
@@ -394,7 +424,7 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
        {
                if (params.size() != 2)
                {
-                       SendError("Protocol violation: NICK message without TS - :"+std::string(who->uuid)+" NICK "+params[0]);
+                       SendError("Protocol violation: Wrong number of parameters for NICK message");
                        return;
                }
 
@@ -404,8 +434,14 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
                        return;
                }
 
+               if ((isdigit(params[0][0])) && (params[0] != who->uuid))
+               {
+                       SendError("Protocol violation: User changing nick to an invalid UID - " + params[0]);
+                       return;
+               }
+
                /* Update timestamp on user when they change nicks */
-               who->age = atoi(params[1].c_str());
+               who->age = ConvToInt(params[1]);
 
                /*
                 * On nick messages, check that the nick doesnt already exist here.
@@ -433,11 +469,11 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
        else
        {
                Command* cmd = ServerInstance->Parser->GetHandler(command);
-               
+
                if (!cmd)
                {
                        irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Unrecognised S2S command :%s %s %s",
+                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Unrecognised S2S command :%s %s %s",
                                who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
                        SendError("Unrecognised command '" + command + "' -- possibly loaded mismatched modules");
                        return;
@@ -446,18 +482,26 @@ void TreeSocket::ProcessConnectedLine(std::string& prefix, std::string& command,
                if (params.size() < cmd->min_params)
                {
                        irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Insufficient parameters for S2S command :%s %s %s",
+                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Insufficient parameters for S2S command :%s %s %s",
                                who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
                        SendError("Insufficient parameters for command '" + command + "'");
                        return;
                }
 
+               if ((!params.empty()) && (params.back().empty()) && (!cmd->allow_empty_last_param))
+               {
+                       // the last param is empty and the command handler doesn't allow that, check if there will be enough params if we drop the last
+                       if (params.size()-1 < cmd->min_params)
+                               return;
+                       params.pop_back();
+               }
+
                CmdResult res = cmd->Handle(params, who);
 
                if (res == CMD_INVALID)
                {
                        irc::stringjoiner pmlist(" ", params, 0, params.size() - 1);
-                       ServerInstance->Logs->Log("m_spanningtree", SPARSE, "Error handling S2S command :%s %s %s",
+                       ServerInstance->Logs->Log("m_spanningtree", LOG_SPARSE, "Error handling S2S command :%s %s %s",
                                who->uuid.c_str(), command.c_str(), pmlist.GetJoined().c_str());
                        SendError("Error handling '" + command + "' -- possibly loaded mismatched modules");
                }
@@ -491,6 +535,9 @@ void TreeSocket::Close()
 
                time_t server_uptime = ServerInstance->Time() - this->age;
                if (server_uptime)
-                       ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", linkID.c_str(), Utils->Creator->TimeToStr(server_uptime).c_str());
+               {
+                       std::string timestr = Utils->Creator->TimeToStr(server_uptime);
+                       ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\2%s\2' was established for %s", linkID.c_str(), timestr.c_str());
+               }
        }
 }