]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket1.cpp
m_spanningtree Get rid of some boilerplate
[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 /** Because most of the I/O gubbins are encapsulated within
35  * BufferedSocket, we just call the superclass constructor for
36  * most of the action, and append a few of our own values
37  * to it.
38  */
39 TreeSocket::TreeSocket(Link* link, Autoconnect* myac, const std::string& ipaddr)
40         : linkID(assign(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 /** When a listening socket gives us a new file descriptor,
54  * we must associate it with a socket without creating a new
55  * connection. This constructor is used for this purpose.
56  */
57 TreeSocket::TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
58         : BufferedSocket(newfd)
59         , linkID("inbound from " + client->addr()), LinkState(WAIT_AUTH_1), MyRoot(NULL), proto_version(0)
60         , burstsent(false), age(ServerInstance->Time())
61 {
62         capab = new CapabData;
63         capab->capab_phase = 0;
64
65         if (via->iohookprov)
66                 via->iohookprov->OnAccept(this, client, server);
67         SendCapabilities(1);
68
69         Utils->timeoutlist[this] = std::pair<std::string, int>(linkID, 30);
70 }
71
72 void TreeSocket::CleanNegotiationInfo()
73 {
74         // connect is good, reset the autoconnect block (if used)
75         if (capab->ac)
76                 capab->ac->position = -1;
77         delete capab;
78         capab = NULL;
79 }
80
81 CullResult TreeSocket::cull()
82 {
83         Utils->timeoutlist.erase(this);
84         if (capab && capab->ac)
85                 Utils->Creator->ConnectServer(capab->ac, false);
86         return this->BufferedSocket::cull();
87 }
88
89 TreeSocket::~TreeSocket()
90 {
91         delete capab;
92 }
93
94 /** When an outbound connection finishes connecting, we receive
95  * this event, and must send our SERVER string to the other
96  * side. If the other side is happy, as outlined in the server
97  * to server docs on the inspircd.org site, the other side
98  * will then send back its own server string.
99  */
100 void TreeSocket::OnConnected()
101 {
102         if (this->LinkState == CONNECTING)
103         {
104                 if (!capab->link->Hook.empty())
105                 {
106                         ServiceProvider* prov = ServerInstance->Modules->FindService(SERVICE_IOHOOK, capab->link->Hook);
107                         if (!prov)
108                         {
109                                 SetError("Could not find hook '" + capab->link->Hook + "' for connection to " + linkID);
110                                 return;
111                         }
112                         static_cast<IOHookProvider*>(prov)->OnConnect(this);
113                 }
114
115                 ServerInstance->SNO->WriteGlobalSno('l', "Connection to \2%s\2[%s] started.", linkID.c_str(),
116                         (capab->link->HiddenFromStats ? "<hidden>" : capab->link->IPAddr.c_str()));
117                 this->SendCapabilities(1);
118         }
119 }
120
121 void TreeSocket::OnError(BufferedSocketError e)
122 {
123         ServerInstance->SNO->WriteGlobalSno('l', "Connection to '\002%s\002' failed with error: %s",
124                 linkID.c_str(), getError().c_str());
125         LinkState = DYING;
126         Close();
127 }
128
129 void TreeSocket::SendError(const std::string &errormessage)
130 {
131         WriteLine("ERROR :"+errormessage);
132         DoWrite();
133         LinkState = DYING;
134         SetError(errormessage);
135 }
136
137 CmdResult CommandSQuit::HandleServer(TreeServer* server, std::vector<std::string>& params)
138 {
139         TreeServer* quitting = Utils->FindServer(params[0]);
140         if (!quitting)
141         {
142                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Squit from unknown server");
143                 return CMD_FAILURE;
144         }
145
146         CmdResult ret = CMD_SUCCESS;
147         if (quitting == server)
148         {
149                 ret = CMD_FAILURE;
150                 server = server->GetParent();
151         }
152         else if (quitting->GetParent() != server)
153                 throw ProtocolException("Attempted to SQUIT a non-directly connected server or the parent");
154
155         server->SQuitChild(quitting, params[1]);
156
157         // XXX: Return CMD_FAILURE when servers SQUIT themselves (i.e. :00S SQUIT 00S :Shutting down)
158         // to stop this message from being forwarded.
159         // The squit logic generates a SQUIT message with our sid as the source and sends it to the
160         // remaining servers.
161         return ret;
162 }
163
164 /** This function is called when we receive data from a remote
165  * server.
166  */
167 void TreeSocket::OnDataReady()
168 {
169         Utils->Creator->loopCall = true;
170         std::string line;
171         while (GetNextLine(line))
172         {
173                 std::string::size_type rline = line.find('\r');
174                 if (rline != std::string::npos)
175                         line.erase(rline);
176                 if (line.find('\0') != std::string::npos)
177                 {
178                         SendError("Read null character from socket");
179                         break;
180                 }
181
182                 try
183                 {
184                         ProcessLine(line);
185                 }
186                 catch (CoreException& ex)
187                 {
188                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error while processing: " + line);
189                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, ex.GetReason());
190                         SendError(ex.GetReason() + " - check the log file for details");
191                 }
192
193                 if (!getError().empty())
194                         break;
195         }
196         if (LinkState != CONNECTED && recvq.length() > 4096)
197                 SendError("RecvQ overrun (line too long)");
198         Utils->Creator->loopCall = false;
199 }