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 * ---------------------------------------------------
17 #include "inspircd_config.h"
19 #include "configreader.h"
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/utsname.h>
27 #include "inspstring.h"
30 #include "helperfuncs.h"
32 #include "socketengine.h"
35 extern ServerConfig* Config;
36 extern InspIRCd* ServerInstance;
40 Lookup* dnslist[MAX_DESCRIPTORS];
41 Lookup* user_fd_to_dns[MAX_DESCRIPTORS];
43 //enum LookupState { reverse, forward };
69 bool DoLookup(std::string nick)
72 userrec* usr = Find(nick);
75 resolver1.SetNS(std::string(Config->DNSServer));
76 if (!resolver1.ReverseLookup(std::string(usr->host), true))
80 strlcpy(u,nick.c_str(),NICKMAX-1);
82 /* ASSOCIATE WITH DNS LOOKUP LIST */
83 if (resolver1.GetFD() != -1)
85 dnslist[resolver1.GetFD()] = this;
86 user_fd_to_dns[usr->fd] = this;
93 bool Done(int fdcheck)
97 // doing forward lookup
99 if (resolver2.HasResult(fdcheck))
101 if (resolver2.GetFD() != -1)
103 dnslist[resolver2.GetFD()] = NULL;
104 std::string ip = resolver2.GetResultIP();
108 if (usr->registered > 3)
110 usr->dns_done = true;
113 if ((hostname != "") && (usr->registered != 7))
115 if ((std::string((char*)inet_ntoa(usr->ip4)) == ip) && (hostname.length() < 65))
117 strlcpy(usr->host,hostname.c_str(),64);
118 strlcpy(usr->dhost,hostname.c_str(),64);
119 /*address_cache::iterator address = addrcache.find(usr->ip4);
120 if (address == addrcache.end())
122 log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
123 addrcache[usr->ip4] = new std::string(hostname);
125 WriteServ(usr->fd,"NOTICE Auth :*** Found your hostname");
127 usr->dns_done = true;
137 usr->dns_done = true;
146 // doing reverse lookup
148 if (resolver1.HasResult(fdcheck))
151 if ((usr) && (usr->dns_done))
153 if (resolver1.GetFD() != -1)
155 dnslist[resolver1.GetFD()] = NULL;
156 user_fd_to_dns[usr->fd] = NULL;
160 if (resolver1.GetFD() != -1)
162 dnslist[resolver1.GetFD()] = NULL;
163 hostname = resolver1.GetResult();
166 user_fd_to_dns[usr->fd] = NULL;
167 if ((usr->registered > 3) || (hostname == ""))
169 WriteServ(usr->fd,"NOTICE Auth :*** Could not resolve your hostname -- Using your IP address instead");
170 usr->dns_done = true;
176 resolver2.ForwardLookup(hostname, true);
177 if (resolver2.GetFD() != -1)
179 dnslist[resolver2.GetFD()] = this;
180 user_fd_to_dns[usr->fd] = this;
191 userrec* usr = Find(u);
200 bool lookup_dns(const std::string &nick)
202 /* First attempt to find the nickname */
203 userrec* u = Find(nick);
206 /* Check the cache */
207 /*address_cache::iterator address = addrcache.find(u->ip4);
208 if (address != addrcache.end())
210 WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
211 log(DEBUG,"Found cached host");
212 strlcpy(u->host,address->second->c_str(),MAXBUF);
213 strlcpy(u->dhost,address->second->c_str(),MAXBUF);
217 /* If the user exists, create a new
218 * lookup object, and associate it
219 * with the user. The lookup object
220 * will maintain the reference table
221 * which we use for quickly finding
222 * dns results. Please note that we
223 * do not associate a lookup with a
224 * userrec* pointer and we use the
225 * nickname instead because, by the
226 * time the DNS lookup has completed,
227 * the nickname could have quit and
228 * if we then try and access the
229 * pointer we get a nice segfault.
231 Lookup* L = new Lookup();
238 void ZapThisDns(int fd)
240 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
243 Lookup *x = user_fd_to_dns[fd];
247 if (x->resolver1.GetFD() > 0)
249 log(DEBUG,"Whacked resolver1");
250 dns_close(x->resolver1.GetFD());
253 if (x->resolver2.GetFD() > 0)
255 log(DEBUG,"Whacked resolver2");
256 dns_close(x->resolver2.GetFD());
261 void dns_poll(int fdcheck)
263 /* Check the given file descriptor is in valid range */
264 if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
267 /* Try and find the file descriptor in our list of
270 Lookup *x = dnslist[fdcheck];
273 /* If it exists check if its a valid fd still */
274 if (x->GetFD() != -1)
276 /* Check if its done, if it is delete it */
277 if (x->Done(fdcheck))
279 /* We don't need to delete the file descriptor
280 * from the socket engine, as dns.cpp tracks it
281 * for us if we are in single-threaded country.
288 /* its fd is dodgy, the dns code probably
289 * bashed it due to error. Free the class.
293 /* If we got down here, the dns lookup was valid, BUT,
294 * its still in progress. Be patient, and wait for
295 * more socketengine events to complete the lookups.
299 /* This FD doesnt belong here, lets be rid of it,
300 * just to be safe so we dont get any more events
303 if (ServerInstance && ServerInstance->SE)
304 ServerInstance->SE->DelFd(fdcheck);