Opened 10 years ago

Last modified 3 years ago

#2967 assigned bug

getpeername() succeeds on unconnected sockets

Reported by: bonefish Owned by: phoudoin
Priority: normal Milestone: R1
Component: Network & Internet/TCP Version: R1/pre-alpha1
Keywords: Cc:
Blocked By: Blocking:
Has a Patch: no Platform: All



A getpeername() after a connect() on a TCP socket succeeds even if

  • the socket is non-blocking and the connection has not been established, or
  • the socket is blocking and connect() timed out.

In either case getpeername() should fail with ENOTCONN.

The problem seems to be that getpeername() is fully implemented in the socket module and doesn't inquire the connection state. Or that the TCP module should set the peer address only when the connection is established and unset it when disconnected.

Change History (3)

comment:1 Changed 6 years ago by phoudoin

Owner: changed from axeld to phoudoin
Status: newassigned

comment:2 Changed 3 years ago by phoudoin

An easy fix is in src/add-ons/kernel/network/stack/net_socket.cpp

socket_getpeername(net_socket* _socket)
    net_socket_private* socket = (net_socket_private*)_socket;
    if (!socket->is_connected)
        return ENOTCONN;


Not great though, in the public socket API part of this module, net_socket_private is supposed to be unknown.

comment:3 Changed 3 years ago by phoudoin

Stupid fix, in fact, as it will break for connectionless sockets, like UDP. It means that, indeed, TCP module should not set peer address public until connected.

Note: See TracTickets for help on using tickets.