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>
47 #include <netinet/in.h>
50 #include "helperfuncs.h"
52 #include "socketengine.h"
54 extern SocketEngine* SE;
55 extern ServerConfig* Config;
59 Lookup* dnslist[65535];
61 //enum LookupState { reverse, forward };
84 bool DoLookup(std::string nick)
87 userrec* usr = Find(nick);
90 resolver1.SetNS(std::string(DNSServer));
91 if (!resolver1.ReverseLookup(std::string(usr->host)))
95 strlcpy(u,nick.c_str(),NICKMAX);
97 /* ASSOCIATE WITH DNS LOOKUP LIST */
98 if (resolver1.GetFD() != -1)
100 dnslist[resolver1.GetFD()] = this;
107 bool Done(int fdcheck)
111 // doing forward lookup
113 if (resolver2.HasResult(fdcheck))
115 if (resolver2.GetFD() != -1)
117 dnslist[resolver2.GetFD()] = NULL;
118 std::string ip = resolver2.GetResultIP();
122 if (usr->registered > 3)
124 usr->dns_done = true;
127 if ((hostname != "") && (usr->registered != 7))
129 if (std::string(usr->ip) == ip)
131 strlcpy(usr->host,hostname.c_str(),MAXBUF);
132 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
134 usr->dns_done = true;
144 usr->dns_done = true;
153 // doing reverse lookup
155 if (resolver1.HasResult(fdcheck))
158 if ((usr) && (usr->dns_done))
160 if (resolver1.GetFD() != -1)
161 dnslist[resolver1.GetFD()] = NULL;
164 if (resolver1.GetFD() != -1)
166 dnslist[resolver1.GetFD()] = NULL;
167 hostname = resolver1.GetResult();
170 if ((usr->registered > 3) || (hostname == ""))
172 usr->dns_done = true;
178 resolver2.ForwardLookup(hostname);
179 if (resolver2.GetFD() != -1)
180 dnslist[resolver2.GetFD()] = this;
190 userrec* usr = Find(u);
199 bool lookup_dns(std::string nick)
201 /* First attempt to find the nickname */
202 userrec* u = Find(nick);
205 /* If the user exists, create a new
206 * lookup object, and associate it
207 * with the user. The lookup object
208 * will maintain the reference table
209 * which we use for quickly finding
210 * dns results. Please note that we
211 * do not associate a lookup with a
212 * userrec* pointer and we use the
213 * nickname instead because, by the
214 * time the DNS lookup has completed,
215 * the nickname could have quit and
216 * if we then try and access the
217 * pointer we get a nice segfault.
219 Lookup* L = new Lookup();
226 void dns_poll(int fdcheck)
228 /* Check the given file descriptor is in valid range */
229 if ((fdcheck < 0) || (fdcheck > 65535))
232 /* Try and find the file descriptor in our list of
235 Lookup *x = dnslist[fdcheck];
238 /* If it exists check if its a valid fd still */
239 if (x->GetFD() != -1)
241 /* Check if its done, if it is delete it */
242 if (x->Done(fdcheck))
244 /* We don't need to delete the file descriptor
245 * from the socket engine, as dns.cpp tracks it
246 * for us if we are in single-threaded country.
253 /* its fd is dodgy, the dns code probably
254 * bashed it due to error. Free the class.
258 /* If we got down here, the dns lookup was valid, BUT,
259 * its still in progress. Be patient, and wait for
260 * more socketengine events to complete the lookups.
264 /* This FD doesnt belong here, lets be rid of it,
265 * just to be safe so we dont get any more events