@@ -308,9 +308,11 @@ namespace ix
308308 }
309309
310310 // Accept a connection.
311- // FIXME: Is this working for ipv6 ?
312- struct sockaddr_in client; // client address information
313- int clientFd; // socket connected to client
311+ // Use sockaddr_storage to accommodate both AF_INET and AF_INET6 addresses.
312+ // sockaddr_in is only 16 bytes; sockaddr_in6 is 28 bytes. On Windows, passing
313+ // a too-small buffer to accept() causes WSAEFAULT (error 10014).
314+ struct sockaddr_storage client; // client address information (IPv4 or IPv6)
315+ int clientFd; // socket connected to client
314316 socklen_t addressLen = sizeof (client);
315317 memset (&client, 0 , sizeof (client));
316318
@@ -347,7 +349,8 @@ namespace ix
347349 if (_addressFamily == AF_INET )
348350 {
349351 char remoteIp4[INET_ADDRSTRLEN ];
350- if (ix::inet_ntop (AF_INET , &client.sin_addr , remoteIp4, INET_ADDRSTRLEN ) == nullptr )
352+ auto * client4 = reinterpret_cast <struct sockaddr_in *>(&client);
353+ if (ix::inet_ntop (AF_INET , &client4->sin_addr , remoteIp4, INET_ADDRSTRLEN ) == nullptr )
351354 {
352355 int err = Socket::getErrno ();
353356 std::stringstream ss;
@@ -360,13 +363,14 @@ namespace ix
360363 continue ;
361364 }
362365
363- remotePort = ix::network_to_host_short (client. sin_port );
366+ remotePort = ix::network_to_host_short (client4-> sin_port );
364367 remoteIp = remoteIp4;
365368 }
366369 else // AF_INET6
367370 {
368371 char remoteIp6[INET6_ADDRSTRLEN ];
369- if (ix::inet_ntop (AF_INET6 , &client.sin_addr , remoteIp6, INET6_ADDRSTRLEN ) ==
372+ auto * client6 = reinterpret_cast <struct sockaddr_in6 *>(&client);
373+ if (ix::inet_ntop (AF_INET6 , &client6->sin6_addr , remoteIp6, INET6_ADDRSTRLEN ) ==
370374 nullptr )
371375 {
372376 int err = Socket::getErrno ();
@@ -380,7 +384,7 @@ namespace ix
380384 continue ;
381385 }
382386
383- remotePort = ix::network_to_host_short (client. sin_port );
387+ remotePort = ix::network_to_host_short (client6-> sin6_port );
384388 remoteIp = remoteIp6;
385389 }
386390
0 commit comments