]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Allow nick!ident@ and ident@ portions in a CIDR mask if given, use match() without...
[user/henk/code/inspircd.git] / src / socket.cpp
index f9b605d3be05a2a1377d18ece0befafca5489261..2a33442fed32097c6bf2ff6a846aa3f62a2f48e6 100644 (file)
 #include "inspstring.h"
 #include "helperfuncs.h"
 #include "socketengine.h"
+#include "wildcard.h"
 #include "message.h"
 
 extern InspIRCd* ServerInstance;
 extern ServerConfig* Config;
 extern time_t TIME;
-extern int openSockfd[MAX_DESCRIPTORS];
 
-InspSocket* socket_ref[MAX_DESCRIPTORS];
+/* Used when comparing CIDR masks for the modulus bits left over */
 
-/** 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.
- */ 
-bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr)
+char inverted_bits[8] = { 0x00, /* 00000000 - 0 bits */
+                         0x80, /* 10000000 - 1 bits */
+                         0xC0, /* 11000000 - 2 bits */
+                         0xE0, /* 11100000 - 3 bits */
+                         0xF0, /* 11110000 - 4 bits */
+                         0xF8, /* 11111000 - 5 bits */
+                         0xFC, /* 11111100 - 6 bits */
+                         0xFE  /* 11111110 - 7 bits */
+};
+
+/* Match raw bytes using CIDR bit matching, used by higher level MatchCIDR() */
+bool MatchCIDRBits(unsigned char* address, unsigned char* mask, unsigned int mask_bits)
 {
-       memset(&server,0,sizeof(server));
-       insp_inaddr addy;
-       bool resolved = false;
-       char resolved_addr[128];
+       unsigned int modulus = mask_bits % 8; /* Number of whole bytes in the mask */
+       unsigned int divisor = mask_bits / 8; /* Remaining bits in the mask after whole bytes are dealt with */
 
-       if (*addr == '*')
-               *addr = 0;
+       /* We shouldnt match anything, /0 is always valid */
+       if (!mask_bits)
+               return true;
+
+       /* First compare the whole bytes, if they dont match, return false */
+       if (memcmp(address, mask, divisor))
+               return false;
+
+       /* Now if there are any remainder bits, we compare them with logic AND */
+       if (modulus)
+               if ((address[divisor] & inverted_bits[modulus]) != (mask[divisor] & inverted_bits[modulus]))
+                       /* If they dont match, return false */
+                       return false;
+
+       /* The address matches the mask, to mask_bits bits of mask */
+       return true;
+}
+
+bool MatchCIDR(const char* address, const char* cidr_mask)
+{
+       return MatchCIDR(address, cidr_mask, false);
+}
+
+/* Match CIDR strings, e.g. 127.0.0.1 to 127.0.0.0/8 or 3ffe:1:5:6::8 to 3ffe:1::0/32
+ * If you have a lot of hosts to match, youre probably better off building your mask once
+ * and then using the lower level MatchCIDRBits directly.
+ */
+bool MatchCIDR(const char* address, const char* cidr_mask, bool match_with_username)
+{
+       unsigned char addr_raw[16];
+       unsigned char mask_raw[16];
+       unsigned int bits = 0;
+       char* mask = NULL;
+
+       /* The caller is trying to match ident@<mask>/bits.
+        * Chop off the ident@ portion, use match() on it
+        * seperately.
+        */
+       if (match_with_username)
+       {
+               /* Duplicate the strings, and try to find the position
+                * of the @ symbol in each */
+               char* address_dupe = strdup(address);
+               char* cidr_dupe = strdup(cidr_mask);
+               
+               char* username_mask_pos = strchr(cidr_dupe, '@');
+               char* username_addr_pos = strchr(address_dupe, '@');
+
+               if (username_mask_pos && username_addr_pos)
+               {
+                       *username_mask_pos = *username_addr_pos = 0;
+
+                       bool result = (match(address_dupe, cidr_dupe) && MatchCIDR(username_addr_pos + 1, username_mask_pos + 1));
+
+                       free(address_dupe);
+                       free(cidr_dupe);
+
+                       return result;
+               }
+               else
+               {
+                       free(address_dupe);
+                       free(cidr_dupe);
+                       mask = strdup(cidr_mask);
+               }
+       }
+       else
+       {
+               mask = strdup(cidr_mask);
+       }
+
+       in_addr  address_in4;
+       in_addr  mask_in4;
+
+
+       char* bits_chars = strchr(mask,'/');
+
+       if (bits_chars)
+       {
+               bits = atoi(bits_chars + 1);
+               *bits_chars = 0;
+       }
+       else
+       {
+               /* No 'number of bits' field! */
+               return false;
+       }
+
+#ifdef SUPPORT_IP6LINKS
+       in6_addr address_in6;
+       in6_addr mask_in6;
 
-       if (*addr && !inet_aton(addr,&addy))
+       if (inet_pton(AF_INET6, address, &address_in6) > 0)
        {
-               /* If they gave a hostname, bind to the IP it resolves to */
-               if (CleanAndResolve(resolved_addr, addr, true))
+               if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
                {
-                       inet_aton(resolved_addr,&addy);
-                       log(DEFAULT,"Resolved binding '%s' -> '%s'",addr,resolved_addr);
-                       server.sin_addr = addy;
-                       resolved = true;
+                       memcpy(&addr_raw, &address_in6.s6_addr, 16);
+                       memcpy(&mask_raw, &mask_in6.s6_addr, 16);
+
+                       if (bits > 128)
+                               bits = 128;
                }
                else
                {
-                       log(DEFAULT,"WARNING: Could not resolve '%s' to an IP for binding to on port %d",addr,port);
+                       free(mask);
                        return false;
                }
        }
-       server.sin_family = AF_INET;
-       if (!resolved)
+       else
+#endif
+       if (inet_pton(AF_INET, address, &address_in4) > 0)
        {
-               if (!*addr)
+               if (inet_pton(AF_INET, mask, &mask_in4) > 0)
                {
-                       server.sin_addr.s_addr = htonl(INADDR_ANY);
+                       memcpy(&addr_raw, &address_in4.s_addr, 4);
+                       memcpy(&mask_raw, &mask_in4.s_addr, 4);
+
+                       if (bits > 32)
+                               bits = 32;
                }
                else
                {
-                       server.sin_addr = addy;
+                       free(mask);
+                       return false;
                }
        }
+       else
+       {
+               free(mask);
+               return false;
+       }
+
+       free(mask);
+       return MatchCIDRBits(addr_raw, mask_raw, bits);
+}
+
+/** This will bind a socket to a port. It works for UDP/TCP.
+ * 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;
+
+       if (*addr == '*')
+               *addr = 0;
+
+       if ((*addr) && (insp_aton(addr,&addy) < 1))
+       {
+               log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
+               return false;;
+       }
+
+#ifdef IPV6
+       server.sin6_family = AF_FAMILY;
+#else
+       server.sin_family = AF_FAMILY;
+#endif
+       if (!*addr)
+       {
+#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;
@@ -104,7 +254,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);
@@ -122,7 +272,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)))
                {
@@ -167,20 +317,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;
@@ -220,13 +373,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));
                }
@@ -247,3 +400,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);
+}
+