cut out more IPv6 code if not supported

This commit is contained in:
Arvid Norberg 2009-04-04 16:59:53 +00:00
parent c6f2aa129c
commit b5bb51c6af
10 changed files with 112 additions and 37 deletions

View File

@ -289,7 +289,9 @@ public:
private:
detail::filter_impl<address_v4::bytes_type> m_filter4;
#if TORRENT_USE_IPV6
detail::filter_impl<address_v6::bytes_type> m_filter6;
#endif
};
class TORRENT_EXPORT port_filter

View File

@ -144,39 +144,18 @@ namespace libtorrent
}
return ret;
}
/*
inline std::ostream& print_address(std::ostream& os, address const& addr)
{
error_code ec;
std::string a = addr.to_string(ec);
if (ec) return os;
os << a;
return os;
}
inline std::ostream& print_endpoint(std::ostream& os, tcp::endpoint const& ep)
{
address const& addr = ep.address();
error_code ec;
std::string a = addr.to_string(ec);
if (ec) return os;
if (addr.is_v6())
os << "[" << a << "]:";
else
os << a << ":";
os << ep.port();
return os;
}
*/
namespace detail
{
template<class OutIt>
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())
{
@ -184,6 +163,7 @@ namespace libtorrent
= a.to_v6().to_bytes();
std::copy(bytes.begin(), bytes.end(), out);
}
#endif
}
template<class InIt>
@ -193,6 +173,7 @@ namespace libtorrent
return address_v4(ip);
}
#if TORRENT_USE_IPV6
template<class InIt>
address read_v6_address(InIt& in)
{
@ -203,6 +184,7 @@ namespace libtorrent
*i = read_uint8(in);
return address_v6(bytes);
}
#endif
template<class Endpoint, class OutIt>
void write_endpoint(Endpoint const& e, OutIt& out)
@ -219,6 +201,7 @@ namespace libtorrent
return Endpoint(addr, port);
}
#if TORRENT_USE_IPV6
template<class Endpoint, class InIt>
Endpoint read_v6_endpoint(InIt& in)
{
@ -226,8 +209,10 @@ namespace libtorrent
int port = read_uint16(in);
return Endpoint(addr, port);
}
#endif
}
#if TORRENT_USE_IPV6
struct v6only
{
v6only(bool enable): m_value(enable) {}
@ -241,6 +226,7 @@ namespace libtorrent
size_t size(Protocol const&) const { return sizeof(m_value); }
int m_value;
};
#endif
#ifdef TORRENT_WINDOWS

View File

@ -54,7 +54,14 @@ namespace libtorrent
udp_socket(io_service& ios, callback_t const& c, connection_queue& cc);
~udp_socket();
bool is_open() const { return m_ipv4_sock.is_open() || m_ipv6_sock.is_open(); }
bool is_open() const
{
return m_ipv4_sock.is_open()
#if TORRENT_USE_IPV6
|| m_ipv6_sock.is_open()
#endif
;
}
io_service& get_io_service() { return m_ipv4_sock.get_io_service(); }
void send(udp::endpoint const& ep, char const* p, int len, error_code& ec);
@ -92,11 +99,15 @@ namespace libtorrent
mutable mutex_t m_mutex;
udp::socket m_ipv4_sock;
udp::socket m_ipv6_sock;
udp::endpoint m_v4_ep;
udp::endpoint m_v6_ep;
char m_v4_buf[1600];
#if TORRENT_USE_IPV6
udp::socket m_ipv6_sock;
udp::endpoint m_v6_ep;
char m_v6_buf[1600];
#endif
int m_bind_port;
char m_outstanding;

View File

@ -161,10 +161,14 @@ namespace libtorrent
error_code ec;
std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
#if TORRENT_USE_IPV6
if (multicast_endpoint.address().is_v4())
#endif
open_multicast_socket(ios, address_v4::any(), loopback);
#if TORRENT_USE_IPV6
else
open_multicast_socket(ios, address_v6::any(), loopback);
#endif
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
, end(interfaces.end()); i != end; ++i)
@ -189,10 +193,7 @@ namespace libtorrent
error_code ec;
boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
if (addr.is_v4())
s->open(udp::v4(), ec);
else
s->open(udp::v6(), ec);
s->open(addr.is_v4() ? udp::v4() : udp::v6(), ec);
if (ec) return;
s->set_option(datagram_socket::reuse_address(true), ec);
if (ec) return;

View File

