Skip to content

Commit

Permalink
Backport changes from feature-bind.
Browse files Browse the repository at this point in the history
  • Loading branch information
rakshasa authored Aug 23, 2019
1 parent f43f9e4 commit 7659cd0
Show file tree
Hide file tree
Showing 50 changed files with 659 additions and 221 deletions.
7 changes: 7 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((c++-mode
(flycheck-clang-language-standard . "c++11")
(flycheck-gcc-language-standard . "c++11")))

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ before_install:
- git clone https://github.com/rakshasa/libtorrent.git
- |
pushd libtorrent \
&& (git checkout ${TRAVIS_BRANCH} || true) \
&& ./autogen.sh \
&& CXX="$COMPILER" ./configure --prefix=/usr \
&& make \
Expand Down
2 changes: 1 addition & 1 deletion rak/file_stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define RAK_FILE_STAT_H

#include <string>
#include <inttypes.h>
#include <cinttypes>
#include <sys/stat.h>

namespace rak {
Expand Down
2 changes: 1 addition & 1 deletion rak/fs_stat.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define RAK_FS_STAT_H

#include <string>
#include <inttypes.h>
#include <cinttypes>

#include <rak/error_number.h>

Expand Down
3 changes: 1 addition & 2 deletions rak/functional_fun.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@

#include <memory>
#include <functional>
#include lt_tr1_functional
#include lt_tr1_memory
#include <memory>

namespace rak {

Expand Down
2 changes: 1 addition & 1 deletion rak/partial_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

#include <cstring>
#include <stdexcept>
#include <inttypes.h>
#include <cinttypes>

namespace rak {

Expand Down
2 changes: 1 addition & 1 deletion rak/priority_queue_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#ifndef RAK_PRIORITY_QUEUE_DEFAULT_H
#define RAK_PRIORITY_QUEUE_DEFAULT_H

#include lt_tr1_functional
#include <functional>
#include <rak/allocators.h>
#include <rak/priority_queue.h>
#include <rak/timer.h>
Expand Down
79 changes: 74 additions & 5 deletions rak/socket_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@
#ifndef RAK_SOCKET_ADDRESS_H
#define RAK_SOCKET_ADDRESS_H

#include <cinttypes>
#include <cstdint>
#include <cstring>
#include <string>
#include <stdexcept>
#include <string>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
Expand Down Expand Up @@ -77,11 +80,13 @@ class socket_address {
static const int pf_local = PF_UNIX;
#endif

bool is_any() const;
bool is_valid() const;
bool is_bindable() const;
bool is_address_any() const;

// Should we need to set AF_UNSPEC?
bool is_valid_inet_class() const { return family() == af_inet || family() == af_inet6; }

void clear() { std::memset(this, 0, sizeof(socket_address)); set_family(); }

sa_family_t family() const { return m_sockaddr.sa_family; }
Expand All @@ -93,6 +98,8 @@ class socket_address {
std::string address_str() const;
bool address_c_str(char* buf, socklen_t size) const;

std::string pretty_address_str() const;

// Attemts to set it as an inet, then an inet6 address. It will
// never set anything but net addresses, no local/unix.
bool set_address_str(const std::string& a) { return set_address_c_str(a.c_str()); }
Expand All @@ -119,6 +126,7 @@ class socket_address {
// extranous bytes and ensure it does not go beyond the size of this
// struct.
void copy(const socket_address& src, size_t length);
void copy_sockaddr(const sockaddr* src);

static socket_address* cast_from(sockaddr* sa) { return reinterpret_cast<socket_address*>(sa); }
static const socket_address* cast_from(const sockaddr* sa) { return reinterpret_cast<const socket_address*>(sa); }
Expand Down Expand Up @@ -215,6 +223,8 @@ class socket_address_inet6 {

void set_address_any() { set_port(0); set_address(in6addr_any); }

std::string pretty_address_str() const;

sa_family_t family() const { return m_sockaddr.sin6_family; }
void set_family() { m_sockaddr.sin6_family = AF_INET6; }

Expand All @@ -233,6 +243,18 @@ class socket_address_inet6 {
struct sockaddr_in6 m_sockaddr;
};

inline bool
socket_address::is_any() const {
switch (family()) {
case af_inet:
return sa_inet()->is_any();
case af_inet6:
return sa_inet6()->is_any();
default:
return false;
}
}

inline bool
socket_address::is_valid() const {
switch (family()) {
Expand Down Expand Up @@ -317,6 +339,21 @@ socket_address::address_c_str(char* buf, socklen_t size) const {
}
}

inline std::string
socket_address::pretty_address_str() const {
switch (family()) {
case af_inet:
return sa_inet()->address_str();
case af_inet6:
return sa_inet6()->pretty_address_str();
case af_unspec:
return std::string("unspec");
default:
return std::string("invalid");
}
}


inline bool
socket_address::set_address_c_str(const char* a) {
if (sa_inet()->set_address_c_str(a)) {
Expand Down Expand Up @@ -348,13 +385,16 @@ socket_address::length() const {
inline void
socket_address::copy(const socket_address& src, size_t length) {
length = std::min(length, sizeof(socket_address));

// Does this get properly optimized?

std::memset(this, 0, sizeof(socket_address));
std::memcpy(this, &src, length);
}

// Should we be able to compare af_unspec?
inline void
socket_address::copy_sockaddr(const sockaddr* src) {
std::memset(this, 0, sizeof(socket_address));
std::memcpy(this, src, socket_address::cast_from(src)->length());
}

inline bool
socket_address::operator == (const socket_address& rhs) const {
Expand Down Expand Up @@ -456,6 +496,35 @@ socket_address_inet6::set_address_c_str(const char* a) {
return inet_pton(AF_INET6, a, &m_sockaddr.sin6_addr);
}

inline std::string
socket_address_inet6::pretty_address_str() const {
char buf[INET6_ADDRSTRLEN + 2 + 6];

if (inet_ntop(family(), &m_sockaddr.sin6_addr, buf + 1, INET6_ADDRSTRLEN) == NULL)
return std::string();

buf[0] = '[';

char* last_char = (char*)std::memchr(buf + 1, 0, INET6_ADDRSTRLEN);

// TODO: Throw exception here.

if (last_char == NULL || last_char >= buf + 1 + INET6_ADDRSTRLEN)
throw std::logic_error("inet_ntop for inet6 returned bad buffer");

*(last_char++) = ']';

if (!is_port_any()) {
if (snprintf(last_char, 7, ":%" PRIu16, port()) == -1)
return std::string("error"); // TODO: Throw here.

} else {
*last_char = '\0';
}

return std::string(buf);
}

inline socket_address
socket_address_inet6::normalize_address() const {
const uint32_t *addr32 = reinterpret_cast<const uint32_t *>(m_sockaddr.sin6_addr.s6_addr);
Expand Down
2 changes: 1 addition & 1 deletion rak/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#define RAK_TIMER_H

#include <limits>
#include <inttypes.h>
#include <cinttypes>
#include <sys/time.h>

namespace rak {
Expand Down
47 changes: 0 additions & 47 deletions scripts/rak_cxx.m4
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,3 @@ AC_DEFUN([RAK_CHECK_CXX11], [
]
)
])

AC_DEFUN([RAK_CHECK_TR1_LIB], [
AC_LANG_PUSH(C++)
AC_MSG_CHECKING(should use TR1 headers)
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <unordered_map>
class Foo; typedef std::unordered_map<Foo*, int> Bar;
Bar b1;
])
], [
AC_MSG_RESULT(no)
AC_DEFINE(USE_TR1_LIB, 0, Define to 1 if you need to use TR1 containers.)
AC_DEFINE([lt_tr1_array], [<array>], [TR1 array])
AC_DEFINE([lt_tr1_functional], [<functional>], [TR1 functional])
AC_DEFINE([lt_tr1_memory], [<memory>], [TR1 memory])
AC_DEFINE([lt_tr1_unordered_map], [<unordered_map>], [TR1 unordered_map])
], [
AC_COMPILE_IFELSE([AC_LANG_SOURCE([
#include <tr1/unordered_map>
class Foo; typedef std::tr1::unordered_map<Foo*, int> Bar;
Bar b1;
])
], [
AC_MSG_RESULT([yes])
AC_DEFINE(USE_TR1_LIB, 1, Define to 1 if you need to use TR1 containers.)
AC_DEFINE([lt_tr1_array], [<tr1/array>], [TR1 array])
AC_DEFINE([lt_tr1_functional], [<tr1/functional>], [TR1 functional])
AC_DEFINE([lt_tr1_memory], [<tr1/memory>], [TR1 memory])
AC_DEFINE([lt_tr1_unordered_map], [<tr1/unordered_map>], [TR1 unordered_map])
], [
AC_MSG_ERROR([No support for C++11 standard library nor TR1 extensions found.])
])
])
AH_VERBATIM(lt_tr1_zzz, [
#if USE_TR1_LIB == 1
namespace std { namespace tr1 {} using namespace tr1; }
#endif
])
AC_LANG_POP(C++)
])
10 changes: 6 additions & 4 deletions src/command_download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ apply_d_change_link(core::Download* download, const torrent::Object::list_type&
case 0:
if (symlink(target.c_str(), link.c_str()) == -1){
lt_log_print(torrent::LOG_TORRENT_WARN, "create_link failed: %s",
rak::error_number::current().c_str());
rak::error_number::current().c_str());
}
break;

Expand Down Expand Up @@ -324,7 +324,7 @@ struct call_add_d_peer_t {

void operator() (const sockaddr* sa, int err) {
if (sa == NULL) {
lt_log_print(torrent::LOG_CONNECTION_WARN, "Could not resolve hostname for added peer.");
lt_log_print(torrent::LOG_TORRENT_WARN, "could not resolve hostname for added peer");
} else {
m_download->download()->add_peer(sa, m_port);
}
Expand Down Expand Up @@ -872,8 +872,10 @@ initialize_command_download() {

CMD2_DL ("d.wanted_chunks", CMD2_ON_DATA(wanted_chunks));

CMD2_DL_V ("d.tracker_announce", std::bind(&torrent::Download::manual_request, CMD2_BIND_DL, false));
CMD2_DL_V ("d.tracker_announce.force", std::bind(&torrent::Download::manual_request, CMD2_BIND_DL, true));
// Do not exposre d.tracker_announce.force to regular users.
CMD2_DL_V ("d.tracker_announce", std::bind(&torrent::Download::manual_request, CMD2_BIND_DL, false));
CMD2_DL_V ("d.tracker_announce.force", std::bind(&torrent::Download::manual_request, CMD2_BIND_DL, true));

CMD2_DL ("d.tracker_numwant", std::bind(&torrent::TrackerList::numwant, CMD2_BIND_TL));
CMD2_DL_VALUE_V ("d.tracker_numwant.set", std::bind(&torrent::TrackerList::set_numwant, CMD2_BIND_TL, std::placeholders::_2));
// TODO: Deprecate 'd.tracker_focus'.
Expand Down
Loading

0 comments on commit 7659cd0

Please sign in to comment.