2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
8 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
10 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "treesocket.h"
29 #include "treeserver.h"
31 #include "commandbuilder.h"
35 * Yes, this function looks a little ugly.
36 * However, in some circumstances we may not have a User, so we need to do things this way.
37 * Returns true if remote or both lost, false otherwise.
38 * Sends SAVEs as appropriate and forces nick change of the user 'u' if our side loses or if both lose.
39 * Does not change the nick of the user that is trying to claim the nick of 'u', i.e. the "remote" user.
41 bool SpanningTreeUtilities::DoCollision(User* u, TreeServer* server, time_t remotets, const std::string& remoteident, const std::string& remoteip, const std::string& remoteuid, const char* collidecmd)
43 // At this point we're sure that a collision happened, increment the counter regardless of who wins
44 ServerInstance->stats.Collisions++;
47 * Under old protocol rules, we would have had to kill both clients.
49 * These days, we have UID. And, so what we do is, force nick change client(s)
50 * involved according to timestamp rules.
54 * Force nick change on OLDER timestamped client
56 * Force nick change on NEWER timestamped client
60 * This stops abusive use of collisions, simplifies problems with loops, and so on.
63 bool bChangeLocal = true;
64 bool bChangeRemote = true;
66 // If the timestamps are not equal only one of the users has to change nick,
67 // otherwise both have to change
68 const time_t localts = u->age;
69 if (remotets != localts)
71 /* first, let's see if ident@host matches. */
72 const std::string& localident = u->ident;
73 const std::string& localip = u->GetIPString();
74 bool SamePerson = (localident == remoteident)
75 && (localip == remoteip);
78 * if ident@ip is equal, and theirs is newer, or
79 * ident@ip differ, and ours is newer
81 if((SamePerson && remotets < localts) ||
82 (!SamePerson && remotets > localts))
84 // Only remote needs to change
89 // Only ours needs to change
90 bChangeRemote = false;
94 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Nick collision on \"%s\" caused by %s: %s/%lu/%s@%s %d <-> %s/%lu/%s@%s %d", u->nick.c_str(), collidecmd,
95 u->uuid.c_str(), (unsigned long)localts, u->ident.c_str(), u->GetIPString().c_str(), bChangeLocal,
96 remoteuid.c_str(), (unsigned long)remotets, remoteident.c_str(), remoteip.c_str(), bChangeRemote);
99 * Send SAVE and accept the losing client with its UID (as we know the SAVE will
100 * not fail under any circumstances -- UIDs are netwide exclusive).
102 * This means that each side of a collide will generate one extra NICK back to where
103 * they have just linked (and where it got the SAVE from), however, it will
104 * be dropped harmlessly as it will come in as :928AAAB NICK 928AAAB, and we already
105 * have 928AAAB's nick set to that.
112 * Local-side nick needs to change. Just in case we are hub, and
113 * this "local" nick is actually behind us, send a SAVE out.
115 CmdBuilder params("SAVE");
116 params.push(u->uuid);
117 params.push(ConvToStr(u->age));
120 u->ChangeNick(u->uuid, CommandSave::SavedTimestamp);
125 * Remote side needs to change. If this happens, we modify the UID or NICK and
126 * send back a SAVE to the source.
128 CmdBuilder("SAVE").push(remoteuid).push_int(remotets).Unicast(server->ServerUser);
131 return bChangeRemote;