]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
DNS timeout fixes!
[user/henk/code/inspircd.git] / src / dnsqueue.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 /* Now with added unF! ;) */
18
19 using namespace std;
20
21 #include "inspircd.h"
22 #include "inspircd_io.h"
23 #include "inspircd_util.h"
24 #include "inspircd_config.h"
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
30 #include <cstdio>
31 #include <time.h>
32 #include <string>
33 #ifdef GCC3
34 #include <ext/hash_map>
35 #else
36 #include <hash_map>
37 #endif
38 #include <map>
39 #include <sstream>
40 #include <vector>
41 #include <errno.h>
42 #include <deque>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <sched.h>
46 #include "connection.h"
47 #include "users.h"
48 #include "servers.h"
49 #include "ctables.h"
50 #include "globals.h"
51 #include "modules.h"
52 #include "dynamic.h"
53 #include "wildcard.h"
54 #include "message.h"
55 #include "mode.h"
56 #include "commands.h"
57 #include "xline.h"
58 #include "inspstring.h"
59 #include "dnsqueue.h"
60 #include <stdlib.h>
61 #include <time.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
64 #include <sys/poll.h>
65 #include <sys/time.h>
66 #include <netinet/in.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <stdio.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include "dns.h"
73
74 #ifdef GCC3
75 #define nspace __gnu_cxx
76 #else
77 #define nspace std
78 #endif
79
80 extern int MaxWhoResults;
81
82 extern std::vector<Module*> modules;
83 extern std::vector<std::string> module_names;
84 extern std::vector<ircd_module*> factory;
85 extern std::vector<int> fd_reap;
86
87 extern int MODCOUNT;
88
89 namespace nspace
90 {
91 #ifdef GCC34
92         template<> struct hash<in_addr>
93 #else
94         template<> struct nspace::hash<in_addr>
95 #endif
96         {
97                 size_t operator()(const struct in_addr &a) const
98                 {
99                         size_t q;
100                         memcpy(&q,&a,sizeof(size_t));
101                         return q;
102                 }
103         };
104 #ifdef GCC34
105         template<> struct hash<string>
106 #else
107         template<> struct nspace::hash<string>
108 #endif
109         {
110                 size_t operator()(const string &s) const
111                 {
112                         char a[MAXBUF];
113                         static struct hash<const char *> strhash;
114                         strlcpy(a,s.c_str(),MAXBUF);
115                         strlower(a);
116                         return strhash(a);
117                 }
118         };
119 }
120
121
122 struct StrHashComp
123 {
124
125         bool operator()(const string& s1, const string& s2) const
126         {
127                 char a[MAXBUF],b[MAXBUF];
128                 strlcpy(a,s1.c_str(),MAXBUF);
129                 strlcpy(b,s2.c_str(),MAXBUF);
130                 return (strcasecmp(a,b) == 0);
131         }
132
133 };
134
135 struct InAddr_HashComp
136 {
137
138         bool operator()(const in_addr &s1, const in_addr &s2) const
139         {
140                 size_t q;
141                 size_t p;
142                 
143                 memcpy(&q,&s1,sizeof(size_t));
144                 memcpy(&p,&s2,sizeof(size_t));
145                 
146                 return (q == p);
147         }
148
149 };
150
151
152 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
153 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
154 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
155 typedef std::deque<command_t> command_table;
156
157 extern user_hash clientlist;
158 extern chan_hash chanlist;
159 extern user_hash whowas;
160 extern command_table cmdlist;
161 extern address_cache IP;
162
163 extern ClassVector Classes;
164
165 extern char DNSServer[MAXBUF];
166
167 class Lookup {
168 private:
169         DNS* resolver;
170         userrec* u;
171 public:
172         Lookup()
173         {
174                 u = NULL;
175                 resolver = NULL;
176         }
177
178         ~Lookup()
179         {
180                 if (resolver)
181                         delete resolver;
182         }
183
184         Lookup(userrec* user)
185         {
186                 u = user;
187                 log(DEBUG,"New Lookup class with DNSServer set to '%s'",DNSServer);
188                 resolver = new DNS(std::string(DNSServer));
189                 resolver->ReverseLookup(std::string(user->host));
190         }
191
192         bool Done()
193         {
194                 if (resolver->HasResult())
195                 {
196                         log(DEBUG,"resolver says result available!");
197                         if (resolver->GetFD() != 0)
198                         {
199                                 log(DEBUG,"Resolver FD is not 0");
200                                 std::string hostname = resolver->GetResult();
201                                 if (u)
202                                 {
203                                         log(DEBUG,"Applying hostname lookup to %s: %s",u->nick,hostname.c_str());
204                                         if (hostname != "")
205                                         {
206                                                 strlcpy(u->host,hostname.c_str(),MAXBUF);
207                                                 WriteServ(u->fd,"NOTICE Auth :Resolved your hostname: %s",hostname.c_str());
208                                                 u->dns_done = true;
209                                                 return true;
210                                         }
211                                         return false;
212                                 }
213                         }
214                         else
215                         {
216                                 u->dns_done = true;
217                                 return true;
218                         }
219                 }
220                 return false;
221         }
222
223         int GetFD()
224         {
225                 if (u)
226                 {
227                         return u->fd;
228                 }
229                 else return 0;
230         }
231 };
232
233 typedef std::vector<Lookup> dns_queue;
234
235 dns_queue dnsq;
236
237 bool lookup_dns(userrec* u)
238 {
239         // place a new user into the queue...
240         log(DEBUG,"Queueing DNS lookup for %s",u->nick);
241         WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
242         Lookup L(u);
243         dnsq.push_back(L);
244         return true;
245 }
246
247 void dns_poll()
248 {
249         // do we have items in the queue?
250         if (dnsq.size())
251         {
252                 // are any ready, or stale?
253                 if (dnsq[0].Done() || (!dnsq[0].GetFD()))
254                 {
255                         log(DEBUG,"****** DNS lookup for fd %d is complete. ******",dnsq[0].GetFD());
256                         dnsq.erase(dnsq.begin());
257                 }
258         }
259 }
260