]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/resolvers.cpp
51197cb11f908eb193f784c657ce5a14a9c7dbfe
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / resolvers.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "../transport.h"
20
21 #include "resolvers.h"
22 #include "main.h"
23 #include "utils.h"
24 #include "treeserver.h"
25 #include "link.h"
26 #include "treesocket.h"
27
28 /* $ModDep: m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
29
30 /** This class is used to resolve server hostnames during /connect and autoconnect.
31  * As of 1.1, the resolver system is seperated out from BufferedSocket, so we must do this
32  * resolver step first ourselves if we need it. This is totally nonblocking, and will
33  * callback to OnLookupComplete or OnError when completed. Once it has completed we
34  * will have an IP address which we can then use to continue our connection.
35  */
36 ServernameResolver::ServernameResolver(Module* me, SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &hostname, Link x, bool &cached, QueryType qt) : Resolver(Instance, hostname, qt, cached, me), MyLink(x), Utils(Util), query(qt), host(hostname), mine(me)
37 {
38         /* Nothing in here, folks */
39 }
40
41 void ServernameResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
42 {
43         /* Initiate the connection, now that we have an IP to use.
44          * Passing a hostname directly to BufferedSocket causes it to
45          * just bail and set its FD to -1.
46          */
47         TreeServer* CheckDupe = Utils->FindServer(MyLink.Name.c_str());
48         if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
49         {
50
51                 if ((!MyLink.Hook.empty()) && (Utils->hooks.find(MyLink.Hook.c_str()) ==  Utils->hooks.end()))
52                         return;
53
54                 TreeSocket* newsocket = new TreeSocket(this->Utils, ServerInstance, result,MyLink.Port,MyLink.Timeout ? MyLink.Timeout : 10,MyLink.Name.c_str(),
55                                                         MyLink.Bind, MyLink.Hook.empty() ? NULL : Utils->hooks[MyLink.Hook.c_str()]);
56                 if (newsocket->GetFd() > -1)
57                 {
58                         /* We're all OK */
59                 }
60                 else
61                 {
62                         /* Something barfed, show the opers */
63                         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",MyLink.Name.c_str(),strerror(errno));
64                         if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
65                                 ServerInstance->SocketCull[newsocket] = newsocket;
66                         Utils->DoFailOver(&MyLink);
67                 }
68         }
69 }
70
71 void ServernameResolver::OnError(ResolverError e, const std::string &errormessage)
72 {
73         /* Ooops! */
74         if (query == DNS_QUERY_AAAA)
75         {
76                 bool cached;
77                 ServernameResolver* snr = new ServernameResolver(mine, Utils, ServerInstance, host, MyLink, cached, DNS_QUERY_A);
78                 ServerInstance->AddResolver(snr, cached);
79                 return;
80         }
81         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s", MyLink.Name.c_str(), errormessage.c_str() );
82         Utils->DoFailOver(&MyLink);
83 }
84