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 "../m_hash.h"
18 #include "socketengine.h"
21 #include "../spanningtree.h"
23 #include "treeserver.h"
25 #include "treesocket.h"
26 #include "resolvers.h"
28 /** Because most of the I/O gubbins are encapsulated within
29 * BufferedSocket, we just call the superclass constructor for
30 * most of the action, and append a few of our own values
33 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr)
36 age = ServerInstance->Time();
37 linkID = assign(link->Name);
38 capab = new CapabData;
41 capab->capab_phase = 0;
44 LinkState = CONNECTING;
45 if (!link->Hook.empty())
47 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, link->Hook);
50 SetError("Could not find hook '" + link->Hook + "' for connection to " + linkID);
53 AddIOHook(prov->creator);
55 DoConnect(ipaddr, link->Port, link->Timeout, link->Bind);
56 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, link->Timeout);
60 /** When a listening socket gives us a new file descriptor,
61 * we must associate it with a socket without creating a new
62 * connection. This constructor is used for this purpose.
64 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
65 : BufferedSocket(newfd), Utils(Util)
67 capab = new CapabData;
68 capab->capab_phase = 0;
70 age = ServerInstance->Time();
71 LinkState = WAIT_AUTH_1;
73 linkID = "inbound from " + client->addr();
75 FOREACH_MOD(I_OnHookIO, OnHookIO(this, via));
77 GetIOHook()->OnStreamSocketAccept(this, client, server);
80 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
83 ServerState TreeSocket::GetLinkState()
85 return this->LinkState;
88 void TreeSocket::CleanNegotiationInfo()
90 // connect is good, reset the autoconnect block (if used)
92 capab->ac->position = -1;
97 CullResult TreeSocket::cull()
99 Utils->timeoutlist.erase(this);
100 if (capab && capab->ac)
101 Utils->Creator->ConnectServer(capab->ac, false);
102 return this->BufferedSocket::cull();
105 TreeSocket::~TreeSocket()
111 /** When an outbound connection finishes connecting, we receive
112 * this event, and must send our SERVER string to the other
113 * side. If the other side is happy, as outlined in the server
114 * to server docs on the inspircd.org site, the other side
115 * will then send back its own server string.
117 void TreeSocket::OnConnected()
119 if (this->LinkState == CONNECTING)
121 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
122 (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
123 this->SendCapabilities(1);
127 void TreeSocket::OnError(BufferedSocketError e)
129 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002 failed with error: %s",
130 linkID.c_str(), getError().c_str());
134 void TreeSocket::SendError(const std::string &errormessage)
136 WriteLine("ERROR :"+errormessage);
139 SetError(errormessage);
142 /** This function forces this server to quit, removing this server
143 * and any users on it (and servers and users below that, etc etc).
144 * It's very slow and pretty clunky, but luckily unless your network
145 * is having a REAL bad hair day, this function shouldnt be called
146 * too many times a month ;-)
148 void TreeSocket::SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users)
150 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"SquitServer for %s from %s",
151 Current->GetName().c_str(), from.c_str());
152 /* recursively squit the servers attached to 'Current'.
153 * We're going backwards so we don't remove users
154 * while we still need them ;)
156 for (unsigned int q = 0; q < Current->ChildCount(); q++)
158 TreeServer* recursive_server = Current->GetChild(q);
159 this->SquitServer(from,recursive_server, num_lost_servers, num_lost_users);
161 /* Now we've whacked the kids, whack self */
163 num_lost_users += Current->QuitUsers(from);
166 /** This is a wrapper function for SquitServer above, which
167 * does some validation first and passes on the SQUIT to all
168 * other remaining servers.
170 void TreeSocket::Squit(TreeServer* Current, const std::string &reason)
172 bool LocalSquit = false;
174 if ((Current) && (Current != Utils->TreeRoot))
176 DelServerEvent(Utils->Creator, Current->GetName());
178 parameterlist params;
179 params.push_back(Current->GetName());
180 params.push_back(":"+reason);
181 Utils->DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
182 if (Current->GetParent() == Utils->TreeRoot)
184 ServerInstance->SNO->WriteGlobalSno('l', "Server \002"+Current->GetName()+"\002 split: "+reason);
189 ServerInstance->SNO->WriteGlobalSno('L', "Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
191 int num_lost_servers = 0;
192 int num_lost_users = 0;
193 std::string from = Current->GetParent()->GetName()+" "+Current->GetName();
194 SquitServer(from, Current, num_lost_servers, num_lost_users);
195 ServerInstance->SNO->WriteToSnoMask(LocalSquit ? 'l' : 'L', "Netsplit complete, lost \002%d\002 user%s on \002%d\002 server%s.",
196 num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
198 Current->GetParent()->DelChild(Current);
201 if (Current == MyRoot)
208 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Squit from unknown server");
211 /** This function is called when we receive data from a remote
214 void TreeSocket::OnDataReady()
216 Utils->Creator->loopCall = true;
218 while (GetNextLine(line))
220 std::string::size_type rline = line.find('\r');
221 if (rline != std::string::npos)
222 line = line.substr(0,rline);
223 if (line.find('\0') != std::string::npos)
225 SendError("Read null character from socket");
229 if (!getError().empty())
232 if (LinkState != CONNECTED && recvq.length() > 4096)
233 SendError("RecvQ overrun (line too long)");
234 Utils->Creator->loopCall = false;