]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Stuff to make dns work protocol-independent
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Mon, 5 Feb 2007 23:43:44 +0000 (23:43 +0000)
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Mon, 5 Feb 2007 23:43:44 +0000 (23:43 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6506 e03df62e-2008-0410-955e-edbf42e46eb7

include/inspircd.h
include/socket.h
src/dns.cpp
src/inspsocket.cpp
src/socket.cpp

index 60a29d5f4dab6e717a0bf73769a18223c65ef028..ee02a32b5188e14853903cbbbcb28b025f83c425 100644 (file)
@@ -589,13 +589,11 @@ class InspIRCd : public classbase
 
        /** Binds a socket on an already open file descriptor
         * @param sockfd A valid file descriptor of an open socket
-        * @param client A sockaddr to use as temporary storage
-        * @param server A sockaddr to use as temporary storage
         * @param port The port number to bind to
         * @param addr The address to bind to (IP only)
         * @return True if the port was bound successfully
         */
-       bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr);
+       bool BindSocket(int sockfd, int port, char* addr, bool dolisten = true);
 
        /** Adds a server name to the list of servers we've seen
         * @param The servername to add
index 63546f887adbcb98e949b64d4cfe8b04448bccd7..4e7d8919252b51c84c61a26c78d4382ad14af913 100644 (file)
@@ -136,7 +136,7 @@ namespace irc
                 * or a negative value upon failure (negative values are invalid file
                 * descriptors)
                 */
-               int OpenTCPSocket(char* addr);
+               int OpenTCPSocket(char* addr, int socktype = SOCK_STREAM);
        };
 };
 
index 3f8cde523825d9894d9c6cebc77ee926ffaa343d..ac6097cd25405ddae7f83cb507e3f1e58949be6b 100644 (file)
@@ -311,50 +311,23 @@ void DNS::Rehash()
                this->cache = new dnscache();
        }
 
-       if (insp_aton(ServerInstance->Config->DNSServer,&addr) > 0)
+       if ((strstr(ServerInstance->Config->DNSServer,"::ffff:") == (char*)&ServerInstance->Config->DNSServer) ||  (strstr(ServerInstance->Config->DNSServer,"::FFFF:") == (char*)&ServerInstance->Config->DNSServer))
        {
-               memcpy(&myserver,&addr,sizeof(insp_inaddr));
-               if ((strstr(ServerInstance->Config->DNSServer,"::ffff:") == (char*)&ServerInstance->Config->DNSServer) ||  (strstr(ServerInstance->Config->DNSServer,"::FFFF:") == (char*)&ServerInstance->Config->DNSServer))
-               {
-                       ServerInstance->Log(DEFAULT,"WARNING: Using IPv4 addresses over IPv6 forces some DNS checks to be disabled.");
-                       ServerInstance->Log(DEFAULT,"         This should not cause a problem, however it is recommended you migrate");
-                       ServerInstance->Log(DEFAULT,"         to a true IPv6 environment.");
-                       this->ip6munge = true;
-               }
+               ServerInstance->Log(DEFAULT,"WARNING: Using IPv4 addresses over IPv6 forces some DNS checks to be disabled.");
+               ServerInstance->Log(DEFAULT,"         This should not cause a problem, however it is recommended you migrate");
+               ServerInstance->Log(DEFAULT,"         to a true IPv6 environment.");
+               this->ip6munge = true;
        }
 
        /* Initialize mastersocket */
-       this->SetFd(socket(PF_PROTOCOL, SOCK_DGRAM, 0));
-       if (this->GetFd() != -1)
-       {
-               /* Did it succeed? */
-               if (fcntl(this->GetFd(), F_SETFL, O_NONBLOCK) != 0)
-               {
-                       /* Couldn't make the socket nonblocking */
-                       shutdown(this->GetFd(),2);
-                       close(this->GetFd());
-                       this->SetFd(-1);
-               }
-       }
+       int s = OpenTCPSocket(ServerInstance->Config->DNSServer, SOCK_DGRAM);
+       this->SetFd(s);
 
        /* Have we got a socket and is it nonblocking? */
        if (this->GetFd() != -1)
        {
-#ifdef IPV6
-               insp_sockaddr addr;
-               memset(&addr,0,sizeof(addr));
-               addr.sin6_family = AF_FAMILY;
-               addr.sin6_port = 0;
-               addr.sin6_addr = in6addr_any;
-#else
-               insp_sockaddr addr;
-               memset(&addr,0,sizeof(addr));
-               addr.sin_family = AF_FAMILY;
-               addr.sin_port = 0;
-               addr.sin_addr.s_addr = INADDR_ANY;
-#endif
                /* Bind the port */
-               if (bind(this->GetFd(),(sockaddr *)&addr,sizeof(addr)) != 0)
+               if (!BindSocket(this->GetFd(), 0, ServerInstance->Config->DNSServer, false))
                {
                        /* Failed to bind */
                        shutdown(this->GetFd(),2);
@@ -536,6 +509,7 @@ 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;
@@ -546,6 +520,7 @@ 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
@@ -558,6 +533,7 @@ 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]);
                }
