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