]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
Removed references to deprecated files servers.* and connection.*
[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 class Lookup {
91 private:
92         DNS resolver;
93         char u[NICKMAX];
94 public:
95         Lookup()
96         {
97                 strcpy(u,"");
98         }
99
100         void Reset()
101         {
102                 strcpy(u,"");
103         }
104
105         ~Lookup()
106         {
107         }
108
109         bool DoLookup(std::string nick)
110         {
111                 userrec* usr = Find(nick);
112                 if (usr)
113                 {
114                         log(DEBUG,"New Lookup class for %s with DNSServer set to '%s'",nick.c_str(),DNSServer);
115                         resolver.SetNS(std::string(DNSServer));
116                         if (!resolver.ReverseLookup(std::string(usr->host)))
117                                 return false;
118                         strlcpy(u,nick.c_str(),NICKMAX);
119                         return true;
120                 }
121                 return false;
122         }
123
124         bool Done()
125         {
126                 userrec* usr = NULL;
127                 if (resolver.HasResult())
128                 {
129                         if (resolver.GetFD() != 0)
130                         {
131                                 std::string hostname = resolver.GetResult();
132                                 log(DEBUG,"RESULT! %s",hostname.c_str());
133                                 usr = Find(u);
134                                 if (usr)
135                                 {
136                                         if (usr->registered > 3)
137                                         {
138                                                 usr->dns_done = true;
139                                                 return true;
140                                         }
141                                         if ((hostname != "") && (usr->registered != 7))
142                                         {
143                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
144                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
145                                                 WriteServ(usr->fd,"NOTICE Auth :Resolved your hostname: %s",hostname.c_str());
146                                                 usr->dns_done = true;
147                                                 return true;
148                                         }
149                                         usr->dns_done = true;
150                                         return true;
151                                 }
152                         }
153                         else
154                         {
155                                 usr = Find(u);
156                                 if (usr)
157                                         usr->dns_done = true;
158                                 return true;
159                         }
160                 }
161                 return false;
162         }
163
164         int GetFD()
165         {
166                 userrec* usr = Find(u);
167                 if (!usr)
168                         return 0;
169                 if (usr->dns_done)
170                         return 0;
171                 return usr->fd;
172         }
173 };
174
175 Lookup dnsq[255];
176
177 bool lookup_dns(std::string nick)
178 {
179         userrec* u = Find(nick);
180         if (u)
181         {
182                 // place a new user into the queue...
183                 log(DEBUG,"Queueing DNS lookup for %s",u->nick);
184                 WriteServ(u->fd,"NOTICE Auth :Looking up your hostname...");
185                 Lookup L;
186                 if (L.DoLookup(nick))
187                 {
188                         for (int j = 0; j < 255; j++)
189                         {
190                                 if (!dnsq[j].GetFD())
191                                 {
192                                         dnsq[j] = L;
193                                         return true;
194                                 }
195                         }
196                         // calculate the maximum value, this saves cpu time later
197                         for (int p = 0; p < 255; p++)
198                                 if (dnsq[p].GetFD())
199                                         max_fd_alloc = p;
200                 }
201                 else
202                 {
203                         return false;
204                 }
205         }
206         return false;
207 }
208
209 void dns_poll()
210 {
211         // do we have items in the queue?
212         for (int j = 0; j <= max_fd_alloc; j++)
213         {
214                 // are any ready, or stale?
215                 if (dnsq[j].GetFD())
216                 {
217                         if (dnsq[j].Done())
218                         {
219                                 dnsq[j].Reset();
220                         }
221                 }
222         }
223         // looks like someones freed an item, recalculate end of list.
224         if ((!dnsq[max_fd_alloc].GetFD()) && (max_fd_alloc != 0))
225                 for (int p = 0; p < 255; p++)
226                         if (dnsq[p].GetFD())
227                                 max_fd_alloc = p;
228
229 }