Initial Commit

This commit is contained in:
2025-11-06 15:51:56 +01:00
commit 0f7191ad54
648 changed files with 170981 additions and 0 deletions

View File

@@ -0,0 +1,145 @@
//
// ip/detail/endpoint.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_IP_DETAIL_ENDPOINT_HPP
#define ASIO_IP_DETAIL_ENDPOINT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <string>
#include "asio/detail/socket_types.hpp"
#include "asio/detail/winsock_init.hpp"
#include "asio/error_code.hpp"
#include "asio/ip/address.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace ip {
namespace detail {
// Helper class for implementing an IP endpoint.
class endpoint
{
public:
// Default constructor.
ASIO_DECL endpoint() noexcept;
// Construct an endpoint using a family and port number.
ASIO_DECL endpoint(int family,
unsigned short port_num) noexcept;
// Construct an endpoint using an address and port number.
ASIO_DECL endpoint(const asio::ip::address& addr,
unsigned short port_num) noexcept;
// Copy constructor.
endpoint(const endpoint& other) noexcept
: data_(other.data_)
{
}
// Assign from another endpoint.
endpoint& operator=(const endpoint& other) noexcept
{
data_ = other.data_;
return *this;
}
// Get the underlying endpoint in the native type.
asio::detail::socket_addr_type* data() noexcept
{
return &data_.base[0];
}
// Get the underlying endpoint in the native type.
const asio::detail::socket_addr_type* data() const noexcept
{
return &data_.base[0];
}
// Get the underlying size of the endpoint in the native type.
std::size_t size() const noexcept
{
if (is_v4())
return sizeof(asio::detail::sockaddr_in4_type);
else
return sizeof(asio::detail::sockaddr_in6_type);
}
// Set the underlying size of the endpoint in the native type.
ASIO_DECL void resize(std::size_t new_size);
// Get the capacity of the endpoint in the native type.
std::size_t capacity() const noexcept
{
return sizeof(data_);
}
// Get the port associated with the endpoint.
ASIO_DECL unsigned short port() const noexcept;
// Set the port associated with the endpoint.
ASIO_DECL void port(unsigned short port_num) noexcept;
// Get the IP address associated with the endpoint.
ASIO_DECL asio::ip::address address() const noexcept;
// Set the IP address associated with the endpoint.
ASIO_DECL void address(
const asio::ip::address& addr) noexcept;
// Compare two endpoints for equality.
ASIO_DECL friend bool operator==(const endpoint& e1,
const endpoint& e2) noexcept;
// Compare endpoints for ordering.
ASIO_DECL friend bool operator<(const endpoint& e1,
const endpoint& e2) noexcept;
// Determine whether the endpoint is IPv4.
bool is_v4() const noexcept
{
return data_.base[0].sa_family == ASIO_OS_DEF(AF_INET);
}
#if !defined(ASIO_NO_IOSTREAM)
// Convert to a string.
ASIO_DECL std::string to_string() const;
#endif // !defined(ASIO_NO_IOSTREAM)
private:
// The underlying IP socket address.
union data_union
{
#if defined(_FORTIFY_SOURCE)
asio::detail::socket_addr_type base[8];
#else // defined(_FORTIFY_SOURCE)
asio::detail::socket_addr_type base[1];
#endif // defined(_FORTIFY_SOURCE)
asio::detail::sockaddr_in4_type v4;
asio::detail::sockaddr_in6_type v6;
} data_;
};
} // namespace detail
} // namespace ip
} // namespace asio
#include "asio/detail/pop_options.hpp"
#if defined(ASIO_HEADER_ONLY)
# include "asio/ip/detail/impl/endpoint.ipp"
#endif // defined(ASIO_HEADER_ONLY)
#endif // ASIO_IP_DETAIL_ENDPOINT_HPP

View File

