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