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