]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/dnsqueue.cpp
2d123a4198b7f5034a353f21273c3c4becf17bbf
[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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include "inspircd_util.h"
23 #include <unistd.h>
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
27 #include <cstdio>
28 #include <time.h>
29 #include <string>
30 #ifdef GCC3
31 #include <ext/hash_map>
32 #else
33 #include <hash_map>
34 #endif
35 #include <map>
36 #include <sstream>
37 #include <vector>
38 #include <deque>
39 #include "users.h"
40 #include "globals.h"
41 #include "inspstring.h"
42 #include "dnsqueue.h"
43 #include <time.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <sys/poll.h>
47 #include <sys/time.h>
48 #include <netinet/in.h>
49 #include <string.h>
50 #include "dns.h"
51 #include "helperfuncs.h"
52 #include "hashcomp.h"
53 #include "socketengine.h"
54
55 extern SocketEngine* SE;
56 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
57 extern user_hash clientlist;
58 extern char DNSServer[MAXBUF];
59
60 class Lookup;
61
62 Lookup* dnslist[65535];
63
64 //enum LookupState { reverse, forward };
65
66 class Lookup {
67 private:
68         DNS resolver1;
69         DNS resolver2;
70         char u[NICKMAX];
71         std::string hostname;
72 public:
73         Lookup()
74         {
75                 strcpy(u,"");
76         }
77
78         void Reset()
79         {
80                 strcpy(u,"");
81         }
82
83         ~Lookup()
84         {
85         }
86
87         bool DoLookup(std::string nick)
88         {
89                 hostname = "";
90                 userrec* usr = Find(nick);
91                 if (usr)
92                 {
93                         resolver1.SetNS(std::string(DNSServer));
94                         if (!resolver1.ReverseLookup(std::string(usr->host)))
95                         {
96                                 return false;
97                         }
98                         strlcpy(u,nick.c_str(),NICKMAX);
99
100                         /* ASSOCIATE WITH DNS LOOKUP LIST */
101                         dnslist[resolver1.GetFD()] = this;
102                         
103                         return true;
104                 }
105                 return false;
106         }
107
108         bool Done(int fdcheck)
109         {
110                 if (hostname != "")
111                 {
112                         // doing forward lookup
113                         userrec* usr = NULL;
114                         if (resolver2.HasResult(fdcheck))
115                         {
116                                 if (resolver2.GetFD() != -1)
117                                 {
118                                         dnslist[resolver2.GetFD()] = NULL;
119                                         std::string ip = resolver2.GetResultIP();
120                                         usr = Find(u);
121                                         if (usr)
122                                         {
123                                                 if (usr->registered > 3)
124                                                 {
125                                                         usr->dns_done = true;
126                                                         return true;
127                                                 }
128                                                 if ((hostname != "") && (usr->registered != 7))
129                                                 {
130                                                         if (std::string(usr->ip) == ip)
131                                                         {
132                                                                 strlcpy(usr->host,hostname.c_str(),MAXBUF);
133                                                                 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
134                                                         }
135                                                         usr->dns_done = true;
136                                                         return true;
137                                                 }
138                                         }
139                                 }
140                                 else
141                                 {
142                                         usr = Find(u);
143                                         if (usr)
144                                         {
145                                                 usr->dns_done = true;
146                                         }
147                                         return true;
148                                 }
149                         }
150                         return false;
151                 }
152                 else
153                 {
154                         // doing reverse lookup
155                         userrec* usr = NULL;
156                         if (resolver1.HasResult(fdcheck))
157                         {
158                                 usr = Find(u);
159                                 if ((usr) && (usr->dns_done))
160                                         return true;
161                                 if (resolver1.GetFD() != -1)
162                                 {
163                                         dnslist[resolver1.GetFD()] = NULL;
164                                         hostname = resolver1.GetResult();
165                                         if (usr)
166                                         {
167                                                 if ((usr->registered > 3) || (hostname == ""))
168                                                 {
169                                                         usr->dns_done = true;
170                                                         return true;
171                                                 }
172                                         }
173                                         if (hostname != "")
174                                         {
175                                                 resolver2.ForwardLookup(hostname);
176                                                 if (resolver2.GetFD())
177                                                         dnslist[resolver2.GetFD()] = this;
178                                         }
179                                 }
180                         }
181                 }
182                 return false;
183         }
184
185         int GetFD()
186         {
187                 userrec* usr = Find(u);
188                 if (!usr)
189                         return 0;
190                 if (usr->dns_done)
191                         return 0;
192                 return usr->fd;
193         }
194 };
195
196 bool lookup_dns(std::string nick)
197 {
198         /* First attempt to find the nickname */
199         userrec* u = Find(nick);
200         if (u)
201         {
202                 /* If the user exists, create a new
203                  * lookup object, and associate it
204                  * with the user. The lookup object
205                  * will maintain the reference table
206                  * which we use for quickly finding
207                  * dns results. Please note that we
208                  * do not associate a lookup with a
209                  * userrec* pointer and we use the
210                  * nickname instead because, by the
211                  * time the DNS lookup has completed,
212                  * the nickname could have quit and
213                  * if we then try and access the
214                  * pointer we get a nice segfault.
215                  */
216                 Lookup* L = new Lookup();
217                 L->DoLookup(nick);
218                 return true;
219         }
220         return false;
221 }
222
223 void dns_poll(int fdcheck)
224 {
225         /* Check the given file descriptor is in valid range */
226         if ((fdcheck < 0) || (fdcheck > 65535))
227                 return;
228
229         /* Try and find the file descriptor in our list of
230          * active DNS lookups
231          */
232         Lookup *x = dnslist[fdcheck];
233         if (x)
234         {
235                 /* If it exists check if its a valid fd still */
236                 if (x->GetFD() != -1)
237                 {
238                         /* Check if its done, if it is delete it */
239                         if (x->Done(fdcheck))
240                         {
241                                 /* We don't need to delete the file descriptor
242                                  * from the socket engine, as dns.cpp tracks it
243                                  * for us if we are in single-threaded country.
244                                  */
245                                 delete x;
246                         }
247                 }
248                 else
249                 {
250                         /* its fd is dodgy, the dns code probably
251                          * bashed it due to error. Free the class.
252                          */
253                         delete x;
254                 }
255                 /* If we got down here, the dns lookup was valid, BUT,
256                  * its still in progress. Be patient, and wait for
257                  * more socketengine events to complete the lookups.
258                  */
259                 return;
260         }
261         log(DEBUG,"DNS: Received an event for an invalid descriptor!");
262 }
263