@@ -0,0 +1,195 @@
//
// ip/detail/impl/endpoint.ipp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP
#define ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <cstring>
#if !defined(ASIO_NO_IOSTREAM)
# include <sstream>
#endif // !defined(ASIO_NO_IOSTREAM)
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/throw_error.hpp"
#include "asio/error.hpp"
#include "asio/ip/detail/endpoint.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace ip {
namespace detail {
endpoint::endpoint() noexcept
: data_()
{
data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
data_.v4.sin_port = 0;
data_.v4.sin_addr.s_addr = ASIO_OS_DEF(INADDR_ANY);
}
endpoint::endpoint(int family, unsigned short port_num) noexcept
: data_()
{
using namespace std; // For memcpy.
if (family == ASIO_OS_DEF(AF_INET))
{
data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
data_.v4.sin_port =
asio::detail::socket_ops::host_to_network_short(port_num);
data_.v4.sin_addr.s_addr = ASIO_OS_DEF(INADDR_ANY);
}
else
{
data_.v6.sin6_family = ASIO_OS_DEF(AF_INET6);
data_.v6.sin6_port =
asio::detail::socket_ops::host_to_network_short(port_num);
data_.v6.sin6_flowinfo = 0;
data_.v6.sin6_addr.s6_addr[0] = 0; data_.v6.sin6_addr.s6_addr[1] = 0;
data_.v6.sin6_addr.s6_addr[2] = 0; data_.v6.sin6_addr.s6_addr[3] = 0;
data_.v6.sin6_addr.s6_addr[4] = 0; data_.v6.sin6_addr.s6_addr[5] = 0;
data_.v6.sin6_addr.s6_addr[6] = 0; data_.v6.sin6_addr.s6_addr[7] = 0;
data_.v6.sin6_addr.s6_addr[8] = 0; data_.v6.sin6_addr.s6_addr[9] = 0;
data_.v6.sin6_addr.s6_addr[10] = 0; data_.v6.sin6_addr.s6_addr[11] = 0;
data_.v6.sin6_addr.s6_addr[12] = 0; data_.v6.sin6_addr.s6_addr[13] = 0;
data_.v6.sin6_addr.s6_addr[14] = 0; data_.v6.sin6_addr.s6_addr[15] = 0;
data_.v6.sin6_scope_id = 0;
}
}
endpoint::endpoint(const asio::ip::address& addr,
unsigned short port_num) noexcept
: data_()
{
using namespace std; // For memcpy.
if (addr.is_v4())
{
data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
data_.v4.sin_port =
asio::detail::socket_ops::host_to_network_short(port_num);
data_.v4.sin_addr.s_addr =
asio::detail::socket_ops::host_to_network_long(
addr.to_v4().to_uint());
}
else
{
data_.v6.sin6_family = ASIO_OS_DEF(AF_INET6);
data_.v6.sin6_port =
asio::detail::socket_ops::host_to_network_short(port_num);
data_.v6.sin6_flowinfo = 0;
asio::ip::address_v6 v6_addr = addr.to_v6();
asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
data_.v6.sin6_scope_id =
static_cast<asio::detail::u_long_type>(
v6_addr.scope_id());
}
}
void endpoint::resize(std::size_t new_size)
{
if (new_size > sizeof(asio::detail::sockaddr_storage_type))
{
asio::error_code ec(asio::error::invalid_argument);
asio::detail::throw_error(ec);
}
}
unsigned short endpoint::port() const noexcept
{
if (is_v4())
{
return asio::detail::socket_ops::network_to_host_short(
data_.v4.sin_port);
}
else
{
return asio::detail::socket_ops::network_to_host_short(
data_.v6.sin6_port);
}
}
void endpoint::port(unsigned short port_num) noexcept
{
if (is_v4())
{
data_.v4.sin_port
= asio::detail::socket_ops::host_to_network_short(port_num);
}
else
{
data_.v6.sin6_port
= asio::detail::socket_ops::host_to_network_short(port_num);
}
}
asio::ip::address endpoint::address() const noexcept
{
using namespace std; // For memcpy.
if (is_v4())
{
return asio::ip::address_v4(
asio::detail::socket_ops::network_to_host_long(
data_.v4.sin_addr.s_addr));
}
else
{
asio::ip::address_v6::bytes_type bytes;
memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
return asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
}
}
void endpoint::address(const asio::ip::address& addr) noexcept
{
endpoint tmp_endpoint(addr, port());
data_ = tmp_endpoint.data_;
}
bool operator==(const endpoint& e1, const endpoint& e2) noexcept
{
return e1.address() == e2.address() && e1.port() == e2.port();
}
bool operator<(const endpoint& e1, const endpoint& e2) noexcept
{
if (e1.address() < e2.address())
return true;
if (e1.address() != e2.address())
return false;
return e1.port() < e2.port();
}
#if !defined(ASIO_NO_IOSTREAM)
std::string endpoint::to_string() const
{
std::ostringstream tmp_os;
tmp_os.imbue(std::locale::classic());
if (is_v4())
tmp_os << address();
else
tmp_os << '[' << address() << ']';
tmp_os << ':' << port();
return tmp_os.str();
}
#endif // !defined(ASIO_NO_IOSTREAM)
} // namespace detail
} // namespace ip
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP

