]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket1.cpp
Fix a bunch of weird indentation and spacing issues.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket1.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
9  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "iohook.h"
28
29 #include "main.h"
30 #include "utils.h"
31 #include "treeserver.h"
32 #include "link.h"
33 #include "treesocket.h"
34 #include "commands.h"
35
36 /** Constructor for outgoing connections.
37  * Because most of the I/O gubbins are encapsulated within
38  * BufferedSocket, we just call DoConnect() for most of the action,
39  * and only do minor initialization tasks ourselves.
40  */
41 TreeSocket::TreeSocket(Link* link, Autoconnect* myac, const irc::sockets::sockaddrs& dest)
42         : linkID(link->Name)
43         , LinkState(CONNECTING)
44         , MyRoot(NULL)
45         , proto_version(0)
46         , burstsent(false)
47         , age(ServerInstance->Time())
48 {
49         capab = new CapabData;
50         capab->link = link;
51         capab->ac = myac;
52         capab->capab_phase = 0;
53         capab->remotesa = dest;
54
55         irc::sockets::sockaddrs bind;
56         memset(&bind, 0, sizeof(bind));
57         if (!link->Bind.empty() && (dest.family() == AF_INET || dest.family() == AF_INET6))
58         {
59                 if (!irc::sockets::aptosa(link->Bind, 0, bind))
60                 {
61                         state = I_ERROR;
62                         SetError("Bind address '" + link->Bind + "' is not a valid IPv4 or IPv6 address");
63                         TreeSocket::OnError(I_ERR_BIND);
64                         return;
65                 }
66                 else if (bind.family() != dest.family())
67                 {
68                         state = I_ERROR;
69                         SetError("Bind address '" + bind.addr() + "' is not the same address family as destination address '" + dest.addr() + "'");
70                         TreeSocket::OnError(I_ERR_BIND);
71                         return;
72                 }
73         }
74
75         DoConnect(dest, bind, link->Timeout);
76         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, link->Timeout);
77         SendCapabilities(1);
78 }
79
80 /** Constructor for incoming connections
81  */
82 TreeSocket::TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
83         : BufferedSocket(newfd)
84         , linkID("inbound from " + client->addr())
85         , LinkState(WAIT_AUTH_1)
86         , MyRoot(NULL)
87         , proto_version(0)
88         , burstsent(false)
89         , age(ServerInstance->Time())
90 {
91         capab = new CapabData;
92         capab->capab_phase = 0;
93         capab->remotesa = *client;
94
95         for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
96         {
97                 ListenSocket::IOHookProvRef& iohookprovref = *i;
98                 if (!iohookprovref)
99                         continue;
100
101                 iohookprovref->OnAccept(this, client, server);
102                 // 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
103                 if (!getError().empty())
104                 {
105                         TreeSocket::OnError(I_ERR_OTHER);
106                         return;
107                 }
108         }
109
110         SendCapabilities(1);
111
112         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, 30);
113 }
114
115 void TreeSocket::CleanNegotiationInfo()
116 {
117         // connect is good, reset the autoconnect block (if used)
118         if (capab->ac)
119                 capab->ac->position = -1;
120         delete capab;
121         capab = NULL;
122 }
123
124 CullResult TreeSocket::cull()
125 {
126         Utils->timeoutlist.erase(this);
127         if (capab && capab->ac)
128                 Utils->Creator->ConnectServer(capab->ac, false);
129         return this->BufferedSocket::cull();
130 }
131
132 TreeSocket::~TreeSocket()
133 {
134         delete capab;
135 }
136
137 /** When an outbound connection finishes connecting, we receive
138  * this event, and must do CAPAB negotiation with the other
139  * side. If the other side is happy, as outlined in the server
140  * to server docs on the inspircd.org site, the other side
141  * will then send back its own SERVER string eventually.
142  */
143 void TreeSocket::OnConnected()
144 {
145         if (this->LinkState == CONNECTING)
146         {
147                 if (!capab->link->Hook.empty())
148                 {
149                         ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
150                         if (!prov)
151                         {
152                                 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
153                                 return;
154                         }
155                         static_cast<IOHookProvider*>(prov)->OnConnect(this);
156                 }
157
158                 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002[%s] started.", linkID.c_str(),
159                         (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
160                 this->SendCapabilities(1);
161         }
162 }
163
164 void TreeSocket::OnError(BufferedSocketError e)
165 {
166         ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
167                 linkID.c_str(), getError().c_str());
168         LinkState = DYING;
169         Close();
170 }
171
172 void TreeSocket::SendError(const std::string &errormessage)
173 {
174         WriteLine("ERROR :"+errormessage);
175         DoWrite();
176         LinkState = DYING;
177         SetError(errormessage);
178 }
179
180 CmdResult CommandSQuit::HandleServer(TreeServer* server, CommandBase::Params& params)
181 {
182         TreeServer* quitting = Utils->FindServer(params[0]);
183         if (!quitting)
184         {
185                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
186                 return CMD_FAILURE;
187         }
188
189         CmdResult ret = CMD_SUCCESS;
190         if (quitting == server)
191         {
192                 ret = CMD_FAILURE;
193                 server = server->GetParent();
194         }
195         else if (quitting->GetParent() != server)
196                 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
197
198         server->SQuitChild(quitting, params[1]);
199
200         // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
201         // to stop this message from being forwarded.
202         // The squit logic generates a SQUIT message with our sid as the source and sends it to the
203         // remaining servers.
204         return ret;
205 }
206
207 /** This function is called when we receive data from a remote
208  * server.
209  */
210 void TreeSocket::OnDataReady()
211 {
212         Utils->Creator->loopCall = true;
213         std::string line;
214         while (GetNextLine(line))
215         {
216                 std::string::size_type rline = line.find('\r');
217                 if (rline != std::string::npos)
218                         line.erase(rline);
219                 if (line.find('\0') != std::string::npos)
220                 {
221                         SendError("Read null character from socket");
222                         break;
223                 }
224
225                 try
226                 {
227                         ProcessLine(line);
228                 }
229                 catch (CoreException& ex)
230                 {
231                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
232                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
233                         SendError(ex.GetReason() + " - check the log file for details");
234                 }
235
236                 if (!getError().empty())
237                         break;
238         }
239         if (LinkState != CONNECTED && recvq.length() > 4096)
240                 SendError("RecvQ overrun (line too long)");
241         Utils->Creator->loopCall = false;
242 }