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 "socketengine.h"
20 #include "../spanningtree.h"
22 #include "treeserver.h"
24 #include "treesocket.h"
25 #include "resolvers.h"
27 /** Because most of the I/O gubbins are encapsulated within
28 * BufferedSocket, we just call the superclass constructor for
29 * most of the action, and append a few of our own values
32 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr)
35 age = ServerInstance->Time();
36 linkID = assign(link->Name);
37 capab = new CapabData;
40 capab->capab_phase = 0;
43 LinkState = CONNECTING;
44 if (!link->Hook.empty())
46 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, link->Hook);
49 SetError("Could not find hook '" + link->Hook + "' for connection to " + linkID);
52 AddIOHook(prov->creator);
54 DoConnect(ipaddr, link->Port, link->Timeout, link->Bind);
55 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, link->Timeout);
59 /** When a listening socket gives us a new file descriptor,
60 * we must associate it with a socket without creating a new
61 * connection. This constructor is used for this purpose.
63 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
64 : BufferedSocket(newfd), Utils(Util)
66 capab = new CapabData;
67 capab->capab_phase = 0;
69 age = ServerInstance->Time();
70 LinkState = WAIT_AUTH_1;
72 linkID = "inbound from " + client->addr();
74 FOREACH_MOD(I_OnHookIO, OnHookIO(this, via));
76 GetIOHook()->OnStreamSocketAccept(this, client, server);
79 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
82 ServerState TreeSocket::GetLinkState()
84 return this->LinkState;
87 void TreeSocket::CleanNegotiationInfo()
89 // connect is good, reset the autoconnect block (if used)
91 capab->ac->position = -1;
96 CullResult TreeSocket::cull()
98 Utils->timeoutlist.erase(this);
99 if (capab && capab->ac)
100 Utils->Creator->ConnectServer(capab->ac, false);
101 return this->BufferedSocket::cull();
104 TreeSocket::~TreeSocket()
110 /** When an outbound connection finishes connecting, we receive
111 * this event, and must send our SERVER string to the other
112 * side. If the other side is happy, as outlined in the server
113 * to server docs on the inspircd.org site, the other side
114 * will then send back its own server string.
116 void TreeSocket::OnConnected()
118 if (this->LinkState == CONNECTING)
120 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
121 (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
122 this->SendCapabilities(1);
126 void TreeSocket::OnError(BufferedSocketError e)
128 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002 failed with error: %s",
129 linkID.c_str(), getError().c_str());
133 void TreeSocket::SendError(const std::string &errormessage)
135 WriteLine("ERROR :"+errormessage);
138 SetError(errormessage);
141 /** This function forces this server to quit, removing this server
142 * and any users on it (and servers and users below that, etc etc).
143 * It's very slow and pretty clunky, but luckily unless your network
144 * is having a REAL bad hair day, this function shouldnt be called
145 * too many times a month ;-)
147 void TreeSocket::SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users)
149 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"SquitServer for %s from %s",
150 Current->GetName().c_str(), from.c_str());
151 /* recursively squit the servers attached to 'Current'.
152 * We're going backwards so we don't remove users
153 * while we still need them ;)
155 for (unsigned int q = 0; q < Current->ChildCount(); q++)
157 TreeServer* recursive_server = Current->GetChild(q);
158 this->SquitServer(from,recursive_server, num_lost_servers, num_lost_users);
160 /* Now we've whacked the kids, whack self */
162 num_lost_users += Current->QuitUsers(from);
165 /** This is a wrapper function for SquitServer above, which
166 * does some validation first and passes on the SQUIT to all
167 * other remaining servers.
169 void TreeSocket::Squit(TreeServer* Current, const std::string &reason)
171 bool LocalSquit = false;
173 if ((Current) && (Current != Utils->TreeRoot))
175 DelServerEvent(Utils->Creator, Current->GetName());
177 parameterlist params;
178 params.push_back(Current->GetName());
179 params.push_back(":"+reason);
180 Utils->DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
181 if (Current->GetParent() == Utils->TreeRoot)
183 ServerInstance->SNO->WriteGlobalSno('l', "Server \002"+Current->GetName()+"\002 split: "+reason);
188 ServerInstance->SNO->WriteGlobalSno('L', "Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
190 int num_lost_servers = 0;
191 int num_lost_users = 0;
192 std::string from = Current->GetParent()->GetName()+" "+Current->GetName();
193 SquitServer(from, Current, num_lost_servers, num_lost_users);
194 ServerInstance->SNO->WriteToSnoMask(LocalSquit ? 'l' : 'L', "Netsplit complete, lost \002%d\002 user%s on \002%d\002 server%s.",
195 num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
197 Current->GetParent()->DelChild(Current);
200 if (Current == MyRoot)
207 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Squit from unknown server");
210 /** This function is called when we receive data from a remote
213 void TreeSocket::OnDataReady()
215 Utils->Creator->loopCall = true;
217 while (GetNextLine(line))
219 std::string::size_type rline = line.find('\r');
220 if (rline != std::string::npos)
221 line = line.substr(0,rline);
222 if (line.find('\0') != std::string::npos)
224 SendError("Read null character from socket");
228 if (!getError().empty())
231 if (LinkState != CONNECTED && recvq.length() > 4096)
232 SendError("RecvQ overrun (line too long)");
233 Utils->Creator->loopCall = false;