+/** 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.
+ */
+int ipmask_check(const struct irc_in_addr *addr, const struct irc_in_addr *mask, unsigned char bits)
+{
+ int k;
+
+ for (k = 0; k < 8; k++) {
+ if (bits < 16)
+ return !(htons(addr->in6_16[k] ^ mask->in6_16[k]) >> (16-bits));
+ if (addr->in6_16[k] != mask->in6_16[k])
+ return 0;
+ if (!(bits -= 16))
+ return 1;
+ }
+ return -1;
+}
+
+/** Convert IP addresses to canonical form for comparison. 6to4 and Teredo addresses
+ * are converted to IPv4 addresses. All other addresses are left alone.
+ * @param[out] out Receives canonical format for address.
+ * @param[in] in IP address to canonicalize.
+ */
+void ip_canonicalize_tunnel(struct irc_in_addr *out, const struct irc_in_addr *in)
+{
+ if (in->in6_16[0] == htons(0x2002)) { /* 6to4 */
+ out->in6_16[0] = out->in6_16[1] = out->in6_16[2] = 0;
+ out->in6_16[3] = out->in6_16[4] = 0;
+ out->in6_16[5] = 0xffff;
+ out->in6_16[6] = in->in6_16[1];
+ out->in6_16[7] = in->in6_16[2];
+ } else if(in->in6_16[0] == htons(0x2001) && in->in6_16[1] == 0) { /* Teredo */
+ out->in6_16[0] = out->in6_16[1] = out->in6_16[2] = 0;
+ out->in6_16[3] = out->in6_16[4] = 0;
+ out->in6_16[5] = 0xffff;
+ out->in6_16[6] = ~(in->in6_16[6]);
+ out->in6_16[7] = ~(in->in6_16[7]);
+ } else
+ memcpy(out, in, sizeof(*out));
+}
+