Skip to content

Commit

Permalink
Merge pull request #113 from rakshasa/ipv6
Browse files Browse the repository at this point in the history
Ipv6
  • Loading branch information
rakshasa authored Oct 10, 2016
2 parents c97b462 + 6f51b39 commit 4d305e3
Show file tree
Hide file tree
Showing 31 changed files with 1,063 additions and 145 deletions.
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Jari Sundell <[email protected]>
Jari Sundell <[email protected]>
12 changes: 7 additions & 5 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
LibTorrent

Copyright (C) 2005-2014, Jari Sundell

LICENSE

GNU GPL, see COPYING. "libtorrent/src/utils/sha_fast.{cc,h}" is
Expand All @@ -15,11 +17,11 @@ compiled if the user wishes to avoid using OpenSSL.

CONTACT

Jari Sundell

Skomakerveien 33
3185 Skoppum, NORWAY

Send bug reports, suggestions and patches to
<[email protected]> or to the mailinglist.

POLLING

"libtorrent/src/torrent/poll.h" provides an abstract class for
implementing any kind of polling the client wishes to use. Currently
epoll and select based polling is included.
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ TORRENT_CHECK_EXECINFO()
TORRENT_CHECK_PTHREAD_SETNAME_NP()
TORRENT_MINCORE()

TORRENT_DISABLE_IPV6
TORRENT_DISABLE_INSTRUMENTATION

