Beispiel #1
0
	void ip_filter::add_rule(address first, address last, int flags)
	{
		if (first.is_v4())
		{
			assert(last.is_v4());
			m_filter4.add_rule(first.to_v4(), last.to_v4(), flags);
		}
		else if (first.is_v6())
		{
			assert(last.is_v6());
			m_filter6.add_rule(first.to_v6(), last.to_v6(), flags);
		}
		else
			assert(false);
	}
Beispiel #2
0
	int ip_filter::access(address const& addr) const
	{
		if (addr.is_v4())
			return m_filter4.access(addr.to_v4());
		assert(addr.is_v6());
		return m_filter6.access(addr.to_v6());
	}
Beispiel #3
0
	void ip_filter::add_rule(address first, address last, boost::uint32_t flags)
	{
		if (first.is_v4())
		{
			TORRENT_ASSERT(last.is_v4());
			m_filter4.add_rule(first.to_v4().to_bytes(), last.to_v4().to_bytes(), flags);
		}
#if TORRENT_USE_IPV6
		else if (first.is_v6())
		{
			TORRENT_ASSERT(last.is_v6());
			m_filter6.add_rule(first.to_v6().to_bytes(), last.to_v6().to_bytes(), flags);
		}
#endif
		else
			TORRENT_ASSERT(false);
	}
Beispiel #4
0
	int ip_filter::access(address const& addr) const
	{
		if (addr.is_v4())
			return m_filter4.access(addr.to_v4().to_bytes());
#if TORRENT_USE_IPV6
		TORRENT_ASSERT(addr.is_v6());
		return m_filter6.access(addr.to_v6().to_bytes());
#else
		return 0;
#endif
	}
	// returns the number of bits in that differ from the right
	// between the addresses.
	int cidr_distance(address const& a1, address const& a2)
	{
		if (a1.is_v4() && a2.is_v4())
		{
			// both are v4
			address_v4::bytes_type b1 = a1.to_v4().to_bytes();
			address_v4::bytes_type b2 = a2.to_v4().to_bytes();
			return address_v4::bytes_type::static_size * 8
				- common_bits(b1.c_array(), b2.c_array(), b1.size());
		}
	
		address_v6::bytes_type b1;
		address_v6::bytes_type b2;
		if (a1.is_v4()) b1 = address_v6::v4_mapped(a1.to_v4()).to_bytes();
		else b1 = a1.to_v6().to_bytes();
		if (a2.is_v4()) b2 = address_v6::v4_mapped(a2.to_v4()).to_bytes();
		else b2 = a2.to_v6().to_bytes();
		return address_v6::bytes_type::static_size * 8
			- common_bits(b1.c_array(), b2.c_array(), b1.size());
	}
Beispiel #6
0
    // returns the number of bits in that differ from the right
    // between the addresses. The larger number, the further apart
    // the IPs are
    int cidr_distance(address const& a1, address const& a2)
    {
#if LIBED2K_USE_IPV6
        if (a1.is_v4() && a2.is_v4())
        {
#endif
            // both are v4
            address_v4::bytes_type b1 = a1.to_v4().to_bytes();
            address_v4::bytes_type b2 = a2.to_v4().to_bytes();
            return address_v4::bytes_type().size() * 8 - common_bits(b1.data(), b2.data(), b1.size());
#if LIBED2K_USE_IPV6
        }

        address_v6::bytes_type b1;
        address_v6::bytes_type b2;
        if (a1.is_v4()) b1 = address_v6::v4_mapped(a1.to_v4()).to_bytes();
        else b1 = a1.to_v6().to_bytes();
        if (a2.is_v4()) b2 = address_v6::v4_mapped(a2.to_v4()).to_bytes();
        else b2 = a2.to_v6().to_bytes();
        return address_v6::bytes_type().size() * 8 - common_bits(b1.data(), b2.data(), b1.size());
#endif
    }
Beispiel #7
0
		void write_address(address const& a, OutIt& out)
		{
			if (a.is_v4())
			{
				write_uint32(a.to_v4().to_ulong(), out);
			}
			else if (a.is_v6())
			{
				address_v6::bytes_type bytes
					= a.to_v6().to_bytes();
				std::copy(bytes.begin(), bytes.end(), out);
			}
		}
Beispiel #8
0
address mask_address(const address& addrIn, uint8_t prefixLen) {
    if (addrIn.is_v4()) {
        prefixLen = std::min<uint8_t>(prefixLen, 32);
        uint32_t mask = get_subnet_mask_v4(prefixLen);
        return address_v4(addrIn.to_v4().to_ulong() & mask);
    }
    struct in6_addr mask;
    struct in6_addr addr6;
    prefixLen = std::min<uint8_t>(prefixLen, 128);
    compute_ipv6_subnet(addrIn.to_v6(), prefixLen, &mask, &addr6);
    address_v6::bytes_type data;
    std::memcpy(data.data(), &addr6, sizeof(addr6));
    return address_v6(data);
}
Beispiel #9
0
    std::string address_to_bytes(address const& a)
    {
#if LIBED2K_USE_IPV6
        if (a.is_v6())
        {
            address_v6::bytes_type b = a.to_v6().to_bytes();
            return std::string((char*)&b[0], b.size());
        }
        else
#endif
        {
            address_v4::bytes_type b = a.to_v4().to_bytes();
            return std::string((char*)&b[0], b.size());
        }
    }
Beispiel #10
0
		void write_address(address const& a, OutIt&& out)
		{
#if TORRENT_USE_IPV6
			if (a.is_v4())
			{
#endif
				write_uint32(a.to_v4().to_ulong(), out);
#if TORRENT_USE_IPV6
			}
			else if (a.is_v6())
			{
				for (auto b : a.to_v6().to_bytes())
					write_uint8(b, out);
			}
#endif
		}
Beispiel #11
0
		void write_address(address const& a, OutIt& out)
		{
#if TORRENT_USE_IPV6
			if (a.is_v4())
			{
#endif
				write_uint32(a.to_v4().to_ulong(), out);
#if TORRENT_USE_IPV6
			}
			else if (a.is_v6())
			{
				address_v6::bytes_type bytes
					= a.to_v6().to_bytes();
				std::copy(bytes.begin(), bytes.end(), out);
			}
#endif
		}
Beispiel #12
0
		void write_address(address const& a, OutIt& out)
		{
#if TORRENT_USE_IPV6
			if (a.is_v4())
			{
#endif
				write_uint32(a.to_v4().to_ulong(), out);
#if TORRENT_USE_IPV6
			}
			else if (a.is_v6())
			{
				typedef address_v6::bytes_type bytes_t;
				bytes_t bytes = a.to_v6().to_bytes();
				for (bytes_t::iterator i = bytes.begin()
					, end(bytes.end()); i != end; ++i)
					write_uint8(*i, out);
			}
#endif
		}