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, const std::string& shost, int iport, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, const std::string& hook)
34 : Utils(Util), IP(shost), myautoconnect(myac)
36 age = ServerInstance->Time();
38 capab = new CapabData;
39 capab->capab_phase = 0;
41 LinkState = CONNECTING;
44 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, hook);
47 SetError("Could not find hook '" + hook + "' for connection to " + ServerName);
50 AddIOHook(prov->creator);
52 DoConnect(shost, iport, maxtime, bindto);
53 Utils->timeoutlist[this] = std::pair<std::string, int>(ServerName, maxtime);
57 /** When a listening socket gives us a new file descriptor,
58 * we must associate it with a socket without creating a new
59 * connection. This constructor is used for this purpose.
61 TreeSocket::TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
62 : BufferedSocket(newfd), Utils(Util)
64 capab = new CapabData;
66 age = ServerInstance->Time();
67 LinkState = WAIT_AUTH_1;
68 capab->capab_phase = 0;
70 myhost = "inbound from " + IP;
72 FOREACH_MOD(I_OnHookIO, OnHookIO(this, via));
74 GetIOHook()->OnStreamSocketAccept(this, client, server);
77 Utils->timeoutlist[this] = std::pair<std::string, int>(myhost, 30);
80 ServerState TreeSocket::GetLinkState()
82 return this->LinkState;
85 void TreeSocket::CleanNegotiationInfo()
91 CullResult TreeSocket::cull()
93 Utils->timeoutlist.erase(this);
95 Utils->Creator->ConnectServer(myautoconnect, false);
96 return this->BufferedSocket::cull();
99 TreeSocket::~TreeSocket()
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->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", myhost.c_str(), (x->HiddenFromStats ? "<hidden>" : this->IP.c_str()));
122 capab->OutboundPass = x->SendPass;
123 this->SendCapabilities(1);
128 /* There is a (remote) chance that between the /CONNECT and the connection
129 * being accepted, some muppet has removed the <link> block and rehashed.
130 * If that happens the connection hangs here until it's closed. Unlikely
131 * and rather harmless.
133 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2 lost link tag(!)", myhost.c_str());
136 void TreeSocket::OnError(BufferedSocketError e)
138 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002 failed with error: %s",
139 myhost.c_str(), getError().c_str());
140 ServerInstance->GlobalCulls.AddItem(this);
143 void TreeSocket::SendError(const std::string &errormessage)
145 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)
157 ServerInstance->Logs->Log("m_spanningtree",DEBUG,"SquitServer for %s from %s",
158 Current->GetName().c_str(), from.c_str());
159 /* recursively squit the servers attached to 'Current'.
160 * We're going backwards so we don't remove users
161 * while we still need them ;)
163 for (unsigned int q = 0; q < Current->ChildCount(); q++)
165 TreeServer* recursive_server = Current->GetChild(q);
166 this->SquitServer(from,recursive_server);
168 /* Now we've whacked the kids, whack self */
170 num_lost_users += Current->QuitUsers(from);
173 /** This is a wrapper function for SquitServer above, which
174 * does some validation first and passes on the SQUIT to all
175 * other remaining servers.
177 void TreeSocket::Squit(TreeServer* Current, const std::string &reason)
179 bool LocalSquit = false;
181 if ((Current) && (Current != Utils->TreeRoot))
183 DelServerEvent(Utils->Creator, Current->GetName());
185 parameterlist params;
186 params.push_back(Current->GetName());
187 params.push_back(":"+reason);
188 Utils->DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
189 if (Current->GetParent() == Utils->TreeRoot)
191 ServerInstance->SNO->WriteGlobalSno('l', "Server \002"+Current->GetName()+"\002 split: "+reason);
196 ServerInstance->SNO->WriteGlobalSno('L', "Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
198 num_lost_servers = 0;
200 std::string from = Current->GetParent()->GetName()+" "+Current->GetName();
201 SquitServer(from, Current);
203 Current->GetParent()->DelChild(Current);
207 ServerInstance->SNO->WriteToSnoMask('l', "Netsplit complete, lost \002%d\002 user%s on \002%d\002 server%s.", num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
209 ServerInstance->SNO->WriteToSnoMask('L', "Netsplit complete, lost \002%d\002 user%s on \002%d\002 server%s.", num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
212 ServerInstance->Logs->Log("m_spanningtree",DEFAULT,"Squit from unknown server");
215 /** This function is called when we receive data from a remote
218 void TreeSocket::OnDataReady()
220 Utils->Creator->loopCall = true;
222 while (GetNextLine(line))
224 std::string::size_type rline = line.find('\r');
225 if (rline != std::string::npos)
226 line = line.substr(0,rline);
227 if (line.find('\0') != std::string::npos)
229 SendError("Read null character from socket");
234 Utils->Creator->loopCall = false;