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