1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
22 #include "inspircd_util.h"
24 #include <sys/errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/utsname.h>
31 #include <ext/hash_map>
41 #include "inspstring.h"
44 #include <sys/types.h>
45 #include <sys/socket.h>
48 #include <netinet/in.h>
51 #include "helperfuncs.h"
53 #include "socketengine.h"
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];
62 Lookup* dnslist[65535];
64 //enum LookupState { reverse, forward };
87 bool DoLookup(std::string nick)
90 userrec* usr = Find(nick);
93 resolver1.SetNS(std::string(DNSServer));
94 if (!resolver1.ReverseLookup(std::string(usr->host)))
98 strlcpy(u,nick.c_str(),NICKMAX);
100 /* ASSOCIATE WITH DNS LOOKUP LIST */
101 if (resolver1.GetFD() != -1)
103 dnslist[resolver1.GetFD()] = this;
110 bool Done(int fdcheck)
114 // doing forward lookup
116 if (resolver2.HasResult(fdcheck))
118 if (resolver2.GetFD() != -1)
120 dnslist[resolver2.GetFD()] = NULL;
121 std::string ip = resolver2.GetResultIP();
125 if (usr->registered > 3)
127 usr->dns_done = true;
130 if ((hostname != "") && (usr->registered != 7))
132 if (std::string(usr->ip) == ip)
134 strlcpy(usr->host,hostname.c_str(),MAXBUF);
135 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
137 usr->dns_done = true;
147 usr->dns_done = true;
156 // doing reverse lookup
158 if (resolver1.HasResult(fdcheck))
161 if ((usr) && (usr->dns_done))
163 if (resolver1.GetFD() != -1)
164 dnslist[resolver1.GetFD()] = NULL;
167 if (resolver1.GetFD() != -1)
169 dnslist[resolver1.GetFD()] = NULL;
170 hostname = resolver1.GetResult();
173 if ((usr->registered > 3) || (hostname == ""))
175 usr->dns_done = true;
181 resolver2.ForwardLookup(hostname);
182 if (resolver2.GetFD() != -1)
183 dnslist[resolver2.GetFD()] = this;
193 userrec* usr = Find(u);
202 bool lookup_dns(std::string nick)
204 /* First attempt to find the nickname */
205 userrec* u = Find(nick);
208 /* If the user exists, create a new
209 * lookup object, and associate it
210 * with the user. The lookup object
211 * will maintain the reference table
212 * which we use for quickly finding
213 * dns results. Please note that we
214 * do not associate a lookup with a
215 * userrec* pointer and we use the
216 * nickname instead because, by the
217 * time the DNS lookup has completed,
218 * the nickname could have quit and
219 * if we then try and access the
220 * pointer we get a nice segfault.
222 Lookup* L = new Lookup();
229 void dns_poll(int fdcheck)
231 /* Check the given file descriptor is in valid range */
232 if ((fdcheck < 0) || (fdcheck > 65535))
235 /* Try and find the file descriptor in our list of
238 Lookup *x = dnslist[fdcheck];
241 /* If it exists check if its a valid fd still */
242 if (x->GetFD() != -1)
244 /* Check if its done, if it is delete it */
245 if (x->Done(fdcheck))
247 /* We don't need to delete the file descriptor
248 * from the socket engine, as dns.cpp tracks it
249 * for us if we are in single-threaded country.
256 /* its fd is dodgy, the dns code probably
257 * bashed it due to error. Free the class.
261 /* If we got down here, the dns lookup was valid, BUT,
262 * its still in progress. Be patient, and wait for
263 * more socketengine events to complete the lookups.
267 /* This FD doesnt belong here, lets be rid of it,
268 * just to be safe so we dont get any more events