LIBTORRENT_LIBS="-ltorrent"
Expand All @@ -126,9 +125,10 @@ AC_OUTPUT([
Makefile
src/Makefile
src/torrent/Makefile
src/torrent/peer/Makefile
src/torrent/data/Makefile
src/torrent/download/Makefile
src/torrent/net/Makefile
src/torrent/peer/Makefile
src/torrent/utils/Makefile
src/data/Makefile
src/dht/Makefile
Expand Down
165 changes: 122 additions & 43 deletions rak/socket_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,11 @@ class socket_address {
const sockaddr* c_sockaddr() const { return &m_sockaddr; }
const sockaddr_in* c_sockaddr_inet() const { return &m_sockaddrInet; }

#ifdef RAK_USE_INET6
socket_address_inet6* sa_inet6() { return reinterpret_cast<socket_address_inet6*>(this); }
const socket_address_inet6* sa_inet6() const { return reinterpret_cast<const socket_address_inet6*>(this); }

sockaddr_in6* c_sockaddr_inet6() { return &m_sockaddrInet6; }
const sockaddr_in6* c_sockaddr_inet6() const { return &m_sockaddrInet6; }
#endif

// Copy a socket address which has the length 'length. Zero out any
// extranous bytes and ensure it does not go beyond the size of this
Expand All @@ -139,13 +137,11 @@ class socket_address {
union {
sockaddr m_sockaddr;
sockaddr_in m_sockaddrInet;
#ifdef RAK_USE_INET6
sockaddr_in6 m_sockaddrInet6;
#endif
};
};

// Remeber to set the AF_INET.
// Remember to set the AF_INET.

class socket_address_inet {
public:
Expand Down Expand Up @@ -184,6 +180,8 @@ class socket_address_inet {

const sockaddr* c_sockaddr() const { return reinterpret_cast<const sockaddr*>(&m_sockaddr); }
const sockaddr_in* c_sockaddr_inet() const { return &m_sockaddr; }

socket_address_inet6 to_mapped_address() const;

bool operator == (const socket_address_inet& rhs) const;
bool operator < (const socket_address_inet& rhs) const;
Expand All @@ -192,57 +190,56 @@ class socket_address_inet {
struct sockaddr_in m_sockaddr;
};

// Unique key for the address, excluding port numbers etc.
class socket_address_key {
class socket_address_inet6 {
public:
// socket_address_host_key() {}
bool is_any() const { return is_port_any() && is_address_any(); }
bool is_valid() const { return !is_port_any() && !is_address_any(); }
bool is_port_any() const { return port() == 0; }
bool is_address_any() const { return std::memcmp(&m_sockaddr.sin6_addr, &in6addr_any, sizeof(in6_addr)) == 0; }

socket_address_key(const socket_address& sa) {
*this = sa;
}
void clear() { std::memset(this, 0, sizeof(socket_address_inet6)); set_family(); }

socket_address_key& operator = (const socket_address& sa) {
if (sa.family() == 0) {
std::memset(this, 0, sizeof(socket_address_key));
uint16_t port() const { return ntohs(m_sockaddr.sin6_port); }
uint16_t port_n() const { return m_sockaddr.sin6_port; }
void set_port(uint16_t p) { m_sockaddr.sin6_port = htons(p); }
void set_port_n(uint16_t p) { m_sockaddr.sin6_port = p; }

} else if (sa.family() == socket_address::af_inet) {
// Using hardware order as we use operator < to compare when
// using inet only.
m_addr.s_addr = sa.sa_inet()->address_h();
in6_addr address() const { return m_sockaddr.sin6_addr; }
const in6_addr* address_ptr() const { return &m_sockaddr.sin6_addr; }
std::string address_str() const;
bool address_c_str(char* buf, socklen_t size) const;

} else {
// When we implement INET6 handling, embed the ipv4 address in
// the ipv6 address.
throw std::logic_error("socket_address_key(...) received an unsupported protocol family.");
}
void set_address(in6_addr a) { m_sockaddr.sin6_addr = a; }
bool set_address_str(const std::string& a) { return set_address_c_str(a.c_str()); }
bool set_address_c_str(const char* a);

return *this;
}
void set_address_any() { set_port(0); set_address(in6addr_any); }

// socket_address_key& operator = (const socket_address_key& sa) {
// }
sa_family_t family() const { return m_sockaddr.sin6_family; }
void set_family() { m_sockaddr.sin6_family = AF_INET6; }

bool operator < (const socket_address_key& sa) const {
// Compare the memory area instead.
return m_addr.s_addr < sa.m_addr.s_addr;
}
sockaddr* c_sockaddr() { return reinterpret_cast<sockaddr*>(&m_sockaddr); }
sockaddr_in6* c_sockaddr_inet6() { return &m_sockaddr; }

const sockaddr* c_sockaddr() const { return reinterpret_cast<const sockaddr*>(&m_sockaddr); }
const sockaddr_in6* c_sockaddr_inet6() const { return &m_sockaddr; }

socket_address normalize_address() const;

bool operator == (const socket_address_inet6& rhs) const;
bool operator < (const socket_address_inet6& rhs) const;

private:
union {
in_addr m_addr;
// #ifdef RAK_USE_INET6
// in_addr6 m_addr6;
// #endif
};
struct sockaddr_in6 m_sockaddr;
};

inline bool
socket_address::is_valid() const {
switch (family()) {
case af_inet:
return sa_inet()->is_valid();
// case af_inet6:
// return sa_inet6().is_valid();
case af_inet6:
return sa_inet6()->is_valid();
default:
return false;
}
Expand All @@ -253,6 +250,8 @@ socket_address::is_bindable() const {
switch (family()) {
case af_inet:
return !sa_inet()->is_address_any();
case af_inet6:
return !sa_inet6()->is_address_any();
default:
return false;
}
Expand All @@ -263,6 +262,8 @@ socket_address::is_address_any() const {
switch (family()) {
case af_inet:
return sa_inet()->is_address_any();
case af_inet6:
return sa_inet6()->is_address_any();
default:
return true;
}
Expand All @@ -273,6 +274,8 @@ socket_address::port() const {
switch (family()) {
case af_inet:
return sa_inet()->port();
case af_inet6:
return sa_inet6()->port();
default:
return 0;
}
Expand All @@ -283,6 +286,8 @@ socket_address::set_port(uint16_t p) {
switch (family()) {
case af_inet:
return sa_inet()->set_port(p);
case af_inet6:
return sa_inet6()->set_port(p);
default:
break;
}
Expand All @@ -293,6 +298,8 @@ socket_address::address_str() const {
switch (family()) {
case af_inet:
return sa_inet()->address_str();
case af_inet6:
return sa_inet6()->address_str();
default:
return std::string();
}
Expand All @@ -303,6 +310,8 @@ socket_address::address_c_str(char* buf, socklen_t size) const {
switch (family()) {
case af_inet:
return sa_inet()->address_c_str(buf, size);
case af_inet6:
return sa_inet6()->address_c_str(buf, size);
default:
return false;
}
Expand All @@ -314,6 +323,10 @@ socket_address::set_address_c_str(const char* a) {
sa_inet()->set_family();
return true;

} else if (sa_inet6()->set_address_c_str(a)) {
sa_inet6()->set_family();
return true;

} else {
return false;
}
Expand All @@ -325,6 +338,8 @@ socket_address::length() const {
switch(family()) {
case af_inet:
return sizeof(sockaddr_in);
case af_inet6:
return sizeof(sockaddr_in6);
default:
return 0;
}
Expand All @@ -349,8 +364,8 @@ socket_address::operator == (const socket_address& rhs) const {
switch (family()) {
case af_inet:
return *sa_inet() == *rhs.sa_inet();
// case af_inet6:
// return *sa_inet6() == *rhs.sa_inet6();
case af_inet6:
return *sa_inet6() == *rhs.sa_inet6();
default:
throw std::logic_error("socket_address::operator == (rhs) invalid type comparison.");
}
Expand All @@ -364,8 +379,8 @@ socket_address::operator < (const socket_address& rhs) const {
switch (family()) {
case af_inet:
return *sa_inet() < *rhs.sa_inet();
// case af_inet6:
// return *sa_inet6() < *rhs.sa_inet6();
case af_inet6:
return *sa_inet6() < *rhs.sa_inet6();
default:
throw std::logic_error("socket_address::operator < (rhs) invalid type comparison.");
}
Expand All @@ -391,6 +406,21 @@ socket_address_inet::set_address_c_str(const char* a) {
return inet_pton(AF_INET, a, &m_sockaddr.sin_addr);
}

inline socket_address_inet6
socket_address_inet::to_mapped_address() const {
uint32_t addr32[4];
addr32[0] = 0;
addr32[1] = 0;
addr32[2] = htonl(0xffff);
addr32[3] = m_sockaddr.sin_addr.s_addr;

socket_address_inet6 sa;
sa.clear();
sa.set_address(*reinterpret_cast<in6_addr *>(addr32));
sa.set_port_n(m_sockaddr.sin_port);
return sa;
}

inline bool
socket_address_inet::operator == (const socket_address_inet& rhs) const {
return
Expand All @@ -406,6 +436,55 @@ socket_address_inet::operator < (const socket_address_inet& rhs) const {
m_sockaddr.sin_port < rhs.m_sockaddr.sin_port);
}

inline std::string
socket_address_inet6::address_str() const {
char buf[INET6_ADDRSTRLEN];

if (!address_c_str(buf, INET6_ADDRSTRLEN))
return std::string();

return std::string(buf);
}

inline bool
socket_address_inet6::address_c_str(char* buf, socklen_t size) const {
return inet_ntop(family(), &m_sockaddr.sin6_addr, buf, size);
}

inline bool
socket_address_inet6::set_address_c_str(const char* a) {
return inet_pton(AF_INET6, a, &m_sockaddr.sin6_addr);
}

inline socket_address
socket_address_inet6::normalize_address() const {
const uint32_t *addr32 = reinterpret_cast<const uint32_t *>(m_sockaddr.sin6_addr.s6_addr);
if (addr32[0] == 0 && addr32[1] == 0 && addr32[2] == htonl(0xffff)) {
socket_address addr4;
addr4.sa_inet()->set_family();
addr4.sa_inet()->set_address_n(addr32[3]);
addr4.sa_inet()->set_port_n(m_sockaddr.sin6_port);
return addr4;
}
return *reinterpret_cast<const socket_address*>(this);
}

inline bool
socket_address_inet6::operator == (const socket_address_inet6& rhs) const {
return
memcmp(&m_sockaddr.sin6_addr, &rhs.m_sockaddr.sin6_addr, sizeof(in6_addr)) == 0 &&
m_sockaddr.sin6_port == rhs.m_sockaddr.sin6_port;
}

inline bool
socket_address_inet6::operator < (const socket_address_inet6& rhs) const {
int addr_comp = memcmp(&m_sockaddr.sin6_addr, &rhs.m_sockaddr.sin6_addr, sizeof(in6_addr));
return
addr_comp < 0 ||
(addr_comp == 0 ||
m_sockaddr.sin6_port < rhs.m_sockaddr.sin6_port);
}

}

#endif
11 changes: 0 additions & 11 deletions scripts/common.m4
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,3 @@ AC_DEFUN([TORRENT_ENABLE_INTERRUPT_SOCKET], [
]
)
])


AC_DEFUN([TORRENT_DISABLE_IPV6], [
AC_ARG_ENABLE(ipv6,
AC_HELP_STRING([--enable-ipv6], [enable ipv6 [[default=no]]]),
[
if test "$enableval" = "yes"; then
AC_DEFINE(RAK_USE_INET6, 1, enable ipv6 stuff)
fi
])
])
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ libtorrent_la_LIBADD = \
torrent/libsub_torrent.la \
torrent/data/libsub_torrentdata.la \
torrent/download/libsub_torrentdownload.la \
torrent/net/libsub_torrentnet.la \
torrent/peer/libsub_torrentpeer.la \
torrent/utils/libsub_torrentutils.la \
data/libsub_data.la \
Expand Down
Loading

0 comments on commit 4d305e3

Please sign in to comment.