Commit c5e3b77b authored by Jakob Otto's avatar Jakob Otto

Add ipv6_endpoint

parent 80bb0a1e
......@@ -68,6 +68,7 @@ set(LIBCAF_CORE_SRCS
src/ipv4_endpoint.cpp
src/ipv4_subnet.cpp
src/ipv6_address.cpp
src/ipv6_endpoint.cpp
src/ipv6_subnet.cpp
src/local_actor.cpp
src/logger.cpp
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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/ipv6_endpoint.hpp"
namespace caf {
/// An IP endpoint that contains an ipv6_address and a port.
using ip_endpoint = ipv6_endpoint;
} // 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. *
******************************************************************************/
#pragma once
#include <deque>
#include <functional>
#include <string>
#include <vector>
#include "caf/ipv6_address.hpp"
#include "caf/meta/type_name.hpp"
namespace caf {
/// A hashable endpoint abstraction for ipv6.
struct ipv6_endpoint : detail::comparable<ipv6_endpoint> {
public:
// -- constructors -----------------------------------------------------------
ipv6_endpoint(ipv6_address address, uint16_t port);
ipv6_endpoint() = default;
ipv6_endpoint(const ipv6_endpoint&) = default;
ipv6_endpoint& operator=(const ipv6_endpoint&) = default;
// -- properties -------------------------------------------------------------
/// Returns the IPv6 address.
ipv6_address address() const noexcept;
/// Sets the address of this endpoint.
void address(ipv6_address x) noexcept;
/// Returns the port of this endpoint.
uint16_t port() const noexcept;
/// Sets the port of this endpoint.
void port(uint16_t x) noexcept;
/// Returns a hash for this object.
size_t hash_code() const noexcept;
/// compares This endpoint to another.
/// Returns 0 if equal, otherwise >0 if this > x and <0 if this < x.
long compare(ipv6_endpoint x) const noexcept;
template <class Inspector>
friend typename Inspector::result_type inspect(Inspector& f,
ipv6_endpoint& x) {
return f(meta::type_name("ipv6_endpoint"), x.address_, x.port_);
}
private:
ipv6_address address_; /// The address of this endpoint.
uint16_t port_; /// The port of this endpoint.
};
std::string to_string(const ipv6_endpoint& ep);
} // namespace caf
namespace std {
template <>
struct hash<caf::ipv6_endpoint> {
size_t operator()(const caf::ipv6_endpoint& ep) const noexcept {
return ep.hash_code();
}
};
} // namespace std
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| 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/ipv6_endpoint.hpp"
#include "caf/detail/fnv_hash.hpp"
#ifdef CAF_WINDOWS
# include <windows.h>
# include <winsock2.h>
# include <ws2ipdef.h>
# include <ws2tcpip.h>
#else
# include <arpa/inet.h>
# include <cerrno>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <sys/socket.h>
# include <unistd.h>
#endif
#ifdef CAF_WINDOWS
using sa_family_t = short;
#endif
using caf::detail::fnv_hash;
using caf::detail::fnv_hash_append;
namespace caf {
ipv6_endpoint::ipv6_endpoint(ipv6_address address, uint16_t port)
: address_(address), port_(port) {
// nop
}
ipv6_address ipv6_endpoint::address() const noexcept {
return address_;
}
void ipv6_endpoint::address(ipv6_address x) noexcept {
address_ = x;
}
uint16_t ipv6_endpoint::port() const noexcept {
return port_;
}
void ipv6_endpoint::port(uint16_t x) noexcept {
port_ = x;
}
size_t ipv6_endpoint::hash_code() const noexcept {
auto result = fnv_hash(address_.data());
return fnv_hash_append(result, port_);
}
long ipv6_endpoint::compare(ipv6_endpoint x) const noexcept {
auto res = address_.compare(x.address());
if (res != 0)
return port_ - x.port();
else
return res;
}
std::string to_string(const ipv6_endpoint& ep) {
return to_string(ep.address()) + ":" + std::to_string(ep.port());
}
} // 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 ipv6_endpoint
#include "caf/ipv6_endpoint.hpp"
#include "caf/test/unit_test.hpp"
#include "caf/ipv6_address.hpp"
using namespace caf;
CAF_TEST(constructing and equality) {
const uint16_t port = 8888;
ipv6_address::array_type localhost_bytes{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}};
ipv6_address addr(localhost_bytes);
ipv6_endpoint ep1(addr, port);
CAF_CHECK_EQUAL(ep1.address(), addr);
CAF_CHECK_EQUAL(ep1.port(), port);
ipv6_endpoint ep2;
ep2.address(addr);
ep2.port(port);
CAF_CHECK_EQUAL(ep2.address(), addr);
CAF_CHECK_EQUAL(ep2.port(), port);
CAF_CHECK_EQUAL(ep1, ep2);
}
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