]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket1.cpp
Merge branch 'insp20' into insp3.
[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/server.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 irc::sockets::sockaddrs& dest)
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         irc::sockets::sockaddrs bind;
49         memset(&bind, 0, sizeof(bind));
50         if (!link->Bind.empty() && (dest.family() == AF_INET || dest.family() == AF_INET6))
51         {
52                 if (!irc::sockets::aptosa(link->Bind, 0, bind))
53                 {
54                         state = I_ERROR;
55                         SetError("Bind address '" + link->Bind + "' is not a valid IPv4 or IPv6 address");
56                         TreeSocket::OnError(I_ERR_BIND);
57                         return;
58                 }
59                 else if (bind.family() != dest.family())
60                 {
61                         state = I_ERROR;
62                         SetError("Bind address '" + bind.addr() + "' is not the same address family as destination address '" + dest.addr() + "'");
63                         TreeSocket::OnError(I_ERR_BIND);
64                         return;
65                 }
66         }
67
68         DoConnect(dest, bind, link->Timeout);
69         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, link->Timeout);
70         SendCapabilities(1);
71 }
72
73 /** Constructor for incoming connections
74  */
75 TreeSocket::TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
76         : BufferedSocket(newfd)
77         , linkID("inbound from " + client->addr()), LinkState(WAIT_AUTH_1), MyRoot(NULL), proto_version(0)
78         , burstsent(false), age(ServerInstance->Time())
79 {
80         capab = new CapabData;
81         capab->capab_phase = 0;
82
83         for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
84         {
85                 ListenSocket::IOHookProvRef& iohookprovref = *i;
86                 if (!iohookprovref)
87                         continue;
88
89                 iohookprovref->OnAccept(this, client, server);
90                 // 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
91                 if (!getError().empty())
92                 {
93                         TreeSocket::OnError(I_ERR_OTHER);
94                         return;
95                 }
96         }
97
98         SendCapabilities(1);
99
100         Utils->timeoutlist[this] = std::pair<std::string, unsigned int>(linkID, 30);
101 }
102
103 void TreeSocket::CleanNegotiationInfo()
104 {
105         // connect is good, reset the autoconnect block (if used)
106         if (capab->ac)
107                 capab->ac->position = -1;
108         delete capab;
109         capab = NULL;
110 }
111
112 CullResult TreeSocket::cull()
113 {
114         Utils->timeoutlist.erase(this);
115         if (capab && capab->ac)
116                 Utils->Creator->ConnectServer(capab->ac, false);
117         return this->BufferedSocket::cull();
118 }
119
120 TreeSocket::~TreeSocket()
121 {
122         delete capab;
123 }
124
125 /** When an outbound connection finishes connecting, we receive
126  * this event, and must do CAPAB negotiation with the other
127  * side. If the other side is happy, as outlined in the server
128  * to server docs on the inspircd.org site, the other side
129  * will then send back its own SERVER string eventually.
130  */
131 void TreeSocket::OnConnected()
132 {
133         if (this->LinkState == CONNECTING)
134         {
135                 if (!capab->link->Hook.empty())
136                 {
137                         ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
138                         if (!prov)
139                         {
140                                 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
141                                 return;
142                         }
143                         static_cast<IOHookProvider*>(prov)->OnConnect(this);
144                 }
145
146                 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \002%s\002[%s] started.", linkID.c_str(),
147                         (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
148                 this->SendCapabilities(1);
149         }
150 }
151
152 void TreeSocket::OnError(BufferedSocketError e)
153 {
154         ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
155                 linkID.c_str(), getError().c_str());
156         LinkState = DYING;
157         Close();
158 }
159
160 void TreeSocket::SendError(const std::string &errormessage)
161 {
162         WriteLine("ERROR :"+errormessage);
163         DoWrite();
164         LinkState = DYING;
165         SetError(errormessage);
166 }
167
168 CmdResult CommandSQuit::HandleServer(TreeServer* server, CommandBase::Params& params)
169 {
170         TreeServer* quitting = Utils->FindServer(params[0]);
171         if (!quitting)
172         {
173                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
174                 return CMD_FAILURE;
175         }
176
177         CmdResult ret = CMD_SUCCESS;
178         if (quitting == server)
179         {
180                 ret = CMD_FAILURE;
181                 server = server->GetParent();
182         }
183         else if (quitting->GetParent() != server)
184                 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
185
186         server->SQuitChild(quitting, params[1]);
187
188         // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
189         // to stop this message from being forwarded.
190         // The squit logic generates a SQUIT message with our sid as the source and sends it to the
191         // remaining servers.
192         return ret;
193 }
194
195 /** This function is called when we receive data from a remote
196  * server.
197  */
198 void TreeSocket::OnDataReady()
199 {
200         Utils->Creator->loopCall = true;
201         std::string line;
202         while (GetNextLine(line))
203         {
204                 std::string::size_type rline = line.find('\r');
205                 if (rline != std::string::npos)
206                         line.erase(rline);
207                 if (line.find('\0') != std::string::npos)
208                 {
209                         SendError("Read null character from socket");
210                         break;
211                 }
212
213                 try
214                 {
215                         ProcessLine(line);
216                 }
217                 catch (CoreException& ex)
218                 {
219                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
220                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
221                         SendError(ex.GetReason() + " - check the log file for details");
222                 }
223
224                 if (!getError().empty())
225                         break;
226         }
227         if (LinkState != CONNECTED && recvq.length() > 4096)
228                 SendError("RecvQ overrun (line too long)");
229         Utils->Creator->loopCall = false;
230 }