]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
f00c9e25de12bcf82be77f3743c6a375186e388e
[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                 strlower(a);
131                 strlower(b);
132                 return (strcasecmp(a,b) == 0);
133         }
134
135 };
136
137 struct InAddr_HashComp
138 {
139
140         bool operator()(const in_addr &s1, const in_addr &s2) const
141         {
142                 size_t q;
143                 size_t p;
144                 
145                 memcpy(&q,&s1,sizeof(size_t));
146                 memcpy(&p,&s2,sizeof(size_t));
147                 
148                 return (q == p);
149         }
150
151 };
152
153
154 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
155 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
156 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
157 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, StrHashComp> whowas_hash;
158 typedef std::deque<command_t> command_table;
159
160 extern user_hash clientlist;
161 extern chan_hash chanlist;
162 extern whowas_hash whowas;
163 extern command_table cmdlist;
164
165 extern ClassVector Classes;
166
167 extern char DNSServer[MAXBUF];
168 long max_fd_alloc = 0;
169
170 extern time_t TIME;
171
172 class Lookup {
173 private:
174         DNS resolver;
175         char u[NICKMAX];
176 public:
177         Lookup()
178         {
179                 strcpy(u,"");
180         }
181
182         void Reset()
183         {
184                 strcpy(u,"");
185         }
186
187         ~Lookup()
188         {
189         }
190
191         bool DoLookup(std::string nick)
192         {
193                 userrec* usr = Find(nick);
194                 if (usr)
195                 {
196                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
197                         resolver.SetNS(std::string(DNSServer));
198                         if (!resolver.ReverseLookup(std::string(usr->host)))
199                                 return false;
200                         strlcpy(u,nick.c_str(),NICKMAX);
201                         return true;
202                 }
203                 return false;
204         }
205
206         bool Done()
207         {
208                 userrec* usr = NULL;
209                 if (resolver.HasResult())
210                 {
211                         if (resolver.GetFD() != 0)
212                         {
213                                 std::string hostname = resolver.GetResult();
214                                 log(DEBUG,"RESULT! %s",hostname.c_str());
215                                 usr = Find(u);
216                                 if (usr)
217                                 {
218                                         if (usr->registered > 3)
219                                         {
220                                                 usr->dns_done = true;
221                                                 return true;
222                                         }
223                                         if ((hostname != "") && (usr->registered != 7))
224                                         {
225                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
226                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
227                                                 WriteServ(usr->fd,"NOTICE Auth :Resolved your hostname: %s",hostname.c_str());
228                                                 usr->dns_done = true;
229                                                 return true;
230                                         }
231                                         usr->dns_done = true;
232                                         return true;
233                                 }
234                         }
235                         else
236                         {
237                                 usr = Find(u);
238                                 if (usr)
239                                         usr->dns_done = true;
240                                 return true;
241                         }
242                 }
243                 return false;
244         }
245
246         int GetFD()
247         {
248                 userrec* usr = Find(u);
249                 if (!usr)
250                         return 0;
251                 if (usr->dns_done)
252                         return 0;
253                 return usr->fd;
254         }
255 };
256
257 Lookup dnsq[MAXBUF];
258
259 bool lookup_dns(std::string nick)
260 {
261         userrec* u = Find(nick);
262         if (u)
263         {
264                 // place a new user into the queue...
265                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
266                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
267                 Lookup L;
268                 if (L.DoLookup(nick))
269                 {
270                         for (int j = 0; j < MAXBUF; j++)
271                         {
272                                 if (!dnsq[j].GetFD())
273                                 {
274                                         dnsq[j] = L;
275                                         return true;
276                                 }
277                         }
278                         // calculate the maximum value, this saves cpu time later
279                         for (int p = 0; p < MAXBUF; p++)
280                                 if (dnsq[p].GetFD())
281                                         max_fd_alloc = p;
282                 }
283                 else
284                 {
285                         return false;
286                 }
287         }
288         return false;
289 }
290
291 void dns_poll()
292 {
293         // do we have items in the queue?
294         for (int j = 0; j <= max_fd_alloc; j++)
295         {
296                 // are any ready, or stale?
297                 if (dnsq[j].GetFD())
298                 {
299                         if (dnsq[j].Done())
300                         {
301                                 dnsq[j].Reset();
302                         }
303                 }
304         }
305         // looks like someones freed an item, recalculate end of list.
306         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
307                 for (int p = 0; p < MAXBUF; p++)
308                         if (dnsq[p].GetFD())
309                                 max_fd_alloc = p;
310
311 }