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"
34 /** Constructor for outgoing connections.
35 * Because most of the I/O gubbins are encapsulated within
36 * BufferedSocket, we just call DoConnect() for most of the action,
37 * and only do minor initialization tasks ourselves.
39 TreeSocket::TreeSocket(Link* link, Autoconnect* myac, const std::string& ipaddr)
40 : linkID(assign(link->Name)), LinkState(CONNECTING), MyRoot(NULL), proto_version(0)
41 , burstsent(false), age(ServerInstance->Time())
43 capab = new CapabData;
46 capab->capab_phase = 0;
48 DoConnect(ipaddr, link->Port, link->Timeout, link->Bind);
49 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, link->Timeout);
53 /** Constructor for incoming connections
55 TreeSocket::TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
56 : BufferedSocket(newfd)
57 , linkID("inbound from " + client->addr()), LinkState(WAIT_AUTH_1), MyRoot(NULL), proto_version(0)
58 , burstsent(false), age(ServerInstance->Time())
60 capab = new CapabData;
61 capab->capab_phase = 0;
64 via->iohookprov->OnAccept(this, client, server);
67 Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
70 void TreeSocket::CleanNegotiationInfo()
72 // connect is good, reset the autoconnect block (if used)
74 capab->ac->position = -1;
79 CullResult TreeSocket::cull()
81 Utils->timeoutlist.erase(this);
82 if (capab && capab->ac)
83 Utils->Creator->ConnectServer(capab->ac, false);
84 return this->BufferedSocket::cull();
87 TreeSocket::~TreeSocket()
92 /** When an outbound connection finishes connecting, we receive
93 * this event, and must do CAPAB negotiation with the other
94 * side. If the other side is happy, as outlined in the server
95 * to server docs on the inspircd.org site, the other side
96 * will then send back its own SERVER string eventually.
98 void TreeSocket::OnConnected()
100 if (this->LinkState == CONNECTING)
102 if (!capab->link->Hook.empty())
104 ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
107 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
110 static_cast<IOHookProvider*>(prov)->OnConnect(this);
113 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
114 (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
115 this->SendCapabilities(1);
119 void TreeSocket::OnError(BufferedSocketError e)
121 ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
122 linkID.c_str(), getError().c_str());
127 void TreeSocket::SendError(const std::string &errormessage)
129 WriteLine("ERROR :"+errormessage);
132 SetError(errormessage);
135 CmdResult CommandSQuit::HandleServer(TreeServer* server, std::vector<std::string>& params)
137 TreeServer* quitting = Utils->FindServer(params[0]);
140 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
144 CmdResult ret = CMD_SUCCESS;
145 if (quitting == server)
148 server = server->GetParent();
150 else if (quitting->GetParent() != server)
151 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
153 server->SQuitChild(quitting, params[1]);
155 // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
156 // to stop this message from being forwarded.
157 // The squit logic generates a SQUIT message with our sid as the source and sends it to the
158 // remaining servers.
162 /** This function is called when we receive data from a remote
165 void TreeSocket::OnDataReady()
167 Utils->Creator->loopCall = true;
169 while (GetNextLine(line))
171 std::string::size_type rline = line.find('\r');
172 if (rline != std::string::npos)
174 if (line.find('\0') != std::string::npos)
176 SendError("Read null character from socket");
184 catch (CoreException& ex)
186 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
187 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
188 SendError(ex.GetReason() + " - check the log file for details");
191 if (!getError().empty())
194 if (LinkState != CONNECTED && recvq.length() > 4096)
195 SendError("RecvQ overrun (line too long)");
196 Utils->Creator->loopCall = false;