net_socket: getpeername now only works on connected socket

Per POSIX.1-2008, getpeername() shall fails if the socket is not
connected.

With this change, it's no longer possible to retrieve the peer from
connectionless protocols such as UDP, even if a peer is assigned to them
via connect(). The same behavior can be replicated among
POSIX-compatible operating systems such as Linux.

Fixes #15081

Change-Id: Ia5631971200959a3d3815332ff1969c4b4dd289b
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2421
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
Leorize 2020-03-25 14:15:25 -05:00 committed by waddlesplash
parent a67bbcb799
commit 04fac889f7

View File

@ -1054,10 +1054,11 @@ socket_connect(net_socket* socket, const struct sockaddr* address,
int
socket_getpeername(net_socket* socket, struct sockaddr* address,
socket_getpeername(net_socket* _socket, struct sockaddr* address,
socklen_t* _addressLength)
{
if (socket->peer.ss_len == 0)
net_socket_private* socket = (net_socket_private*)_socket;
if (!socket->is_connected || socket->peer.ss_len == 0)
return ENOTCONN;
memcpy(address, &socket->peer, min_c(*_addressLength, socket->peer.ss_len));