1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "../transport.h"
19 #include "resolvers.h"
22 #include "treeserver.h"
24 #include "treesocket.h"
26 /* $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 */
28 /** This class is used to resolve server hostnames during /connect and autoconnect.
29 * As of 1.1, the resolver system is seperated out from BufferedSocket, so we must do this
30 * resolver step first ourselves if we need it. This is totally nonblocking, and will
31 * callback to OnLookupComplete or OnError when completed. Once it has completed we
32 * will have an IP address which we can then use to continue our connection.
34 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)
36 /* Nothing in here, folks */
39 void ServernameResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
41 /* Initiate the connection, now that we have an IP to use.
42 * Passing a hostname directly to BufferedSocket causes it to
43 * just bail and set its FD to -1.
45 TreeServer* CheckDupe = Utils->FindServer(MyLink.Name.c_str());
46 if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
49 if ((!MyLink.Hook.empty()) && (Utils->hooks.find(MyLink.Hook.c_str()) == Utils->hooks.end()))
52 TreeSocket* newsocket = new TreeSocket(this->Utils, ServerInstance, result,MyLink.Port,MyLink.Timeout ? MyLink.Timeout : 10,MyLink.Name.c_str(),
53 MyLink.Bind, MyLink.Hook.empty() ? NULL : Utils->hooks[MyLink.Hook.c_str()]);
54 if (newsocket->GetFd() > -1)
60 /* Something barfed, show the opers */
61 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",MyLink.Name.c_str(),strerror(errno));
62 if (ServerInstance->SocketCull.find(newsocket) == ServerInstance->SocketCull.end())
63 ServerInstance->SocketCull[newsocket] = newsocket;
64 Utils->DoFailOver(&MyLink);
69 void ServernameResolver::OnError(ResolverError e, const std::string &errormessage)
72 if (query == DNS_QUERY_AAAA)
75 ServernameResolver* snr = new ServernameResolver(mine, Utils, ServerInstance, host, MyLink, cached, DNS_QUERY_A);
76 ServerInstance->AddResolver(snr, cached);
79 ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: Unable to resolve hostname - %s", MyLink.Name.c_str(), errormessage.c_str() );
80 Utils->DoFailOver(&MyLink);