}
};
+CachedQuery::CachedQuery(const std::string &res, unsigned int ttl) : data(res)
+{
+ expires = ServerInstance->Time() + ttl;
+}
+
+int CachedQuery::CalcTTLRemaining()
+{
+ int n = expires - ServerInstance->Time();
+ return (n < 0 ? 0 : n);
+}
+
/* Allocate the processing buffer */
DNSRequest::DNSRequest(DNS* dns, int rid, const std::string &original) : dnsobj(dns)
{
return NULL;
/* Create an id */
- id = this->PRNG() & DNS::MAX_REQUEST_ID;
-
- /* If this id is already 'in flight', pick another. */
- while (requests[id])
- id = this->PRNG() & DNS::MAX_REQUEST_ID;
+ do {
+ id = ServerInstance->GenRandomInt(DNS::MAX_REQUEST_ID);
+ } while (requests[id]);
DNSRequest* req = new DNSRequest(this, id, original);
void DNS::Rehash()
{
- int portpass = 0;
-
if (this->GetFd() > -1)
{
- if (ServerInstance && ServerInstance->SE)
- ServerInstance->SE->DelFd(this);
+ ServerInstance->SE->DelFd(this);
ServerInstance->SE->Shutdown(this, 2);
ServerInstance->SE->Close(this);
this->SetFd(-1);
{
ServerInstance->SE->SetReuse(s);
ServerInstance->SE->NonBlocking(s);
- /* Bind the port - port 0 INADDR_ANY */
- if (!ServerInstance->BindSocket(this->GetFd(), portpass, "", false))
+ irc::sockets::sockaddrs bindto;
+ memset(&bindto, 0, sizeof(bindto));
+ bindto.sa.sa_family = myserver.sa.sa_family;
+ if (ServerInstance->SE->Bind(this->GetFd(), bindto) < 0)
{
/* Failed to bind */
- ServerInstance->Logs->Log("RESOLVER",DEBUG,"Error binding dns socket");
+ ServerInstance->Logs->Log("RESOLVER",SPARSE,"Error binding dns socket - hostnames will NOT resolve");
ServerInstance->SE->Shutdown(this, 2);
ServerInstance->SE->Close(this);
this->SetFd(-1);
}
-
- if (this->GetFd() >= 0)
+ else if (!ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE))
{
- /* Hook the descriptor into the socket engine */
- if (ServerInstance && ServerInstance->SE)
- {
- if (!ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE))
- {
- ServerInstance->Logs->Log("RESOLVER",DEFAULT,"Internal error starting DNS - hostnames will NOT resolve.");
- ServerInstance->SE->Shutdown(this, 2);
- ServerInstance->SE->Close(this);
- this->SetFd(-1);
- }
- }
+ ServerInstance->Logs->Log("RESOLVER",SPARSE,"Internal error starting DNS - hostnames will NOT resolve.");
+ ServerInstance->SE->Shutdown(this, 2);
+ ServerInstance->SE->Close(this);
+ this->SetFd(-1);
}
}
else
{
- ServerInstance->Logs->Log("RESOLVER",DEBUG,"Error creating dns socket");
+ ServerInstance->Logs->Log("RESOLVER",SPARSE,"Error creating DNS socket - hostnames will NOT resolve");
}
}
DNS::MakeIP6Int(query, &i);
}
else
+ {
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS::GetNameForce IPv6 bad format for '%s'", ip);
/* Invalid IP address */
return -1;
+ }
}
else
{
sprintf(query,"%d.%d.%d.%d.in-addr.arpa",c[3],c[2],c[1],c[0]);
}
else
+ {
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS::GetNameForce IPv4 bad format for '%s'", ip);
/* Invalid IP address */
return -1;
+ }
}
- if ((length = this->MakePayload(query, DNS_QUERY_PTR, 1, (unsigned char*)&h.payload)) == -1)
+ length = this->MakePayload(query, DNS_QUERY_PTR, 1, (unsigned char*)&h.payload);
+ if (length == -1)
+ {
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS::GetNameForce can't query '%s' using '%s' because it's too long", ip, query);
return -1;
+ }
DNSRequest* req = this->AddQuery(&h, id, ip);
- if ((!req) || (req->SendRequests(&h, length, DNS_QUERY_PTR) == -1))
+ if (!req)
+ {
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS::GetNameForce can't add query (resolver down?)");
return -1;
+ }
+
+ if (req->SendRequests(&h, length, DNS_QUERY_PTR) == -1)
+ {
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS::GetNameForce can't send (firewall?)");
+ return -1;
+ }
return id;
}
/* Did we get the whole header? */
if (length < 12)
{
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"GetResult didn't get a full packet (len=%d)", length);
/* Nope - something screwed up. */
return DNSResult(-1,"",0,"");
}
*
* -- Thanks jilles for pointing this one out.
*/
- if (memcmp(&from, &myserver, sizeof(irc::sockets::sockaddrs)))
+ if (from != myserver)
{
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"Got a result from the wrong server! Bad NAT or DNS forging attempt? '%s' != '%s'",
+ from.str().c_str(), myserver.str().c_str());
return DNSResult(-1,"",0,"");
}
if (!requests[this_id])
{
/* Somehow we got a DNS response for a request we never made... */
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"Hmm, got a result that we didn't ask for (id=%lx). Ignoring.", this_id);
return DNSResult(-1,"",0,"");
}
else
break;
default:
+ ServerInstance->Logs->Log("RESOLVER",DEBUG,"DNS request with unknown query type %d", querytype);
this->myid = -1;
break;
}
if (this->myid == -1)
{
- this->OnError(RESOLVER_NSDOWN, "Nameserver is down");
- throw ModuleException("Resolver: Couldnt get an id to make a request");
- /* We shouldnt get here really */
- return;
+ throw ModuleException("Resolver: Couldn't get an id to make a request");
}
else
{
}
}
}
-
-/** Generate pseudo-random number */
-unsigned long DNS::PRNG()
-{
- unsigned long val = 0;
- timeval n;
- serverstats* s = ServerInstance->stats;
- gettimeofday(&n,NULL);
- val = (n.tv_usec ^ (getpid() ^ geteuid()) ^ ((this->currid++)) ^ s->statsAccept) + n.tv_sec;
- val = val + (s->statsCollisions ^ s->statsDnsGood) - s->statsDnsBad;
- val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv) - ServerInstance->ports.size();
- return val;
-}