net/socket/getsockname.c: Add psock_getsockname function

This commit is contained in:
Xiang Xiao 2018-08-26 14:43:13 -06:00 committed by Gregory Nutt
parent fd1405d976
commit 3eedabfded
2 changed files with 108 additions and 36 deletions

View File

@ -1070,6 +1070,46 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option,
int psock_setsockopt(FAR struct socket *psock, int level, int option,
FAR const void *value, socklen_t value_len);
/****************************************************************************
* Name: psock_getsockname
*
* Description:
* The psock_getsockname() function retrieves the locally-bound name of the
* the specified socket, stores this address in the sockaddr structure pointed
* to by the 'addr' argument, and stores the length of this address in the
* object pointed to by the 'addrlen' argument.
*
* If the actual length of the address is greater than the length of the
* supplied sockaddr structure, the stored address will be truncated.
*
* If the socket has not been bound to a local name, the value stored in
* the object pointed to by address is unspecified.
*
* Parameters:
* psock Socket structure of socket to operate on
* addr sockaddr structure to receive data [out]
* addrlen Length of sockaddr structure [in/out]
*
* Returned Value:
* On success, 0 is returned, the 'addr' argument points to the address
* of the socket, and the 'addrlen' argument points to the length of the
* address. Otherwise, -1 is returned and errno is set to indicate the error.
* Possible errno values that may be returned include:
*
* EBADF - The socket argument is not a valid file descriptor.
* ENOTSOCK - The socket argument does not refer to a socket.
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
* ENOTCONN - The socket is not connected or otherwise has not had the
* peer pre-specified.
* EINVAL - The socket has been shut down.
* ENOBUFS - Insufficient resources were available in the system to
* complete the function.
*
****************************************************************************/
int psock_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
FAR socklen_t *addrlen);
/****************************************************************************
* Name: psock_getpeername
*

View File

@ -56,6 +56,69 @@
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: psock_getsockname
*
* Description:
* The psock_getsockname() function retrieves the locally-bound name of the
* specified socket, stores this address in the sockaddr structure pointed
* to by the 'addr' argument, and stores the length of this address in the
* object pointed to by the 'addrlen' argument.
*
* If the actual length of the address is greater than the length of the
* supplied sockaddr structure, the stored address will be truncated.
*
* If the socket has not been bound to a local name, the value stored in
* the object pointed to by address is unspecified.
*
* Parameters:
* psock Socket structure of socket to operate on
* addr sockaddr structure to receive data [out]
* addrlen Length of sockaddr structure [in/out]
*
* Returned Value:
* On success, 0 is returned, the 'addr' argument points to the address
* of the socket, and the 'addrlen' argument points to the length of the
* address. Otherwise, -1 is returned and errno is set to indicate the error.
* Possible errno values that may be returned include:
*
* EBADF - The socket argument is not a valid file descriptor.
* ENOTSOCK - The socket argument does not refer to a socket.
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
* EINVAL - The socket has been shut down.
* ENOBUFS - Insufficient resources were available in the system to
* complete the function.
*
****************************************************************************/
int psock_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
{
/* Verify that the psock corresponds to valid, allocated socket */
if (psock == NULL || psock->s_crefs <= 0)
{
return -EBADF;
}
/* Some sanity checking... Shouldn't need this on a buckled up embedded
* system (?)
*/
#ifdef CONFIG_DEBUG_FEATURES
if (addr == NULL || addrlen <= 0)
{
return -EINVAL;
}
#endif
/* Let the address family's send() method handle the operation */
DEBUGASSERT(psock->s_sockif != NULL &&
psock->s_sockif->si_getsockname != NULL);
return psock->s_sockif->si_getsockname(psock, addr, addrlen);
}
/****************************************************************************
* Name: getsockname
*
@ -71,7 +134,7 @@
* If the socket has not been bound to a local name, the value stored in
* the object pointed to by address is unspecified.
*
* Input Parameters:
* Parameters:
* sockfd Socket descriptor of socket [in]
* addr sockaddr structure to receive data [out]
* addrlen Length of sockaddr structure [in/out]
@ -95,48 +158,17 @@ int getsockname(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
{
FAR struct socket *psock = sockfd_socket(sockfd);
int ret;
int errcode;
/* Verify that the sockfd corresponds to valid, allocated socket */
if (psock == NULL || psock->s_crefs <= 0)
{
errcode = EBADF;
goto errout;
}
/* Some sanity checking... Shouldn't need this on a buckled up embedded
* system (?)
*/
#ifdef CONFIG_DEBUG_FEATURES
if (addr == NULL || addrlen <= 0)
{
errcode = EINVAL;
goto errout;
}
#endif
/* Let the address family's send() method handle the operation */
DEBUGASSERT(psock->s_sockif != NULL &&
psock->s_sockif->si_getsockname != NULL);
ret = psock->s_sockif->si_getsockname(psock, addr, addrlen);
/* Check for failure */
/* Let psock_getsockname() do all of the work */
ret = psock_getsockname(psock, addr, addrlen);
if (ret < 0)
{
errcode = -ret;
goto errout;
set_errno(-ret);
return ERROR;
}
return OK;
errout:
set_errno(errcode);
return ERROR;
}
#endif /* CONFIG_NET */