2011-05-30 16:26:04 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/getsockname.c
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
2017-06-22 15:03:37 +02:00
|
|
|
* Copyright (C) 2011-2012, 2017 Gregory Nutt. All rights reserved.
|
2012-03-04 00:18:34 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2012-06-08 00:00:19 +02:00
|
|
|
#include <string.h>
|
2011-05-30 16:26:04 +02:00
|
|
|
#include <errno.h>
|
2017-03-31 16:58:14 +02:00
|
|
|
#include <assert.h>
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/net/net.h>
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2014-07-05 22:40:29 +02:00
|
|
|
#include "socket/socket.h"
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-08-26 22:43:13 +02:00
|
|
|
* Name: psock_getsockname
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-08-26 22:43:13 +02:00
|
|
|
* The psock_getsockname() function retrieves the locally-bound name of the
|
2015-01-16 15:51:18 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2018-08-26 22:43:13 +02:00
|
|
|
* Parameters:
|
|
|
|
* psock Socket structure of socket to operate on
|
2015-01-16 15:51:18 +01:00
|
|
|
* 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:
|
|
|
|
*
|
2017-07-14 17:04:19 +02:00
|
|
|
* EBADF - The socket argument is not a valid file descriptor.
|
|
|
|
* ENOTSOCK - The socket argument does not refer to a socket.
|
2015-01-16 15:51:18 +01:00
|
|
|
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
|
2017-07-14 17:04:19 +02:00
|
|
|
* EINVAL - The socket has been shut down.
|
|
|
|
* ENOBUFS - Insufficient resources were available in the system to
|
|
|
|
* complete the function.
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-25 19:31:42 +02:00
|
|
|
int psock_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2018-08-26 22:43:13 +02:00
|
|
|
/* Verify that the psock corresponds to valid, allocated socket */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
if (psock == NULL || psock->s_crefs <= 0)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2018-08-26 22:43:13 +02:00
|
|
|
return -EBADF;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 07:37:25 +01:00
|
|
|
/* Some sanity checking... */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2020-02-21 07:37:25 +01:00
|
|
|
if (addr == NULL || addrlen == NULL)
|
2015-01-16 15:51:18 +01:00
|
|
|
{
|
2018-08-26 22:43:13 +02:00
|
|
|
return -EINVAL;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
/* Let the address family's send() method handle the operation */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2017-07-14 17:04:19 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL &&
|
|
|
|
psock->s_sockif->si_getsockname != NULL);
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2018-08-26 22:43:13 +02:00
|
|
|
return psock->s_sockif->si_getsockname(psock, addr, addrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getsockname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The 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:
|
|
|
|
* sockfd Socket descriptor of socket [in]
|
|
|
|
* 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 getsockname(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
|
|
|
int ret;
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2018-08-26 22:43:13 +02:00
|
|
|
/* Let psock_getsockname() do all of the work */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2018-08-26 22:43:13 +02:00
|
|
|
ret = psock_getsockname(psock, addr, addrlen);
|
2015-01-16 15:51:18 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-12-24 15:09:55 +01:00
|
|
|
_SO_SETERRNO(psock, -ret);
|
2018-08-26 22:43:13 +02:00
|
|
|
return ERROR;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|