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];
42 //enum LookupState { reverse, forward };
67 bool DoLookup(std::string nick)
70 userrec* usr = Find(nick);
73 resolver1.SetNS(std::string(Config->DNSServer));
77 strlcpy(u,nick.c_str(),NICKMAX-1);
80 usr->dns_fd = resolver1.GetFD();
82 /* ASSOCIATE WITH DNS LOOKUP LIST */
83 if (resolver1.GetFD() != -1)
85 dnslist[resolver1.GetFD()] = this;
92 bool Done(int fdcheck)
96 // doing forward lookup
98 if (resolver2.HasResult(fdcheck))
100 if (resolver2.GetFD() != -1)
102 dnslist[resolver2.GetFD()] = NULL;
103 std::string ip = resolver2.GetResultIP();
107 if (usr->registered > 3)
109 usr->dns_done = true;
112 if ((hostname != "") && (usr->registered != 7))
114 if ((std::string((char*)inet_ntoa(usr->ip4)) == ip) && (hostname.length() < 65))
116 strlcpy(usr->host,hostname.c_str(),64);
117 strlcpy(usr->dhost,hostname.c_str(),64);
118 /*address_cache::iterator address = addrcache.find(usr->ip4);
119 if (address == addrcache.end())
121 log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
122 addrcache[usr->ip4] = new std::string(hostname);
124 WriteServ(usr->fd,"NOTICE Auth :*** Found your hostname");
126 usr->dns_done = true;
139 usr->dns_done = true;
151 // doing reverse lookup
153 if (resolver1.HasResult(fdcheck))
156 if ((usr) && (usr->dns_done))
158 if (resolver1.GetFD() != -1)
159 dnslist[resolver1.GetFD()] = NULL;
162 if (resolver1.GetFD() != -1)
164 dnslist[resolver1.GetFD()] = NULL;
165 hostname = resolver1.GetResult();
168 if ((usr->registered > 3) || (hostname == ""))
170 WriteServ(usr->fd,"NOTICE Auth :*** Could not resolve your hostname -- Using your IP address instead");
171 usr->dns_done = true;
175 if ((hostname != "") && (usr))
177 resolver2.ForwardLookup(hostname, true);
178 if (resolver2.GetFD() != -1)
180 dnslist[resolver2.GetFD()] = this;
182 usr->dns_fd = resolver2.GetFD();
194 userrec* usr = Find(u);
203 bool lookup_dns(const std::string &nick)
205 /* First attempt to find the nickname */
206 userrec* u = Find(nick);
209 /* Check the cache */
210 /*address_cache::iterator address = addrcache.find(u->ip4);
211 if (address != addrcache.end())
213 WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
214 log(DEBUG,"Found cached host");
215 strlcpy(u->host,address->second->c_str(),MAXBUF);
216 strlcpy(u->dhost,address->second->c_str(),MAXBUF);
220 /* If the user exists, create a new
221 * lookup object, and associate it
222 * with the user. The lookup object
223 * will maintain the reference table
224 * which we use for quickly finding
225 * dns results. Please note that we
226 * do not associate a lookup with a
227 * userrec* pointer and we use the
228 * nickname instead because, by the
229 * time the DNS lookup has completed,
230 * the nickname could have quit and
231 * if we then try and access the
232 * pointer we get a nice segfault.
234 Lookup* L = new Lookup();
241 void dns_poll(int fdcheck)
243 /* Check the given file descriptor is in valid range */
244 if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
247 /* Try and find the file descriptor in our list of
250 Lookup *x = dnslist[fdcheck];
253 /* If it exists check if its a valid fd still */
254 if (x->GetFD() != -1)
256 /* Check if its done, if it is delete it */
257 if (x->Done(fdcheck))
259 /* We don't need to delete the file descriptor
260 * from the socket engine, as dns.cpp tracks it
261 * for us if we are in single-threaded country.
268 /* its fd is dodgy, the dns code probably
269 * bashed it due to error. Free the class.
273 /* If we got down here, the dns lookup was valid, BUT,
274 * its still in progress. Be patient, and wait for
275 * more socketengine events to complete the lookups.
279 /* This FD doesnt belong here, lets be rid of it,
280 * just to be safe so we dont get any more events
283 if (ServerInstance && ServerInstance->SE)
284 ServerInstance->SE->DelFd(fdcheck);