@ -422,6 +422,7 @@ namespace libtorrent
peers_ent = 0;
}
#if TORRENT_USE_IPV6
entry const* ipv6_peers = e.find_key("peers6");
if (ipv6_peers && ipv6_peers->type() == entry::string_t)
{
@ -444,6 +445,9 @@ namespace libtorrent
{
ipv6_peers = 0;
}
#else
entry const* ipv6_peers = 0;
#endif
if (peers_ent == 0 && ipv6_peers == 0)
{
@ -464,8 +468,10 @@ namespace libtorrent
char const* p = &ip[0];
if (ip.size() == address_v4::bytes_type::static_size)
external_ip = detail::read_v4_address(p);
#if TORRENT_USE_IPV6
else if (ip.size() == address_v6::bytes_type::static_size)
external_ip = detail::read_v6_address(p);
#endif
}
entry const* complete_ent = e.find_key("complete");

View File

@ -45,11 +45,13 @@ namespace libtorrent
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);
}
@ -58,14 +60,23 @@ namespace libtorrent
{
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
}
ip_filter::filter_tuple_t ip_filter::export_filter() const
{
#if TORRENT_USE_IPV6
return boost::make_tuple(m_filter4.export_filter<address_v4>()
, m_filter6.export_filter<address_v6>());
#else
return boost::make_tuple(m_filter4.export_filter<address_v4>()
, std::vector<ip_range<address_v6> >());
#endif
}
void port_filter::add_rule(boost::uint16_t first, boost::uint16_t last, int flags)

View File

@ -98,8 +98,10 @@ namespace
char const* in = e->string_ptr();
if (e->string_length() == 6)
epl.push_back(read_v4_endpoint<EndpointType>(in));
#if TORRENT_USE_IPV6
else if (e->string_length() == 18)
epl.push_back(read_v6_endpoint<EndpointType>(in));
#endif
}
}
@ -118,8 +120,10 @@ namespace
std::string::const_iterator in = p.begin();
if (p.size() == 6)
epl.push_back(read_v4_endpoint<EndpointType>(in));
#if TORRENT_USE_IPV6
else if (p.size() == 18)
epl.push_back(read_v6_endpoint<EndpointType>(in));
#endif
}
}
@ -668,9 +672,11 @@ namespace libtorrent { namespace dht
if (p->string_length() == 6 + 20)
m.nodes.push_back(libtorrent::dht::node_entry(
id, read_v4_endpoint<udp::endpoint>(in)));
#if TORRENT_USE_IPV6
else if (p->string_length() == 18 + 20)
m.nodes.push_back(libtorrent::dht::node_entry(
id, read_v6_endpoint<udp::endpoint>(in)));
#endif
}
#ifdef TORRENT_DHT_VERBOSE_LOGGING
log_line << " n2: " << m.nodes.size();

View File

