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));
74 if (!resolver1.ReverseLookup(std::string(usr->host), true))
78 strlcpy(u,nick.c_str(),NICKMAX-1);
80 /* ASSOCIATE WITH DNS LOOKUP LIST */
81 if (resolver1.GetFD() != -1)
83 dnslist[resolver1.GetFD()] = this;
90 bool Done(int fdcheck)
94 // doing forward lookup
96 if (resolver2.HasResult(fdcheck))
98 if (resolver2.GetFD() != -1)
100 dnslist[resolver2.GetFD()] = NULL;
101 std::string ip = resolver2.GetResultIP();
105 if (usr->registered > 3)
107 usr->dns_done = true;
110 if ((hostname != "") && (usr->registered != 7))
112 if ((std::string((char*)inet_ntoa(usr->ip4)) == ip) && (hostname.length() < 65))
114 strlcpy(usr->host,hostname.c_str(),64);
115 strlcpy(usr->dhost,hostname.c_str(),64);
116 /*address_cache::iterator address = addrcache.find(usr->ip4);
117 if (address == addrcache.end())
119 log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
120 addrcache[usr->ip4] = new std::string(hostname);
122 WriteServ(usr->fd,"NOTICE Auth :*** Found your hostname");
124 usr->dns_done = true;
134 usr->dns_done = true;
143 // doing reverse lookup
145 if (resolver1.HasResult(fdcheck))
148 if ((usr) && (usr->dns_done))
150 if (resolver1.GetFD() != -1)
151 dnslist[resolver1.GetFD()] = NULL;
154 if (resolver1.GetFD() != -1)
156 dnslist[resolver1.GetFD()] = NULL;
157 hostname = resolver1.GetResult();
160 if ((usr->registered > 3) || (hostname == ""))
162 WriteServ(usr->fd,"NOTICE Auth :*** Could not resolve your hostname -- Using your IP address instead");
163 usr->dns_done = true;
169 resolver2.ForwardLookup(hostname, true);
170 if (resolver2.GetFD() != -1)
171 dnslist[resolver2.GetFD()] = this;
181 userrec* usr = Find(u);
190 bool lookup_dns(const std::string &nick)
192 /* First attempt to find the nickname */
193 userrec* u = Find(nick);
196 /* Check the cache */
197 /*address_cache::iterator address = addrcache.find(u->ip4);
198 if (address != addrcache.end())
200 WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
201 log(DEBUG,"Found cached host");
202 strlcpy(u->host,address->second->c_str(),MAXBUF);
203 strlcpy(u->dhost,address->second->c_str(),MAXBUF);
207 /* If the user exists, create a new
208 * lookup object, and associate it
209 * with the user. The lookup object
210 * will maintain the reference table
211 * which we use for quickly finding
212 * dns results. Please note that we
213 * do not associate a lookup with a
214 * userrec* pointer and we use the
215 * nickname instead because, by the
216 * time the DNS lookup has completed,
217 * the nickname could have quit and
218 * if we then try and access the
219 * pointer we get a nice segfault.
221 Lookup* L = new Lookup();
228 void dns_poll(int fdcheck)
230 /* Check the given file descriptor is in valid range */
231 if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
234 /* Try and find the file descriptor in our list of
237 Lookup *x = dnslist[fdcheck];
240 /* If it exists check if its a valid fd still */
241 if (x->GetFD() != -1)
243 /* Check if its done, if it is delete it */
244 if (x->Done(fdcheck))
246 /* We don't need to delete the file descriptor
247 * from the socket engine, as dns.cpp tracks it
248 * for us if we are in single-threaded country.
255 /* its fd is dodgy, the dns code probably
256 * bashed it due to error. Free the class.
260 /* If we got down here, the dns lookup was valid, BUT,
261 * its still in progress. Be patient, and wait for
262 * more socketengine events to complete the lookups.
266 /* This FD doesnt belong here, lets be rid of it,
267 * just to be safe so we dont get any more events
270 if (ServerInstance && ServerInstance->SE)
271 ServerInstance->SE->DelFd(fdcheck);