]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/resolvers.cpp
Add HasFd to EventHandler and switch code to use it.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / resolvers.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013, 2016 Adam <Adam@anope.org>
6  *   Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2007, 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
29 #include "cachetimer.h"
30 #include "resolvers.h"
31 #include "main.h"
32 #include "utils.h"
33 #include "treeserver.h"
34 #include "link.h"
35 #include "treesocket.h"
36
37 /** This class is used to resolve server hostnames during /connect and autoconnect.
38  * As of 1.1, the resolver system is seperated out from BufferedSocket, so we must do this
39  * resolver step first ourselves if we need it. This is totally nonblocking, and will
40  * callback to OnLookupComplete or OnError when completed. Once it has completed we
41  * will have an IP address which we can then use to continue our connection.
42  */
43 ServernameResolver::ServernameResolver(DNS::Manager* mgr, const std::string& hostname, Link* x, DNS::QueryType qt, Autoconnect* myac)
44         : DNS::Request(mgr, Utils->Creator, hostname, qt)
45         , query(qt), host(hostname), MyLink(x), myautoconnect(myac)
46 {
47 }
48
49 void ServernameResolver::OnLookupComplete(const DNS::Query *r)
50 {
51         const DNS::ResourceRecord* const ans_record = r->FindAnswerOfType(this->question.type);
52         if (!ans_record)
53         {
54                 OnError(r);
55                 return;
56         }
57
58         irc::sockets::sockaddrs sa;
59         if (!irc::sockets::aptosa(ans_record->rdata, MyLink->Port, sa))
60         {
61                 // We had a result but it wasn't a valid IPv4/IPv6.
62                 OnError(r);
63                 return;
64         }
65
66         /* Initiate the connection, now that we have an IP to use.
67          * Passing a hostname directly to BufferedSocket causes it to
68          * just bail and set its FD to -1.
69          */
70         TreeServer* CheckDupe = Utils->FindServer(MyLink->Name);
71         if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
72         {
73                 TreeSocket* newsocket = new TreeSocket(MyLink, myautoconnect, sa);
74                 if (!newsocket->HasFd())
75                 {
76                         /* Something barfed, show the opers */
77                         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",
78                                 MyLink->Name.c_str(), newsocket->getError().c_str());
79                         ServerInstance->GlobalCulls.AddItem(newsocket);
80                 }
81         }
82 }
83
84 void ServernameResolver::OnError(const DNS::Query *r)
85 {
86         if (r->error == DNS::ERROR_UNLOADED)
87         {
88                 // We're being unloaded, skip the snotice and ConnectServer() below to prevent autoconnect creating new sockets
89                 return;
90         }
91
92         if (query == DNS::QUERY_AAAA)
93         {
94                 ServernameResolver* snr = new ServernameResolver(this->manager, host, MyLink, DNS::QUERY_A, myautoconnect);
95                 try
96                 {
97                         this->manager->Process(snr);
98                         return;
99                 }
100                 catch (DNS::Exception &)
101                 {
102                         delete snr;
103                 }
104         }
105
106         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s", MyLink->Name.c_str(), this->manager->GetErrorStr(r->error).c_str());
107         Utils->Creator->ConnectServer(myautoconnect, false);
108 }
109
110 SecurityIPResolver::SecurityIPResolver(Module* me, DNS::Manager* mgr, const std::string& hostname, Link* x, DNS::QueryType qt)
111         : DNS::Request(mgr, me, hostname, qt)
112         , MyLink(x), mine(me), host(hostname), query(qt)
113 {
114 }
115
116 void SecurityIPResolver::OnLookupComplete(const DNS::Query *r)
117 {
118         for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i != Utils->LinkBlocks.end(); ++i)
119         {
120                 Link* L = *i;
121                 if (L->IPAddr == host)
122                 {
123                         for (std::vector<DNS::ResourceRecord>::const_iterator j = r->answers.begin(); j != r->answers.end(); ++j)
124                         {
125                                 const DNS::ResourceRecord& ans_record = *j;
126                                 if (ans_record.type == this->question.type)
127                                         Utils->ValidIPs.push_back(ans_record.rdata);
128                         }
129                         break;
130                 }
131         }
132 }
133
134 void SecurityIPResolver::OnError(const DNS::Query *r)
135 {
136         // This can be called because of us being unloaded but we don't have to do anything differently
137         if (query == DNS::QUERY_AAAA)
138         {
139                 SecurityIPResolver* res = new SecurityIPResolver(mine, this->manager, host, MyLink, DNS::QUERY_A);
140                 try
141                 {
142                         this->manager->Process(res);
143                         return;
144                 }
145                 catch (DNS::Exception &)
146                 {
147                         delete res;
148                 }
149         }
150         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Could not resolve IP associated with Link '%s': %s",
151                 MyLink->Name.c_str(), this->manager->GetErrorStr(r->error).c_str());
152 }
153
154 CacheRefreshTimer::CacheRefreshTimer()
155         : Timer(3600, true)
156 {
157 }
158
159 bool CacheRefreshTimer::Tick(time_t TIME)
160 {
161         Utils->RefreshIPCache();
162         return true;
163 }