Commit d8343bd3 authored by Jakob Otto's avatar Jakob Otto

Add conversion functions for ip_endpoint

parent 7f2798df
...@@ -19,6 +19,7 @@ set(LIBCAF_NET_SRCS ...@@ -19,6 +19,7 @@ set(LIBCAF_NET_SRCS
src/socket_manager.cpp src/socket_manager.cpp
src/stream_socket.cpp src/stream_socket.cpp
src/scribe.cpp src/scribe.cpp
src/convert_ip_endpoint.cpp
) )
add_custom_target(libcaf_net) add_custom_target(libcaf_net)
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* 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. *
* *
* 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. *
******************************************************************************/
#pragma once
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_endpoint.hpp"
namespace caf {
namespace detail {
sockaddr_in6 to_sockaddr(const ip_endpoint& ep);
ip_endpoint to_ip_endpoint(const sockaddr_in6& addr);
} // namespace detail
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* 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. *
* *
* 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. *
******************************************************************************/
#include "caf/detail/convert_ip_endpoint.hpp"
namespace caf {
namespace detail {
sockaddr_in6 to_sockaddr(const ip_endpoint& ep) {
sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
addr.sin6_port = ntohs(ep.port());
memcpy(&addr.sin6_addr, ep.address().bytes().data(), ep.address().size());
return addr;
}
ip_endpoint to_ip_endpoint(const sockaddr_in6& addr) {
ip_endpoint ep;
ep.port(htons(addr.sin6_port));
ipv6_address ip_addr;
memcpy(ip_addr.bytes().data(), &addr.sin6_addr, ip_addr.size());
ep.address(ip_addr);
return ep;
}
} // namespace detail
} // namespace caf
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* 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. *
* *
* 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. *
******************************************************************************/
#define CAF_SUITE convert_ip_endpoint
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/test/dsl.hpp"
#include <cstring>
#include "caf/detail/socket_sys_includes.hpp"
using namespace caf;
using namespace caf::detail;
CAF_TEST(sockaddr roundtrip) {
sockaddr_in6 source_addr = {};
source_addr.sin6_family = AF_INET6;
source_addr.sin6_port = htons(23);
source_addr.sin6_addr = in6addr_loopback;
auto ep = to_ip_endpoint(source_addr);
auto dest_addr = to_sockaddr(ep);
CAF_CHECK_EQUAL(memcmp(&source_addr, &dest_addr, sizeof(sockaddr_in6)), 0);
}
CAF_TEST(ip_endpoint roundtrip) {
ip_endpoint source_ep;
if (auto err = detail::parse("[::1]:55555", source_ep))
CAF_FAIL("unable to parse input: " << err);
auto addr = to_sockaddr(source_ep);
auto dest_ep = to_ip_endpoint(addr);
CAF_CHECK_EQUAL(source_ep, dest_ep);
}
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