]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Fixed bug #85
[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_config.h"
22 #include "inspircd.h"
23 #include "inspircd_io.h"
24 #include "inspircd_util.h"
25 #include <unistd.h>
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
29 #include <cstdio>
30 #include <time.h>
31 #include <string>
32 #ifdef GCC3
33 #include <ext/hash_map>
34 #else
35 #include <hash_map>
36 #endif
37 #include <map>
38 #include <sstream>
39 #include <vector>
40 #include <deque>
41 #include "users.h"
42 #include "ctables.h"
43 #include "globals.h"
44 #include "modules.h"
45 #include "dynamic.h"
46 #include "wildcard.h"
47 #include "message.h"
48 #include "mode.h"
49 #include "commands.h"
50 #include "xline.h"
51 #include "inspstring.h"
52 #include "dnsqueue.h"
53 #include <time.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/poll.h>
57 #include <sys/time.h>
58 #include <netinet/in.h>
59 #include <string.h>
60 #include "dns.h"
61 #include "helperfuncs.h"
62 #include "hashcomp.h"
63
64 extern int MaxWhoResults;
65
66 extern std::vector<Module*> modules;
67 extern std::vector<std::string> module_names;
68 extern std::vector<ircd_module*> factory;
69
70 extern int MODCOUNT;
71
72 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
73 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
74 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp> address_cache;
75 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp> whowas_hash;
76 typedef std::deque<command_t> command_table;
77
78 extern user_hash clientlist;
79 extern chan_hash chanlist;
80 extern whowas_hash whowas;
81 extern command_table cmdlist;
82
83 extern ClassVector Classes;
84
85 extern char DNSServer[MAXBUF];
86 long max_fd_alloc = 0;
87
88 extern time_t TIME;
89
90 //enum LookupState { reverse, forward };
91
92 class Lookup {
93 private:
94         DNS resolver1;
95         DNS resolver2;
96         char u[NICKMAX];
97         std::string hostname;
98 public:
99         Lookup()
100         {
101                 strcpy(u,"");
102         }
103
104         void Reset()
105         {
106                 strcpy(u,"");
107                 log(DEBUG,"Reset class Lookup");
108         }
109
110         ~Lookup()
111         {
112         }
113
114         bool DoLookup(std::string nick)
115         {
116                 hostname = "";
117                 userrec* usr = Find(nick);
118                 if (usr)
119                 {
120                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
121                         resolver1.SetNS(std::string(DNSServer));
122                         if (!resolver1.ReverseLookup(std::string(usr->host)))
123                         {
124                                 log(DEBUG,"ReverseLookup didnt return true, we're outta here");
125                                 return false;
126                         }
127                         strlcpy(u,nick.c_str(),NICKMAX);
128                         return true;
129                 }
130                 log(DEBUG,"We couldnt find that user");
131                 return false;
132         }
133
134         bool Done()
135         {
136                 if (hostname != "")
137                 {
138                         log(DEBUG,"Doing forward lookup here with host %s",hostname.c_str());
139                         // doing forward lookup
140                         userrec* usr = NULL;
141                         if (resolver2.HasResult())
142                         {
143                                 log(DEBUG,"resolver2 has result");
144                                 if (resolver2.GetFD() != 0)
145                                 {
146                                         std::string ip = resolver2.GetResultIP();
147                                         log(DEBUG,"FORWARD RESULT! %s",ip.c_str());
148
149                                         usr = Find(u);
150                                         if (usr)
151                                         {
152                                                 if (usr->registered > 3)
153                                                 {
154                                                         log(DEBUG,"Point 1: Returning true as usr->dns_done is true");
155                                                         usr->dns_done = true;
156                                                         return true;
157                                                 }
158                                                 if ((hostname != "") && (usr->registered != 7))
159                                                 {
160                                                         if (std::string(usr->ip) == ip)
161                                                         {
162                                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
163                                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
164                                                                 log(DEBUG,"Forward and reverse match, assigning hostname");
165                                                         }
166                                                         else
167                                                         {
168                                                                 log(DEBUG,"AWOOGA! Forward lookup doesn't match reverse: R='%s',F='%s',IP='%s'",hostname.c_str(),ip.c_str(),usr->ip);
169                                                         }
170                                                         usr->dns_done = true;
171                                                         return true;
172                                                 }
173                                         }
174                                 }
175                                 else
176                                 {
177                                         usr = Find(u);
178                                         if (usr)
179                                         {
180                                                 log(DEBUG,"Point 2: Returning true");
181                                                 usr->dns_done = true;
182                                         }
183                                         return true;
184                                 }
185                         }
186                         log(DEBUG,"Returning false in forward");
187                         return false;
188                 }
189                 else
190                 {
191                         // doing reverse lookup
192                         userrec* usr = NULL;
193                         if (resolver1.HasResult())
194                         {
195                                 usr = Find(u);
196                                 if ((usr) && (usr->dns_done))
197                                         return true;
198                                 if (resolver1.GetFD() != 0)
199                                 {
200                                         hostname = resolver1.GetResult();
201                                         if (usr)
202                                         {
203                                                 if ((usr->registered > 3) || (hostname == ""))
204                                                 {
205                                                         log(DEBUG,"Hostname is blank and user->registered > 3, returning true and setting done");
206                                                         usr->dns_done = true;
207                                                         return true;
208                                                 }
209                                         }
210                                         if (hostname != "")
211                                         {
212                                                 log(DEBUG,"Starting forwardlookup now for host '%s'...",hostname.c_str());
213                                                 resolver2.ForwardLookup(hostname);
214                                         }
215                                 }
216                         }
217                 }
218                 log(DEBUG,"Returning false");
219                 return false;
220         }
221
222         int GetFD()
223         {
224                 userrec* usr = Find(u);
225                 if (!usr)
226                         return 0;
227                 if (usr->dns_done)
228                         return 0;
229                 return usr->fd;
230         }
231 };
232
233 Lookup dnsq[255];
234
235 bool lookup_dns(std::string nick)
236 {
237         userrec* u = Find(nick);
238         if (u)
239         {
240                 // place a new user into the queue...
241                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
242                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
243                 Lookup L;
244                 if (L.DoLookup(nick))
245                 {
246                         for (int j = 0; j < 255; j++)
247                         {
248                                 if (!dnsq[j].GetFD())
249                                 {
250                                         dnsq[j] = L;
251                                         return true;
252                                 }
253                         }
254                         // calculate the maximum value, this saves cpu time later
255                         for (int p = 0; p < 255; p++)
256                                 if (dnsq[p].GetFD())
257                                         max_fd_alloc = p;
258                 }
259                 else
260                 {
261                         return false;
262                 }
263         }
264         return false;
265 }
266
267 void dns_poll()
268 {
269         // do we have items in the queue?
270         for (int j = 0; j <= max_fd_alloc; j++)
271         {
272                 // are any ready, or stale?
273                 if (dnsq[j].GetFD())
274                 {
275                         if (dnsq[j].Done())
276                         {
277                                 dnsq[j].Reset();
278                         }
279                 }
280         }
281         // looks like someones freed an item, recalculate end of list.
282         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
283                 for (int p = 0; p < 255; p++)
284                         if (dnsq[p].GetFD())
285                                 max_fd_alloc = p;
286
287 }