]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
90365b5dc960d0956fcfda9df2575a9b4a9f1704
[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 <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 "connection.h"
42 #include "users.h"
43 #include "servers.h"
44 #include "ctables.h"
45 #include "globals.h"
46 #include "modules.h"
47 #include "dynamic.h"
48 #include "wildcard.h"
49 #include "message.h"
50 #include "mode.h"
51 #include "commands.h"
52 #include "xline.h"
53 #include "inspstring.h"
54 #include "dnsqueue.h"
55 #include <time.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/poll.h>
59 #include <sys/time.h>
60 #include <netinet/in.h>
61 #include <string.h>
62 #include "dns.h"
63 #include "helperfuncs.h"
64 #include "hashcomp.h"
65
66 extern int MaxWhoResults;
67
68 extern std::vector<Module*, __single_client_alloc> modules;
69 extern std::vector<std::string, __single_client_alloc> module_names;
70 extern std::vector<ircd_module*, __single_client_alloc> factory;
71 extern std::vector<int, __single_client_alloc> fd_reap;
72
73 extern int MODCOUNT;
74
75 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp, __single_client_alloc> user_hash;
76 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp, __single_client_alloc> chan_hash;
77 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, irc::InAddr_HashComp, __single_client_alloc> address_cache;
78 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, irc::StrHashComp, __single_client_alloc> whowas_hash;
79 typedef std::deque<command_t, __single_client_alloc> 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 class Lookup {
94 private:
95         DNS resolver;
96         char u[NICKMAX];
97 public:
98         Lookup()
99         {
100                 strcpy(u,"");
101         }
102
103         void Reset()
104         {
105                 strcpy(u,"");
106         }
107
108         ~Lookup()
109         {
110         }
111
112         bool DoLookup(std::string nick)
113         {
114                 userrec* usr = Find(nick);
115                 if (usr)
116                 {
117                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
118                         resolver.SetNS(std::string(DNSServer));
119                         if (!resolver.ReverseLookup(std::string(usr->host)))
120                                 return false;
121                         strlcpy(u,nick.c_str(),NICKMAX);
122                         return true;
123                 }
124                 return false;
125         }
126
127         bool Done()
128         {
129                 userrec* usr = NULL;
130                 if (resolver.HasResult())
131                 {
132                         if (resolver.GetFD() != 0)
133                         {
134                                 std::string hostname = resolver.GetResult();
135                                 log(DEBUG,"RESULT! %s",hostname.c_str());
136                                 usr = Find(u);
137                                 if (usr)
138                                 {
139                                         if (usr->registered > 3)
140                                         {
141                                                 usr->dns_done = true;
142                                                 return true;
143                                         }
144                                         if ((hostname != "") && (usr->registered != 7))
145                                         {
146                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
147                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
148                                                 WriteServ(usr->fd,"NOTICE Auth :Resolved your hostname: %s",hostname.c_str());
149                                                 usr->dns_done = true;
150                                                 return true;
151                                         }
152                                         usr->dns_done = true;
153                                         return true;
154                                 }
155                         }
156                         else
157                         {
158                                 usr = Find(u);
159                                 if (usr)
160                                         usr->dns_done = true;
161                                 return true;
162                         }
163                 }
164                 return false;
165         }
166
167         int GetFD()
168         {
169                 userrec* usr = Find(u);
170                 if (!usr)
171                         return 0;
172                 if (usr->dns_done)
173                         return 0;
174                 return usr->fd;
175         }
176 };
177
178 Lookup dnsq[255];
179
180 bool lookup_dns(std::string nick)
181 {
182         userrec* u = Find(nick);
183         if (u)
184         {
185                 // place a new user into the queue...
186                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
187                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
188                 Lookup L;
189                 if (L.DoLookup(nick))
190                 {
191                         for (int j = 0; j < 255; j++)
192                         {
193                                 if (!dnsq[j].GetFD())
194                                 {
195                                         dnsq[j] = L;
196                                         return true;
197                                 }
198                         }
199                         // calculate the maximum value, this saves cpu time later
200                         for (int p = 0; p < 255; p++)
201                                 if (dnsq[p].GetFD())
202                                         max_fd_alloc = p;
203                 }
204                 else
205                 {
206                         return false;
207                 }
208         }
209         return false;
210 }
211
212 void dns_poll()
213 {
214         // do we have items in the queue?
215         for (int j = 0; j <= max_fd_alloc; j++)
216         {
217                 // are any ready, or stale?
218                 if (dnsq[j].GetFD())
219                 {
220                         if (dnsq[j].Done())
221                         {
222                                 dnsq[j].Reset();
223                         }
224                 }
225         }
226         // looks like someones freed an item, recalculate end of list.
227         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
228                 for (int p = 0; p < 255; p++)
229                         if (dnsq[p].GetFD())
230                                 max_fd_alloc = p;
231
232 }