Commit 41957aa3 authored by Marian Triebe's avatar Marian Triebe

Add support for dualstack IPv6

Closes #174, closes #156
parent 57ca0479
...@@ -419,7 +419,6 @@ class default_multiplexer : public multiplexer { ...@@ -419,7 +419,6 @@ class default_multiplexer : public multiplexer {
std::pair<native_socket, native_socket> m_pipe; std::pair<native_socket, native_socket> m_pipe;
std::thread::id m_tid; std::thread::id m_tid;
}; };
default_multiplexer& get_multiplexer_singleton(); default_multiplexer& get_multiplexer_singleton();
...@@ -658,20 +657,20 @@ class stream : public event_handler { ...@@ -658,20 +657,20 @@ class stream : public event_handler {
} }
// reading & writing // reading & writing
Socket m_sock; Socket m_sock;
// reading // reading
manager_ptr m_reader; manager_ptr m_reader;
size_t m_threshold; size_t m_threshold;
size_t m_collected; size_t m_collected;
size_t m_max; size_t m_max;
receive_policy_flag m_rd_flag; receive_policy_flag m_rd_flag;
buffer_type m_rd_buf; buffer_type m_rd_buf;
// writing // writing
manager_ptr m_writer; manager_ptr m_writer;
bool m_writing; bool m_writing;
size_t m_written; size_t m_written;
buffer_type m_wr_buf; buffer_type m_wr_buf;
buffer_type m_wr_offline_buf; buffer_type m_wr_offline_buf;
}; };
/** /**
...@@ -763,7 +762,9 @@ class acceptor : public event_handler { ...@@ -763,7 +762,9 @@ class acceptor : public event_handler {
void removed_from_loop(operation op) override { void removed_from_loop(operation op) override {
CAF_LOG_TRACE("m_accept_sock.fd = " << m_accept_sock.fd() CAF_LOG_TRACE("m_accept_sock.fd = " << m_accept_sock.fd()
<< "op = " << static_cast<int>(op)); << "op = " << static_cast<int>(op));
if (op == operation::read) m_mgr.reset(); if (op == operation::read) {
m_mgr.reset();
}
} }
protected: protected:
...@@ -780,31 +781,17 @@ class acceptor : public event_handler { ...@@ -780,31 +781,17 @@ class acceptor : public event_handler {
}; };
native_socket new_ipv4_connection_impl(const std::string&, uint16_t); native_socket new_tcp_connection_impl(const std::string&, uint16_t,
optional<protocol> preferred = none);
default_socket new_ipv4_connection(const std::string& host, uint16_t port); default_socket new_tcp_connection(const std::string& host, uint16_t port);
template <class Socket>
void ipv4_connect(Socket& sock, const std::string& host, uint16_t port) {
sock = new_ipv4_connection(host, port);
}
std::pair<native_socket, uint16_t> std::pair<native_socket, uint16_t>
new_ipv4_acceptor_impl(uint16_t port, const char* addr, bool reuse_addr); new_tcp_acceptor_impl(uint16_t port, const char* addr, bool reuse_addr);
std::pair<default_socket_acceptor, uint16_t> std::pair<default_socket_acceptor, uint16_t>
new_ipv4_acceptor(uint16_t port, const char* addr = nullptr, new_tcp_acceptor(uint16_t port, const char* addr = nullptr,
bool reuse_addr = false); bool reuse_addr = false);
template <class SocketAcceptor>
uint16_t ipv4_bind(SocketAcceptor& sock,
uint16_t port,
const char* addr = nullptr) {
CAF_LOGF_TRACE(CAF_ARG(port));
auto acceptor = new_ipv4_acceptor(port, addr);
sock = std::move(acceptor.first);
return acceptor.second;
}
} // namespace network } // namespace network
} // namespace io } // namespace io
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include "caf/optional.hpp"
#include "caf/io/network/protocol.hpp" #include "caf/io/network/protocol.hpp"
namespace caf { namespace caf {
...@@ -57,6 +59,12 @@ class interfaces { ...@@ -57,6 +59,12 @@ class interfaces {
*/ */
static std::vector<std::string> list_addresses(protocol proc, static std::vector<std::string> list_addresses(protocol proc,
bool include_localhost = true); bool include_localhost = true);
/**
* Returns `pair<string, protocol>` for given host address.
**/
static optional<std::pair<std::string, protocol>>
get_addrinfo_of_host(const std::string& host, optional<protocol> preferred = none);
}; };
} // namespace network } // namespace network
......
This diff is collapsed.
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
#include "caf/io/network/interfaces.hpp" #include "caf/io/network/interfaces.hpp"
#include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <net/if.h> #include <net/if.h>
...@@ -144,6 +144,39 @@ std::vector<std::string> interfaces::list_addresses(protocol proc, ...@@ -144,6 +144,39 @@ std::vector<std::string> interfaces::list_addresses(protocol proc,
return result; return result;
} }
optional<std::pair<std::string, protocol>>
interfaces::get_addrinfo_of_host(const std::string& host,
optional<protocol> preferred) {
addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM;
if (preferred) {
hint.ai_family = *preferred == protocol::ipv4 ? AF_INET : AF_INET6;
}
addrinfo* tmp = nullptr;
if (getaddrinfo(host.c_str(), nullptr, &hint, &tmp)) {
return none;
}
std::unique_ptr<addrinfo, decltype(freeaddrinfo)*> addrs{tmp, freeaddrinfo};
for (auto i = addrs.get(); i != nullptr; i = i->ai_next) {
auto family = i->ai_family;
if (family == AF_INET || family == AF_INET6) {
char buffer[INET6_ADDRSTRLEN];
auto res = family == AF_INET
? inet_ntop(family,
&reinterpret_cast<sockaddr_in*>(i->ai_addr)->sin_addr,
buffer, sizeof(buffer))
: inet_ntop(family,
&reinterpret_cast<sockaddr_in6*>(i->ai_addr)->sin6_addr,
buffer, sizeof(buffer));
if (res != nullptr) {
return {{res, family == AF_INET ? protocol::ipv4 : protocol::ipv6}};
}
}
}
return none;
}
} // namespace network } // namespace network
} // namespace io } // namespace io
} // namespace caf } // namespace caf
...@@ -65,6 +65,7 @@ int main() { ...@@ -65,6 +65,7 @@ int main() {
auto port = publish_at_some_port(4242, d); auto port = publish_at_some_port(4242, d);
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
io::unpublish(d, port); io::unpublish(d, port);
CAF_CHECKPOINT();
// must fail now // must fail now
try { try {
auto oops = io::remote_actor("127.0.0.1", port); auto oops = io::remote_actor("127.0.0.1", port);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment