]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Fixed to do forwards properly with uninitialized data
[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                 log(DEBUG,"Create class Lookup");
103         }
104
105         void Reset()
106         {
107                 strcpy(u,"");
108                 log(DEBUG,"Reset class Lookup");
109         }
110
111         ~Lookup()
112         {
113                 log(DEBUG,"Delete class Lookup");
114         }
115
116         bool DoLookup(std::string nick)
117         {
118                 hostname = "";
119                 userrec* usr = Find(nick);
120                 if (usr)
121                 {
122                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
123                         resolver1.SetNS(std::string(DNSServer));
124                         if (!resolver1.ReverseLookup(std::string(usr->host)))
125                         {
126                                 log(DEBUG,"ReverseLookup didnt return true, we're outta here");
127                                 return false;
128                         }
129                         strlcpy(u,nick.c_str(),NICKMAX);
130                         return true;
131                 }
132                 log(DEBUG,"We couldnt find that user");
133                 return false;
134         }
135
136         bool Done()
137         {
138                 if (hostname != "")
139                 {
140                         log(DEBUG,"Doing forward lookup here with host %s",hostname.c_str());
141                         // doing forward lookup
142                         userrec* usr = NULL;
143                         if (resolver2.HasResult())
144                         {
145                                 log(DEBUG,"resolver2 has result");
146                                 if (resolver2.GetFD() != 0)
147                                 {
148                                         std::string ip = resolver2.GetResultIP();
149                                         log(DEBUG,"FORWARD RESULT! %s",ip.c_str());
150
151                                         usr = Find(u);
152                                         if (usr)
153                                         {
154                                                 if (usr->registered > 3)
155                                                 {
156                                                         log(DEBUG,"Point 1: Returning true as usr->dns_done is true");
157                                                         usr->dns_done = true;
158                                                         return true;
159                                                 }
160                                                 if ((hostname != "") && (usr->registered != 7))
161                                                 {
162                                                         if (std::string(usr->ip) == ip)
163                                                         {
164                                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
165                                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
166                                                                 log(DEBUG,"Forward and reverse match, assigning hostname");
167                                                         }
168                                                         else
169                                                         {
170                                                                 log(DEBUG,"AWOOGA! Forward lookup doesn't match reverse: R='%s',F='%s',IP='%s'",hostname.c_str(),ip.c_str(),usr->ip);
171                                                         }
172                                                         usr->dns_done = true;
173                                                         return true;
174                                                 }
175                                         }
176                                 }
177                                 else
178                                 {
179                                         usr = Find(u);
180                                         if (usr)
181                                         {
182                                                 log(DEBUG,"Point 2: Returning true");
183                                                 usr->dns_done = true;
184                                         }
185                                         return true;
186                                 }
187                         }
188                         log(DEBUG,"Returning false in forward");
189                         return false;
190                 }
191                 else
192                 {
193                         // doing reverse lookup
194                         userrec* usr = NULL;
195                         if (resolver1.HasResult())
196                         {
197                                 usr = Find(u);
198                                 if ((usr) && (usr->dns_done))
199                                         return true;
200                                 if (resolver1.GetFD() != 0)
201                                 {
202                                         hostname = resolver1.GetResult();
203                                         if (usr)
204                                         {
205                                                 if ((usr->registered > 3) || (hostname == ""))
206                                                 {
207                                                         log(DEBUG,"Hostname is blank and user->registered > 3, returning true and setting done");
208                                                         usr->dns_done = true;
209                                                         return true;
210                                                 }
211                                         }
212                                         if (hostname != "")
213                                         {
214                                                 log(DEBUG,"Starting forwardlookup now for host '%s'...",hostname.c_str());
215                                                 resolver2.ForwardLookup(hostname);
216                                         }
217                                 }
218                         }
219                 }
220                 log(DEBUG,"Returning false");
221                 return false;
222         }
223
224         int GetFD()
225         {
226                 userrec* usr = Find(u);
227                 if (!usr)
228                         return 0;
229                 if (usr->dns_done)
230                         return 0;
231                 return usr->fd;
232         }
233 };
234
235 Lookup dnsq[255];
236
237 bool lookup_dns(std::string nick)
238 {
239         userrec* u = Find(nick);
240         if (u)
241         {
242                 // place a new user into the queue...
243                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
244                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
245                 Lookup L;
246                 if (L.DoLookup(nick))
247                 {
248                         for (int j = 0; j < 255; j++)
249                         {
250                                 if (!dnsq[j].GetFD())
251                                 {
252                                         dnsq[j] = L;
253                                         return true;
254                                 }
255                         }
256                         // calculate the maximum value, this saves cpu time later
257                         for (int p = 0; p < 255; p++)
258                                 if (dnsq[p].GetFD())
259                                         max_fd_alloc = p;
260                 }
261                 else
262                 {
263                         return false;
264                 }
265         }
266         return false;
267 }
268
269 void dns_poll()
270 {
271         // do we have items in the queue?
272         for (int j = 0; j <= max_fd_alloc; j++)
273         {
274                 // are any ready, or stale?
275                 if (dnsq[j].GetFD())
276                 {
277                         if (dnsq[j].Done())
278                         {
279                                 dnsq[j].Reset();
280                         }
281                 }
282         }
283         // looks like someones freed an item, recalculate end of list.
284         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
285                 for (int p = 0; p < 255; p++)
286                         if (dnsq[p].GetFD())
287                                 max_fd_alloc = p;
288
289 }