mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 00:14:22 +08:00
net/socket/getsockname.c: Add psock_getsockname function
This commit is contained in:
@@ -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,
|
int psock_setsockopt(FAR struct socket *psock, int level, int option,
|
||||||
FAR const void *value, socklen_t value_len);
|
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
|
* Name: psock_getpeername
|
||||||
*
|
*
|
||||||
|
|||||||
+68
-36
@@ -56,6 +56,69 @@
|
|||||||
* Public Functions
|
* 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
|
* Name: getsockname
|
||||||
*
|
*
|
||||||
@@ -71,7 +134,7 @@
|
|||||||
* If the socket has not been bound to a local name, the value stored in
|
* If the socket has not been bound to a local name, the value stored in
|
||||||
* the object pointed to by address is unspecified.
|
* the object pointed to by address is unspecified.
|
||||||
*
|
*
|
||||||
* Input Parameters:
|
* Parameters:
|
||||||
* sockfd Socket descriptor of socket [in]
|
* sockfd Socket descriptor of socket [in]
|
||||||
* addr sockaddr structure to receive data [out]
|
* addr sockaddr structure to receive data [out]
|
||||||
* addrlen Length of sockaddr structure [in/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);
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
||||||
int ret;
|
int ret;
|
||||||
int errcode;
|
|
||||||
|
|
||||||
/* Verify that the sockfd corresponds to valid, allocated socket */
|
/* Let psock_getsockname() do all of the work */
|
||||||
|
|
||||||
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 */
|
|
||||||
|
|
||||||
|
ret = psock_getsockname(psock, addr, addrlen);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
errcode = -ret;
|
set_errno(-ret);
|
||||||
goto errout;
|
return ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
|
|
||||||
errout:
|
|
||||||
set_errno(errcode);
|
|
||||||
return ERROR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* CONFIG_NET */
|
#endif /* CONFIG_NET */
|
||||||
|
|||||||
Reference in New Issue
Block a user