-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-
-static int ipmask_parse_ipv4(const char *in, struct in_addr *out)
-{
- int class;
- char ipname[16];
- int ad[4] = { 0 };
- int bits = 0;
-
- class = sscanf(in, "%d.%d.%d.%d/%d", &ad[0], &ad[1], &ad[2], &ad[3], &bits);
- if (class != 5)
- bits = class * 8;
- ircd_snprintf(0, ipname, sizeof(ipname), "%d.%d.%d.%d", ad[0], ad[1], ad[2], ad[3]);
- out->s_addr = inet_addr(ipname);
- return bits;
-}
-
-int ipmask_parse(const char *in, struct irc_in_addr *mask, unsigned char *bits_ptr)
-{
- struct in_addr ipv4;
- char *p;
- int bits = 0;
-
- if (check_if_ipmask(in)) {
- bits = ipmask_parse_ipv4(in, &ipv4);
- mask->in6_16[0] = mask->in6_16[1] = mask->in6_16[2] = 0;
- mask->in6_16[3] = mask->in6_16[4] = mask->in6_16[5] = 0;
- memcpy(&mask->in6_16[6], &ipv4.s_addr, sizeof(ipv4.s_addr));
- bits += 96;
- } else {
- if (!(p = strchr(in, '/')))
- bits = 128;
- else
- *p = 0;
- if (!ircd_aton(mask, in)) {
- if (p)
- *p = '/';
- return 0;
- }
- if (p) {
- bits = atoi(p + 1);
- *p = '/';
- }
- }
-
- if (bits_ptr)
- *bits_ptr = bits;
- return 1;
-}
-
+/** Test whether an address matches the most significant bits of a mask.
+ * @param[in] addr Address to test.
+ * @param[in] mask Address to test against.
+ * @param[in] bits Number of bits to test.
+ * @return 0 on mismatch, 1 if bits < 128 and all bits match; -1 if
+ * bits == 128 and all bits match.
+ */