]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Add 'no such server' on remote stats to invalid name
[user/henk/code/inspircd.git] / src / socket.cpp
index c4129f67cfeff4316dcca72a19901a4f1a327eb8..d826bfb8c0a8a8491f63ab2e751f3f4a02123918 100644 (file)
 extern InspIRCd* ServerInstance;
 extern ServerConfig* Config;
 extern time_t TIME;
-extern int openSockfd[MAX_DESCRIPTORS];
 
 /** This will bind a socket to a port. It works for UDP/TCP.
- * If a hostname is given to bind to, the function will first
- * attempt to resolve the hostname, then bind to the IP the 
- * hostname resolves to. This is a blocking lookup blocking for
- * a maximum of one second before it times out, using the DNS
- * server specified in the configuration file.
+ * It can only bind to IP addresses, if you wish to bind to hostnames
+ * you should first resolve them using class 'Resolver'.
  */ 
 bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
 {
        memset(&server,0,sizeof(server));
        insp_inaddr addy;
-       bool resolved = false;
-       char resolved_addr[128];
 
        if (*addr == '*')
                *addr = 0;
 
-       if (*addr && !inet_aton(addr,&addy))
+       if ((*addr) && (insp_aton(addr,&addy) < 1))
        {
-               /* If they gave a hostname, bind to the IP it resolves to */
-               if (CleanAndResolve(resolved_addr, addr, true))
-               {
-                       inet_aton(resolved_addr,&addy);
-                       log(DEFAULT,"Resolved binding '%s' -> '%s'",addr,resolved_addr);
-                       server.sin_addr = addy;
-                       resolved = true;
-               }
-               else
-               {
-                       log(DEFAULT,"WARNING: Could not resolve '%s' to an IP for binding to on port %d",addr,port);
-                       return false;
-               }
+               log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
+               return false;;
        }
-       server.sin_family = AF_INET;
-       if (!resolved)
+
+#ifdef IPV6
+       server.sin6_family = AF_FAMILY;
+#else
+       server.sin_family = AF_FAMILY;
+#endif
+       if (!*addr)
        {
-               if (!*addr)
-               {
-                       server.sin_addr.s_addr = htonl(INADDR_ANY);
-               }
-               else
-               {
-                       server.sin_addr = addy;
-               }
+#ifdef IPV6
+               memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
+#else
+               server.sin_addr.s_addr = htonl(INADDR_ANY);
+#endif
+       }
+       else
+       {
+#ifdef IPV6
+               memcpy(&addy, &server.sin6_addr, sizeof(in6_addr));
+#else
+               server.sin_addr = addy;
+#endif
        }
+#ifdef IPV6
+       server.sin6_port = htons(port);
+#else
        server.sin_port = htons(port);
+#endif
        if (bind(sockfd,(struct sockaddr*)&server,sizeof(server)) < 0)
        {
                return false;
@@ -102,7 +99,7 @@ int OpenTCPSocket()
        int on = 1;
        struct linger linger = { 0 };
   
-       if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
+       if ((sockfd = socket (AF_FAMILY, SOCK_STREAM, 0)) < 0)
        {
                log(DEFAULT,"Error creating TCP socket: %s",strerror(errno));
                return (ERROR);
@@ -120,7 +117,7 @@ int OpenTCPSocket()
 
 bool HasPort(int port, char* addr)
 {
-       for (int count = 0; count < ServerInstance->stats->BoundPortCount; count++)
+       for (unsigned long count = 0; count < ServerInstance->stats->BoundPortCount; count++)
        {
                if ((port == Config->ports[count]) && (!strcasecmp(Config->addrs[count],addr)))
                {
@@ -165,20 +162,23 @@ int BindPorts(bool bail)
                {
                        for (int count = InitialPortCount; count < InitialPortCount + PortCount; count++)
                        {
-                               if ((openSockfd[count] = OpenTCPSocket()) == ERROR)
+                               if ((Config->openSockfd[count] = OpenTCPSocket()) == ERROR)
                                {
-                                       log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[count],Config->addrs[count],Config->ports[count]);
+                                       log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[count],Config->addrs[count],Config->ports[count]);
                                        return ERROR;
                                }
-                               if (!BindSocket(openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
+                               if (!BindSocket(Config->openSockfd[count],client,server,Config->ports[count],Config->addrs[count]))
                                {
                                        log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
                                }
                                else
                                {
                                        /* Associate the new open port with a slot in the socket engine */
-                                       ServerInstance->SE->AddFd(openSockfd[count],true,X_LISTEN);
-                                       BoundPortCount++;
+                                       if (Config->openSockfd[count] > -1)
+                                       {
+                                               ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
+                                               BoundPortCount++;
+                                       }
                                }
                        }
                        return InitialPortCount + BoundPortCount;
@@ -218,13 +218,13 @@ int BindPorts(bool bail)
 
        for (int count = 0; count < PortCount; count++)
        {
-               if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
+               if ((Config->openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
                {
-                       log(DEBUG,"Bad fd %d binding port [%s:%d]",openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
+                       log(DEBUG,"Bad fd %d binding port [%s:%d]",Config->openSockfd[BoundPortCount],Config->addrs[count],Config->ports[count]);
                        return ERROR;
                }
 
-               if (!BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
+               if (!BindSocket(Config->openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]))
                {
                        log(DEFAULT,"Failed to bind port [%s:%d]: %s",Config->addrs[count],Config->ports[count],strerror(errno));
                }
@@ -245,3 +245,16 @@ int BindPorts(bool bail)
 
        return BoundPortCount;
 }
+
+const char* insp_ntoa(insp_inaddr n)
+{
+       static char buf[1024];
+       inet_ntop(AF_FAMILY, &n, buf, sizeof(buf));
+       return buf;
+}
+
+int insp_aton(const char* a, insp_inaddr* n)
+{
+       return inet_pton(AF_FAMILY, a, n);
+}
+