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;
56 address_cache addrcache;
60 Lookup* dnslist[MAX_DESCRIPTORS];
62 //enum LookupState { reverse, forward };
87 bool DoLookup(std::string nick)
90 userrec* usr = Find(nick);
93 resolver1.SetNS(std::string(Config->DNSServer));
94 if (!resolver1.ReverseLookup(std::string(usr->host)))
98 strlcpy(u,nick.c_str(),NICKMAX-1);
100 /* ASSOCIATE WITH DNS LOOKUP LIST */
101 if (resolver1.GetFD() != -1)
103 dnslist[resolver1.GetFD()] = this;
110 bool Done(int fdcheck)
114 // doing forward lookup
116 if (resolver2.HasResult(fdcheck))
118 if (resolver2.GetFD() != -1)
120 dnslist[resolver2.GetFD()] = NULL;
121 std::string ip = resolver2.GetResultIP();
125 if (usr->registered > 3)
127 usr->dns_done = true;
130 if ((hostname != "") && (usr->registered != 7))
132 if (std::string((char*)inet_ntoa(usr->ip4)) == ip)
134 strlcpy(usr->host,hostname.c_str(),MAXBUF);
135 strlcpy(usr->dhost,hostname.c_str(),MAXBUF);
136 /*address_cache::iterator address = addrcache.find(usr->ip4);
137 if (address == addrcache.end())
139 log(DEBUG,"Caching hostname %s -> %s",(char*)inet_ntoa(usr->ip4),hostname.c_str());
140 addrcache[usr->ip4] = new std::string(hostname);
142 WriteServ(usr->fd,"NOTICE Auth :*** Found your hostname");
144 usr->dns_done = true;
154 usr->dns_done = true;
163 // doing reverse lookup
165 if (resolver1.HasResult(fdcheck))
168 if ((usr) && (usr->dns_done))
170 if (resolver1.GetFD() != -1)
171 dnslist[resolver1.GetFD()] = NULL;
174 if (resolver1.GetFD() != -1)
176 dnslist[resolver1.GetFD()] = NULL;
177 hostname = resolver1.GetResult();
180 if ((usr->registered > 3) || (hostname == ""))
182 WriteServ(usr->fd,"NOTICE Auth :*** Could not resolve your hostname -- Using your IP address instead");
183 usr->dns_done = true;
189 resolver2.ForwardLookup(hostname);
190 if (resolver2.GetFD() != -1)
191 dnslist[resolver2.GetFD()] = this;
201 userrec* usr = Find(u);
210 bool lookup_dns(std::string nick)
212 /* First attempt to find the nickname */
213 userrec* u = Find(nick);
216 /* Check the cache */
217 /*address_cache::iterator address = addrcache.find(u->ip4);
218 if (address != addrcache.end())
220 WriteServ(u->fd,"NOTICE Auth :*** Found your hostname (cached)");
221 log(DEBUG,"Found cached host");
222 strlcpy(u->host,address->second->c_str(),MAXBUF);
223 strlcpy(u->dhost,address->second->c_str(),MAXBUF);
227 /* If the user exists, create a new
228 * lookup object, and associate it
229 * with the user. The lookup object
230 * will maintain the reference table
231 * which we use for quickly finding
232 * dns results. Please note that we
233 * do not associate a lookup with a
234 * userrec* pointer and we use the
235 * nickname instead because, by the
236 * time the DNS lookup has completed,
237 * the nickname could have quit and
238 * if we then try and access the
239 * pointer we get a nice segfault.
241 Lookup* L = new Lookup();
248 void dns_poll(int fdcheck)
250 /* Check the given file descriptor is in valid range */
251 if ((fdcheck < 0) || (fdcheck > MAX_DESCRIPTORS))
254 /* Try and find the file descriptor in our list of
257 Lookup *x = dnslist[fdcheck];
260 /* If it exists check if its a valid fd still */
261 if (x->GetFD() != -1)
263 /* Check if its done, if it is delete it */
264 if (x->Done(fdcheck))
266 /* We don't need to delete the file descriptor
267 * from the socket engine, as dns.cpp tracks it
268 * for us if we are in single-threaded country.
275 /* its fd is dodgy, the dns code probably
276 * bashed it due to error. Free the class.
280 /* If we got down here, the dns lookup was valid, BUT,
281 * its still in progress. Be patient, and wait for
282 * more socketengine events to complete the lookups.
286 /* This FD doesnt belong here, lets be rid of it,
287 * just to be safe so we dont get any more events
290 if (ServerInstance && ServerInstance->SE)
291 ServerInstance->SE->DelFd(fdcheck);