99.01% Lines (100/101) 100.00% Functions (16/16)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_NATIVE_DETAIL_ENDPOINT_CONVERT_HPP 10   #ifndef BOOST_COROSIO_NATIVE_DETAIL_ENDPOINT_CONVERT_HPP
11   #define BOOST_COROSIO_NATIVE_DETAIL_ENDPOINT_CONVERT_HPP 11   #define BOOST_COROSIO_NATIVE_DETAIL_ENDPOINT_CONVERT_HPP
12   12  
13   #include <boost/corosio/endpoint.hpp> 13   #include <boost/corosio/endpoint.hpp>
14   #include <boost/corosio/family.hpp> 14   #include <boost/corosio/family.hpp>
15   #include <boost/corosio/local_endpoint.hpp> 15   #include <boost/corosio/local_endpoint.hpp>
16   #include <boost/corosio/detail/platform.hpp> 16   #include <boost/corosio/detail/platform.hpp>
17   17  
18   #include <algorithm> 18   #include <algorithm>
19   #include <cstring> 19   #include <cstring>
20   20  
21   #if BOOST_COROSIO_POSIX 21   #if BOOST_COROSIO_POSIX
22   #include <sys/socket.h> 22   #include <sys/socket.h>
23   #include <sys/un.h> 23   #include <sys/un.h>
24   #include <netinet/in.h> 24   #include <netinet/in.h>
25   #include <arpa/inet.h> 25   #include <arpa/inet.h>
26   #else 26   #else
27   #ifndef WIN32_LEAN_AND_MEAN 27   #ifndef WIN32_LEAN_AND_MEAN
28   #define WIN32_LEAN_AND_MEAN 28   #define WIN32_LEAN_AND_MEAN
29   #endif 29   #endif
30   #ifndef NOMINMAX 30   #ifndef NOMINMAX
31   #define NOMINMAX 31   #define NOMINMAX
32   #endif 32   #endif
33   #include <winsock2.h> 33   #include <winsock2.h>
34   #include <ws2tcpip.h> 34   #include <ws2tcpip.h>
35   #endif 35   #endif
36   36  
37   #include <cstddef> // offsetof 37   #include <cstddef> // offsetof
38   #include <tuple> 38   #include <tuple>
39   39  
40   #ifndef AF_UNIX 40   #ifndef AF_UNIX
41   #define AF_UNIX 1 41   #define AF_UNIX 1
42   #endif 42   #endif
43   43  
44   namespace boost::corosio::detail { 44   namespace boost::corosio::detail {
45   45  
46   /** Convert IPv4 endpoint to sockaddr_in. 46   /** Convert IPv4 endpoint to sockaddr_in.
47   47  
48   @param ep The endpoint to convert. Must be IPv4 (is_v4() == true); 48   @param ep The endpoint to convert. Must be IPv4 (is_v4() == true);
49   a wrong-family endpoint terminates (throwing conversion inside 49   a wrong-family endpoint terminates (throwing conversion inside
50   a noexcept function). 50   a noexcept function).
51   @return A sockaddr_in structure with fields in network byte order. 51   @return A sockaddr_in structure with fields in network byte order.
52   */ 52   */
53   inline sockaddr_in 53   inline sockaddr_in
HITCBC 54   10533 to_sockaddr_in(endpoint const& ep) noexcept 54   10792 to_sockaddr_in(endpoint const& ep) noexcept
55   { 55   {
HITCBC 56   10533 sockaddr_in sa{}; 56   10792 sockaddr_in sa{};
HITCBC 57   10533 sa.sin_family = AF_INET; 57   10792 sa.sin_family = AF_INET;
HITCBC 58   10533 sa.sin_port = htons(ep.port()); 58   10792 sa.sin_port = htons(ep.port());
HITCBC 59   10533 auto bytes = ep.address().to_v4().to_bytes(); 59   10792 auto bytes = ep.address().to_v4().to_bytes();
HITCBC 60   10533 std::memcpy(&sa.sin_addr, bytes.data(), 4); 60   10792 std::memcpy(&sa.sin_addr, bytes.data(), 4);
HITCBC 61   10533 return sa; 61   10792 return sa;
62   } 62   }
63   63  
64   /** Convert IPv6 endpoint to sockaddr_in6. 64   /** Convert IPv6 endpoint to sockaddr_in6.
65   65  
66   @param ep The endpoint to convert. Must be IPv6 (is_v6() == true); 66   @param ep The endpoint to convert. Must be IPv6 (is_v6() == true);
67   a wrong-family endpoint terminates (throwing conversion inside 67   a wrong-family endpoint terminates (throwing conversion inside
68   a noexcept function). 68   a noexcept function).
69   @return A sockaddr_in6 structure with fields in network byte order. 69   @return A sockaddr_in6 structure with fields in network byte order.
70   */ 70   */
71   inline sockaddr_in6 71   inline sockaddr_in6
HITCBC 72   56 to_sockaddr_in6(endpoint const& ep) noexcept 72   56 to_sockaddr_in6(endpoint const& ep) noexcept
73   { 73   {
HITCBC 74   56 sockaddr_in6 sa{}; 74   56 sockaddr_in6 sa{};
HITCBC 75   56 sa.sin6_family = AF_INET6; 75   56 sa.sin6_family = AF_INET6;
HITCBC 76   56 sa.sin6_port = htons(ep.port()); 76   56 sa.sin6_port = htons(ep.port());
HITCBC 77   56 auto addr = ep.address().to_v6(); 77   56 auto addr = ep.address().to_v6();
HITCBC 78   56 auto bytes = addr.to_bytes(); 78   56 auto bytes = addr.to_bytes();
HITCBC 79   56 std::memcpy(&sa.sin6_addr, bytes.data(), 16); 79   56 std::memcpy(&sa.sin6_addr, bytes.data(), 16);
HITCBC 80   56 sa.sin6_scope_id = addr.scope_id(); 80   56 sa.sin6_scope_id = addr.scope_id();
HITCBC 81   56 return sa; 81   56 return sa;
82   } 82   }
83   83  
84   /** Create endpoint from sockaddr_in. 84   /** Create endpoint from sockaddr_in.
85   85  
86   @param sa The sockaddr_in structure with fields in network byte order. 86   @param sa The sockaddr_in structure with fields in network byte order.
87   @return An endpoint with address and port extracted from sa. 87   @return An endpoint with address and port extracted from sa.
88   */ 88   */
89   inline endpoint 89   inline endpoint
HITCBC 90   26697 from_sockaddr_in(sockaddr_in const& sa) noexcept 90   27155 from_sockaddr_in(sockaddr_in const& sa) noexcept
91   { 91   {
92   ipv4_address::bytes_type bytes; 92   ipv4_address::bytes_type bytes;
HITCBC 93   26697 std::memcpy(bytes.data(), &sa.sin_addr, 4); 93   27155 std::memcpy(bytes.data(), &sa.sin_addr, 4);
HITCBC 94   26697 return endpoint(ipv4_address(bytes), ntohs(sa.sin_port)); 94   27155 return endpoint(ipv4_address(bytes), ntohs(sa.sin_port));
95   } 95   }
96   96  
97   /** Create endpoint from sockaddr_in6. 97   /** Create endpoint from sockaddr_in6.
98   98  
99   @param sa The sockaddr_in6 structure with fields in network byte order. 99   @param sa The sockaddr_in6 structure with fields in network byte order.
100   @return An endpoint with address and port extracted from sa. 100   @return An endpoint with address and port extracted from sa.
101   */ 101   */
102   inline endpoint 102   inline endpoint
HITCBC 103   87 from_sockaddr_in6(sockaddr_in6 const& sa) noexcept 103   87 from_sockaddr_in6(sockaddr_in6 const& sa) noexcept
104   { 104   {
105   ipv6_address::bytes_type bytes; 105   ipv6_address::bytes_type bytes;
HITCBC 106   87 std::memcpy(bytes.data(), &sa.sin6_addr, 16); 106   87 std::memcpy(bytes.data(), &sa.sin6_addr, 16);
HITCBC 107   87 return endpoint(ipv6_address(bytes, sa.sin6_scope_id), ntohs(sa.sin6_port)); 107   87 return endpoint(ipv6_address(bytes, sa.sin6_scope_id), ntohs(sa.sin6_port));
108   } 108   }
109   109  
110   /** Convert an IPv4 endpoint to an IPv4-mapped IPv6 sockaddr_in6. 110   /** Convert an IPv4 endpoint to an IPv4-mapped IPv6 sockaddr_in6.
111   111  
112   Produces a `sockaddr_in6` with the `::ffff:` prefix, suitable 112   Produces a `sockaddr_in6` with the `::ffff:` prefix, suitable
113   for passing an IPv4 destination to a dual-stack IPv6 socket. 113   for passing an IPv4 destination to a dual-stack IPv6 socket.
114   114  
115   @param ep The endpoint to convert. Must be IPv4 (is_v4() == true); 115   @param ep The endpoint to convert. Must be IPv4 (is_v4() == true);
116   a wrong-family endpoint terminates (throwing conversion inside 116   a wrong-family endpoint terminates (throwing conversion inside
117   a noexcept function). 117   a noexcept function).
118   @return A sockaddr_in6 with the IPv4-mapped address. 118   @return A sockaddr_in6 with the IPv4-mapped address.
119   */ 119   */
120   inline sockaddr_in6 120   inline sockaddr_in6
HITCBC 121   2 to_v4_mapped_sockaddr_in6(endpoint const& ep) noexcept 121   2 to_v4_mapped_sockaddr_in6(endpoint const& ep) noexcept
122   { 122   {
HITCBC 123   2 sockaddr_in6 sa{}; 123   2 sockaddr_in6 sa{};
HITCBC 124   2 sa.sin6_family = AF_INET6; 124   2 sa.sin6_family = AF_INET6;
HITCBC 125   2 sa.sin6_port = htons(ep.port()); 125   2 sa.sin6_port = htons(ep.port());
126   // The mapping constructor supplies the ::ffff:0:0/96 prefix 126   // The mapping constructor supplies the ::ffff:0:0/96 prefix
HITCBC 127   2 auto bytes = ipv6_address(ep.address().to_v4()).to_bytes(); 127   2 auto bytes = ipv6_address(ep.address().to_v4()).to_bytes();
HITCBC 128   2 std::memcpy(&sa.sin6_addr, bytes.data(), 16); 128   2 std::memcpy(&sa.sin6_addr, bytes.data(), 16);
HITCBC 129   2 return sa; 129   2 return sa;
130   } 130   }
131   131  
132   /** Convert endpoint to sockaddr_storage. 132   /** Convert endpoint to sockaddr_storage.
133   133  
134   Dispatches to @ref to_sockaddr_in or @ref to_sockaddr_in6 134   Dispatches to @ref to_sockaddr_in or @ref to_sockaddr_in6
135   based on the endpoint's address family. 135   based on the endpoint's address family.
136   136  
137   @param ep The endpoint to convert. 137   @param ep The endpoint to convert.
138   @param storage Output parameter filled with the sockaddr. 138   @param storage Output parameter filled with the sockaddr.
139   @return The length of the filled sockaddr structure. 139   @return The length of the filled sockaddr structure.
140   */ 140   */
141   inline socklen_t 141   inline socklen_t
HITCBC 142   10570 to_sockaddr(endpoint const& ep, sockaddr_storage& storage) noexcept 142   10829 to_sockaddr(endpoint const& ep, sockaddr_storage& storage) noexcept
143   { 143   {
HITCBC 144   10570 std::memset(&storage, 0, sizeof(storage)); 144   10829 std::memset(&storage, 0, sizeof(storage));
HITCBC 145   10570 if (ep.is_v4()) 145   10829 if (ep.is_v4())
146   { 146   {
HITCBC 147   10519 auto sa = to_sockaddr_in(ep); 147   10778 auto sa = to_sockaddr_in(ep);
HITCBC 148   10519 std::memcpy(&storage, &sa, sizeof(sa)); 148   10778 std::memcpy(&storage, &sa, sizeof(sa));
HITCBC 149   10519 return sizeof(sa); 149   10778 return sizeof(sa);
150   } 150   }
HITCBC 151   51 auto sa6 = to_sockaddr_in6(ep); 151   51 auto sa6 = to_sockaddr_in6(ep);
HITCBC 152   51 std::memcpy(&storage, &sa6, sizeof(sa6)); 152   51 std::memcpy(&storage, &sa6, sizeof(sa6));
HITCBC 153   51 return sizeof(sa6); 153   51 return sizeof(sa6);
154   } 154   }
155   155  
156   /** Convert endpoint to sockaddr_storage for a specific socket family. 156   /** Convert endpoint to sockaddr_storage for a specific socket family.
157   157  
158   When the socket is AF_INET6 and the endpoint is IPv4, the address 158   When the socket is AF_INET6 and the endpoint is IPv4, the address
159   is converted to an IPv4-mapped IPv6 address (`::ffff:x.x.x.x`) so 159   is converted to an IPv4-mapped IPv6 address (`::ffff:x.x.x.x`) so
160   dual-stack sockets can connect to IPv4 destinations. 160   dual-stack sockets can connect to IPv4 destinations.
161   161  
162   @param ep The endpoint to convert. 162   @param ep The endpoint to convert.
163   @param socket_family The address family of the socket (AF_INET or 163   @param socket_family The address family of the socket (AF_INET or
164   AF_INET6). 164   AF_INET6).
165   @param storage Output parameter filled with the sockaddr. 165   @param storage Output parameter filled with the sockaddr.
166   @return The length of the filled sockaddr structure. 166   @return The length of the filled sockaddr structure.
167   */ 167   */
168   inline socklen_t 168   inline socklen_t
HITCBC 169   8307 to_sockaddr( 169   8566 to_sockaddr(
170   endpoint const& ep, int socket_family, sockaddr_storage& storage) noexcept 170   endpoint const& ep, int socket_family, sockaddr_storage& storage) noexcept
171   { 171   {
172   // IPv4 endpoint on IPv6 socket: use IPv4-mapped address. 172   // IPv4 endpoint on IPv6 socket: use IPv4-mapped address.
173   // The reverse mismatch (v4-mapped IPv6 endpoint on an AF_INET 173   // The reverse mismatch (v4-mapped IPv6 endpoint on an AF_INET
174   // socket) is deliberately not bridged; the kernel rejects it. 174   // socket) is deliberately not bridged; the kernel rejects it.
HITCBC 175   8307 if (ep.is_v4() && socket_family == AF_INET6) 175   8566 if (ep.is_v4() && socket_family == AF_INET6)
176   { 176   {
HITCBC 177   2 std::memset(&storage, 0, sizeof(storage)); 177   2 std::memset(&storage, 0, sizeof(storage));
HITCBC 178   2 auto sa6 = to_v4_mapped_sockaddr_in6(ep); 178   2 auto sa6 = to_v4_mapped_sockaddr_in6(ep);
HITCBC 179   2 std::memcpy(&storage, &sa6, sizeof(sa6)); 179   2 std::memcpy(&storage, &sa6, sizeof(sa6));
HITCBC 180   2 return sizeof(sa6); 180   2 return sizeof(sa6);
181   } 181   }
HITCBC 182   8305 return to_sockaddr(ep, storage); 182   8564 return to_sockaddr(ep, storage);
183   } 183   }
184   184  
185   /** Create endpoint from sockaddr_storage. 185   /** Create endpoint from sockaddr_storage.
186   186  
187   Dispatches on `ss_family` to reconstruct the appropriate 187   Dispatches on `ss_family` to reconstruct the appropriate
188   IPv4 or IPv6 endpoint. 188   IPv4 or IPv6 endpoint.
189   189  
190   @param storage The sockaddr_storage with fields in network byte order. 190   @param storage The sockaddr_storage with fields in network byte order.
191   @return An endpoint with address and port extracted from storage. 191   @return An endpoint with address and port extracted from storage.
192   */ 192   */
193   inline endpoint 193   inline endpoint
HITCBC 194   26757 from_sockaddr(sockaddr_storage const& storage) noexcept 194   27215 from_sockaddr(sockaddr_storage const& storage) noexcept
195   { 195   {
HITCBC 196   26757 if (storage.ss_family == AF_INET) 196   27215 if (storage.ss_family == AF_INET)
197   { 197   {
198   sockaddr_in sa; 198   sockaddr_in sa;
HITCBC 199   26674 std::memcpy(&sa, &storage, sizeof(sa)); 199   27132 std::memcpy(&sa, &storage, sizeof(sa));
HITCBC 200   26674 return from_sockaddr_in(sa); 200   27132 return from_sockaddr_in(sa);
201   } 201   }
HITCBC 202   83 if (storage.ss_family == AF_INET6) 202   83 if (storage.ss_family == AF_INET6)
203   { 203   {
204   sockaddr_in6 sa6; 204   sockaddr_in6 sa6;
HITCBC 205   83 std::memcpy(&sa6, &storage, sizeof(sa6)); 205   83 std::memcpy(&sa6, &storage, sizeof(sa6));
HITCBC 206   83 return from_sockaddr_in6(sa6); 206   83 return from_sockaddr_in6(sa6);
207   } 207   }
MISUBC 208   ✗ return endpoint{}; 208   ✗ return endpoint{};
209   } 209   }
210   210  
211   /** Convert a native address family to the portable one. 211   /** Convert a native address family to the portable one.
212   212  
213   @param af The native family (`AF_INET`, `AF_INET6`, or anything 213   @param af The native family (`AF_INET`, `AF_INET6`, or anything
214   else, which maps to v4 as the inert value). 214   else, which maps to v4 as the inert value).
215   @return The portable family. 215   @return The portable family.
216   */ 216   */
217   inline corosio::family 217   inline corosio::family
HITCBC 218   8500 to_family(int af) noexcept 218   8759 to_family(int af) noexcept
219   { 219   {
HITCBC 220   8500 return af == AF_INET6 ? corosio::family::v6 : corosio::family::v4; 220   8759 return af == AF_INET6 ? corosio::family::v6 : corosio::family::v4;
221   } 221   }
222   222  
223   /** Convert the portable address family to the native one. 223   /** Convert the portable address family to the native one.
224   224  
225   @param f The portable family. 225   @param f The portable family.
226   @return `AF_INET` or `AF_INET6`. 226   @return `AF_INET` or `AF_INET6`.
227   */ 227   */
228   inline int 228   inline int
HITCBC 229   10728 native_family(corosio::family f) noexcept 229   10987 native_family(corosio::family f) noexcept
230   { 230   {
HITCBC 231   10728 return f == corosio::family::v6 ? AF_INET6 : AF_INET; 231   10987 return f == corosio::family::v6 ? AF_INET6 : AF_INET;
232   } 232   }
233   233  
234   /** Return the native address family for an endpoint. 234   /** Return the native address family for an endpoint.
235   235  
236   @param ep The endpoint to query. 236   @param ep The endpoint to query.
237   @return `AF_INET` for IPv4, `AF_INET6` for IPv6. 237   @return `AF_INET` for IPv4, `AF_INET6` for IPv6.
238   */ 238   */
239   inline int 239   inline int
240   endpoint_family(endpoint const& ep) noexcept 240   endpoint_family(endpoint const& ep) noexcept
241   { 241   {
242   return ep.is_v6() ? AF_INET6 : AF_INET; 242   return ep.is_v6() ? AF_INET6 : AF_INET;
243   } 243   }
244   244  
245   /** Return the address family of a socket descriptor. 245   /** Return the address family of a socket descriptor.
246   246  
247   @param fd The socket file descriptor. 247   @param fd The socket file descriptor.
248   @return AF_INET, AF_INET6, or AF_UNSPEC on failure. 248   @return AF_INET, AF_INET6, or AF_UNSPEC on failure.
249   */ 249   */
250   inline int 250   inline int
HITCBC 251   17008 socket_family( 251   17526 socket_family(
252   #if BOOST_COROSIO_POSIX 252   #if BOOST_COROSIO_POSIX
253   int fd 253   int fd
254   #else 254   #else
255   std::uintptr_t fd 255   std::uintptr_t fd
256   #endif 256   #endif
257   ) noexcept 257   ) noexcept
258   { 258   {
HITCBC 259   17008 sockaddr_storage storage{}; 259   17526 sockaddr_storage storage{};
HITCBC 260   17008 socklen_t len = sizeof(storage); 260   17526 socklen_t len = sizeof(storage);
HITCBC 261   17008 if (getsockname( 261   17526 if (getsockname(
262   #if BOOST_COROSIO_POSIX 262   #if BOOST_COROSIO_POSIX
263   fd, 263   fd,
264   #else 264   #else
265   static_cast<SOCKET>(fd), 265   static_cast<SOCKET>(fd),
266   #endif 266   #endif
HITCBC 267   17008 reinterpret_cast<sockaddr*>(&storage), &len) != 0) 267   17526 reinterpret_cast<sockaddr*>(&storage), &len) != 0)
HITCBC 268   2 return AF_UNSPEC; 268   2 return AF_UNSPEC;
HITCBC 269   17006 return storage.ss_family; 269   17524 return storage.ss_family;
270   } 270   }
271   271  
272   //---------------------------------------------------------- 272   //----------------------------------------------------------
273   // local_endpoint (AF_UNIX) conversions 273   // local_endpoint (AF_UNIX) conversions
274   //---------------------------------------------------------- 274   //----------------------------------------------------------
275   275  
276   // Platform-agnostic sockaddr_un alias. POSIX uses the real 276   // Platform-agnostic sockaddr_un alias. POSIX uses the real
277   // sockaddr_un from <sys/un.h>; Windows uses a private struct 277   // sockaddr_un from <sys/un.h>; Windows uses a private struct
278   // matching the layout (same approach as Boost.Asio). 278   // matching the layout (same approach as Boost.Asio).
279   #if BOOST_COROSIO_POSIX 279   #if BOOST_COROSIO_POSIX
280   using un_sa_t = sockaddr_un; 280   using un_sa_t = sockaddr_un;
281   #else 281   #else
282   struct un_sa_t 282   struct un_sa_t
283   { 283   {
284   u_short sun_family; 284   u_short sun_family;
285   char sun_path[108]; 285   char sun_path[108];
286   }; 286   };
287   #endif 287   #endif
288   288  
289   /** Convert a local_endpoint to sockaddr_storage. 289   /** Convert a local_endpoint to sockaddr_storage.
290   290  
291   @param ep The local endpoint to convert. 291   @param ep The local endpoint to convert.
292   @param storage Output parameter filled with the sockaddr_un. 292   @param storage Output parameter filled with the sockaddr_un.
293   @return The length of the filled sockaddr structure. 293   @return The length of the filled sockaddr structure.
294   */ 294   */
295   inline socklen_t 295   inline socklen_t
HITCBC 296   290 to_sockaddr(local_endpoint const& ep, sockaddr_storage& storage) noexcept 296   290 to_sockaddr(local_endpoint const& ep, sockaddr_storage& storage) noexcept
297   { 297   {
HITCBC 298   290 std::memset(&storage, 0, sizeof(storage)); 298   290 std::memset(&storage, 0, sizeof(storage));
HITCBC 299   290 un_sa_t sa{}; 299   290 un_sa_t sa{};
HITCBC 300   290 sa.sun_family = AF_UNIX; 300   290 sa.sun_family = AF_UNIX;
HITCBC 301   290 auto path = ep.path(); 301   290 auto path = ep.path();
HITCBC 302   580 auto copy_len = (std::min)( 302   580 auto copy_len = (std::min)(
HITCBC 303   290 path.size(), 303   290 path.size(),
HITCBC 304   290 (std::min)(local_endpoint::max_path_length, sizeof(sa.sun_path))); 304   290 (std::min)(local_endpoint::max_path_length, sizeof(sa.sun_path)));
HITCBC 305   290 if (copy_len > 0) 305   290 if (copy_len > 0)
HITCBC 306   290 std::memcpy(sa.sun_path, path.data(), copy_len); 306   290 std::memcpy(sa.sun_path, path.data(), copy_len);
HITCBC 307   290 std::memcpy(&storage, &sa, sizeof(sa)); 307   290 std::memcpy(&storage, &sa, sizeof(sa));
308   308  
HITCBC 309   290 if (ep.is_abstract()) 309   290 if (ep.is_abstract())
HITCBC 310   6 return static_cast<socklen_t>(offsetof(un_sa_t, sun_path) + copy_len); 310   6 return static_cast<socklen_t>(offsetof(un_sa_t, sun_path) + copy_len);
HITCBC 311   284 return static_cast<socklen_t>(sizeof(sa)); 311   284 return static_cast<socklen_t>(sizeof(sa));
312   } 312   }
313   313  
314   /** Convert a local_endpoint to sockaddr_storage (family-aware overload). 314   /** Convert a local_endpoint to sockaddr_storage (family-aware overload).
315   315  
316   The socket_family parameter is ignored for Unix sockets since 316   The socket_family parameter is ignored for Unix sockets since
317   there is no dual-stack mapping. 317   there is no dual-stack mapping.
318   318  
319   @param ep The local endpoint to convert. 319   @param ep The local endpoint to convert.
320   @param socket_family Ignored. 320   @param socket_family Ignored.
321   @param storage Output parameter filled with the sockaddr_un. 321   @param storage Output parameter filled with the sockaddr_un.
322   @return The length of the filled sockaddr structure. 322   @return The length of the filled sockaddr structure.
323   */ 323   */
324   inline socklen_t 324   inline socklen_t
HITCBC 325   201 to_sockaddr( 325   201 to_sockaddr(
326   local_endpoint const& ep, 326   local_endpoint const& ep,
327   int /*socket_family*/, 327   int /*socket_family*/,
328   sockaddr_storage& storage) noexcept 328   sockaddr_storage& storage) noexcept
329   { 329   {
HITCBC 330   201 return to_sockaddr(ep, storage); 330   201 return to_sockaddr(ep, storage);
331   } 331   }
332   332  
333   /** Create a local_endpoint from sockaddr_storage. 333   /** Create a local_endpoint from sockaddr_storage.
334   334  
335   @param storage The sockaddr_storage (must have ss_family == AF_UNIX). 335   @param storage The sockaddr_storage (must have ss_family == AF_UNIX).
336   @param len The address length returned by the kernel. 336   @param len The address length returned by the kernel.
337   @return A local_endpoint with the path extracted from the 337   @return A local_endpoint with the path extracted from the
338   sockaddr_un, or an empty endpoint if the family is not AF_UNIX. 338   sockaddr_un, or an empty endpoint if the family is not AF_UNIX.
339   */ 339   */
340   inline local_endpoint 340   inline local_endpoint
HITCBC 341   946 from_sockaddr_local(sockaddr_storage const& storage, socklen_t len) noexcept 341   946 from_sockaddr_local(sockaddr_storage const& storage, socklen_t len) noexcept
342   { 342   {
HITCBC 343   946 if (storage.ss_family != AF_UNIX) 343   946 if (storage.ss_family != AF_UNIX)
HITCBC 344   1 return local_endpoint{}; 344   1 return local_endpoint{};
345   345  
HITCBC 346   945 un_sa_t sa{}; 346   945 un_sa_t sa{};
HITCBC 347   945 std::memcpy( 347   945 std::memcpy(
HITCBC 348   945 &sa, &storage, (std::min)(static_cast<std::size_t>(len), sizeof(sa))); 348   945 &sa, &storage, (std::min)(static_cast<std::size_t>(len), sizeof(sa)));
349   349  
HITCBC 350   945 auto path_offset = offsetof(un_sa_t, sun_path); 350   945 auto path_offset = offsetof(un_sa_t, sun_path);
HITCBC 351   945 if (static_cast<std::size_t>(len) <= path_offset) 351   945 if (static_cast<std::size_t>(len) <= path_offset)
HITCBC 352   707 return local_endpoint{}; 352   707 return local_endpoint{};
353   353  
354   // Clamp to the buffer: a foreign len may overstate the payload, 354   // Clamp to the buffer: a foreign len may overstate the payload,
355   // and sun_path is the struct's last member. 355   // and sun_path is the struct's last member.
HITCBC 356   476 auto path_len = (std::min)(static_cast<std::size_t>(len) - path_offset, 356   476 auto path_len = (std::min)(static_cast<std::size_t>(len) - path_offset,
HITCBC 357   238 sizeof(sa.sun_path)); 357   238 sizeof(sa.sun_path));
358   358  
359   // Non-abstract paths may be null-terminated by the kernel 359   // Non-abstract paths may be null-terminated by the kernel
HITCBC 360   238 if (path_len > 0 && sa.sun_path[0] != '\0') 360   238 if (path_len > 0 && sa.sun_path[0] != '\0')
361   { 361   {
362   auto* end = 362   auto* end =
HITCBC 363   232 static_cast<char const*>(std::memchr(sa.sun_path, '\0', path_len)); 363   232 static_cast<char const*>(std::memchr(sa.sun_path, '\0', path_len));
HITCBC 364   232 if (end) 364   232 if (end)
HITCBC 365   232 path_len = static_cast<std::size_t>(end - sa.sun_path); 365   232 path_len = static_cast<std::size_t>(end - sa.sun_path);
366   } 366   }
367   367  
368   // A foreign sun_path may exceed corosio's cap; the factory then 368   // A foreign sun_path may exceed corosio's cap; the factory then
369   // yields the empty endpoint, which is what we return. 369   // yields the empty endpoint, which is what we return.
HITCBC 370   238 return std::get<1>( 370   238 return std::get<1>(
HITCBC 371   476 make_local_endpoint(std::string_view(sa.sun_path, path_len))); 371   476 make_local_endpoint(std::string_view(sa.sun_path, path_len)));
372   } 372   }
373   373  
374   //---------------------------------------------------------- 374   //----------------------------------------------------------
375   // Tag-dispatch helpers for templatized reactor code. 375   // Tag-dispatch helpers for templatized reactor code.
376   // Overload resolution selects the correct conversion based 376   // Overload resolution selects the correct conversion based
377   // on the Endpoint type. 377   // on the Endpoint type.
378   //---------------------------------------------------------- 378   //----------------------------------------------------------
379   379  
380   /** Convert sockaddr_storage to an IP endpoint (tag overload). 380   /** Convert sockaddr_storage to an IP endpoint (tag overload).
381   381  
382   @param storage The sockaddr_storage with fields in network byte order. 382   @param storage The sockaddr_storage with fields in network byte order.
383   @param len The address length returned by the kernel. 383   @param len The address length returned by the kernel.
384   @return An endpoint with address and port extracted from storage. 384   @return An endpoint with address and port extracted from storage.
385   */ 385   */
386   inline endpoint 386   inline endpoint
HITCBC 387   26756 from_sockaddr_as( 387   27214 from_sockaddr_as(
388   sockaddr_storage const& storage, 388   sockaddr_storage const& storage,
389   socklen_t /*len*/, 389   socklen_t /*len*/,
390   endpoint const&) noexcept 390   endpoint const&) noexcept
391   { 391   {
HITCBC 392   26756 return from_sockaddr(storage); 392   27214 return from_sockaddr(storage);
393   } 393   }
394   394  
395   /** Convert sockaddr_storage to a local_endpoint (tag overload). 395   /** Convert sockaddr_storage to a local_endpoint (tag overload).
396   396  
397   @param storage The sockaddr_storage. 397   @param storage The sockaddr_storage.
398   @param len The address length returned by the kernel. 398   @param len The address length returned by the kernel.
399   @return A local_endpoint with path extracted from storage. 399   @return A local_endpoint with path extracted from storage.
400   */ 400   */
401   inline local_endpoint 401   inline local_endpoint
HITCBC 402   946 from_sockaddr_as( 402   946 from_sockaddr_as(
403   sockaddr_storage const& storage, 403   sockaddr_storage const& storage,
404   socklen_t len, 404   socklen_t len,
405   local_endpoint const&) noexcept 405   local_endpoint const&) noexcept
406   { 406   {
HITCBC 407   946 return from_sockaddr_local(storage, len); 407   946 return from_sockaddr_local(storage, len);
408   } 408   }
409   409  
410   } // namespace boost::corosio::detail 410   } // namespace boost::corosio::detail
411   411  
412   #endif 412   #endif