attempt to fix #212. Store 4 bytes and 16 bytes arrays instead of in_addr in ip-filter

This commit is contained in:
Arvid Norberg 2007-12-24 21:49:46 +00:00
parent d5748419c1
commit 81cf247cda
2 changed files with 25 additions and 24 deletions

View File

@ -76,9 +76,9 @@ namespace detail
template<class Addr> template<class Addr>
Addr zero() Addr zero()
{ {
typename Addr::bytes_type zero; Addr zero;
std::fill(zero.begin(), zero.end(), 0); std::fill(zero.begin(), zero.end(), 0);
return Addr(zero); return zero;
} }
template<> template<>
@ -87,8 +87,8 @@ namespace detail
template<class Addr> template<class Addr>
Addr plus_one(Addr const& a) Addr plus_one(Addr const& a)
{ {
typename Addr::bytes_type tmp(a.to_bytes()); Addr tmp(a);
typedef typename Addr::bytes_type::reverse_iterator iter; typedef typename Addr::reverse_iterator iter;
for (iter i = tmp.rbegin() for (iter i = tmp.rbegin()
, end(tmp.rend()); i != end; ++i) , end(tmp.rend()); i != end; ++i)
{ {
@ -99,7 +99,7 @@ namespace detail
} }
*i = 0; *i = 0;
} }
return Addr(tmp); return tmp;
} }
inline boost::uint16_t plus_one(boost::uint16_t val) { return val + 1; } inline boost::uint16_t plus_one(boost::uint16_t val) { return val + 1; }
@ -107,8 +107,8 @@ namespace detail
template<class Addr> template<class Addr>
Addr minus_one(Addr const& a) Addr minus_one(Addr const& a)
{ {
typename Addr::bytes_type tmp(a.to_bytes()); Addr tmp(a);
typedef typename Addr::bytes_type::reverse_iterator iter; typedef typename Addr::reverse_iterator iter;
for (iter i = tmp.rbegin() for (iter i = tmp.rbegin()
, end(tmp.rend()); i != end; ++i) , end(tmp.rend()); i != end; ++i)
{ {
@ -119,7 +119,7 @@ namespace detail
} }
*i = (std::numeric_limits<typename iter::value_type>::max)(); *i = (std::numeric_limits<typename iter::value_type>::max)();
} }
return Addr(tmp); return tmp;
} }
inline boost::uint16_t minus_one(boost::uint16_t val) { return val - 1; } inline boost::uint16_t minus_one(boost::uint16_t val) { return val - 1; }
@ -127,9 +127,9 @@ namespace detail
template<class Addr> template<class Addr>
Addr max_addr() Addr max_addr()
{ {
typename Addr::bytes_type tmp; Addr tmp;
std::fill(tmp.begin(), tmp.end() std::fill(tmp.begin(), tmp.end()
, (std::numeric_limits<typename Addr::bytes_type::value_type>::max)()); , (std::numeric_limits<typename Addr::value_type>::max)());
return Addr(tmp); return Addr(tmp);
} }
@ -220,23 +220,24 @@ namespace detail
return i->access; return i->access;
} }
std::vector<ip_range<Addr> > export_filter() const template <class ExternalAddressType>
std::vector<ip_range<ExternalAddressType> > export_filter() const
{ {
std::vector<ip_range<Addr> > ret; std::vector<ip_range<ExternalAddressType> > ret;
ret.reserve(m_access_list.size()); ret.reserve(m_access_list.size());
for (typename range_t::const_iterator i = m_access_list.begin() for (typename range_t::const_iterator i = m_access_list.begin()
, end(m_access_list.end()); i != end;) , end(m_access_list.end()); i != end;)
{ {
ip_range<Addr> r; ip_range<ExternalAddressType> r;
r.first = i->start; r.first = ExternalAddressType(i->start);
r.flags = i->access; r.flags = i->access;
++i; ++i;
if (i == end) if (i == end)
r.last = max_addr<Addr>(); r.last = ExternalAddressType(max_addr<Addr>());
else else
r.last = minus_one(i->start); r.last = ExternalAddressType(minus_one(i->start));
ret.push_back(r); ret.push_back(r);
} }
@ -288,8 +289,8 @@ public:
private: private:
detail::filter_impl<address_v4> m_filter4; detail::filter_impl<address_v4::bytes_type> m_filter4;
detail::filter_impl<address_v6> m_filter6; detail::filter_impl<address_v6::bytes_type> m_filter6;
}; };
class TORRENT_EXPORT port_filter class TORRENT_EXPORT port_filter

View File

@ -44,12 +44,12 @@ namespace libtorrent
if (first.is_v4()) if (first.is_v4())
{ {
TORRENT_ASSERT(last.is_v4()); TORRENT_ASSERT(last.is_v4());
m_filter4.add_rule(first.to_v4(), last.to_v4(), flags); m_filter4.add_rule(first.to_v4().to_bytes(), last.to_v4().to_bytes(), flags);
} }
else if (first.is_v6()) else if (first.is_v6())
{ {
TORRENT_ASSERT(last.is_v6()); TORRENT_ASSERT(last.is_v6());
m_filter6.add_rule(first.to_v6(), last.to_v6(), flags); m_filter6.add_rule(first.to_v6().to_bytes(), last.to_v6().to_bytes(), flags);
} }
else else
TORRENT_ASSERT(false); TORRENT_ASSERT(false);
@ -58,15 +58,15 @@ namespace libtorrent
int ip_filter::access(address const& addr) const int ip_filter::access(address const& addr) const
{ {
if (addr.is_v4()) if (addr.is_v4())
return m_filter4.access(addr.to_v4()); return m_filter4.access(addr.to_v4().to_bytes());
TORRENT_ASSERT(addr.is_v6()); TORRENT_ASSERT(addr.is_v6());
return m_filter6.access(addr.to_v6()); return m_filter6.access(addr.to_v6().to_bytes());
} }
ip_filter::filter_tuple_t ip_filter::export_filter() const ip_filter::filter_tuple_t ip_filter::export_filter() const
{ {
return boost::make_tuple(m_filter4.export_filter() return boost::make_tuple(m_filter4.export_filter<address_v4>()
, m_filter6.export_filter()); , m_filter6.export_filter<address_v6>());
} }
void port_filter::add_rule(boost::uint16_t first, boost::uint16_t last, int flags) void port_filter::add_rule(boost::uint16_t first, boost::uint16_t last, int flags)