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