getsockname
getsockname is a networking function that retrieves the local address to which a socket is bound, or the address it would be bound to if the socket is not yet bound. It applies to sockets using IPv4, IPv6, and Unix domain sockets, and is commonly used to discover the local endpoint of a connection or to determine which local address the system would use for binding.
In POSIX systems, the prototype is typically int getsockname(int sockfd, struct sockaddr *name, socklen_t *addrlen). On
Return value is zero on success and -1 on error in POSIX, with errno set to indicate
Behavior notes: if the socket is already bound, getsockname returns the local address and port bound to
The result is returned in a sockaddr structure; the sa_family field identifies the family, and the data