]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/user_resolver.cpp
Move DNS timeouts into the UserResolver class, where they should have been (but seems...
[user/henk/code/inspircd.git] / src / user_resolver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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
16 UserResolver::UserResolver(InspIRCd* Instance, User* user, std::string to_resolve, QueryType qt, bool &cache) :
17         Resolver(Instance, to_resolve, qt, cache), bound_user(user)
18 {
19         this->fwd = (qt == DNS_QUERY_A || qt == DNS_QUERY_AAAA);
20         this->bound_fd = user->GetFd();
21 }
22
23 void UserResolver::OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum)
24 {
25         ServerInstance->Log(DEBUG, "Got a result (lookup complete)");
26         /* We are only interested in the first matching result */
27         if (resultnum)
28                 return;
29
30         if ((!this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
31         {
32                 this->bound_user->stored_host = result;
33                 try
34                 {
35                         /* Check we didnt time out */
36                         if (this->bound_user->registered != REG_ALL)
37                         {
38                                 bool cached;
39 #ifdef IPV6
40                                 if (this->bound_user->GetProtocolFamily() == AF_INET6)
41                                 {
42                                         /* IPV6 forward lookup (with possibility of 4in6) */
43                                         const char* ip = this->bound_user->GetIPString();
44                                         bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, (!strncmp(ip, "0::ffff:", 8) ? DNS_QUERY_A : DNS_QUERY_AAAA), cached);
45                                 }
46                                 else
47                                         /* IPV4 lookup (mixed protocol mode) */
48 #endif
49                                 /* IPV4 lookup (ipv4 only mode) */
50                                 bound_user->res_forward = new UserResolver(this->ServerInstance, this->bound_user, result, DNS_QUERY_A, cached);
51                                 this->ServerInstance->AddResolver(bound_user->res_forward, cached);
52                         }
53                 }
54                 catch (CoreException& e)
55                 {
56                         ServerInstance->Log(DEBUG,"Error in resolver: %s",e.GetReason());
57                 }
58         }
59         else if ((this->fwd) && (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user))
60         {
61                 /* Both lookups completed */
62                 std::string result2("0::ffff:");
63                 result2.append(result);
64                 if (this->bound_user->GetIPString() == result || this->bound_user->GetIPString() == result2)
65                 {
66                         std::string hostname = this->bound_user->stored_host;
67                         if (hostname.length() < 65)
68                         {
69                                 /* Check we didnt time out */
70                                 if ((this->bound_user->registered != REG_ALL) && (!this->bound_user->dns_done))
71                                 {
72                                         /* Hostnames starting with : are not a good thing (tm) */
73                                         if (*(hostname.c_str()) == ':')
74                                                 hostname.insert(0, "0");
75
76                                         this->bound_user->WriteServ("NOTICE Auth :*** Found your hostname (%s)%s", hostname.c_str(), (cached ? " -- cached" : ""));
77                                         this->bound_user->dns_done = true;
78                                         strlcpy(this->bound_user->dhost, hostname.c_str(),64);
79                                         strlcpy(this->bound_user->host, hostname.c_str(),64);
80                                         /* Invalidate cache */
81                                         this->bound_user->InvalidateCache();
82                                 }
83                         }
84                         else
85                         {
86                                 if (!this->bound_user->dns_done)
87                                 {
88                                         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());
89                                         this->bound_user->dns_done = true;
90                                 }
91                         }
92                 }
93                 else
94                 {
95                         if (!this->bound_user->dns_done)
96                         {
97                                 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());
98                                 this->bound_user->dns_done = true;
99                         }
100                 }
101         }
102 }
103
104 void UserResolver::OnError(ResolverError e, const std::string &errormessage)
105 {
106         ServerInstance->Log(DEBUG, "Resolver error: " + errormessage);
107         if (ServerInstance->SE->GetRef(this->bound_fd) == this->bound_user)
108         {
109                 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());
110                 this->bound_user->dns_done = true;
111                 ServerInstance->stats->statsDnsBad++;
112         }
113 }
114
115