Skip to content

Commit 6ff4869

Browse files
committed
Fix AF_INET6 accept(0 address handling
1 parent 150e3d8 commit 6ff4869

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

ixwebsocket/IXSocketServer.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,12 @@ 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
314-
socklen_t addressLen = sizeof(client);
315-
memset(&client, 0, sizeof(client));
311+
sockaddr_storage clientStorage; // client address information (v4 or v6)
312+
int clientFd; // socket connected to client
313+
socklen_t addressLen = sizeof(clientStorage);
314+
memset(&clientStorage, 0, sizeof(clientStorage));
316315

317-
if ((clientFd = accept(_serverFd, (struct sockaddr*) &client, &addressLen)) < 0)
316+
if ((clientFd = accept(_serverFd, (struct sockaddr*) &clientStorage, &addressLen)) < 0)
318317
{
319318
if (!Socket::isWaitNeeded())
320319
{
@@ -346,8 +345,10 @@ namespace ix
346345

347346
if (_addressFamily == AF_INET)
348347
{
348+
auto* client = reinterpret_cast<sockaddr_in*>(&clientStorage);
349+
349350
char remoteIp4[INET_ADDRSTRLEN];
350-
if (ix::inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
351+
if (ix::inet_ntop(AF_INET, &client->sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
351352
{
352353
int err = Socket::getErrno();
353354
std::stringstream ss;
@@ -360,13 +361,15 @@ namespace ix
360361
continue;
361362
}
362363

363-
remotePort = ix::network_to_host_short(client.sin_port);
364+
remotePort = ix::network_to_host_short(client->sin_port);
364365
remoteIp = remoteIp4;
365366
}
366367
else // AF_INET6
367368
{
369+
auto* client = reinterpret_cast<sockaddr_in6*>(&clientStorage);
370+
368371
char remoteIp6[INET6_ADDRSTRLEN];
369-
if (ix::inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) ==
372+
if (ix::inet_ntop(AF_INET6, &client->sin6_addr, remoteIp6, INET6_ADDRSTRLEN) ==
370373
nullptr)
371374
{
372375
int err = Socket::getErrno();
@@ -380,7 +383,7 @@ namespace ix
380383
continue;
381384
}
382385

383-
remotePort = ix::network_to_host_short(client.sin_port);
386+
remotePort = ix::network_to_host_short(client->sin6_port);
384387
remoteIp = remoteIp6;
385388
}
386389

0 commit comments

Comments
 (0)