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