include/boost/corosio/detail/endpoint_convert.hpp
100.0% Lines (20/20)
100.0% Functions (4/4)
37.5% Branches (3/8)
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Vinnie Falco ([email protected]) | |||
| 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 <sys/socket.h> | |||
| 20 | #include <netinet/in.h> | |||
| 21 | #include <arpa/inet.h> | |||
| 22 | #else | |||
| 23 | #ifndef WIN32_LEAN_AND_MEAN | |||
| 24 | #define WIN32_LEAN_AND_MEAN | |||
| 25 | #endif | |||
| 26 | #ifndef NOMINMAX | |||
| 27 | #define NOMINMAX | |||
| 28 | #endif | |||
| 29 | #include <WinSock2.h> | |||
| 30 | #include <Ws2tcpip.h> | |||
| 31 | #endif | |||
| 32 | ||||
| 33 | namespace boost::corosio::detail { | |||
| 34 | ||||
| 35 | /** Convert IPv4 endpoint to sockaddr_in. | |||
| 36 | ||||
| 37 | @param ep The endpoint to convert. Must be IPv4 (is_v4() == true). | |||
| 38 | @return A sockaddr_in structure with fields in network byte order. | |||
| 39 | */ | |||
| 40 | inline sockaddr_in | |||
| 41 | 8159 | to_sockaddr_in(endpoint const& ep) noexcept | ||
| 42 | { | |||
| 43 | 8159 | sockaddr_in sa{}; | ||
| 44 | 8159 | sa.sin_family = AF_INET; | ||
| 45 |
1/2✓ Branch 0 taken 8159 times.
✗ Branch 1 not taken.
|
8159 | sa.sin_port = htons(ep.port()); | |
| 46 | 8159 | auto bytes = ep.v4_address().to_bytes(); | ||
| 47 | 8159 | std::memcpy(&sa.sin_addr, bytes.data(), 4); | ||
| 48 | 8159 | 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 sockaddr_in6 | |||
| 57 | 2 | to_sockaddr_in6(endpoint const& ep) noexcept | ||
| 58 | { | |||
| 59 | 2 | sockaddr_in6 sa{}; | ||
| 60 | 2 | sa.sin6_family = AF_INET6; | ||
| 61 | 2 | sa.sin6_port = htons(ep.port()); | ||
| 62 | 2 | auto bytes = ep.v6_address().to_bytes(); | ||
| 63 | 2 | std::memcpy(&sa.sin6_addr, bytes.data(), 16); | ||
| 64 | 2 | return sa; | ||
| 65 | } | |||
| 66 | ||||
| 67 | /** Create endpoint from sockaddr_in. | |||
| 68 | ||||
| 69 | @param sa The sockaddr_in structure with fields in network byte order. | |||
| 70 | @return An endpoint with address and port extracted from sa. | |||
| 71 | */ | |||
| 72 | inline endpoint | |||
| 73 | 22429 | from_sockaddr_in(sockaddr_in const& sa) noexcept | ||
| 74 | { | |||
| 75 | ipv4_address::bytes_type bytes; | |||
| 76 | 22429 | std::memcpy(bytes.data(), &sa.sin_addr, 4); | ||
| 77 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 22429 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
22429 | return endpoint(ipv4_address(bytes), ntohs(sa.sin_port)); | |
| 78 | } | |||
| 79 | ||||
| 80 | /** Create endpoint from sockaddr_in6. | |||
| 81 | ||||
| 82 | @param sa The sockaddr_in6 structure with fields in network byte order. | |||
| 83 | @return An endpoint with address and port extracted from sa. | |||
| 84 | */ | |||
| 85 | inline endpoint | |||
| 86 | 8 | from_sockaddr_in6(sockaddr_in6 const& sa) noexcept | ||
| 87 | { | |||
| 88 | ipv6_address::bytes_type bytes; | |||
| 89 | 8 | std::memcpy(bytes.data(), &sa.sin6_addr, 16); | ||
| 90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | return endpoint(ipv6_address(bytes), ntohs(sa.sin6_port)); | |
| 91 | } | |||
| 92 | ||||
| 93 | } // namespace boost::corosio::detail | |||
| 94 | ||||
| 95 | #endif | |||
| 96 |