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