]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/resolvers.cpp
Fix a lot of calls to match() and uses of wildcard.h (which doesn't exist anymore...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / resolvers.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 "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 "m_spanningtree/resolvers.h"
22 #include "m_spanningtree/main.h"
23 #include "m_spanningtree/utils.h"
24 #include "m_spanningtree/treeserver.h"
25 #include "m_spanningtree/link.h"
26 #include "m_spanningtree/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, int resultnum)
42 {
43         if (resultnum)
44                 return;
45
46         /* Initiate the connection, now that we have an IP to use.
47          * Passing a hostname directly to BufferedSocket causes it to
48          * just bail and set its FD to -1.
49          */
50         TreeServer* CheckDupe = Utils->FindServer(MyLink.Name.c_str());
51         if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
52         {
53
54                 if ((!MyLink.Hook.empty()) && (Utils->hooks.find(MyLink.Hook.c_str()) ==  Utils->hooks.end()))
55                         return;
56
57                 TreeSocket* newsocket = new TreeSocket(this->Utils, ServerInstance, result,MyLink.Port,false,MyLink.Timeout ? MyLink.Timeout : 10,MyLink.Name.c_str(),
58                                                         MyLink.Bind, MyLink.Hook.empty() ? NULL : Utils->hooks[MyLink.Hook.c_str()]);
59                 if (newsocket->GetFd() > -1)
60                 {
61                         /* We're all OK */
62                 }
63                 else
64                 {
65                         /* Something barfed, show the opers */
66                         Utils->Creator->RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: %s.",MyLink.Name.c_str(),strerror(errno));
67                         if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
68                                 ServerInstance->SocketCull[newsocket] = newsocket;
69                         Utils->DoFailOver(&MyLink);
70                 }
71         }
72 }
73
74 void ServernameResolver::OnError(ResolverError e, const std::string &errormessage)
75 {
76         /* Ooops! */
77         if (query == DNS_QUERY_AAAA)
78         {
79                 bool cached;
80                 ServernameResolver* snr = new ServernameResolver(mine, Utils, ServerInstance, host, MyLink, cached, DNS_QUERY_A);
81                 ServerInstance->AddResolver(snr, cached);
82                 return;
83         }
84         Utils->Creator->RemoteMessage(NULL, "CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s", MyLink.Name.c_str(), errormessage.c_str() );
85         Utils->DoFailOver(&MyLink);
86 }
87