]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket1.cpp
Check for errors after calling IOHookProvider::OnAccept()
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket1.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "iohook.h"
25
26 #include "main.h"
27 #include "modules/spanningtree.h"
28 #include "utils.h"
29 #include "treeserver.h"
30 #include "link.h"
31 #include "treesocket.h"
32 #include "commands.h"
33
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.
38  */
39 TreeSocket::TreeSocket(Link* link, Autoconnect* myac, const std::string& ipaddr)
40         : linkID(link->Name), LinkState(CONNECTING), MyRoot(NULL), proto_version(0)
41         , burstsent(false), age(ServerInstance->Time())
42 {
43         capab = new CapabData;
44         capab->link = link;
45         capab->ac = myac;
46         capab->capab_phase = 0;
47
48         DoConnect(ipaddr, link->Port, link->Timeout, link->Bind);
49         Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, link->Timeout);
50         SendCapabilities(1);
51 }
52
53 /** Constructor for incoming connections
54  */
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())
59 {
60         capab = new CapabData;
61         capab->capab_phase = 0;
62
63         for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
64         {
65                 ListenSocket::IOHookProvRef& iohookprovref = *i;
66                 if (!iohookprovref)
67                         continue;
68
69                 iohookprovref->OnAccept(this, client, server);
70                 // IOHook could have encountered a fatal error, e.g. if the TLS ClientHello was already in the queue and there was no common TLS version
71                 if (!getError().empty())
72                 {
73                         TreeSocket::OnError(I_ERR_OTHER);
74                         return;
75                 }
76         }
77
78         SendCapabilities(1);
79
80         Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
81 }
82
83 void TreeSocket::CleanNegotiationInfo()
84 {
85         // connect is good, reset the autoconnect block (if used)
86         if (capab->ac)
87                 capab->ac->position = -1;
88         delete capab;
89         capab = NULL;
90 }
91
92 CullResult TreeSocket::cull()
93 {
94         Utils->timeoutlist.erase(this);
95         if (capab && capab->ac)
96                 Utils->Creator->ConnectServer(capab->ac, false);
97         return this->BufferedSocket::cull();
98 }
99
100 TreeSocket::~TreeSocket()
101 {
102         delete capab;
103 }
104
105 /** When an outbound connection finishes connecting, we receive
106  * this event, and must do CAPAB negotiation with 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 eventually.
110  */
111 void TreeSocket::OnConnected()
112 {
113         if (this->LinkState == CONNECTING)
114         {
115                 if (!capab->link->Hook.empty())
116                 {
117                         ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
118                         if (!prov)
119                         {
120                                 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
121                                 return;
122                         }
123                         static_cast<IOHookProvider*>(prov)->OnConnect(this);
124                 }
125
126                 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
127                         (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
128                 this->SendCapabilities(1);
129         }
130 }
131
132 void TreeSocket::OnError(BufferedSocketError e)
133 {
134         ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
135                 linkID.c_str(), getError().c_str());
136         LinkState = DYING;
137         Close();
138 }
139
140 void TreeSocket::SendError(const std::string &errormessage)
141 {
142         WriteLine("ERROR :"+errormessage);
143         DoWrite();
144         LinkState = DYING;
145         SetError(errormessage);
146 }
147
148 CmdResult CommandSQuit::HandleServer(TreeServer* server, std::vector<std::string>& params)
149 {
150         TreeServer* quitting = Utils->FindServer(params[0]);
151         if (!quitting)
152         {
153                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
154                 return CMD_FAILURE;
155         }
156
157         CmdResult ret = CMD_SUCCESS;
158         if (quitting == server)
159         {
160                 ret = CMD_FAILURE;
161                 server = server->GetParent();
162         }
163         else if (quitting->GetParent() != server)
164                 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
165
166         server->SQuitChild(quitting, params[1]);
167
168         // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
169         // to stop this message from being forwarded.
170         // The squit logic generates a SQUIT message with our sid as the source and sends it to the
171         // remaining servers.
172         return ret;
173 }
174
175 /** This function is called when we receive data from a remote
176  * server.
177  */
178 void TreeSocket::OnDataReady()
179 {
180         Utils->Creator->loopCall = true;
181         std::string line;
182         while (GetNextLine(line))
183         {
184                 std::string::size_type rline = line.find('\r');
185                 if (rline != std::string::npos)
186                         line.erase(rline);
187                 if (line.find('\0') != std::string::npos)
188                 {
189                         SendError("Read null character from socket");
190                         break;
191                 }
192
193                 try
194                 {
195                         ProcessLine(line);
196                 }
197                 catch (CoreException& ex)
198                 {
199                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
200                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
201                         SendError(ex.GetReason() + " - check the log file for details");
202                 }
203
204                 if (!getError().empty())
205                         break;
206         }
207         if (LinkState != CONNECTED && recvq.length() > 4096)
208                 SendError("RecvQ overrun (line too long)");
209         Utils->Creator->loopCall = false;
210 }