]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Added back ip->host caching now we can make it not be a performance hit (we write...
[user/henk/code/inspircd.git] / src / dnsqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
26 #include <cstdio>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #include "users.h"
39 #include "globals.h"
40 #include "inspstring.h"
41 #include "dnsqueue.h"
42 #include <time.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/time.h>
46 #include <netinet/in.h>
47 #include <string.h>
48 #include "dns.h"
49 #include "helperfuncs.h"
50 #include "hashcomp.h"
51 #include "socketengine.h"
52
53 extern ServerConfig* Config;
54 extern InspIRCd* ServerInstance;
55
56 address_cache addrcache;
57
58 class Lookup;
59
60 Lookup* dnslist[MAX_DESCRIPTORS];
61
62 //enum LookupState { reverse, forward };
63
64 class Lookup {
65 private:
66         DNS resolver1;
67         DNS resolver2;
68         char u[NICKMAX];
69         std::string hostname;
70 public:
71         Lookup()
72         {
73                 *u = 0;
74         }
75
76         void Reset()
77         {
78                 *u = 0;
79         }
80
81         ~Lookup()
82         {
83         }
84
85         bool DoLookup(std::string nick)
86         {
87                 hostname = "";
88                 userrec* usr = Find(nick);
89                 if (usr)
90                 {
91                         resolver1.SetNS(std::string(Config->DNSServer));
92                         if (!resolver1.ReverseLookup(std::string(usr->host)))
93                         {
94                                 return false;
95                         }
96                         strlcpy(u,nick.c_str(),NICKMAX);
97
98                         /* ASSOCIATE WITH DNS LOOKUP LIST */
99                         if (resolver1.GetFD() != -1)
100                         {
101                                 dnslist[resolver1.GetFD()] = this;
102                                 return true;
103                         }
104                 }
105                 return false;
106         }
107
108         bool Done(int fdcheck)
109         {
110                 if (hostname != "")
111                 {
112                         // doing forward lookup
113                         userrec* usr = NULL;
114                         if (resolver2.HasResult(fdcheck))
115                         {
116                                 if (resolver2.GetFD() != -1)
117                                 {
118                                         dnslist[resolver2.GetFD()] = NULL;
119                                         std::string ip = resolver2.GetResultIP();
120                                         usr = Find(u);
121                                         if (usr)
122                                         {
123                                                 if (usr->registered > 3)
124                                                 {
125                                                         usr->dns_done = true;
126                                                         return true;
127                                                 }
128                                                 if ((hostname != "") && (usr->registered != 7))
129                                                 {
130                                                         if (std::string((char*)inet_ntoa(usr->ip4)) == ip)
131                                                         {
132                                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
133                                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
134                                                                 address_hash::iterator address = addrcache.find(usr->ip4);
135                                                                 if (address == addrcache.end())
136                                                                 {
137                                                                         log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
138                                                                         addrcache[usr->ip4] = new std::string(hostname);
139                                                                 }
140                                                         }
141                                                         usr->dns_done = true;
142                                                         return true;
143                                                 }
144                                         }
145                                 }
146                                 else
147                                 {
148                                         usr = Find(u);
149                                         if (usr)
150                                         {
151                                                 usr->dns_done = true;
152                                         }
153                                         return true;
154                                 }
155                         }
156                         return false;
157                 }
158                 else
159                 {
160                         // doing reverse lookup
161                         userrec* usr = NULL;
162                         if (resolver1.HasResult(fdcheck))
163                         {
164                                 usr = Find(u);
165                                 if ((usr) && (usr->dns_done))
166                                 {
167                                         if (resolver1.GetFD() != -1)
168                                                 dnslist[resolver1.GetFD()] = NULL;
169                                         return true;
170                                 }
171                                 if (resolver1.GetFD() != -1)
172                                 {
173                                         dnslist[resolver1.GetFD()] = NULL;
174                                         hostname = resolver1.GetResult();
175                                         if (usr)
176                                         {
177                                                 if ((usr->registered > 3) || (hostname == ""))
178                                                 {
179                                                         usr->dns_done = true;
180                                                         return true;
181                                                 }
182                                         }
183                                         if (hostname != "")
184                                         {
185                                                 resolver2.ForwardLookup(hostname);
186                                                 if (resolver2.GetFD() != -1)
187                                                         dnslist[resolver2.GetFD()] = this;
188                                         }
189                                 }
190                         }
191                 }
192                 return false;
193         }
194
195         int GetFD()
196         {
197                 userrec* usr = Find(u);
198                 if (!usr)
199                         return 0;
200                 if (usr->dns_done)
201                         return 0;
202                 return usr->fd;
203         }
204 };
205
206 bool lookup_dns(std::string nick)
207 {
208         /* First attempt to find the nickname */
209         userrec* u = Find(nick);
210         if (u)
211         {
212                 /* Check the cache */
213                 address_hash::iterator address = addrcache.find(u->ip4);
214                 if (address != addrcache.end())
215                 {
216                         /* Theyre in the cache, dont waste a lookup */
217                         WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
218                         log(DEBUG,"Found cached host");
219                         strlcpy(u->host,address->second->c_str(),MAXBUF);
220                         strlcpy(u->dhost,address->second->c_str(),MAXBUF);
221                         u->dns_done = true;
222                         return true;
223                 }
224                 /* If the user exists, create a new
225                  * lookup object, and associate it
226                  * with the user. The lookup object
227                  * will maintain the reference table
228                  * which we use for quickly finding
229                  * dns results. Please note that we
230                  * do not associate a lookup with a
231                  * userrec* pointer and we use the
232                  * nickname instead because, by the
233                  * time the DNS lookup has completed,
234                  * the nickname could have quit and
235                  * if we then try and access the
236                  * pointer we get a nice segfault.
237                  */
238                 Lookup* L = new Lookup();
239                 L->DoLookup(nick);
240                 return true;
241         }
242         return false;
243 }
244
245 void dns_poll(int fdcheck)
246 {
247         /* Check the given file descriptor is in valid range */
248         if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
249                 return;
250
251         /* Try and find the file descriptor in our list of
252          * active DNS lookups
253          */
254         Lookup *x = dnslist[fdcheck];
255         if (x)
256         {
257                 /* If it exists check if its a valid fd still */
258                 if (x->GetFD() != -1)
259                 {
260                         /* Check if its done, if it is delete it */
261                         if (x->Done(fdcheck))
262                         {
263                                 /* We don't need to delete the file descriptor
264                                  * from the socket engine, as dns.cpp tracks it
265                                  * for us if we are in single-threaded country.
266                                  */
267                                 delete x;
268                         }
269                 }
270                 else
271                 {
272                         /* its fd is dodgy, the dns code probably
273                          * bashed it due to error. Free the class.
274                          */
275                         delete x;
276                 }
277                 /* If we got down here, the dns lookup was valid, BUT,
278                  * its still in progress. Be patient, and wait for
279                  * more socketengine events to complete the lookups.
280                  */
281                 return;
282         }
283         /* This FD doesnt belong here, lets be rid of it,
284          * just to be safe so we dont get any more events
285          * about it.
286          */
287         if (ServerInstance && ServerInstance->SE)
288                 ServerInstance->SE->DelFd(fdcheck);
289 }
290