libs/corosio/src/corosio/src/detail/endpoint_convert.hpp
100.0% Lines (20/20)
100.0% Functions (4/4)
-% Branches (0/0)
libs/corosio/src/corosio/src/detail/endpoint_convert.hpp
| Line | Hits | Source Code |
|---|---|---|
| 1 | // | |
| 2 | // Copyright (c) 2026 Vinnie Falco (vinnie dot falco at gmail dot com) | |
| 3 | // | |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
| 6 | // | |
| 7 | // Official repository: https://github.com/cppalliance/corosio | |
| 8 | // | |
| 9 | ||
| 10 | #ifndef BOOST_COROSIO_DETAIL_ENDPOINT_CONVERT_HPP | |
| 11 | #define BOOST_COROSIO_DETAIL_ENDPOINT_CONVERT_HPP | |
| 12 | ||
| 13 | #include <boost/corosio/endpoint.hpp> | |
| 14 | #include <boost/corosio/detail/platform.hpp> | |
| 15 | ||
| 16 | #include <cstring> | |
| 17 | ||
| 18 | #if BOOST_COROSIO_POSIX | |
| 19 | #include <netinet/in.h> | |
| 20 | #include <arpa/inet.h> | |
| 21 | #else | |
| 22 | #ifndef WIN32_LEAN_AND_MEAN | |
| 23 | #define WIN32_LEAN_AND_MEAN | |
| 24 | #endif | |
| 25 | #ifndef NOMINMAX | |
| 26 | #define NOMINMAX | |
| 27 | #endif | |
| 28 | #include <WinSock2.h> | |
| 29 | #include <Ws2tcpip.h> | |
| 30 | #endif | |
| 31 | ||
| 32 | namespace boost::corosio::detail { | |
| 33 | ||
| 34 | /** Convert IPv4 endpoint to sockaddr_in. | |
| 35 | ||
| 36 | @param ep The endpoint to convert. Must be IPv4 (is_v4() == true). | |
| 37 | @return A sockaddr_in structure with fields in network byte order. | |
| 38 | */ | |
| 39 | inline | |
| 40 | sockaddr_in | |
| 41 | 4837 | to_sockaddr_in(endpoint const& ep) noexcept |
| 42 | { | |
| 43 | 4837 | sockaddr_in sa{}; |
| 44 | 4837 | sa.sin_family = AF_INET; |
| 45 | 4837 | sa.sin_port = htons(ep.port()); |
| 46 | 4837 | auto bytes = ep.v4_address().to_bytes(); |
| 47 | 4837 | std::memcpy(&sa.sin_addr, bytes.data(), 4); |
| 48 | 4837 | return sa; |
| 49 | } | |
| 50 | ||
| 51 | /** Convert IPv6 endpoint to sockaddr_in6. | |
| 52 | ||
| 53 | @param ep The endpoint to convert. Must be IPv6 (is_v6() == true). | |
| 54 | @return A sockaddr_in6 structure with fields in network byte order. | |
| 55 | */ | |
| 56 | inline | |
| 57 | sockaddr_in6 | |
| 58 | 2 | to_sockaddr_in6(endpoint const& ep) noexcept |
| 59 | { | |
| 60 | 2 | sockaddr_in6 sa{}; |
| 61 | 2 | sa.sin6_family = AF_INET6; |
| 62 | 2 | sa.sin6_port = htons(ep.port()); |
| 63 | 2 | auto bytes = ep.v6_address().to_bytes(); |
| 64 | 2 | std::memcpy(&sa.sin6_addr, bytes.data(), 16); |
| 65 | 2 | return sa; |
| 66 | } | |
| 67 | ||
| 68 | /** Create endpoint from sockaddr_in. | |
| 69 | ||
| 70 | @param sa The sockaddr_in structure with fields in network byte order. | |
| 71 | @return An endpoint with address and port extracted from sa. | |
| 72 | */ | |
| 73 | inline | |
| 74 | endpoint | |
| 75 | 14249 | from_sockaddr_in(sockaddr_in const& sa) noexcept |
| 76 | { | |
| 77 | ipv4_address::bytes_type bytes; | |
| 78 | 14249 | std::memcpy(bytes.data(), &sa.sin_addr, 4); |
| 79 | 14249 | return endpoint(ipv4_address(bytes), ntohs(sa.sin_port)); |
| 80 | } | |
| 81 | ||
| 82 | /** Create endpoint from sockaddr_in6. | |
| 83 | ||
| 84 | @param sa The sockaddr_in6 structure with fields in network byte order. | |
| 85 | @return An endpoint with address and port extracted from sa. | |
| 86 | */ | |
| 87 | inline | |
| 88 | endpoint | |
| 89 | 2 | from_sockaddr_in6(sockaddr_in6 const& sa) noexcept |
| 90 | { | |
| 91 | ipv6_address::bytes_type bytes; | |
| 92 | 2 | std::memcpy(bytes.data(), &sa.sin6_addr, 16); |
| 93 | 2 | return endpoint(ipv6_address(bytes), ntohs(sa.sin6_port)); |
| 94 | } | |
| 95 | ||
| 96 | } // namespace boost::corosio::detail | |
| 97 | ||
| 98 | #endif | |
| 99 |