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
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
2020-07-22 10:30:44 +02:00
|
|
|
* address. Otherwise, -1 is returned and errno is set to indicate the
|
|
|
|
* error. Possible errno values that may be returned include:
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
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
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock == NULL || psock->s_conn == NULL)
|
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
|
|
|
|
2022-08-31 15:43:11 +02: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
|
|
|
|
2023-03-06 05:10:27 +01:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL);
|
|
|
|
if (psock->s_sockif->si_getsockname == NULL)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
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
|
2020-07-22 10:30:44 +02:00
|
|
|
* address. Otherwise, -1 is returned and errno is set to indicate the
|
|
|
|
* error. Possible errno values that may be returned include:
|
2018-08-26 22:43:13 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-22 10:30:44 +02:00
|
|
|
int getsockname(int sockfd, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
2018-08-26 22:43:13 +02:00
|
|
|
{
|
2022-10-27 12:17:35 +02:00
|
|
|
FAR struct socket *psock;
|
2018-08-26 22:43:13 +02:00
|
|
|
int ret;
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2022-10-27 12:17:35 +02:00
|
|
|
/* Get the underlying socket structure */
|
|
|
|
|
|
|
|
ret = sockfd_socket(sockfd, &psock);
|
|
|
|
|
2018-08-26 22:43:13 +02:00
|
|
|
/* Let psock_getsockname() do all of the work */
|
2015-01-16 15:51:18 +01:00
|
|
|
|
2022-10-27 12:17:35 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = psock_getsockname(psock, addr, addrlen);
|
|
|
|
}
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-02-14 06:32:41 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 06:32:41 +01:00
|
|
|
return ret;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|