1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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"
23 #include <sys/errno.h>
24 #include <sys/ioctl.h>
25 #include <sys/utsname.h>
30 #include <ext/hash_map>
40 #include "inspstring.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
49 #include "helperfuncs.h"
51 #include "socketengine.h"
53 extern ServerConfig* Config;
54 extern InspIRCd* ServerInstance;
58 Lookup* dnslist[MAX_DESCRIPTORS];
60 //enum LookupState { reverse, forward };
83 bool DoLookup(std::string nick)
86 userrec* usr = Find(nick);
89 resolver1.SetNS(std::string(Config->DNSServer));
90 if (!resolver1.ReverseLookup(std::string(usr->host)))
94 strlcpy(u,nick.c_str(),NICKMAX);
96 /* ASSOCIATE WITH DNS LOOKUP LIST */
97 if (resolver1.GetFD() != -1)
99 dnslist[resolver1.GetFD()] = this;
106 bool Done(int fdcheck)
110 // doing forward lookup
112 if (resolver2.HasResult(fdcheck))
114 if (resolver2.GetFD() != -1)
116 dnslist[resolver2.GetFD()] = NULL;
117 std::string ip = resolver2.GetResultIP();
121 if (usr->registered > 3)
123 usr->dns_done = true;
126 if ((hostname != "") && (usr->registered != 7))
128 if (std::string(usr->ip) == ip)
130 strlcpy(usr->host,hostname.c_str(),MAXBUF);
131 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
133 usr->dns_done = true;
143 usr->dns_done = true;
152 // doing reverse lookup
154 if (resolver1.HasResult(fdcheck))
157 if ((usr) && (usr->dns_done))
159 if (resolver1.GetFD() != -1)
160 dnslist[resolver1.GetFD()] = NULL;
163 if (resolver1.GetFD() != -1)
165 dnslist[resolver1.GetFD()] = NULL;
166 hostname = resolver1.GetResult();
169 if ((usr->registered > 3) || (hostname == ""))
171 usr->dns_done = true;
177 resolver2.ForwardLookup(hostname);
178 if (resolver2.GetFD() != -1)
179 dnslist[resolver2.GetFD()] = this;
189 userrec* usr = Find(u);
198 bool lookup_dns(std::string nick)
200 /* First attempt to find the nickname */
201 userrec* u = Find(nick);
204 /* If the user exists, create a new
205 * lookup object, and associate it
206 * with the user. The lookup object
207 * will maintain the reference table
208 * which we use for quickly finding
209 * dns results. Please note that we
210 * do not associate a lookup with a
211 * userrec* pointer and we use the
212 * nickname instead because, by the
213 * time the DNS lookup has completed,
214 * the nickname could have quit and
215 * if we then try and access the
216 * pointer we get a nice segfault.
218 Lookup* L = new Lookup();
225 void dns_poll(int fdcheck)
227 /* Check the given file descriptor is in valid range */
228 if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
231 /* Try and find the file descriptor in our list of
234 Lookup *x = dnslist[fdcheck];
237 /* If it exists check if its a valid fd still */
238 if (x->GetFD() != -1)
240 /* Check if its done, if it is delete it */
241 if (x->Done(fdcheck))
243 /* We don't need to delete the file descriptor
244 * from the socket engine, as dns.cpp tracks it
245 * for us if we are in single-threaded country.
252 /* its fd is dodgy, the dns code probably
253 * bashed it due to error. Free the class.
257 /* If we got down here, the dns lookup was valid, BUT,
258 * its still in progress. Be patient, and wait for
259 * more socketengine events to complete the lookups.
263 /* This FD doesnt belong here, lets be rid of it,
264 * just to be safe so we dont get any more events
267 if (ServerInstance && ServerInstance->SE)
268 ServerInstance->SE->DelFd(fdcheck);