1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 "../transport.h"
18 #include "../m_hash.h"
19 #include "socketengine.h"
23 #include "treeserver.h"
25 #include "treesocket.h"
26 #include "resolvers.h"
27 #include "handshaketimer.h"
29 /* $ModDep: m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_hash.h m_spanningtree/handshaketimer.h */
32 /** Because most of the I/O gubbins are encapsulated within
33 * BufferedSocket, we just call the superclass constructor for
34 * most of the action, and append a few of our own values
37 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, std::string shost, int iport, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, Module* HookMod)
38 : Utils(Util), IP(shost), myautoconnect(myac)
40 age = ServerInstance->Time();
44 LinkState = CONNECTING;
45 DoConnect(shost, iport, maxtime, bindto);
46 Utils->timeoutlist[this] = std::pair<std::string, int>(ServerName, maxtime);
48 BufferedSocketHookRequest(this, Utils->Creator, HookMod).Send();
52 /** When a listening socket gives us a new file descriptor,
53 * we must associate it with a socket without creating a new
54 * connection. This constructor is used for this purpose.
56 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, char* ip, Autoconnect* myac, Module* HookMod)
57 : BufferedSocket(newfd), Utils(Util), IP(ip), myautoconnect(myac)
59 age = ServerInstance->Time();
60 LinkState = WAIT_AUTH_1;
63 /* If we have a transport module hooked to the parent, hook the same module to this
64 * socket, and set a timer waiting for handshake before we send CAPAB etc.
67 BufferedSocketHookRequest(this, Utils->Creator, HookMod).Send();
69 hstimer = new HandshakeTimer(this, Utils->LinkBlocks[0], this->Utils, 1);
70 ServerInstance->Timers->AddTimer(hstimer);
72 /* Fix by Brain - inbound sockets need a timeout, too. 30 secs should be pleanty */
73 Utils->timeoutlist[this] = std::pair<std::string, int>("<from " + std::string(ip) + ">", 30);
76 ServerState TreeSocket::GetLinkState()
78 return this->LinkState;
81 void TreeSocket::CleanNegotiationInfo()
84 OptModuleList.clear();
87 theirchallenge.clear();
91 bool TreeSocket::cull()
94 BufferedSocketUnhookRequest(this, Utils->Creator, GetIOHook()).Send();
95 Utils->timeoutlist.erase(this);
96 return this->BufferedSocket::cull();
99 TreeSocket::~TreeSocket()
102 ServerInstance->Timers->DelTimer(hstimer);
105 /** When an outbound connection finishes connecting, we receive
106 * this event, and must send our SERVER string to the other
107 * side. If the other side is happy, as outlined in the server
108 * to server docs on the inspircd.org site, the other side
109 * will then send back its own server string.
111 void TreeSocket::OnConnected()
113 if (this->LinkState == CONNECTING)
115 /* we do not need to change state here. */
116 for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); ++i)
119 if (x->Name == this->myhost)
121 ServerInstance->SNO->WriteToSnoMask('l', "Connection to \2%s\2[%s] started.", myhost.c_str(), (x->HiddenFromStats ? "<hidden>" : this->IP.c_str()));
122 this->OutboundPass = x->SendPass;
125 ServerInstance->SNO->WriteToSnoMask('l', "Connection to \2%s\2[%s] using transport \2%s\2", myhost.c_str(), (x->HiddenFromStats ? "<hidden>" : this->IP.c_str()), x->Hook.c_str());
126 hstimer = new HandshakeTimer(this, &(*x), this->Utils, 1);
127 ServerInstance->Timers->AddTimer(hstimer);
130 this->SendCapabilities(1);
135 /* There is a (remote) chance that between the /CONNECT and the connection
136 * being accepted, some muppet has removed the <link> block and rehashed.
137 * If that happens the connection hangs here until it's closed. Unlikely
138 * and rather harmless.
140 ServerInstance->SNO->WriteToSnoMask('l', "Connection to \2%s\2 lost link tag(!)", myhost.c_str());
143 void TreeSocket::OnError(BufferedSocketError e)
148 ServerInstance->SNO->WriteToSnoMask('l', "Connection failed: Connection to \002%s\002 refused", myhost.c_str());
149 Utils->Creator->ConnectServer(myautoconnect);
152 ServerInstance->SNO->WriteToSnoMask('l', "Connection failed: Could not create socket (%s)", strerror(errno));
155 ServerInstance->SNO->WriteToSnoMask('l', "Connection failed: Error binding socket to address or port (%s)", strerror(errno));
158 ServerInstance->SNO->WriteToSnoMask('l', "Connection failed: I/O error on connection (%s)", errno ? strerror(errno) : "Connection closed unexpectedly");
160 case I_ERR_NOMOREFDS:
161 ServerInstance->SNO->WriteToSnoMask('l', "Connection failed: Operating system is out of file descriptors!");
164 if ((errno) && (errno != EINPROGRESS) && (errno != EAGAIN))
165 ServerInstance->SNO->WriteToSnoMask('l', "Connection to \002%s\002 failed with OS error: %s", myhost.c_str(), strerror(errno));
170 void TreeSocket::SendError(const std::string &errormessage)
172 /* Display the error locally as well as sending it remotely */
173 ServerInstance->SNO->WriteToSnoMask('l', "Sent \2ERROR\2 to %s: %s", (this->InboundServerName.empty() ? this->IP.c_str() : this->InboundServerName.c_str()), errormessage.c_str());
174 WriteLine("ERROR :"+errormessage);
175 SetError(errormessage);
178 /** This function forces this server to quit, removing this server
179 * and any users on it (and servers and users below that, etc etc).
180 * It's very slow and pretty clunky, but luckily unless your network
181 * is having a REAL bad hair day, this function shouldnt be called
182 * too many times a month ;-)
184 void TreeSocket::SquitServer(std::string &from, TreeServer* Current)
186 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"SquitServer for %s from %s",
187 Current->GetName().c_str(), from.c_str());
188 /* recursively squit the servers attached to 'Current'.
189 * We're going backwards so we don't remove users
190 * while we still need them ;)
192 for (unsigned int q = 0; q < Current->ChildCount(); q++)
194 TreeServer* recursive_server = Current->GetChild(q);
195 this->SquitServer(from,recursive_server);
197 /* Now we've whacked the kids, whack self */
199 num_lost_users += Current->QuitUsers(from);
202 /** This is a wrapper function for SquitServer above, which
203 * does some validation first and passes on the SQUIT to all
204 * other remaining servers.
206 void TreeSocket::Squit(TreeServer* Current, const std::string &reason)
208 bool LocalSquit = false;
210 if ((Current) && (Current != Utils->TreeRoot))
212 Event rmode((char*)Current->GetName().c_str(), (Module*)Utils->Creator, "lost_server");
215 parameterlist params;
216 params.push_back(Current->GetName());
217 params.push_back(":"+reason);
218 Utils->DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
219 if (Current->GetParent() == Utils->TreeRoot)
221 ServerInstance->SNO->WriteToSnoMask('l', "Server \002"+Current->GetName()+"\002 split: "+reason);
226 ServerInstance->SNO->WriteToSnoMask('L', "Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
228 num_lost_servers = 0;
230 std::string from = Current->GetParent()->GetName()+" "+Current->GetName();
231 SquitServer(from, Current);
233 Current->GetParent()->DelChild(Current);
236 ServerInstance->SNO->WriteToSnoMask('l', "Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
238 ServerInstance->SNO->WriteToSnoMask('L', "Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
241 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Squit from unknown server");
244 /** This function is called when we receive data from a remote
247 void TreeSocket::OnDataReady()
249 Utils->Creator->loopCall = true;
250 /* While there is at least one new line in the buffer,
251 * do something useful (we hope!) with it.
253 while (recvq.find("\n") != std::string::npos)
255 std::string ret = recvq.substr(0,recvq.find("\n")-1);
256 recvq = recvq.substr(recvq.find("\n")+1,recvq.length()-recvq.find("\n"));
257 /* Use rfind here not find, as theres more
258 * chance of the \r being near the end of the
259 * string, not the start.
261 if (ret.find("\r") != std::string::npos)
262 ret = recvq.substr(0,recvq.find("\r")-1);
265 Utils->Creator->loopCall = false;