/**************************************************************************** * net/socket/inet_sockif.c * * Copyright (C) 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * 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 #include #include #include #include #include #include #include "tcp/tcp.h" #include "udp/udp.h" #include "socket/socket.h" #if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6) /**************************************************************************** * Private Function Prototypes ****************************************************************************/ static int inet_setup(FAR struct socket *psock, int protocol); static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf, size_t len, int flags); static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf, size_t len, int flags, FAR const struct sockaddr *to, socklen_t tolen); /**************************************************************************** * Public Data ****************************************************************************/ const struct sock_intf_s g_inet_sockif = { inet_setup, /* si_setup */ inet_send, /* si_send */ inet_sendto, /* si_sendto */ }; /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** * Name: inet_tcp_alloc * * Description: * Allocate and attach a TCP connection structure. * ****************************************************************************/ #ifdef NET_TCP_HAVE_STACK static int inet_tcp_alloc(FAR struct socket *psock) { /* Allocate the TCP connection structure */ FAR struct tcp_conn_s *conn = tcp_alloc(psock->s_domain); if (conn == NULL) { /* Failed to reserve a connection structure */ nerr("ERROR: Failed to reserve TCP connection structure\n"); return -ENOMEM; } /* Set the reference count on the connection structure. This reference * count will be incremented only if the socket is dup'ed */ DEBUGASSERT(conn->crefs == 0); conn->crefs = 1; /* Save the pre-allocated connection in the socket structure */ psock->s_conn = conn; return OK; } #endif /* NET_TCP_HAVE_STACK */ /**************************************************************************** * Name: inet_udp_alloc * * Description: * Allocate and attach a UDP connection structure. * ****************************************************************************/ #ifdef NET_UDP_HAVE_STACK static int inet_udp_alloc(FAR struct socket *psock) { /* Allocate the UDP connection structure */ FAR struct udp_conn_s *conn = udp_alloc(psock->s_domain); if (conn == NULL) { /* Failed to reserve a connection structure */ nerr("ERROR: Failed to reserve UDP connection structure\n"); return -ENOMEM; } /* Set the reference count on the connection structure. This reference * count will be incremented only if the socket is dup'ed */ DEBUGASSERT(conn->crefs == 0); conn->crefs = 1; /* Save the pre-allocated connection in the socket structure */ psock->s_conn = conn; return OK; } #endif /* NET_UDP_HAVE_STACK */ /**************************************************************************** * Name: inet_setup * * Description: * Called for socket() to verify that the provided socket type and * protocol are usable by this address family. Perform any family- * specific socket fields. * * NOTE: This is common logic for both the AF_INET and AF_INET6 address * families. * * Parameters: * psock A pointer to a user allocated socket structure to be initialized. * protocol (see sys/socket.h) * * Returned Value: * Zero (OK) is returned on success. Otherwise, a negater errno value is * returned. * ****************************************************************************/ static int inet_setup(FAR struct socket *psock, int protocol) { /* Allocate the appropriate connection structure. This reserves the * the connection structure is is unallocated at this point. It will * not actually be initialized until the socket is connected. * * Only SOCK_STREAM and SOCK_DGRAM and possible SOCK_RAW are supported. */ switch (psock->s_type) { #ifdef CONFIG_NET_TCP case SOCK_STREAM: if (protocol != 0 && protocol != IPPROTO_TCP) { nerr("ERROR: Unsupported stream protocol: %d\n", protocol); return -EPROTONOSUPPORT; } #ifdef NET_TCP_HAVE_STACK /* Allocate and attach the TCP connection structure */ return inet_tcp_alloc(psock); #else return = -ENETDOWN; #endif #endif /* CONFIG_NET_TCP */ #ifdef CONFIG_NET_UDP case SOCK_DGRAM: if (protocol != 0 && protocol != IPPROTO_UDP) { nerr("ERROR: Unsupported datagram protocol: %d\n", protocol); return -EPROTONOSUPPORT; } #ifdef NET_UDP_HAVE_STACK /* Allocate and attach the UDP connection structure */ return inet_udp_alloc(psock); #else return -ENETDOWN; #endif #endif /* CONFIG_NET_UDP */ default: nerr("ERROR: Unsupported type: %d\n", psock->s_type); return -EPROTONOSUPPORT; } } /**************************************************************************** * Name: inet_send * * Description: * The inet_send() call may be used only when the socket is in a connected * state (so that the intended recipient is known). * * Parameters: * psock An instance of the internal socket structure. * buf Data to send * len Length of data to send * flags Send flags * * Returned Value: * On success, returns the number of characters sent. On error, -1 is * returned, and errno is set appropriately (see send() for the list of * appropriate errors values. * ****************************************************************************/ static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf, size_t len, int flags) { ssize_t ret; switch (psock->s_type) { #ifdef CONFIG_NET_TCP case SOCK_STREAM: { #ifdef CONFIG_NET_6LOWPAN /* Try 6LoWPAN TCP packet send */ ret = psock_6lowpan_tcp_send(psock, buf, len); #if defined(CONFIG_NETDEV_MULTINIC) && defined(NET_TCP_HAVE_STACK) if (ret < 0) { /* TCP/IP packet send */ ret = psock_tcp_send(psock, buf, len); } #endif /* CONFIG_NETDEV_MULTINIC && NET_TCP_HAVE_STACK */ #elif defined(NET_TCP_HAVE_STACK) ret = psock_tcp_send(psock, buf, len); #else ret = -ENOSYS; #endif /* CONFIG_NET_6LOWPAN */ } break; #endif /* CONFIG_NET_TCP */ #ifdef CONFIG_NET_UDP case SOCK_DGRAM: { #if defined(CONFIG_NET_6LOWPAN) /* Try 6LoWPAN UDP packet send */ ret = psock_6lowpan_udp_send(psock, buf, len); #if defined(CONFIG_NETDEV_MULTINIC) && defined(NET_UDP_HAVE_STACK) if (ret < 0) { /* UDP/IP packet send */ ret = psock_udp_send(psock, buf, len); } #endif /* CONFIG_NETDEV_MULTINIC && NET_UDP_HAVE_STACK */ #elif defined(NET_UDP_HAVE_STACK) /* Only UDP/IP packet send */ ret = psock_udp_send(psock, buf, len); #else ret = -ENOSYS; #endif /* CONFIG_NET_6LOWPAN */ } break; #endif /* CONFIG_NET_UDP */ default: { /* EDESTADDRREQ. Signifies that the socket is not connection-mode * and no peer address is set. */ nerr("ERROR: Bad socket type: %d\n", psock->s_type); ret = -EDESTADDRREQ; } break; } return ret; } /**************************************************************************** * Name: inet_sendto * * Description: * Implements the sendto() operation for the case of the AF_INET and * AF_INET6 sockets. * * Parameters: * psock A pointer to a NuttX-specific, internal socket structure * buf Data to send * len Length of data to send * flags Send flags * to Address of recipient * tolen The length of the address structure * * Returned Value: * On success, returns the number of characters sent. On error, -1 is * returned, and errno is set appropriately (see send_to() for the list of * appropriate errors values. * ****************************************************************************/ static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf, size_t len, int flags, FAR const struct sockaddr *to, socklen_t tolen) { socklen_t minlen; ssize_t nsent; /* Verify that a valid address has been provided */ switch (to->sa_family) { #ifdef CONFIG_NET_IPv4 case AF_INET: minlen = sizeof(struct sockaddr_in); break; #endif #ifdef CONFIG_NET_IPv6 case AF_INET6: minlen = sizeof(struct sockaddr_in6); break; #endif #ifdef CONFIG_NET_LOCAL_DGRAM case AF_LOCAL: minlen = sizeof(sa_family_t); break; #endif default: nerr("ERROR: Unrecognized address family: %d\n", to->sa_family); return -EAFNOSUPPORT; } if (tolen < minlen) { nerr("ERROR: Invalid address length: %d < %d\n", tolen, minlen); return -EBADF; } #ifdef CONFIG_NET_UDP /* If this is a connected socket, then return EISCONN */ if (psock->s_type != SOCK_DGRAM) { nerr("ERROR: Connected socket\n"); return -EBADF; } /* Now handle the INET sendto() operation */ #if defined(CONFIG_NET_6LOWPAN) /* Try 6LoWPAN UDP packet sendto() */ nsent = psock_6lowpan_udp_sendto(psock, buf, len, flags, to, tolen); #if defined(CONFIG_NETDEV_MULTINIC) && defined(NET_UDP_HAVE_STACK) if (nsent < 0) { /* UDP/IP packet sendto */ nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen); } #endif /* CONFIG_NETDEV_MULTINIC && NET_UDP_HAVE_STACK */ #elif defined(NET_UDP_HAVE_STACK) nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen); #else nsent = -ENOSYS; #endif /* CONFIG_NET_6LOWPAN */ #else nsent = -EISCONN; #endif /* CONFIG_NET_UDP */ return nsent; } /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** * Name: * * Description: * * Parameters: * * Returned Value: * * Assumptions: * ****************************************************************************/ #endif /* CONFIG_NET_IPv4 || CONFIG_NET_IPv6 */