net_socket: getsockname() now works with unnamed sockets

getsockname() now set the socket family and zero all other fields
of sockaddr if an unnamed socket is passed as an argument. This allows
retrieving the socket family from an unnamed socket.

This change is compatible with POSIX and appears to aligns with
Linux, FreeBSD, DragonflyBSD and OpenBSD behaviors.

Fixes #14312

Change-Id: I55dbf4d6738399941bef71e6b3d7201cf78876df
Reviewed-on: https://review.haiku-os.org/594
Reviewed-by: Axel Dörfler <axeld@pinc-software.de>
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
This commit is contained in:
Leorize 2018-09-26 09:52:34 +07:00 committed by Jérôme Duval
parent f6381ea210
commit c29a7780db

View File

@ -1066,8 +1066,15 @@ int
socket_getsockname(net_socket* socket, struct sockaddr* address,
socklen_t* _addressLength)
{
if (socket->address.ss_len == 0)
return ENOTCONN;
if (socket->address.ss_len == 0) {
struct sockaddr buffer;
memset(&buffer, 0, sizeof(buffer));
buffer.sa_family = socket->family;
memcpy(address, &buffer, min_c(*_addressLength, sizeof(buffer)));
*_addressLength = sizeof(buffer);
return B_OK;
}
memcpy(address, &socket->address, min_c(*_addressLength,
socket->address.ss_len));