The getsockname function returns the sockaddr (socket address) for a given socket NAME and PROTOCOL.
getsockname NAME, PROTOCOL