]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/resolvers.cpp
Change to Duration for second param
[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 "socket.h"
16 #include "xline.h"
17
18 #include "resolvers.h"
19 #include "main.h"
20 #include "utils.h"
21 #include "treeserver.h"
22 #include "link.h"
23 #include "treesocket.h"
24
25 /* $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 */
26
27 /** This class is used to resolve server hostnames during /connect and autoconnect.
28  * As of 1.1, the resolver system is seperated out from BufferedSocket, so we must do this
29  * resolver step first ourselves if we need it. This is totally nonblocking, and will
30  * callback to OnLookupComplete or OnError when completed. Once it has completed we
31  * will have an IP address which we can then use to continue our connection.
32  */
33 ServernameResolver::ServernameResolver(SpanningTreeUtilities* Util, const std::string &hostname, Link* x, bool &cached, QueryType qt, Autoconnect* myac)
34         : Resolver(hostname, qt, cached, Util->Creator), Utils(Util), query(qt), host(hostname), MyLink(x), myautoconnect(myac)
35 {
36 }
37
38 void ServernameResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
39 {
40         /* Initiate the connection, now that we have an IP to use.
41          * Passing a hostname directly to BufferedSocket causes it to
42          * just bail and set its FD to -1.
43          */
44         TreeServer* CheckDupe = Utils->FindServer(MyLink->Name.c_str());
45         if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
46         {
47                 TreeSocket* newsocket = new TreeSocket(Utils, result, MyLink->Port, MyLink->Timeout ? MyLink->Timeout : 10,
48                         MyLink->Name.c_str(), MyLink->Bind, myautoconnect, MyLink->Hook);
49                 if (newsocket->GetFd() > -1)
50                 {
51                         /* We're all OK */
52                 }
53                 else
54                 {
55                         /* Something barfed, show the opers */
56                         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",
57                                 MyLink->Name.c_str(), newsocket->getError().c_str());
58                         ServerInstance->GlobalCulls.AddItem(newsocket);
59                 }
60         }
61 }
62
63 void ServernameResolver::OnError(ResolverError e, const std::string &errormessage)
64 {
65         /* Ooops! */
66         if (query == DNS_QUERY_AAAA)
67         {
68                 bool cached;
69                 ServernameResolver* snr = new ServernameResolver(Utils, host, MyLink, cached, DNS_QUERY_A, myautoconnect);
70                 ServerInstance->AddResolver(snr, cached);
71                 return;
72         }
73         ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s", MyLink->Name.c_str(), errormessage.c_str() );
74         Utils->Creator->ConnectServer(myautoconnect, false);
75 }
76