2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/spanningtree.h"
29 #include "treeserver.h"
31 #include "treesocket.h"
33 /** Because most of the I/O gubbins are encapsulated within
34 * BufferedSocket, we just call the superclass constructor for
35 * most of the action, and append a few of our own values
38 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr)
41 age = ServerInstance->Time();
42 linkID = assign(link->Name);
43 capab = new CapabData;
46 capab->capab_phase = 0;
49 ConnectionFailureShown = false;
50 LinkState = CONNECTING;
51 if (!link->Hook.empty())
53 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, link->Hook);
56 SetError("Could not find hook '" + link->Hook + "' for connection to " + linkID);
59 AddIOHook(static_cast<IOHook*>(prov));
61 DoConnect(ipaddr, link->Port, link->Timeout, link->Bind);
62 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, link->Timeout);
66 /** When a listening socket gives us a new file descriptor,
67 * we must associate it with a socket without creating a new
68 * connection. This constructor is used for this purpose.
70 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
71 : BufferedSocket(newfd), Utils(Util)
73 capab = new CapabData;
74 capab->capab_phase = 0;
76 age = ServerInstance->Time();
77 LinkState = WAIT_AUTH_1;
79 ConnectionFailureShown = false;
80 linkID = "inbound from " + client->addr();
82 FOREACH_MOD(I_OnHookIO, OnHookIO(this, via));
84 GetIOHook()->OnStreamSocketAccept(this, client, server);
87 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
90 ServerState TreeSocket::GetLinkState()
92 return this->LinkState;
95 void TreeSocket::CleanNegotiationInfo()
97 // connect is good, reset the autoconnect block (if used)
99 capab->ac->position = -1;
104 CullResult TreeSocket::cull()
106 Utils->timeoutlist.erase(this);
107 if (capab && capab->ac)
108 Utils->Creator->ConnectServer(capab->ac, false);
109 return this->BufferedSocket::cull();
112 TreeSocket::~TreeSocket()
118 /** When an outbound connection finishes connecting, we receive
119 * this event, and must send our SERVER string to the other
120 * side. If the other side is happy, as outlined in the server
121 * to server docs on the inspircd.org site, the other side
122 * will then send back its own server string.
124 void TreeSocket::OnConnected()
126 if (this->LinkState == CONNECTING)
128 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
129 (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
130 this->SendCapabilities(1);
134 void TreeSocket::OnError(BufferedSocketError e)
136 ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
137 linkID.c_str(), getError().c_str());
141 void TreeSocket::SendError(const std::string &errormessage)
143 WriteLine("ERROR :"+errormessage);
146 SetError(errormessage);
149 /** This function forces this server to quit, removing this server
150 * and any users on it (and servers and users below that, etc etc).
151 * It's very slow and pretty clunky, but luckily unless your network
152 * is having a REAL bad hair day, this function shouldnt be called
153 * too many times a month ;-)
155 void TreeSocket::SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users)
157 std::string servername = Current->GetName();
158 ServerInstance->Logs->Log("m_spanningtree", LOG_DEBUG, "SquitServer for %s from %s",
159 servername.c_str(), from.c_str());
160 /* recursively squit the servers attached to 'Current'.
161 * We're going backwards so we don't remove users
162 * while we still need them ;)
164 for (unsigned int q = 0; q < Current->ChildCount(); q++)
166 TreeServer* recursive_server = Current->GetChild(q);
167 this->SquitServer(from,recursive_server, num_lost_servers, num_lost_users);
169 /* Now we've whacked the kids, whack self */
171 num_lost_users += Current->QuitUsers(from);
174 /** This is a wrapper function for SquitServer above, which
175 * does some validation first and passes on the SQUIT to all
176 * other remaining servers.
178 void TreeSocket::Squit(TreeServer* Current, const std::string &reason)
180 bool LocalSquit = false;
182 if ((Current) && (Current != Utils->TreeRoot))
184 DelServerEvent(Utils->Creator, Current->GetName());
186 if (!Current->GetSocket() || Current->GetSocket()->Introduced())
188 parameterlist params;
189 params.push_back(Current->GetID());
190 params.push_back(":"+reason);
191 Utils->DoOneToAllButSender(Current->GetParent()->GetID(),"SQUIT",params,Current->GetName());
194 if (Current->GetParent() == Utils->TreeRoot)
196 ServerInstance->SNO->WriteGlobalSno('l', "Server \002"+Current->GetName()+"\002 split: "+reason);
201 ServerInstance->SNO->WriteGlobalSno('L', "Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
203 int num_lost_servers = 0;
204 int num_lost_users = 0;
205 std::string from = Current->GetParent()->GetName()+" "+Current->GetName();
206 SquitServer(from, Current, num_lost_servers, num_lost_users);
207 ServerInstance->SNO->WriteToSnoMask(LocalSquit ? 'l' : 'L', "Netsplit complete, lost \002%d\002 user%s on \002%d\002 server%s.",
208 num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
210 Current->GetParent()->DelChild(Current);
213 if (Current == MyRoot)
220 ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Squit from unknown server");
223 /** This function is called when we receive data from a remote
226 void TreeSocket::OnDataReady()
228 Utils->Creator->loopCall = true;
230 while (GetNextLine(line))
232 std::string::size_type rline = line.find('\r');
233 if (rline != std::string::npos)
234 line = line.substr(0,rline);
235 if (line.find('\0') != std::string::npos)
237 SendError("Read null character from socket");
241 if (!getError().empty())
244 if (LinkState != CONNECTED && recvq.length() > 4096)
245 SendError("RecvQ overrun (line too long)");
246 Utils->Creator->loopCall = false;
249 bool TreeSocket::Introduced()
251 return (capab == NULL);