Commit 99b787bf authored by Jakob Otto's avatar Jakob Otto

Add conversions for ipv4

parent 65c6d9a3
......@@ -19,14 +19,18 @@
#pragma once
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/fwd.hpp"
namespace caf {
namespace detail {
sockaddr_in6 to_sockaddr(const ip_endpoint& ep);
sockaddr_in6 to_sockaddr(const ipv6_endpoint& ep);
ip_endpoint to_ip_endpoint(const sockaddr_in6& addr);
ipv6_endpoint to_ip_endpoint(const sockaddr_in6& addr);
sockaddr_in to_sockaddr(const ipv4_endpoint& ep);
ipv4_endpoint to_ip_endpoint(const sockaddr_in& addr);
} // namespace detail
} // namespace caf
......@@ -17,11 +17,13 @@
******************************************************************************/
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/ipv4_endpoint.hpp"
#include "caf/ipv6_endpoint.hpp"
namespace caf {
namespace detail {
sockaddr_in6 to_sockaddr(const ip_endpoint& ep) {
sockaddr_in6 to_sockaddr(const ipv6_endpoint& ep) {
sockaddr_in6 addr = {};
addr.sin6_family = AF_INET6;
addr.sin6_port = ntohs(ep.port());
......@@ -29,7 +31,7 @@ sockaddr_in6 to_sockaddr(const ip_endpoint& ep) {
return addr;
}
ip_endpoint to_ip_endpoint(const sockaddr_in6& addr) {
ipv6_endpoint to_ip_endpoint(const sockaddr_in6& addr) {
ip_endpoint ep;
ep.port(htons(addr.sin6_port));
ipv6_address ip_addr;
......@@ -38,5 +40,22 @@ ip_endpoint to_ip_endpoint(const sockaddr_in6& addr) {
return ep;
}
sockaddr_in to_sockaddr(const ipv4_endpoint& ep) {
sockaddr_in addr = {};
addr.sin_family = AF_INET;
addr.sin_port = ntohs(ep.port());
memcpy(&addr.sin_addr, ep.address().bytes().data(), ep.address().size());
return addr;
}
ipv4_endpoint to_ip_endpoint(const sockaddr_in& addr) {
ipv4_endpoint ep;
ep.port(htons(addr.sin_port));
ipv4_address ip_addr;
memcpy(ip_addr.bytes().data(), &addr.sin_addr, ip_addr.size());
ep.address(ip_addr);
return ep;
}
} // namespace detail
} // namespace caf
......@@ -25,11 +25,13 @@
#include <cstring>
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ipv4_endpoint.hpp"
#include "caf/ipv6_endpoint.hpp"
using namespace caf;
using namespace caf::detail;
CAF_TEST(sockaddr roundtrip) {
CAF_TEST(sockaddr_in6 roundtrip) {
sockaddr_in6 source_addr = {};
source_addr.sin6_family = AF_INET6;
source_addr.sin6_port = htons(23);
......@@ -39,11 +41,30 @@ CAF_TEST(sockaddr roundtrip) {
CAF_CHECK_EQUAL(memcmp(&source_addr, &dest_addr, sizeof(sockaddr_in6)), 0);
}
CAF_TEST(ip_endpoint roundtrip) {
ip_endpoint source_ep;
CAF_TEST(ipv6_endpoint roundtrip) {
ipv6_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);
}
CAF_TEST(sockaddr_in4 roundtrip) {
sockaddr_in source_addr = {};
source_addr.sin_family = AF_INET;
source_addr.sin_port = htons(23);
source_addr.sin_addr.s_addr = INADDR_LOOPBACK;
auto ep = to_ip_endpoint(source_addr);
auto dest_addr = to_sockaddr(ep);
CAF_CHECK_EQUAL(memcmp(&source_addr, &dest_addr, sizeof(sockaddr_in)), 0);
}
CAF_TEST(ipv4_endpoint roundtrip) {
ipv4_endpoint source_ep;
if (auto err = detail::parse("127.0.0.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