2017-07-14 18:54:54 +02:00
|
|
|
/****************************************************************************
|
2017-08-06 22:48:19 +02:00
|
|
|
* net/inet/ipv6_getsockname.c
|
2017-07-14 18:54:54 +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
|
2017-07-14 18:54:54 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-07-14 18:54:54 +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.
|
2017-07-14 18:54:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
2017-07-14 23:52:37 +02:00
|
|
|
#include "netdev/netdev.h"
|
2017-09-18 21:14:57 +02:00
|
|
|
#include "udp/udp.h"
|
|
|
|
#include "tcp/tcp.h"
|
2017-08-06 22:48:19 +02:00
|
|
|
#include "inet/inet.h"
|
2017-07-14 18:54:54 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ipv6_getsockname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The ipv6_getsockname() function retrieves the locally-bound name of the
|
|
|
|
* specified PF_NET6 socket.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-07-14 18:54:54 +02:00
|
|
|
* 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, a negated errno value is returned. See
|
|
|
|
* getsockname() for the list of returned error values.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ipv6_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_in6 *outaddr = (FAR struct sockaddr_in6 *)addr;
|
|
|
|
FAR struct net_driver_s *dev;
|
|
|
|
net_ipv6addr_t *lipaddr;
|
|
|
|
net_ipv6addr_t *ripaddr;
|
|
|
|
|
|
|
|
/* Check if enough space has been provided for the full address */
|
|
|
|
|
|
|
|
if (*addrlen < sizeof(struct sockaddr_in6))
|
|
|
|
{
|
|
|
|
/* This function is supposed to return the partial address if
|
|
|
|
* a smaller buffer has been provided. This support has not
|
|
|
|
* been implemented.
|
|
|
|
*/
|
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2017-07-14 18:54:54 +02:00
|
|
|
|
|
|
|
/* Set the port number */
|
|
|
|
|
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
|
|
|
#ifdef NET_TCP_HAVE_STACK
|
|
|
|
case SOCK_STREAM:
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct tcp_conn_s *tcp_conn = psock->s_conn;
|
2017-07-14 18:54:54 +02:00
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
outaddr->sin6_port = tcp_conn->lport; /* Already in network byte order */
|
2017-07-14 18:54:54 +02:00
|
|
|
lipaddr = &tcp_conn->u.ipv6.laddr;
|
|
|
|
ripaddr = &tcp_conn->u.ipv6.raddr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NET_UDP_HAVE_STACK
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct udp_conn_s *udp_conn = psock->s_conn;
|
2017-07-14 18:54:54 +02:00
|
|
|
|
2017-08-08 22:24:12 +02:00
|
|
|
outaddr->sin6_port = udp_conn->lport; /* Already in network byte order */
|
2017-07-14 18:54:54 +02:00
|
|
|
lipaddr = &udp_conn->u.ipv6.laddr;
|
|
|
|
ripaddr = &udp_conn->u.ipv6.raddr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
/* Check if bound to the IPv6 unspecified address */
|
2017-07-14 18:54:54 +02:00
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
if (net_ipv6addr_cmp(lipaddr, g_ipv6_unspecaddr))
|
2017-07-14 18:54:54 +02:00
|
|
|
{
|
|
|
|
outaddr->sin6_family = AF_INET6;
|
2018-06-23 20:53:27 +02:00
|
|
|
memcpy(outaddr->sin6_addr.in6_u.u6_addr8, g_ipv6_unspecaddr, 16);
|
2017-07-14 18:54:54 +02:00
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
2017-07-14 23:52:37 +02:00
|
|
|
|
2017-07-14 18:54:54 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
/* Find the device matching the IPv6 address in the connection structure.
|
2018-06-23 20:53:27 +02:00
|
|
|
* NOTE: listening sockets have no ripaddr. Work around is to use the
|
2017-07-14 18:54:54 +02:00
|
|
|
* lipaddr when ripaddr is not available.
|
|
|
|
*/
|
|
|
|
|
2018-06-23 20:53:27 +02:00
|
|
|
if (net_ipv6addr_cmp(ripaddr, g_ipv6_unspecaddr))
|
2017-07-14 18:54:54 +02:00
|
|
|
{
|
|
|
|
ripaddr = lipaddr;
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:20:44 +01:00
|
|
|
dev = netdev_findby_ripv6addr(*lipaddr, *ripaddr);
|
2017-07-14 18:54:54 +02:00
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
net_unlock();
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the address family and the IP address */
|
|
|
|
|
|
|
|
outaddr->sin6_family = AF_INET6;
|
2023-08-25 11:37:11 +02:00
|
|
|
net_ipv6addr_copy(outaddr->sin6_addr.in6_u.u6_addr8,
|
2023-12-13 10:06:13 +01:00
|
|
|
netdev_ipv6_srcaddr(dev, *lipaddr));
|
2017-07-14 18:54:54 +02:00
|
|
|
*addrlen = sizeof(struct sockaddr_in6);
|
2017-07-14 23:52:37 +02:00
|
|
|
|
2017-07-14 18:54:54 +02:00
|
|
|
net_unlock();
|
|
|
|
|
|
|
|
/* Return success */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|