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 UserResolver::UserResolver(User* user, std::string to_resolve, QueryType qt, bool &cache) :
18 Resolver(to_resolve, qt, cache), bound_user(user)
20 this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
21 this->bound_fd = user->GetFd();
24 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
26 UserResolver *res_forward; // for forward-resolution
28 if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
30 this->bound_user->stored_host = result;
33 /* Check we didnt time out */
34 if (this->bound_user->registered != REG_ALL)
37 if (this->bound_user->client_sa.sa.sa_family == AF_INET6)
39 /* IPV6 forward lookup */
40 res_forward = new UserResolver(bound_user, result, DNS_QUERY_AAAA, lcached);
45 res_forward = new UserResolver(bound_user, result, DNS_QUERY_A, lcached);
47 ServerInstance->AddResolver(res_forward, lcached);
50 catch (CoreException& e)
52 ServerInstance->Logs->Log("RESOLVER", DEBUG,"Error in resolver: %s",e.GetReason());
55 else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
57 /* Both lookups completed */
59 irc::sockets::sockaddrs* user_ip = &this->bound_user->client_sa;
60 bool rev_match = false;
61 if (user_ip->sa.sa_family == AF_INET6)
63 struct in6_addr res_bin;
64 if (inet_pton(AF_INET6, result.c_str(), &res_bin))
66 rev_match = !memcmp(&user_ip->in6.sin6_addr, &res_bin, sizeof(res_bin));
71 struct in_addr res_bin;
72 if (inet_pton(AF_INET, result.c_str(), &res_bin))
74 rev_match = !memcmp(&user_ip->in4.sin_addr, &res_bin, sizeof(res_bin));
80 std::string hostname = this->bound_user->stored_host;
81 if (hostname.length() < 65)
83 /* Check we didnt time out */
84 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
86 /* Hostnames starting with : are not a good thing (tm) */
87 if (hostname[0] == ':')
88 hostname.insert(0, "0");
90 this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
91 this->bound_user->dns_done = true;
92 this->bound_user->dhost.assign(hostname, 0, 64);
93 this->bound_user->host.assign(hostname, 0, 64);
94 /* Invalidate cache */
95 this->bound_user->InvalidateCache();
100 if (!this->bound_user->dns_done)
102 this->bound_user->WriteServ("NOTICE Auth :*** Your hostname is longer than the maximum of 64 characters, using your IP address (%s) instead.", this->bound_user->GetIPString());
103 this->bound_user->dns_done = true;
109 if (!this->bound_user->dns_done)
111 this->bound_user->WriteServ("NOTICE Auth :*** Your hostname does not match up with your IP address. Sorry, using your IP address (%s) instead.", this->bound_user->GetIPString());
112 this->bound_user->dns_done = true;
116 // Save some memory by freeing this up; it's never used again in the user's lifetime.
117 this->bound_user->stored_host.resize(0);
121 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
123 if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
125 this->bound_user->WriteServ("NOTICE Auth :*** Could not resolve your hostname: %s; using your IP address (%s) instead.", errormessage.c_str(), this->bound_user->GetIPString());
126 this->bound_user->dns_done = true;
127 this->bound_user->stored_host.resize(0);
128 ServerInstance->stats->statsDnsBad++;