]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/dns.cpp
Same tweak, again
[user/henk/code/inspircd.git] / src / dns.cpp
index ea6b000a9e3d788373b571feb731475fdffa29e7..49cfa3f1fbc3b380b83cef6b8fcc4654858a3a58 100644 (file)
@@ -21,21 +21,26 @@ Please do not assume that firedns works like this,
 looks like this, walks like this or tastes like this.
 */
 
+#ifndef WIN32
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#else
+#include "inspircd_win32wrapper.h"
+#endif
+
 #include "dns.h"
 #include "inspircd.h"
 #include "socketengine.h"
 #include "configreader.h"
 #include "socket.h"
 
-using irc::sockets::insp_sockaddr;
 using irc::sockets::insp_inaddr;
 using irc::sockets::insp_ntoa;
 using irc::sockets::insp_aton;
+using irc::sockets::OpenTCPSocket;
 
 /** Masks to mask off the responses we get from the DNSRequest methods
  */
@@ -91,12 +96,11 @@ class DNSRequest
        unsigned char*  res;            /* Result processing buffer */
        unsigned int    rr_class;       /* Request class */
        QueryType       type;           /* Request type */
-       insp_inaddr     myserver;       /* DNS server address*/
        DNS*            dnsobj;         /* DNS caller (where we get our FD from) */
        unsigned long   ttl;            /* Time to live */
        std::string     orig;           /* Original requested name/ip */
 
-       DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, const std::string &original);
+       DNSRequest(InspIRCd* Instance, DNS* dns, int id, const std::string &original);
        ~DNSRequest();
        DNSInfo ResultIsReady(DNSHeader &h, int length);
        int SendRequests(const DNSHeader *header, const int length, QueryType qt);
@@ -146,11 +150,10 @@ class RequestTimeout : public InspTimer
 };
 
 /* Allocate the processing buffer */
-DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, const std::string &original) : dnsobj(dns)
+DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, int id, const std::string &original) : dnsobj(dns)
 {
        res = new unsigned char[512];
        *res = 0;
-       memcpy(&myserver, &server, sizeof(insp_inaddr));
        orig = original;
        RequestTimeout* RT = new RequestTimeout(Instance->Config->dns_timeout ? Instance->Config->dns_timeout : 5, Instance, this, id);
        Instance->Timers->AddTimer(RT); /* The timer manager frees this */
@@ -206,7 +209,6 @@ inline void DNS::EmptyHeader(unsigned char *output, const DNSHeader *header, con
 /** Send requests we have previously built down the UDP socket */
 int DNSRequest::SendRequests(const DNSHeader *header, const int length, QueryType qt)
 {
-       insp_sockaddr addr;
        unsigned char payload[sizeof(DNSHeader)];
 
        this->rr_class = 1;
@@ -214,18 +216,36 @@ int DNSRequest::SendRequests(const DNSHeader *header, const int length, QueryTyp
                
        DNS::EmptyHeader(payload,header,length);
 
-       memset(&addr,0,sizeof(addr));
 #ifdef IPV6
-       memcpy(&addr.sin6_addr,&myserver,sizeof(addr.sin6_addr));
-       addr.sin6_family = AF_FAMILY;
-       addr.sin6_port = htons(DNS::QUERY_PORT);
+       if (this->dnsobj->socketfamily == AF_INET6)
+       {
+               sockaddr_in6 addr;
+               memset(&addr,0,sizeof(addr));
+               memcpy(&addr.sin6_addr,&dnsobj->myserver6,sizeof(addr.sin6_addr));
+               addr.sin6_family = AF_INET6;
+               addr.sin6_port = htons(DNS::QUERY_PORT);
+               if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
+                       return -1;
+       }
+       else
+       {
+               sockaddr_in addr;
+               memset(&addr,0,sizeof(addr));
+               memcpy(&addr.sin_addr.s_addr,&dnsobj->myserver4,sizeof(addr.sin_addr));
+               addr.sin_family = AF_INET;
+               addr.sin_port = htons(DNS::QUERY_PORT);
+               if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
+                       return -1;
+       }
 #else
-       memcpy(&addr.sin_addr.s_addr,&myserver,sizeof(addr.sin_addr));
-       addr.sin_family = AF_FAMILY;
+       sockaddr_in addr;
+       memset(&addr,0,sizeof(addr));
+       memcpy(&addr.sin_addr.s_addr, &dnsobj->myserver4, sizeof(addr.sin_addr));
+       addr.sin_family = AF_INET;
        addr.sin_port = htons(DNS::QUERY_PORT);
-#endif
-       if (sendto(dnsobj->GetFd(), payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) == -1)
+       if (sendto(dnsobj->GetFd(), (const char*)payload, length + 12, 0, (sockaddr *) &addr, sizeof(addr)) != length+12)
                return -1;
+#endif
 
        return 0;
 }
@@ -244,7 +264,7 @@ DNSRequest* DNS::AddQuery(DNSHeader *header, int &id, const char* original)
        while (requests[id])
                id = this->PRNG() & DNS::MAX_REQUEST_ID;
 
-       DNSRequest* req = new DNSRequest(ServerInstance, this, this->myserver, id, original);
+       DNSRequest* req = new DNSRequest(ServerInstance, this, id, original);
 
        header->id[0] = req->id[0] = id >> 8;
        header->id[1] = req->id[1] = id & 0xFF;
@@ -292,6 +312,7 @@ int DNS::PruneCache()
 void DNS::Rehash()
 {
        ip6munge = false;
+       int portpass = 0;
 
        if (this->GetFd() > -1)
        {
@@ -323,12 +344,15 @@ void DNS::Rehash()
        if (strchr(ServerInstance->Config->DNSServer,':'))
        {
                this->socketfamily = AF_INET6;
-               inet_pton(AF_INET6, &this->myserver6, ServerInstance->Config->DNSServer);
+               inet_pton(AF_INET6, ServerInstance->Config->DNSServer, &this->myserver6);
        }
        else
-               inet_aton(&this->myserver4, ServerInstance->Config->DNSServer);
+       {
+               inet_aton(ServerInstance->Config->DNSServer, &this->myserver4);
+               portpass = -1;
+       }
 #else
-       inet_aton(&this->myserver4, ServerInstance->Config->DNSServer);
+       inet_aton(ServerInstance->Config->DNSServer, &this->myserver4);
 #endif
 
        /* Initialize mastersocket */
@@ -338,8 +362,8 @@ void DNS::Rehash()
        /* Have we got a socket and is it nonblocking? */
        if (this->GetFd() != -1)
        {
-               /* Bind the port */
-               if (!BindSocket(this->GetFd(), 0, ServerInstance->Config->DNSServer, false))
+               /* Bind the port - port 0 INADDR_ANY */
+               if (!ServerInstance->BindSocket(this->GetFd(), portpass, "", false))
                {
                        /* Failed to bind */
                        shutdown(this->GetFd(),2);
@@ -521,7 +545,6 @@ int DNS::GetName(const insp_inaddr *ip)
 /** Start lookup of an IP address to a hostname */
 int DNS::GetNameForce(const char *ip, ForceProtocol fp)
 {
-       ServerInstance->Log(DEBUG,"GetNameForce: %s", ip);
        char query[128];
        DNSHeader h;
        int id;
@@ -532,7 +555,6 @@ int DNS::GetNameForce(const char *ip, ForceProtocol fp)
                in6_addr i;
                if (inet_pton(AF_INET6, ip, &i) > 0)
                {
-                       ServerInstance->Log(DEBUG,"Resolve to ipv6");
                        DNS::MakeIP6Int(query, &i);
                }
                else
@@ -545,7 +567,6 @@ int DNS::GetNameForce(const char *ip, ForceProtocol fp)
                in_addr i;
                if (inet_aton(ip, &i))
                {
-                       ServerInstance->Log(DEBUG,"Resolve to ipv4");
                        unsigned char* c = (unsigned char*)&i.s_addr;
                        sprintf(query,"%d.%d.%d.%d.in-addr.arpa",c[3],c[2],c[1],c[0]);
                }
@@ -594,17 +615,22 @@ DNSResult DNS::GetResult()
        DNSHeader header;
        DNSRequest *req;
        unsigned char buffer[sizeof(DNSHeader)];
-       sockaddr from;
-       socklen_t x = sizeof(from);
-       const char* ipaddr_from = "";
+       sockaddr* from = new sockaddr[2];
+#ifdef IPV6
+       socklen_t x = this->socketfamily == AF_INET ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
+#else
+       socklen_t x = sizeof(sockaddr_in);
+#endif
+       const char* ipaddr_from;
        unsigned short int port_from = 0;
 
-       int length = recvfrom(this->GetFd(),buffer,sizeof(DNSHeader),0,&from,&x);
+       int length = recvfrom(this->GetFd(),(char*)buffer,sizeof(DNSHeader),0,from,&x);
 
        /* Did we get the whole header? */
        if (length < 12)
        {
                /* Nope - something screwed up. */
+               delete[] from;
                return DNSResult(-1,"",0,"");
        }
 
@@ -618,13 +644,24 @@ DNSResult DNS::GetResult()
         * -- Thanks jilles for pointing this one out.
         */
 #ifdef IPV6
-       ipaddr_from = insp_ntoa(((sockaddr_in6*)&from)->sin6_addr);
-       port_from = ntohs(((sockaddr_in6*)&from)->sin6_port);
+       char nbuf[MAXBUF];
+       if (this->socketfamily == AF_INET6)
+       {
+               ipaddr_from = inet_ntop(AF_INET6, &((sockaddr_in6*)from)->sin6_addr, nbuf, sizeof(nbuf));
+               port_from = ntohs(((sockaddr_in6*)from)->sin6_port);
+       }
+       else
+       {
+               ipaddr_from = inet_ntoa(((sockaddr_in*)from)->sin_addr);
+               port_from = ntohs(((sockaddr_in*)from)->sin_port);
+       }
 #else
-       ipaddr_from = insp_ntoa(((sockaddr_in*)&from)->sin_addr);
-       port_from = ntohs(((sockaddr_in*)&from)->sin_port);
+       ipaddr_from = inet_ntoa(((sockaddr_in*)from)->sin_addr);
+       port_from = ntohs(((sockaddr_in*)from)->sin_port);
 #endif
 
+       delete[] from;
+
        /* We cant perform this security check if you're using 4in6.
         * Tough luck to you, choose one or't other!
         */
@@ -1114,13 +1151,22 @@ void DNS::CleanResolvers(Module* module)
 /** Generate pseudo-random number */
 unsigned long DNS::PRNG()
 {
+#ifndef WIN32
        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) - s->BoundPortCount;
+       val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv) - ServerInstance->Config->ports.size();
        return val;
+#else
+       unsigned long val = 0;
+       serverstats* s = ServerInstance->stats;
+       val = (time(NULL) ^ GetCurrentProcessId() ^ GetCurrentThreadId() ^ (this->currid++)) ^ s->statsAccept + time(NULL);
+       val = val + s->statsCollisions ^ s->statsDnsGood - s->statsDnsBad;
+       val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv);
+       return val;
+#endif
 }