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