bool irc::sockets::cidr_mask::match(const irc::sockets::sockaddrs& addr) const { if (addr.family() != type) return false; irc::sockets::cidr_mask tmp(addr, length); return tmp == *this; }
static void sa2cidr(irc::sockets::cidr_mask& cidr, const irc::sockets::sockaddrs& sa, unsigned char range) { const unsigned char* base; unsigned char target_byte; memset(cidr.bits, 0, sizeof(cidr.bits)); cidr.type = sa.family(); switch (cidr.type) { case AF_UNIX: // XXX: UNIX sockets don't support CIDR. This fix is non-ideal but I can't // really think of another way to handle it. cidr.length = 0; return; case AF_INET: cidr.length = range > 32 ? 32 : range; target_byte = sizeof(sa.in4.sin_addr); base = (unsigned char*)&sa.in4.sin_addr; break; case AF_INET6: cidr.length = range > 128 ? 128 : range; target_byte = sizeof(sa.in6.sin6_addr); base = (unsigned char*)&sa.in6.sin6_addr; break; default: // If we have reached this point then we have encountered a bug. ServerInstance->Logs.Log("SOCKET", LOG_DEBUG, "BUG: sa2cidr(): socket type %d is unknown!", cidr.type); cidr.length = 0; return; } unsigned int border = cidr.length / 8; unsigned int bitmask = (0xFF00 >> (range & 7)) & 0xFF; for(unsigned int i=0; i < target_byte; i++) { if (i < border) cidr.bits[i] = base[i]; else if (i == border) cidr.bits[i] = base[i] & bitmask; else return; } }
bool irc::sockets::sockaddrs::operator==(const irc::sockets::sockaddrs& other) const { if (family() != other.family()) return false; switch (family()) { case AF_INET: return (in4.sin_port == other.in4.sin_port) && (in4.sin_addr.s_addr == other.in4.sin_addr.s_addr); case AF_INET6: return (in6.sin6_port == other.in6.sin6_port) && !memcmp(in6.sin6_addr.s6_addr, other.in6.sin6_addr.s6_addr, 16); case AF_UNIX: return !strcmp(un.sun_path, other.un.sun_path); } // If we have reached this point then we have encountered a bug. ServerInstance->Logs.Log("SOCKET", LOG_DEBUG, "BUG: irc::sockets::sockaddrs::operator==(): socket type %d is unknown!", family()); return !memcmp(this, &other, sizeof(*this)); }