Commit 8e1541ee authored by Joseph Noir's avatar Joseph Noir

Formatting

parent 60be58d1
......@@ -32,8 +32,7 @@ class interfaces {
public:
/// Returns a native IPv4 or IPv6 translation of `host`.
static optional<std::pair<std::string, ip>>
native_address(const std::string& host,
optional<ip> preferred = none);
native_address(const std::string& host, optional<ip> preferred = none);
/// Returns the host and protocol available for a local server socket
static std::vector<std::pair<std::string, ip>>
......
......@@ -25,10 +25,7 @@ namespace caf {
namespace net {
// IP version tag.
enum class ip {
v4,
v6
};
enum class ip { v4, v6 };
/// @relates ip
inline std::string to_string(ip x) {
......
......@@ -18,12 +18,11 @@
#pragma once
#include "caf/expected.hpp"
#include "caf/variant.hpp"
#include "caf/fwd.hpp"
#include "caf/net/network_socket.hpp"
#include "caf/sec.hpp"
#include "caf/variant.hpp"
namespace caf {
namespace net {
......
......@@ -28,32 +28,30 @@ namespace caf {
namespace net {
struct tcp {
/// Creates a new TCP socket to accept connections on a given port.
/// @param port The port to listen on.
/// @param addr Only accepts connections originating from this address.
/// @param reuse_addr Optionally sets the SO_REUSEADDR option on the socket.
/// @relates stream_socket
static expected<stream_socket> make_accept_socket(uint16_t port,
const char* addr = nullptr,
bool reuse_addr = false);
/// Create a `stream_socket` connected to `host`:`port` via the
/// `preferred` IP version.
/// @param host The remote host to connecto to.
/// @param port The port on the remote host to connect to.
/// @preferred Preferred IP version.
/// @returns The connected socket or an error.
/// @relates stream_socket
static expected<stream_socket> make_connected_socket(std::string host,
uint16_t port,
optional<ip> preferred = none);
/// Accept a connection on `x`.
/// @param x Listening endpoint.
/// @returns The socket that handles the accepted connection.
/// @relates stream_socket
static expected<stream_socket> accept(stream_socket x);
/// Creates a new TCP socket to accept connections on a given port.
/// @param port The port to listen on.
/// @param addr Only accepts connections originating from this address.
/// @param reuse_addr Optionally sets the SO_REUSEADDR option on the socket.
/// @relates stream_socket
static expected<stream_socket> make_accept_socket(uint16_t port,
const char* addr = nullptr,
bool reuse_addr = false);
/// Create a `stream_socket` connected to `host`:`port` via the
/// `preferred` IP version.
/// @param host The remote host to connecto to.
/// @param port The port on the remote host to connect to.
/// @preferred Preferred IP version.
/// @returns The connected socket or an error.
/// @relates stream_socket
static expected<stream_socket>
make_connected_socket(std::string host, uint16_t port,
optional<ip> preferred = none);
/// Accept a connection on `x`.
/// @param x Listening endpoint.
/// @returns The socket that handles the accepted connection.
/// @relates stream_socket
static expected<stream_socket> accept(stream_socket x);
};
} // namespace net
......
......@@ -28,7 +28,6 @@
#include "caf/policy/scribe.hpp"
#include "caf/send.hpp"
namespace caf {
namespace policy {
......@@ -54,18 +53,19 @@ public:
template <class Parent>
bool handle_read_event(Parent& parent) {
auto sck = net::tcp::accept(net::socket_cast<net::stream_socket>(acceptor_));
auto sck = net::tcp::accept(
net::socket_cast<net::stream_socket>(acceptor_));
if (!sck) {
CAF_LOG_ERROR("accept failed:" << sck.error());
return false;
}
auto mpx = parent.multiplexer();
if (!mpx) {
CAF_LOG_DEBUG("could not acquire multiplexer to create a new endpoint manager");
CAF_LOG_DEBUG(
"could not acquire multiplexer to create a new endpoint manager");
return false;
}
auto child = make_endpoint_manager(mpx, parent.system(),
scribe{*sck},
auto child = make_endpoint_manager(mpx, parent.system(), scribe{*sck},
parent.application().make());
if (auto err = child->init()) {
return false;
......
......@@ -18,33 +18,33 @@
#include "caf/net/interfaces.hpp"
#include <algorithm>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#ifdef CAF_WINDOWS
# ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0600
# endif
# include <iostream>
# include <winsock2.h>
# include <ws2tcpip.h>
# include <iphlpapi.h>
# ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x0600
# endif
# include <iostream>
# include <winsock2.h>
# include <ws2tcpip.h>
# include <iphlpapi.h>
#else
# include <sys/socket.h>
# include <netinet/in.h>
# include <net/if.h>
# include <unistd.h>
# include <netdb.h>
# include <ifaddrs.h>
# include <sys/ioctl.h>
# include <arpa/inet.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <net/if.h>
# include <unistd.h>
# include <netdb.h>
# include <ifaddrs.h>
# include <sys/ioctl.h>
# include <arpa/inet.h>
#endif
#include <map>
#include <string>
#include <memory>
#include <string>
#include <utility>
#include "caf/detail/socket_sys_includes.hpp"
......@@ -69,24 +69,22 @@ void* fetch_in_addr(int family, sockaddr* addr) {
return vptr(&reinterpret_cast<sockaddr_in6*>(addr)->sin6_addr);
}
int fetch_addr_str(bool get_ipv4, bool get_ipv6,
char (&buf)[INET6_ADDRSTRLEN],
int fetch_addr_str(bool get_ipv4, bool get_ipv6, char (&buf)[INET6_ADDRSTRLEN],
sockaddr* addr) {
if (addr == nullptr)
return AF_UNSPEC;
auto family = addr->sa_family;
auto in_addr = fetch_in_addr(family, addr);
return ((family == AF_INET && get_ipv4) || (family == AF_INET6 && get_ipv6))
&& inet_ntop(family, in_addr, buf, INET6_ADDRSTRLEN) == buf
? family
: AF_UNSPEC;
&& inet_ntop(family, in_addr, buf, INET6_ADDRSTRLEN) == buf
? family
: AF_UNSPEC;
}
} // namespace
optional<std::pair<std::string, ip>>
interfaces::native_address(const std::string& host,
optional<ip> preferred) {
interfaces::native_address(const std::string& host, optional<ip> preferred) {
addrinfo hint;
memset(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM;
......@@ -100,8 +98,7 @@ interfaces::native_address(const std::string& host,
for (auto i = addrs.get(); i != nullptr; i = i->ai_next) {
auto family = fetch_addr_str(true, true, buffer, i->ai_addr);
if (family != AF_UNSPEC)
return std::make_pair(buffer, family == AF_INET ? ip::v4
: ip::v6);
return std::make_pair(buffer, family == AF_INET ? ip::v4 : ip::v6);
}
return none;
}
......
......@@ -9,7 +9,7 @@
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. * *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
......
......@@ -90,12 +90,8 @@ caf::expected<socket> new_ip_acceptor_impl(uint16_t port, const char* addr,
reinterpret_cast<setsockopt_ptr>(&on),
static_cast<socket_size_type>(sizeof(on))));
}
using sockaddr_type =
typename std::conditional<
Family == AF_INET,
sockaddr_in,
sockaddr_in6
>::type;
using sockaddr_type = typename std::conditional<
Family == AF_INET, sockaddr_in, sockaddr_in6>::type;
sockaddr_type sa;
memset(&sa, 0, sizeof(sockaddr_type));
family_of(sa) = Family;
......@@ -105,8 +101,8 @@ caf::expected<socket> new_ip_acceptor_impl(uint16_t port, const char* addr,
inet_pton(Family, addr, &addr_of(sa)));
port_of(sa) = htons(port);
CAF_NET_SYSCALL("bind", res, !=, 0,
bind(fd, reinterpret_cast<sockaddr*>(&sa),
static_cast<socket_size_type>(sizeof(sa))));
bind(fd, reinterpret_cast<sockaddr*>(&sa),
static_cast<socket_size_type>(sizeof(sa))));
return sguard.release();
}
......@@ -114,22 +110,18 @@ template <int Family>
bool ip_connect(socket fd, const std::string& host, uint16_t port,
bool nonblock = true) {
CAF_LOG_TRACE("Family =" << (Family == AF_INET ? "AF_INET" : "AF_INET6")
<< CAF_ARG(fd.id) << CAF_ARG(host));
<< CAF_ARG(fd.id) << CAF_ARG(host));
static_assert(Family == AF_INET || Family == AF_INET6, "invalid family");
using sockaddr_type =
typename std::conditional<
Family == AF_INET,
sockaddr_in,
sockaddr_in6
>::type;
using sockaddr_type = typename std::conditional<
Family == AF_INET, sockaddr_in, sockaddr_in6>::type;
sockaddr_type sa;
memset(&sa, 0, sizeof(sockaddr_type));
inet_pton(Family, host.c_str(), &addr_of(sa));
family_of(sa) = Family;
port_of(sa) = htons(port);
port_of(sa) = htons(port);
using sa_ptr = const sockaddr*;
return ::connect(fd.id, reinterpret_cast<sa_ptr>(&sa), sizeof(sa)) == 0
|| (nonblock && errno == EINPROGRESS);
|| (nonblock && errno == EINPROGRESS);
}
} // namespace
......@@ -147,8 +139,9 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
for (auto& elem : addrs) {
auto hostname = elem.first.c_str();
auto p = elem.second == ip::v4
? new_ip_acceptor_impl<AF_INET>(port, hostname, reuse_addr, any)
: new_ip_acceptor_impl<AF_INET6>(port, hostname, reuse_addr, any);
? new_ip_acceptor_impl<AF_INET>(port, hostname, reuse_addr, any)
: new_ip_acceptor_impl<AF_INET6>(port, hostname, reuse_addr,
any);
if (!p) {
CAF_LOG_DEBUG(p.error());
continue;
......@@ -158,9 +151,9 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
}
if (fd == invalid_socket_id) {
CAF_LOG_WARNING("could not open tcp socket on:" << CAF_ARG(port)
<< CAF_ARG(addr_str));
return make_error(sec::cannot_open_port, "tcp socket creation failed",
port, addr_str);
<< CAF_ARG(addr_str));
return make_error(sec::cannot_open_port, "tcp socket creation failed", port,
addr_str);
}
socket_guard sguard{fd};
CAF_NET_SYSCALL("listen", tmp, !=, 0, listen(fd.id, SOMAXCONN));
......@@ -169,9 +162,9 @@ expected<stream_socket> tcp::make_accept_socket(uint16_t port, const char* addr,
return socket_cast<stream_socket>(sguard.release());
}
expected<stream_socket>
tcp::make_connected_socket(std::string host, uint16_t port,
optional<ip> preferred) {
expected<stream_socket> tcp::make_connected_socket(std::string host,
uint16_t port,
optional<ip> preferred) {
CAF_LOG_TRACE(CAF_ARG(host) << CAF_ARG(port) << CAF_ARG(preferred));
CAF_LOG_DEBUG("try to connect to:" << CAF_ARG(host) << CAF_ARG(port));
auto res = interfaces::native_address(host, std::move(preferred));
......@@ -192,8 +185,8 @@ tcp::make_connected_socket(std::string host, uint16_t port,
socket_guard sguard(fd);
if (proto == ip::v6) {
if (ip_connect<AF_INET6>(fd, res->first, port)) {
CAF_LOG_INFO("successfully connected to (IPv6):"
<< CAF_ARG(host) << CAF_ARG(port));
CAF_LOG_INFO("successfully connected to (IPv6):" << CAF_ARG(host)
<< CAF_ARG(port));
return socket_cast<stream_socket>(sguard.release());
}
sguard.close();
......@@ -202,11 +195,11 @@ tcp::make_connected_socket(std::string host, uint16_t port,
}
if (!ip_connect<AF_INET>(fd, res->first, port)) {
CAF_LOG_WARNING("could not connect to:" << CAF_ARG(host) << CAF_ARG(port));
return make_error(sec::cannot_connect_to_node,
"ip_connect failed", host, port);
return make_error(sec::cannot_connect_to_node, "ip_connect failed", host,
port);
}
CAF_LOG_INFO("successfully connected to (IPv4):"
<< CAF_ARG(host) << CAF_ARG(port));
CAF_LOG_INFO("successfully connected to (IPv4):" << CAF_ARG(host)
<< CAF_ARG(port));
return socket_cast<stream_socket>(sguard.release());
}
......
......@@ -18,11 +18,11 @@
#define CAF_SUITE doorman
#include "caf/policy/doorman.hpp"
#include "caf/net/endpoint_manager.hpp"
#include "caf/net/make_endpoint_manager.hpp"
#include "caf/net/multiplexer.hpp"
#include "caf/net/tcp.hpp"
#include "caf/policy/doorman.hpp"
#include "caf/test/dsl.hpp"
......@@ -77,7 +77,8 @@ public:
template <class Transport>
void resolve(Transport&, std::string path, actor listener) {
anon_send(listener, resolve_atom::value, "the resolved path is still " + path);
anon_send(listener, resolve_atom::value,
"the resolved path is still " + path);
}
template <class Transport>
......@@ -127,8 +128,7 @@ CAF_TEST(doorman accept) {
auto acceptor = unbox(tcp::make_accept_socket(0, nullptr, false));
auto port = unbox(local_port(socket_cast<network_socket>(acceptor)));
CAF_MESSAGE("opened acceptor on port " << port);
auto mgr = make_endpoint_manager(mpx, sys,
policy::doorman{acceptor},
auto mgr = make_endpoint_manager(mpx, sys, policy::doorman{acceptor},
dummy_application_factory{});
CAF_CHECK_EQUAL(mgr->init(), none);
handle_io_event();
......
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