]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Extra safety checks
[user/henk/code/inspircd.git] / src / socket.cpp
index d1eca25fce491930dc7222f4dbccde6a4c826417..276ccb2702e756fbaf46c669e32d55a31250d611 100644 (file)
@@ -27,6 +27,107 @@ extern InspIRCd* ServerInstance;
 extern ServerConfig* Config;
 extern time_t TIME;
 
+/* Used when comparing CIDR masks for the modulus bits left over */
+
+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)
+{
+       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 */
+
+       /* 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;
+}
+
+/* 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)
+{
+       unsigned char addr_raw[16];
+       unsigned char mask_raw[16];
+       unsigned int bits = 0;
+       char* 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 (inet_pton(AF_INET6, address, &address_in6) > 0)
+       {
+               if (inet_pton(AF_INET6, mask, &mask_in6) > 0)
+               {
+                       memcpy(&addr_raw, &address_in6.s6_addr, 16);
+                       memcpy(&mask_raw, &mask_in6.s6_addr, 16);
+               }
+               else
+               {
+                       free(mask);
+                       return false;
+               }
+       }
+       else
+#endif
+       if (inet_pton(AF_INET, address, &address_in4) > 0)
+       {
+               if (inet_pton(AF_INET, mask, &mask_in4) > 0)
+               {
+                       memcpy(&addr_raw, &address_in4.s_addr, 4);
+                       memcpy(&mask_raw, &mask_in4.s_addr, 4);
+               }
+               else
+               {
+                       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'.
@@ -39,7 +140,7 @@ bool BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server, int port
        if (*addr == '*')
                *addr = 0;
 
-       if ((*addr) && (inet_aton(addr,&addy) < 1))
+       if ((*addr) && (insp_aton(addr,&addy) < 1))
        {
                log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
                return false;;