View File

@@ -0,0 +1,566 @@
//
// detail/socket_option.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_IP_DETAIL_SOCKET_OPTION_HPP
#define ASIO_IP_DETAIL_SOCKET_OPTION_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/throw_exception.hpp"
#include "asio/ip/address.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace ip {
namespace detail {
namespace socket_option {
// Helper template for implementing multicast enable loopback options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_enable_loopback
{
public:
#if defined(__sun) || defined(__osf__)
typedef unsigned char ipv4_value_type;
typedef unsigned char ipv6_value_type;
#elif defined(_AIX) || defined(__hpux) || defined(__QNXNTO__)
typedef unsigned char ipv4_value_type;
typedef unsigned int ipv6_value_type;
#else
typedef int ipv4_value_type;
typedef int ipv6_value_type;
#endif
// Default constructor.
multicast_enable_loopback()
: ipv4_value_(0),
ipv6_value_(0)
{
}
// Construct with a specific option value.
explicit multicast_enable_loopback(bool v)
: ipv4_value_(v ? 1 : 0),
ipv6_value_(v ? 1 : 0)
{
}
// Set the value of the boolean.
multicast_enable_loopback& operator=(bool v)
{
ipv4_value_ = v ? 1 : 0;
ipv6_value_ = v ? 1 : 0;
return *this;
}
// Get the current value of the boolean.
bool value() const
{
return !!ipv4_value_;
}
// Convert to bool.
operator bool() const
{
return !!ipv4_value_;
}
// Test for false.
bool operator!() const
{
return !ipv4_value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the boolean data.
template <typename Protocol>
void* data(const Protocol& protocol)
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the address of the boolean data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the boolean data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
// Set the size of the boolean data.
template <typename Protocol>
void resize(const Protocol& protocol, std::size_t s)
{
if (protocol.family() == PF_INET6)
{
if (s != sizeof(ipv6_value_))
{
std::length_error ex("multicast_enable_loopback socket option resize");
asio::detail::throw_exception(ex);
}
ipv4_value_ = ipv6_value_ ? 1 : 0;
}
else
{
if (s != sizeof(ipv4_value_))
{
std::length_error ex("multicast_enable_loopback socket option resize");
asio::detail::throw_exception(ex);
}
ipv6_value_ = ipv4_value_ ? 1 : 0;
}
}
private:
ipv4_value_type ipv4_value_;
ipv6_value_type ipv6_value_;
};
// Helper template for implementing unicast hops options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class unicast_hops
{
public:
// Default constructor.
unicast_hops()
: value_(0)
{
}
// Construct with a specific option value.
explicit unicast_hops(int v)
: value_(v)
{
}
// Set the value of the option.
unicast_hops& operator=(int v)
{
value_ = v;
return *this;
}
// Get the current value of the option.
int value() const
{
return value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the data.
template <typename Protocol>
int* data(const Protocol&)
{
return &value_;
}
// Get the address of the data.
template <typename Protocol>
const int* data(const Protocol&) const
{
return &value_;
}
// Get the size of the data.
template <typename Protocol>
std::size_t size(const Protocol&) const
{
return sizeof(value_);
}
// Set the size of the data.
template <typename Protocol>
void resize(const Protocol&, std::size_t s)
{
if (s != sizeof(value_))
{
std::length_error ex("unicast hops socket option resize");
asio::detail::throw_exception(ex);
}
#if defined(__hpux)
if (value_ < 0)
value_ = value_ & 0xFF;
#endif
}
private:
int value_;
};
// Helper template for implementing multicast hops options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_hops
{
public:
#if defined(ASIO_WINDOWS) && defined(UNDER_CE)
typedef int ipv4_value_type;
#else
typedef unsigned char ipv4_value_type;
#endif
typedef int ipv6_value_type;
// Default constructor.
multicast_hops()
: ipv4_value_(0),
ipv6_value_(0)
{
}
// Construct with a specific option value.
explicit multicast_hops(int v)
{
if (v < 0 || v > 255)
{
std::out_of_range ex("multicast hops value out of range");
asio::detail::throw_exception(ex);
}
ipv4_value_ = (ipv4_value_type)v;
ipv6_value_ = v;
}
// Set the value of the option.
multicast_hops& operator=(int v)
{
if (v < 0 || v > 255)
{
std::out_of_range ex("multicast hops value out of range");
asio::detail::throw_exception(ex);
}
ipv4_value_ = (ipv4_value_type)v;
ipv6_value_ = v;
return *this;
}
// Get the current value of the option.
int value() const
{
return ipv6_value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the data.
template <typename Protocol>
void* data(const Protocol& protocol)
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the address of the data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
// Set the size of the data.
template <typename Protocol>
void resize(const Protocol& protocol, std::size_t s)
{
if (protocol.family() == PF_INET6)
{
if (s != sizeof(ipv6_value_))
{
std::length_error ex("multicast hops socket option resize");
asio::detail::throw_exception(ex);
}
if (ipv6_value_ < 0)
ipv4_value_ = 0;
else if (ipv6_value_ > 255)
ipv4_value_ = 255;
else
ipv4_value_ = (ipv4_value_type)ipv6_value_;
}
else
{
if (s != sizeof(ipv4_value_))
{
std::length_error ex("multicast hops socket option resize");
asio::detail::throw_exception(ex);
}
ipv6_value_ = ipv4_value_;
}
}
private:
ipv4_value_type ipv4_value_;
ipv6_value_type ipv6_value_;
};
// Helper template for implementing ip_mreq-based options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_request
{
public:
// Default constructor.
multicast_request()
: ipv4_value_(), // Zero-initialisation gives the "any" address.
ipv6_value_() // Zero-initialisation gives the "any" address.
{
}
// Construct with multicast address only.
explicit multicast_request(const address& multicast_address)
: ipv4_value_(), // Zero-initialisation gives the "any" address.
ipv6_value_() // Zero-initialisation gives the "any" address.
{
if (multicast_address.is_v6())
{
using namespace std; // For memcpy.
address_v6 ipv6_address = multicast_address.to_v6();
address_v6::bytes_type bytes = ipv6_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
ipv6_value_.ipv6mr_interface = ipv6_address.scope_id();
}
else
{
ipv4_value_.imr_multiaddr.s_addr =
asio::detail::socket_ops::host_to_network_long(
multicast_address.to_v4().to_uint());
ipv4_value_.imr_interface.s_addr =
asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
}
}
// Construct with multicast address and IPv4 address specifying an interface.
explicit multicast_request(const address_v4& multicast_address,
const address_v4& network_interface = address_v4::any())
: ipv6_value_() // Zero-initialisation gives the "any" address.
{
ipv4_value_.imr_multiaddr.s_addr =
asio::detail::socket_ops::host_to_network_long(
multicast_address.to_uint());
ipv4_value_.imr_interface.s_addr =
asio::detail::socket_ops::host_to_network_long(
network_interface.to_uint());
}
// Construct with multicast address and IPv6 network interface index.
explicit multicast_request(
const address_v6& multicast_address,
unsigned long network_interface = 0)
: ipv4_value_() // Zero-initialisation gives the "any" address.
{
using namespace std; // For memcpy.
address_v6::bytes_type bytes = multicast_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
if (network_interface)
ipv6_value_.ipv6mr_interface = network_interface;
else
ipv6_value_.ipv6mr_interface = multicast_address.scope_id();
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the option data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the option data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
private:
asio::detail::in4_mreq_type ipv4_value_;
asio::detail::in6_mreq_type ipv6_value_;
};
// Helper template for implementing options that specify a network interface.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class network_interface
{
public:
// Default constructor.
network_interface()
{
ipv4_value_.s_addr =
asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
ipv6_value_ = 0;
}
// Construct with IPv4 interface.
explicit network_interface(const address_v4& ipv4_interface)
{
ipv4_value_.s_addr =
asio::detail::socket_ops::host_to_network_long(
ipv4_interface.to_uint());
ipv6_value_ = 0;
}
// Construct with IPv6 interface.
explicit network_interface(unsigned int ipv6_interface)
{
ipv4_value_.s_addr =
asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
ipv6_value_ = ipv6_interface;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the option data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the option data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
private:
asio::detail::in4_addr_type ipv4_value_;
unsigned int ipv6_value_;
};
} // namespace socket_option
} // namespace detail
} // namespace ip
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_IP_DETAIL_SOCKET_OPTION_HPP