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>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/udp.h>
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2015-05-31 16:34:03 +02:00
|
|
|
#include "utils/utils.h"
|
2014-06-27 17:56:45 +02:00
|
|
|
#include "netdev/netdev.h"
|
2014-07-06 20:34:27 +02:00
|
|
|
#include "tcp/tcp.h"
|
2014-07-05 22:40:29 +02:00
|
|
|
#include "udp/udp.h"
|
|
|
|
#include "socket/socket.h"
|
2017-03-31 16:58:14 +02:00
|
|
|
#include "usrsock/usrsock.h"
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: get_ipv4_sockname
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The getsockname() function retrieves the locally-bound name of the
|
2015-01-16 15:51:18 +01:00
|
|
|
* specified PF_NET socket.
|
2011-05-30 16:26:04 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
2015-01-16 15:51:18 +01:00
|
|
|
* psock Point to the socket structure instance [in]
|
2011-05-30 16:26:04 +02: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:
|
|
|
|
*
|
|
|
|
* EBADF - The socket argument is not a valid file descriptor.
|
|
|
|
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
|
|
|
|
* EINVAL - The socket has been shut down.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
int ipv4_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
2011-05-30 16:26:04 +02:00
|
|
|
{
|
2014-06-28 00:48:12 +02:00
|
|
|
FAR struct net_driver_s *dev;
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
|
2011-05-30 16:26:04 +02:00
|
|
|
FAR struct sockaddr_in *outaddr = (FAR struct sockaddr_in *)addr;
|
|
|
|
#endif
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
|
|
|
in_addr_t lipaddr;
|
|
|
|
in_addr_t ripaddr;
|
2011-05-30 16:26:04 +02:00
|
|
|
#endif
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
/* Check if enough space has been provided for the full address */
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
if (*addrlen < sizeof(struct sockaddr_in))
|
|
|
|
{
|
|
|
|
/* This function is supposed to return the partial address if
|
|
|
|
* a smaller buffer has been provided. This support has not
|
|
|
|
* been implemented.
|
|
|
|
*/
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the port number */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
2011-05-30 16:26:04 +02:00
|
|
|
{
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
2015-01-16 15:51:18 +01:00
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
|
|
|
FAR struct tcp_conn_s *tcp_conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
|
|
|
outaddr->sin_port = tcp_conn->lport; /* Already in network byte order */
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-16 17:01:54 +01:00
|
|
|
lipaddr = tcp_conn->u.ipv4.laddr;
|
|
|
|
ripaddr = tcp_conn->u.ipv4.raddr;
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_UDP_HAVE_STACK
|
2015-01-16 15:51:18 +01:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
|
|
|
FAR struct udp_conn_s *udp_conn = (FAR struct udp_conn_s *)psock->s_conn;
|
|
|
|
outaddr->sin_port = udp_conn->lport; /* Already in network byte order */
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-16 17:01:54 +01:00
|
|
|
lipaddr = udp_conn->u.ipv4.laddr;
|
|
|
|
ripaddr = udp_conn->u.ipv4.raddr;
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 15:03:37 +02:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-16 15:51:18 +01:00
|
|
|
/* The socket/connection does not know its IP address unless
|
|
|
|
* CONFIG_NETDEV_MULTINIC is selected. Otherwise the design supports only
|
|
|
|
* a single network device and only the network device knows the IP address.
|
2011-05-30 16:26:04 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-22 09:06:04 +02:00
|
|
|
if (lipaddr == 0)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
|
|
|
|
outaddr->sin_family = AF_INET;
|
|
|
|
outaddr->sin_addr.s_addr = 0;
|
|
|
|
*addrlen = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2017-06-22 15:03:37 +02:00
|
|
|
return OK;
|
2017-06-22 09:06:04 +02:00
|
|
|
}
|
2017-06-22 15:03:37 +02:00
|
|
|
#endif
|
2017-06-22 09:06:04 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2017-06-19 15:48:26 +02:00
|
|
|
/* Find the device matching the IPv4 address in the connection structure.
|
|
|
|
* NOTE: listening sockets have no ripaddr. Work around is to use the
|
|
|
|
* lipaddr when ripaddr is not available.
|
|
|
|
*/
|
2017-06-22 09:06:04 +02:00
|
|
|
|
2017-06-19 12:52:48 +02:00
|
|
|
if (ripaddr == 0)
|
|
|
|
{
|
|
|
|
ripaddr = lipaddr;
|
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
dev = netdev_findby_ipv4addr(lipaddr, ripaddr);
|
|
|
|
#else
|
|
|
|
/* There is only one, the first network device in the list. */
|
|
|
|
|
|
|
|
dev = g_netdevices;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!dev)
|
2011-05-30 16:26:04 +02:00
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-16 15:51:18 +01:00
|
|
|
return -EINVAL;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
/* Set the address family and the IP address */
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
|
2015-01-16 15:51:18 +01:00
|
|
|
outaddr->sin_family = AF_INET;
|
|
|
|
outaddr->sin_addr.s_addr = dev->d_ipaddr;
|
|
|
|
*addrlen = sizeof(struct sockaddr_in);
|
2011-05-30 16:26:04 +02:00
|
|
|
#endif
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2011-05-30 16:26:04 +02:00
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: ipv6_getsockname
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The getsockname() function retrieves the locally-bound name of the
|
|
|
|
* specified PF_NET6 socket.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock Point to the socket structure instance [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.
|
|
|
|
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
|
|
|
|
* EINVAL - The socket has been shut down.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-01-16 15:51:18 +01:00
|
|
|
int ipv6_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev;
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
|
2015-01-16 15:51:18 +01:00
|
|
|
FAR struct sockaddr_in6 *outaddr = (FAR struct sockaddr_in6 *)addr;
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
|
|
|
net_ipv6addr_t *lipaddr;
|
|
|
|
net_ipv6addr_t *ripaddr;
|
2011-05-30 16:26:04 +02:00
|
|
|
#endif
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
/* Check if enough space has been provided for the full address */
|
|
|
|
|
|
|
|
if (*addrlen < sizeof(struct sockaddr_in6))
|
2015-10-08 23:10:04 +02:00
|
|
|
{
|
|
|
|
/* This function is supposed to return the partial address if
|
|
|
|
* a smaller buffer has been provided. This support has not
|
|
|
|
* been implemented.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
/* Set the port number */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
2011-05-30 16:26:04 +02:00
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
2015-01-21 01:14:09 +01:00
|
|
|
outaddr->sin6_port = tcp_conn->lport; /* Already in network byte order */
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-21 01:14:09 +01:00
|
|
|
lipaddr = &tcp_conn->u.ipv6.laddr;
|
|
|
|
ripaddr = &tcp_conn->u.ipv6.raddr;
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef NET_UDP_HAVE_STACK
|
2011-05-30 16:26:04 +02:00
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *udp_conn = (FAR struct udp_conn_s *)psock->s_conn;
|
2015-01-21 01:14:09 +01:00
|
|
|
outaddr->sin6_port = udp_conn->lport; /* Already in network byte order */
|
2015-01-16 15:51:18 +01:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-21 01:14:09 +01:00
|
|
|
lipaddr = &udp_conn->u.ipv6.laddr;
|
|
|
|
ripaddr = &udp_conn->u.ipv6.raddr;
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2015-01-16 17:01:54 +01:00
|
|
|
return -EOPNOTSUPP;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 15:03:37 +02:00
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2015-01-16 15:51:18 +01:00
|
|
|
/* The socket/connection does not know its IP address unless
|
|
|
|
* CONFIG_NETDEV_MULTINIC is selected. Otherwise the design supports only
|
|
|
|
* a single network device and only the network device knows the IP address.
|
2011-05-30 16:26:04 +02:00
|
|
|
*/
|
|
|
|
|
2017-06-22 09:06:04 +02:00
|
|
|
if (net_ipv6addr_cmp(lipaddr, g_ipv6_allzeroaddr))
|
|
|
|
{
|
|
|
|
#if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
|
|
|
|
outaddr->sin6_family = AF_INET6;
|
|
|
|
memcpy(outaddr->sin6_addr.in6_u.u6_addr8, g_ipv6_allzeroaddr, 16);
|
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
|
|
|
return OK;
|
|
|
|
}
|
2017-06-22 15:03:37 +02:00
|
|
|
#endif
|
2017-06-22 09:06:04 +02:00
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
2017-06-19 15:48:26 +02:00
|
|
|
/* Find the device matching the IPv6 address in the connection structure.
|
|
|
|
* NOTE: listening sockets have no ripaddr. Work around is to use the
|
|
|
|
* lipaddr when ripaddr is not available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (net_ipv6addr_cmp(ripaddr, g_ipv6_allzeroaddr))
|
2017-06-19 12:52:48 +02:00
|
|
|
{
|
|
|
|
ripaddr = lipaddr;
|
|
|
|
}
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
dev = netdev_findby_ipv6addr(*lipaddr, *ripaddr);
|
|
|
|
#else
|
|
|
|
/* There is only one, the first network device in the list. */
|
|
|
|
|
2011-05-30 16:26:04 +02:00
|
|
|
dev = g_netdevices;
|
2015-01-16 15:51:18 +01:00
|
|
|
#endif
|
|
|
|
|
2011-05-30 16:26:04 +02:00
|
|
|
if (!dev)
|
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-01-16 15:51:18 +01:00
|
|
|
return -EINVAL;
|
2011-05-30 16:26:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the address family and the IP address */
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
|
2015-01-21 01:14:09 +01:00
|
|
|
outaddr->sin6_family = AF_INET6;
|
2015-01-16 17:01:54 +01:00
|
|
|
memcpy(outaddr->sin6_addr.in6_u.u6_addr8, dev->d_ipv6addr, 16);
|
2011-05-30 16:26:04 +02:00
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
return OK;
|
2015-01-16 15:51:18 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: getsockname
|
2015-01-16 15:51:18 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* EOPNOTSUPP - The operation is not supported for this socket's protocol.
|
|
|
|
* EINVAL - The socket has been shut down.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int getsockname(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
|
|
|
int ret;
|
2016-06-11 22:40:07 +02:00
|
|
|
int errcode;
|
2015-01-16 15:51:18 +01:00
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EBADF;
|
2015-01-16 15:51:18 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some sanity checking... Shouldn't need this on a buckled up embedded
|
|
|
|
* system (?)
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2015-01-16 15:51:18 +01:00
|
|
|
if (!addr || !addrlen)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EINVAL;
|
2015-01-16 15:51:18 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
#ifdef CONFIG_NET_USRSOCK
|
|
|
|
if (psock->s_type == SOCK_USRSOCK_TYPE)
|
|
|
|
{
|
|
|
|
FAR struct usrsock_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
DEBUGASSERT(conn);
|
|
|
|
|
|
|
|
/* Handle usrsock getsockname */
|
|
|
|
|
|
|
|
ret = usrsock_getsockname(conn, addr, addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
/* Handle by address domain */
|
|
|
|
|
|
|
|
switch (psock->s_domain)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
case PF_INET:
|
|
|
|
ret = ipv4_getsockname(psock, addr, addrlen);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
case PF_INET6:
|
|
|
|
ret = ipv6_getsockname(psock, addr, addrlen);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case PF_PACKET:
|
|
|
|
default:
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = EAFNOSUPPORT;
|
2015-01-16 15:51:18 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for failure */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 22:40:07 +02:00
|
|
|
errcode = -ret;
|
2015-01-16 15:51:18 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2011-05-30 16:26:04 +02:00
|
|
|
|
|
|
|
errout:
|
2016-06-11 22:40:07 +02:00
|
|
|
set_errno(errcode);
|
2011-05-30 16:26:04 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|