+
+std::string DNS::GetResultIP()
+{
+ char r[1024];
+ log(DEBUG,"DNS: GetResultIP()");
+ result = dns_getresult(this->myfd);
+ if (this->myfd != -1)
+ {
+ dns_close(this->myfd);
+ this->myfd = -1;
+ }
+ if (result)
+ {
+ if (ServerInstance && ServerInstance->stats)
+ ServerInstance->stats->statsDnsGood++;
+ unsigned char a = (unsigned)result[0];
+ unsigned char b = (unsigned)result[1];
+ unsigned char c = (unsigned)result[2];
+ unsigned char d = (unsigned)result[3];
+ snprintf(r,1024,"%u.%u.%u.%u",a,b,c,d);
+ return r;
+ }
+ else
+ {
+ if (ServerInstance && ServerInstance->stats)
+ ServerInstance->stats->statsDnsBad++;
+ log(DEBUG,"DANGER WILL ROBINSON! NXDOMAIN for forward lookup, but we got a reverse lookup!");
+ return "";
+ }
+}
+
+
+
+#ifdef THREADED_DNS
+
+/* This function is a thread function which can be thought of as a lightweight process
+ * to all you non-threaded people. In actuality its so much more, and pretty damn cool.
+ * With threaded dns enabled, each user which connects gets a thread attached to their
+ * user record when their DNS lookup starts. This function starts in parallel, and
+ * commences a blocking dns lookup. Because its a seperate thread, this occurs without
+ * actually blocking the main application. Once the dns lookup is completed, the thread
+ * checks if the user is still around by checking their fd against the reference table,
+ * and if they are, writes the hostname into the struct and terminates, after setting
+ * userrec::dns_done to true. Because this is multi-threaded it can make proper use of
+ * SMP setups (like the one i have here *grin*).
+ * This is in comparison to the non-threaded dns, which must monitor the thread sockets
+ * in a nonblocking fashion, consuming more resources to do so.
+ *
+ * NB: Yes this does scale, thank you. Even with large numbers of connecting clients
+ * in any one timeframe, they wont all connect *at the same time* therefore any argument
+ * of "but there will be thousands of threads it'll blow up" is moot, ive tested this and
+ * there will only ever be somewhere around the listen backlog in number of pending
+ * lookups at any one time. This is significant on any modern SMP system.
+ */
+void* dns_task(void* arg)
+{
+ userrec* u = (userrec*)arg;
+ int thisfd = u->fd;
+
+ log(DEBUG,"DNS thread for user %s",u->nick);
+ DNS dns1(Config->DNSServer);
+ DNS dns2(Config->DNSServer);
+ std::string host;
+ std::string ip;
+ int iterations = 0;
+
+ if (dns1.ReverseLookup(inet_ntoa(u->ip4),false))
+ {
+ /* FIX: Dont make these infinite! */
+ while ((!dns1.HasResult()) && (++iterations < 20))
+ usleep(100);
+
+ if (iterations < 20)
+ {
+ if (dns1.GetFD() != -1)
+ {
+ host = dns1.GetResult();
+ if (host != "")
+ {
+ if (dns2.ForwardLookup(host, false))
+ {
+ iterations = 0;
+ while ((!dns2.HasResult()) && (++iterations < 20))
+ usleep(100);
+
+ if (iterations < 20)
+ {
+ if (dns2.GetFD() != -1)
+ {
+ ip = dns2.GetResultIP();
+ if (ip == std::string(inet_ntoa(u->ip4)))
+ {
+ if (host.length() < 65)
+ {
+ if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd]))
+ {
+ if (!u->dns_done)
+ {
+ strcpy(u->host,host.c_str());
+ if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd]))
+ {
+ strcpy(u->dhost,host.c_str());
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ if ((fd_ref_table[thisfd] == u) && (fd_ref_table[thisfd]))
+ u->dns_done = true;
+ log(DEBUG,"THREAD EXIT");
+ return NULL;
+}
+#endif
+
+Resolver::Resolver(const std::string &source, bool forward, const std::string &dnsserver = "") : input(source), fwd(forward), server(dnsserver)
+{
+ if (this->server != "")
+ Query.SetNS(this->server);
+ else
+ Query.SetNS(Config->DNSServer);
+
+ if (forward)
+ {
+ Query.ForwardLookup(input.c_str(), false);
+ this->fd = Query.GetFD();
+ }
+ else
+ {
+ Query.ReverseLookup(input.c_str(), false);
+ this->fd = Query.GetFD();
+ }
+ if (fd < 0)
+ {
+ log(DEBUG,"Resolver::Resolver: RESOLVER_NSDOWN");
+ this->OnError(RESOLVER_NSDOWN);
+ ModuleException e("Resolver: Nameserver is down");
+ throw e;
+ /* We shouldnt get here really */
+ return;
+ }
+
+ if (ServerInstance && ServerInstance->SE)
+ {
+ log(DEBUG,"Resolver::Resolver: this->fd=%d",this->fd);
+ ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_CLASSDNS);
+ }
+ else
+ {
+ log(DEBUG,"Resolver::Resolver: RESOLVER_NOTREADY");
+ this->OnError(RESOLVER_NOTREADY);
+ ModuleException e("Resolver: Core not initialized yet");
+ throw e;
+ /* We shouldnt get here really */
+ return;
+ }
+}
+
+Resolver::~Resolver()
+{
+ log(DEBUG,"Resolver::~Resolver");
+ if (ServerInstance && ServerInstance->SE)
+ ServerInstance->SE->DelFd(this->fd);
+}
+
+int Resolver::GetFd()
+{
+ return this->fd;
+}
+
+bool Resolver::ProcessResult()
+{
+ log(DEBUG,"Resolver::ProcessResult");
+ if (this->fwd)
+ result = Query.GetResultIP();
+ else
+ result = Query.GetResult();
+
+ if (result != "")
+ {
+ log(DEBUG,"Resolver::OnLookupComplete(%s)",result.c_str());
+ this->OnLookupComplete(result);
+ return true;
+ }
+ else
+ {
+ log(DEBUG,"Resolver::OnError(RESOLVER_NXDOMAIN)");
+ this->OnError(RESOLVER_NXDOMAIN);
+ return false;
+ }
+}
+
+void Resolver::OnLookupComplete(const std::string &result)
+{
+}
+
+void Resolver::OnError(ResolverError e)
+{
+}
+
+void dns_deal_with_classes(int fd)
+{
+ log(DEBUG,"dns_deal_with_classes(%d)",fd);
+ if ((fd > -1) && (dns_classes[fd]))
+ {
+ log(DEBUG,"Valid fd %d",fd);
+ dns_classes[fd]->ProcessResult();
+ delete dns_classes[fd];
+ dns_classes[fd] = NULL;
+ }
+}
+
+bool dns_add_class(Resolver* r)
+{
+ log(DEBUG,"dns_add_class");
+ if ((r) && (r->GetFd() > -1))
+ {
+ if (!dns_classes[r->GetFd()])
+ {
+ log(DEBUG,"dns_add_class: added class");
+ dns_classes[r->GetFd()] = r;
+ return true;
+ }
+ else
+ {
+ log(DEBUG,"Space occupied!");
+ return false;
+ }
+ }
+ else
+ {
+ log(DEBUG,"Bad class");
+ delete r;
+ return true;
+ }
+}
+
+void init_dns()
+{
+ memset(dns_classes,0,sizeof(dns_classes));
+}
+