index ea2220686d2fdae35285dedf4e98c23ee41555e5..08b8f4cd698c2fa907b9d0f41494fd059c9210ff 100644 (file)
@@ -65,7 +65,7 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
                }
                else
                {
-                       if (!SI->BindSocket(this->fd,this->client,this->server,aport,(char*)ipaddr.c_str()))
+                       if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
                        {
                                this->Close();
                                this->fd = -1;
index 111299b09653cfcbe4641bf994fce9fdd457a0b0..a32a7f98e17e0c64b0a3992074b115802f4cc7c2 100644 (file)
@@ -40,7 +40,7 @@ ListenSocket::ListenSocket(InspIRCd* Instance, int sockfd, insp_sockaddr client,
 {
        this->SetFd(sockfd);
        Instance->Log(DEBUG,"CRAP");
-       if (!Instance->BindSocket(this->fd,client,server,port,addr))
+       if (!Instance->BindSocket(this->fd,port,addr))
                this->fd = -1;
 #ifdef IPV6
        if ((!*addr) || (strchr(addr,':')))
@@ -312,7 +312,7 @@ void irc::sockets::NonBlocking(int s)
  * It can only bind to IP addresses, if you wish to bind to hostnames
  * you should first resolve them using class 'Resolver'.
  */ 
-bool InspIRCd::BindSocket(int sockfd, insp_sockaddr clientn, insp_sockaddr servern, int port, char* addr)
+bool InspIRCd::BindSocket(int sockfd, int port, char* addr, bool dolisten)
 {
        /* We allocate 2 of these, because sockaddr_in6 is larger than sockaddr (ugh, hax) */
        sockaddr* server = new sockaddr[2];
@@ -404,22 +404,28 @@ bool InspIRCd::BindSocket(int sockfd, insp_sockaddr clientn, insp_sockaddr serve
        }
        else
        {
-               if (listen(sockfd, Config->MaxConn) == -1)
+               if (dolisten)
                {
-                       this->Log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
-                       return false;
+                       if (listen(sockfd, Config->MaxConn) == -1)
+                       {
+                               this->Log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
+                               return false;
+                       }
+                       else
+                       {
+                               NonBlocking(sockfd);
+                               return true;
+                       }
                }
                else
                {
-                       NonBlocking(sockfd);
                        return true;
                }
        }
 }
 
-
 // Open a TCP Socket
-int irc::sockets::OpenTCPSocket(char* addr)
+int irc::sockets::OpenTCPSocket(char* addr, int socktype)
 {
        int sockfd;
        int on = 1;
@@ -429,16 +435,16 @@ int irc::sockets::OpenTCPSocket(char* addr)
        if (strchr(addr,':') || (!*addr))
        {
                printf("IPV6 OPENTCPSOCKET DO\n");
-               sockfd = socket (PF_INET6, SOCK_STREAM, 0);
+               sockfd = socket (PF_INET6, socktype, 0);
        }
        else
        {
                printf("IPV6->IPV4 OPENTCPSOCKET DO\n");
-               sockfd = socket (PF_INET, SOCK_STREAM, 0);
+               sockfd = socket (PF_INET, socktype, 0);
        }
        if (sockfd < 0)
 #else
-       if ((sockfd = socket (PF_INET, SOCK_STREAM, 0)) < 0)
+       if ((sockfd = socket (PF_INET, socktype, 0)) < 0)
 #endif
        {
                printf("SOCKET FAIL: %s\n", strerror(errno));