@ -641,6 +641,7 @@ namespace aux {
s.sock.reset(new socket_acceptor(m_io_service));
s.sock->open(ep.protocol(), ec);
s.sock->set_option(socket_acceptor::reuse_address(true), ec);
#if TORRENT_USE_IPV6
if (ep.protocol() == tcp::v6())
{
s.sock->set_option(v6only(v6_only), ec);
@ -649,6 +650,7 @@ namespace aux {
s.sock->set_option(v6_protection_level(PROTECTION_LEVEL_UNRESTRICTED), ec);
#endif
}
#endif
s.sock->bind(ep, ec);
while (ec && retries > 0)
{

View File

@ -48,7 +48,9 @@ udp_socket::udp_socket(asio::io_service& ios, udp_socket::callback_t const& c
, connection_queue& cc)
: m_callback(c)
, m_ipv4_sock(ios)
#if TORRENT_USE_IPV6
, m_ipv6_sock(ios)
#endif
, m_bind_port(0)
, m_outstanding(0)
, m_socks5_sock(ios)
@ -89,7 +91,7 @@ void udp_socket::send(udp::endpoint const& ep, char const* p, int len, error_cod
{
CHECK_MAGIC;
// if the sockets are closed, the udp_socket is closing too
if (!m_ipv4_sock.is_open() && !m_ipv6_sock.is_open()) return;
if (!is_open()) return;
if (m_tunnel_packets)
{
@ -98,10 +100,14 @@ void udp_socket::send(udp::endpoint const& ep, char const* p, int len, error_cod
return;
}
#if TORRENT_USE_IPV6
if (ep.address().is_v4() && m_ipv4_sock.is_open())
#endif
m_ipv4_sock.send_to(asio::buffer(p, len), ep, 0, ec);
#if TORRENT_USE_IPV6
else
m_ipv6_sock.send_to(asio::buffer(p, len), ep, 0, ec);
#endif
}
void udp_socket::on_read(udp::socket* s, error_code const& e, std::size_t bytes_transferred)
@ -134,10 +140,16 @@ void udp_socket::on_read(udp::socket* s, error_code const& e, std::size_t bytes_
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
#if TORRENT_USE_IPV6
if (s == &m_ipv4_sock)
#endif
m_callback(e, m_v4_ep, 0, 0);
#if TORRENT_USE_IPV6
else
m_callback(e, m_v6_ep, 0, 0);
#endif
#ifndef BOOST_NO_EXCEPTIONS
} catch(std::exception&) {}
#endif
@ -164,19 +176,26 @@ void udp_socket::on_read(udp::socket* s, error_code const& e, std::size_t bytes_
if (m_abort) return;
#if TORRENT_USE_IPV6
if (s == &m_ipv4_sock)
#endif
s->async_receive_from(asio::buffer(m_v4_buf, sizeof(m_v4_buf))
, m_v4_ep, boost::bind(&udp_socket::on_read, this, s, _1, _2));
#if TORRENT_USE_IPV6
else
s->async_receive_from(asio::buffer(m_v6_buf, sizeof(m_v6_buf))
, m_v6_ep, boost::bind(&udp_socket::on_read, this, s, _1, _2));
#endif
++m_outstanding;
return;
}
#if TORRENT_USE_IPV6
if (s == &m_ipv4_sock)
#endif
{
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
@ -202,6 +221,7 @@ void udp_socket::on_read(udp::socket* s, error_code const& e, std::size_t bytes_
s->async_receive_from(asio::buffer(m_v4_buf, sizeof(m_v4_buf))
, m_v4_ep, boost::bind(&udp_socket::on_read, this, s, _1, _2));
}
#if TORRENT_USE_IPV6
else
{
#ifndef BOOST_NO_EXCEPTIONS
@ -229,6 +249,7 @@ void udp_socket::on_read(udp::socket* s, error_code const& e, std::size_t bytes_
s->async_receive_from(asio::buffer(m_v6_buf, sizeof(m_v6_buf))
, m_v6_ep, boost::bind(&udp_socket::on_read, this, s, _1, _2));
}
#endif
++m_outstanding;
}
@ -250,10 +271,14 @@ void udp_socket::wrap(udp::endpoint const& ep, char const* p, int len, error_cod
iovec[0] = asio::const_buffer(header, h - header);
iovec[1] = asio::const_buffer(p, len);
#if TORRENT_USE_IPV6
if (m_proxy_addr.address().is_v4() && m_ipv4_sock.is_open())
#endif
m_ipv4_sock.send_to(iovec, m_proxy_addr, 0, ec);
#if TORRENT_USE_IPV6
else
m_ipv6_sock.send_to(iovec, m_proxy_addr, 0, ec);
#endif
}
// unwrap the UDP packet from the SOCKS5 header
@ -279,11 +304,13 @@ void udp_socket::unwrap(error_code const& e, char const* buf, int size)
// IPv4
sender = read_v4_endpoint<udp::endpoint>(p);
}
#if TORRENT_USE_IPV6
else if (atyp == 4)
{
// IPv6
sender = read_v6_endpoint<udp::endpoint>(p);
}
#endif
else
{
// domain name not supported
@ -300,7 +327,9 @@ void udp_socket::close()
error_code ec;
m_ipv4_sock.close(ec);
#if TORRENT_USE_IPV6
m_ipv6_sock.close(ec);
#endif
m_socks5_sock.close(ec);
m_resolver.cancel();
m_abort = true;
@ -325,7 +354,9 @@ void udp_socket::bind(udp::endpoint const& ep, error_code& ec)
mutex_t::scoped_lock l(m_mutex);
if (m_ipv4_sock.is_open()) m_ipv4_sock.close(ec);
#if TORRENT_USE_IPV6
if (m_ipv6_sock.is_open()) m_ipv6_sock.close(ec);
#endif
if (ep.address().is_v4())
{
@ -336,6 +367,7 @@ void udp_socket::bind(udp::endpoint const& ep, error_code& ec)
m_ipv4_sock.async_receive_from(asio::buffer(m_v4_buf, sizeof(m_v4_buf))
, m_v4_ep, boost::bind(&udp_socket::on_read, this, &m_ipv4_sock, _1, _2));
}
#if TORRENT_USE_IPV6
else
{
m_ipv6_sock.set_option(v6only(true), ec);
@ -345,6 +377,7 @@ void udp_socket::bind(udp::endpoint const& ep, error_code& ec)
m_ipv6_sock.async_receive_from(asio::buffer(m_v6_buf, sizeof(m_v6_buf))
, m_v6_ep, boost::bind(&udp_socket::on_read, this, &m_ipv6_sock, _1, _2));
}
#endif
++m_outstanding;
m_bind_port = ep.port();
}
@ -357,7 +390,9 @@ void udp_socket::bind(int port)
error_code ec;
if (m_ipv4_sock.is_open()) m_ipv4_sock.close(ec);
#if TORRENT_USE_IPV6
if (m_ipv6_sock.is_open()) m_ipv6_sock.close(ec);
#endif
m_ipv4_sock.open(udp::v4(), ec);
if (!ec)
@ -367,6 +402,7 @@ void udp_socket::bind(int port)
, m_v4_ep, boost::bind(&udp_socket::on_read, this, &m_ipv4_sock, _1, _2));
++m_outstanding;
}
#if TORRENT_USE_IPV6
m_ipv6_sock.open(udp::v6(), ec);
if (!ec)
{
@ -376,6 +412,7 @@ void udp_socket::bind(int port)
, m_v6_ep, boost::bind(&udp_socket::on_read, this, &m_ipv6_sock, _1, _2));
++m_outstanding;
}
#endif
m_bind_port = port;
}

View File

@ -102,15 +102,17 @@ namespace libtorrent { namespace
std::string& pla = pex["added"].string();
std::string& pld = pex["dropped"].string();
std::string& plf = pex["added.f"].string();
std::string& pla6 = pex["added6"].string();
std::string& pld6 = pex["dropped6"].string();
std::string& plf6 = pex["added6.f"].string();
std::back_insert_iterator<std::string> pla_out(pla);
std::back_insert_iterator<std::string> pld_out(pld);
std::back_insert_iterator<std::string> plf_out(plf);
#if TORRENT_USE_IPV6
std::string& pla6 = pex["added6"].string();
std::string& pld6 = pex["dropped6"].string();
std::string& plf6 = pex["added6.f"].string();
std::back_insert_iterator<std::string> pla6_out(pla6);
std::back_insert_iterator<std::string> pld6_out(pld6);
std::back_insert_iterator<std::string> plf6_out(plf6);
#endif
std::set<tcp::endpoint> dropped;
m_old_peers.swap(dropped);
@ -149,11 +151,13 @@ namespace libtorrent { namespace
detail::write_endpoint(remote, pla_out);
detail::write_uint8(flags, plf_out);
}
#if TORRENT_USE_IPV6
else
{
detail::write_endpoint(remote, pla6_out);
detail::write_uint8(flags, plf6_out);
}
#endif
++num_added;
++m_peers_in_message;
}
@ -170,8 +174,10 @@ namespace libtorrent { namespace
{
if (i->address().is_v4())
detail::write_endpoint(*i, pld_out);
#if TORRENT_USE_IPV6
else
detail::write_endpoint(*i, pld6_out);
#endif
++m_peers_in_message;
}
@ -265,6 +271,7 @@ namespace libtorrent { namespace
}
}
#if TORRENT_USE_IPV6
lazy_entry const* p6 = pex_msg.dict_find("added6");
lazy_entry const* p6f = pex_msg.dict_find("added6.f");
if (p6 != 0
@ -288,6 +295,7 @@ namespace libtorrent { namespace
p.peer_from_tracker(adr, pid, peer_info::pex, flags);
}
}
#endif
return true;
}
@ -338,13 +346,16 @@ namespace libtorrent { namespace
pex["dropped"].string();
std::string& pla = pex["added"].string();
std::string& plf = pex["added.f"].string();
std::back_insert_iterator<std::string> pla_out(pla);
std::back_insert_iterator<std::string> plf_out(plf);
#if TORRENT_USE_IPV6
pex["dropped6"].string();
std::string& pla6 = pex["added6"].string();
std::string& plf6 = pex["added6.f"].string();
std::back_insert_iterator<std::string> pla_out(pla);
std::back_insert_iterator<std::string> plf_out(plf);
std::back_insert_iterator<std::string> pla6_out(pla6);
std::back_insert_iterator<std::string> plf6_out(plf6);
#endif
int num_added = 0;
for (torrent::peer_iterator i = m_torrent.begin()
@ -374,11 +385,13 @@ namespace libtorrent { namespace
detail::write_endpoint(remote, pla_out);
detail::write_uint8(flags, plf_out);
}
#if TORRENT_USE_IPV6
else
{
detail::write_endpoint(remote, pla6_out);
detail::write_uint8(flags, plf6_out);
}
#endif
++num_added;
}
std::vector<char> pex_msg;