Socket interface: Added bind() and connect() interfaces.

This commit is contained in:
Gregory Nutt
2017-07-13 09:27:56 -06:00
parent 44ce0cce2f
commit 85b1ae4cf0
15 changed files with 1203 additions and 978 deletions
+4
View File
@@ -103,6 +103,10 @@ struct socket; /* Forward reference */
struct sock_intf_s
{
CODE int (*si_setup)(FAR struct socket *psock, int protocol);
CODE int (*si_bind)(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
CODE int (*si_connect)(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
CODE ssize_t (*si_send)(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags);
CODE ssize_t (*si_sendto)(FAR struct socket *psock, FAR const void *buf,
+155 -1
View File
@@ -57,7 +57,11 @@
* Private Function Prototypes
****************************************************************************/
static ssize_t local_setup(FAR struct socket *psock, int protocol);
static int local_setup(FAR struct socket *psock, int protocol);
static int local_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
static int local_connect(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
static ssize_t local_send(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags);
static ssize_t local_sendto(FAR struct socket *psock, FAR const void *buf,
@@ -71,6 +75,8 @@ static ssize_t local_sendto(FAR struct socket *psock, FAR const void *buf,
const struct sock_intf_s g_local_sockif =
{
local_setup, /* si_setup */
local_bind, /* si_bind */
local_connect, /* si_connect */
local_send, /* si_send */
local_sendto, /* si_sendto */
local_recvfrom /* si_recvfrom */
@@ -171,6 +177,154 @@ static int local_setup(FAR struct socket *psock, int protocol)
}
}
/****************************************************************************
* Name: local_bind
*
* Description:
* local_bind() gives the socket 'psock' the local address 'addr'. 'addr'
* is 'addrlen' bytes long. Traditionally, this is called "assigning a
* name to a socket." When a socket is created with socket(), it exists
* in a name space (address family) but has no name assigned.
*
* Parameters:
* psock Socket structure of the socket to bind
* addr Socket local address
* addrlen Length of 'addr'
*
* Returned Value:
* 0 on success; A negated errno value is returned on failure. See
* bind() for a list a appropriate error values.
*
****************************************************************************/
static int local_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen)
{
int ret;
/* Verify that a valid address has been provided */
if (addr->sa_family != AF_LOCAL || addrlen < sizeof(sa_family_t))
{
nerr("ERROR: Invalid address length: %d < %d\n",
addrlen, sizeof(sa_family_t));
return -EBADF;
}
/* Perform the binding depending on the protocol type */
switch (psock->s_type)
{
/* Bind a local TCP/IP stream or datagram socket */
#if defined(ONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
#ifdef CONFIG_NET_TCP
case SOCK_STREAM:
#endif
#ifdef CONFIG_NET_UDP
case SOCK_DGRAM:
#endif
{
/* Bind the Unix domain connection structure */
ret psock_local_bind(psock, addr, addrlen);
/* Mark the socket bound */
if (ret >= 0)
{
psock->s_flags |= _SF_BOUND;
}
}
break;
#endif /* CONFIG_NET_TCP || CONFIG_NET_UDP*/
default:
ret = -EBADF;
break;
}
return ret;
}
/****************************************************************************
* Name: local_connect
*
* Description:
* local_connect() connects the local socket referred to by the structure
* 'psock' to the address specified by 'addr'. The addrlen argument
* specifies the size of 'addr'. The format of the address in 'addr' is
* determined by the address space of the socket 'psock'.
*
* If the socket 'psock' is of type SOCK_DGRAM then 'addr' is the address
* to which datagrams are sent by default, and the only address from which
* datagrams are received. If the socket is of type SOCK_STREAM or
* SOCK_SEQPACKET, this call attempts to make a connection to the socket
* that is bound to the address specified by 'addr'.
*
* Generally, connection-based protocol sockets may successfully
* local_connect() only once; connectionless protocol sockets may use
* local_connect() multiple times to change their association.
* Connectionless sockets may dissolve the association by connecting to
* an address with the sa_family member of sockaddr set to AF_UNSPEC.
*
* Parameters:
* psock Pointer to a socket structure initialized by psock_socket()
* addr Server address (form depends on type of socket)
* addrlen Length of actual 'addr'
*
* Returned Value:
* 0 on success; a negated errno value on failue. See connect() for the
* list of appropriate errno values to be returned.
*
****************************************************************************/
static int local_connect(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen)
{
/* Verify that a valid address has been provided */
if (addr->sa_family != AF_LOCAL || addrlen < sizeof(sa_family_t))
{
return -EBADF;
}
/* Perform the connection depending on the protocol type */
switch (psock->s_type)
{
#ifdef CONFIG_NET_TCP
case SOCK_STREAM:
{
/* Verify that the socket is not already connected */
if (_SS_ISCONNECTED(psock->s_flags))
{
return -EISCONN;
}
/* It's not... Connect to the local Unix domain server */
return psock_local_connect(psock, addr);
}
break;
#endif /* CONFIG_NET_TCP */
#ifdef CONFIG_NET_UDP
case SOCK_DGRAM:
{
/* Perform the datagram connection logic */
return psock_local_connect(psock, addr);
}
break;
#endif /* CONFIG_NET_UDP */
default:
return -EBADF;
}
}
/****************************************************************************
* Name: local_send
*
+121 -1
View File
@@ -55,7 +55,11 @@
* Private Function Prototypes
****************************************************************************/
static ssize_t pkt_setup(FAR struct socket *psock, int protocol);
static int pkt_setup(FAR struct socket *psock, int protocol);
static int pkt_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
static int pkt_connect(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
static ssize_t pkt_send(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags);
static ssize_t pkt_sendto(FAR struct socket *psock, FAR const void *buf,
@@ -69,6 +73,8 @@ static ssize_t pkt_sendto(FAR struct socket *psock, FAR const void *buf,
const struct sock_intf_s g_pkt_sockif =
{
pkt_setup, /* si_setup */
pkt_bind, /* si_bind */
pkt_connect, /* si_connect */
pkt_send, /* si_send */
pkt_sendto, /* si_sendto */
pkt_recvfrom /* si_recvfrom */
@@ -151,6 +157,120 @@ static int pkt_setup(FAR struct socket *psock, int protocol)
}
}
/****************************************************************************
* Name: pkt_connect
*
* Description:
* pkt_connect() connects the local socket referred to by the structure
* 'psock' to the address specified by 'addr'. The addrlen argument
* specifies the size of 'addr'. The format of the address in 'addr' is
* determined by the address space of the socket 'psock'.
*
* If the socket 'psock' is of type SOCK_DGRAM then 'addr' is the address
* to which datagrams are sent by default, and the only address from which
* datagrams are received. If the socket is of type SOCK_STREAM or
* SOCK_SEQPACKET, this call attempts to make a connection to the socket
* that is bound to the address specified by 'addr'.
*
* Generally, connection-based protocol sockets may successfully
* pkt_connect() only once; connectionless protocol sockets may use
* pkt_connect() multiple times to change their association.
* Connectionless sockets may dissolve the association by connecting to
* an address with the sa_family member of sockaddr set to AF_UNSPEC.
*
* Parameters:
* psock Pointer to a socket structure initialized by psock_socket()
* addr Server address (form depends on type of socket)
* addrlen Length of actual 'addr'
*
* Returned Value:
* 0 on success; a negated errno value on failue. See connect() for the
* list of appropriate errno values to be returned.
*
****************************************************************************/
static int pkt_connect(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen)
{
return -EAFNOSUPPORT;
}
/****************************************************************************
* Name: pkt_bind
*
* Description:
* pkt_bind() gives the socket 'psock' the local address 'addr'. 'addr'
* is 'addrlen' bytes long. Traditionally, this is called "assigning a
* name to a socket." When a socket is created with socket(), it exists
* in a name space (address family) but has no name assigned.
*
* Parameters:
* psock Socket structure of the socket to bind
* addr Socket local address
* addrlen Length of 'addr'
*
* Returned Value:
* 0 on success; A negated errno value is returned on failure. See
* bind() for a list a appropriate error values.
*
****************************************************************************/
static int pkt_bind(FAR struct socket *psock, FAR const struct sockaddr *addr,
socklen_t addrlen)
{
#if 0
char hwaddr[6] = /* our MAC for debugging */
{
0x00, 0xa1, 0xb1, 0xc1, 0xd1, 0xe1
};
#endif
char hwaddr[6] = /* MAC from ifconfig */
{
0x00, 0xe0, 0xde, 0xad, 0xbe, 0xef
};
int ifindex;
/* Verify that a valid address has been provided */
if (addr->sa_family != AF_PACKET || addrlen < sizeof(struct sockaddr_ll)
{
nerr("ERROR: Invalid address length: %d < %d\n",
addrlen, sizeof(struct sockaddr_ll);
return -EBADF;
}
/* Bind a raw socket to an network device. */
if (psock->s_type == SOCK_RAW)
{
FAR struct pkt_conn_s *conn = (FAR struct pkt_conn_s *)psock->s_conn;
/* Look at the addr and identify network interface */
ifindex = addr->sll_ifindex;
#if 0
/* Get the MAC address of that interface */
memcpy(hwaddr, g_netdevices->d_mac.ether, 6);
#endif
/* Put ifindex and mac address into connection */
conn->ifindex = ifindex;
memcpy(conn->lmac, hwaddr, 6);
/* Mark the socket bound */
psock->s_flags |= _SF_BOUND;
return OK;
}
else
{
return -EBADF;
}
}
/****************************************************************************
* Name: pkt_send
*
+2 -2
View File
@@ -44,9 +44,9 @@ SOCK_CSRCS += net_dupsd2.c net_clone.c net_poll.c net_vfcntl.c
SOCK_CSRCS += net_sockif.c
ifeq ($(CONFIG_NET_IPv4),y)
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c
else ifeq ($(CONFIG_NET_IPv6),y)
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c
SOCK_CSRCS += inet_sockif.c inet_recvfrom.c inet_connect.c
endif
# TCP/IP support
+8 -225
View File
@@ -1,7 +1,7 @@
/****************************************************************************
* net/socket/bind.c
*
* Copyright (C) 2007-2009, 2012, 2014-2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2012, 2014-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -38,85 +38,23 @@
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <string.h>
#include <debug.h>
#include <assert.h>
#ifdef CONFIG_NET_PKT
# include <netpacket/packet.h>
#endif
#include <errno.h>
#include <debug.h>
#include <nuttx/net/net.h>
#include <nuttx/net/udp.h>
#include "socket/socket.h"
#include "netdev/netdev.h"
#include "tcp/tcp.h"
#include "udp/udp.h"
#include "pkt/pkt.h"
#include "local/local.h"
#include "usrsock/usrsock.h"
#ifdef CONFIG_NET
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pkt_bind
*
* Description:
* Bind a raw socket to an network device.
*
* Parameters:
* conn AF_PACKET connection structure
* addr Peer address information
*
* Returned Value:
* 0 on success; -1 on error with errno set appropriately
*
****************************************************************************/
#ifdef CONFIG_NET_PKT
static int pkt_bind(FAR struct pkt_conn_s *conn,
FAR const struct sockaddr_ll *addr)
{
int ifindex;
#if 0
char hwaddr[6] = /* our MAC for debugging */
{
0x00, 0xa1, 0xb1, 0xc1, 0xd1, 0xe1
};
#endif
char hwaddr[6] = /* MAC from ifconfig */
{
0x00, 0xe0, 0xde, 0xad, 0xbe, 0xef
};
/* Look at the addr and identify network interface */
ifindex = addr->sll_ifindex;
#if 0
/* Get the MAC address of that interface */
memcpy(hwaddr, g_netdevices->d_mac.ether, 6);
#endif
/* Put ifindex and mac address into connection */
conn->ifindex = ifindex;
memcpy(conn->lmac, hwaddr, 6);
return OK;
}
#endif /* CONFIG_NET_PKT */
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -169,165 +107,10 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
goto errout;
}
/* Verify that a valid address has been provided */
/* Let the address family's connect() method handle the operation */
switch (addr->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
case AF_LOCAL:
minlen = sizeof(sa_family_t);
break;
#endif
#ifdef CONFIG_NET_PKT
case AF_PACKET:
minlen = sizeof(struct sockaddr_ll);
break;
#endif
default:
nerr("ERROR: Unrecognized address family: %d\n", addr->sa_family);
errcode = EAFNOSUPPORT;
goto errout;
}
if (addrlen < minlen)
{
nerr("ERROR: Invalid address length: %d < %d\n", addrlen, minlen);
errcode = EBADF;
goto errout;
}
/* Perform the binding depending on the protocol type */
switch (psock->s_type)
{
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn);
/* Perform the usrsock bind operation */
ret = usrsock_bind(conn, addr, addrlen);
}
break;
#endif
#ifdef CONFIG_NET_PKT
case SOCK_RAW:
ret = pkt_bind(psock->s_conn, lladdr);
break;
#endif
/* Bind a stream socket which may either be TCP/IP or a local, Unix
* domain socket.
*/
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_LOCAL_STREAM)
case SOCK_STREAM:
{
#ifdef CONFIG_NET_LOCAL_STREAM
#ifdef CONFIG_NET_TCP
/* Is this a Unix domain socket? */
if (psock->s_domain == PF_LOCAL)
#endif
{
/* Bind the Unix domain connection structure */
ret = psock_local_bind(psock, addr, addrlen);
}
#endif /* CONFIG_NET_LOCAL_STREAM */
#ifdef CONFIG_NET_TCP
#ifdef CONFIG_NET_LOCAL_STREAM
else
#endif
{
#ifdef NET_TCP_HAVE_STACK
/* Bind the TCP/IP connection structure */
ret = tcp_bind(psock->s_conn, addr);
#else
ret = -ENOSYS;
#endif
}
#endif /* CONFIG_NET_TCP */
/* Mark the socket bound */
if (ret >= 0)
{
psock->s_flags |= _SF_BOUND;
}
}
break;
#endif /* CONFIG_NET_TCP || CONFIG_NET_LOCAL_STREAM */
/* Bind a datagram socket which may either be TCP/IP or a local, Unix
* domain socket.
*/
#if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_LOCAL_DGRAM)
case SOCK_DGRAM:
{
#ifdef CONFIG_NET_LOCAL_DGRAM
#ifdef CONFIG_NET_UDP
/* Is this a Unix domain socket? */
if (psock->s_domain == PF_LOCAL)
#endif
{
/* Bind the Unix domain connection structure */
ret = psock_local_bind(psock, addr, addrlen);
}
#endif /* CONFIG_NET_LOCAL_DGRAM */
#ifdef CONFIG_NET_UDP
#ifdef CONFIG_NET_LOCAL_DGRAM
else
#endif
{
#ifdef NET_UDP_HAVE_STACK
/* Bind the UDPP/IP connection structure */
ret = udp_bind(psock->s_conn, addr);
#else
ret = -ENOSYS;
#endif
}
#endif /* CONFIG_NET_UDP */
/* Mark the socket bound */
if (ret >= 0)
{
psock->s_flags |= _SF_BOUND;
}
}
break;
#endif /* CONFIG_NET_UDP || CONFIG_NET_LOCAL_DGRAM */
default:
errcode = EBADF;
goto errout;
}
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_bind != NULL);
ret = psock->s_sockif->si_bind(psock, addr, addrlen);
/* Was the bind successful */
+9 -552
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+10
View File
@@ -1650,6 +1650,16 @@ ssize_t inet_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
break;
#endif /* CONFIG_NET_UDP || CONFIG_NET_LOCAL_DGRAM */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
/* Perform the usrsock recvfrom operation */
ret = usrsock_recvfrom(psock, buf, len, from, fromlen);
}
break;
#endif
default:
{
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
+274 -46
View File
@@ -49,6 +49,7 @@
#include "tcp/tcp.h"
#include "udp/udp.h"
#include "usrsock/usrsock.h"
#include "socket/socket.h"
#if defined(CONFIG_NET_IPv4) || defined(CONFIG_NET_IPv6)
@@ -57,12 +58,14 @@
* Private Function Prototypes
****************************************************************************/
static int inet_setup(FAR struct socket *psock, int protocol);
static int inet_setup(FAR struct socket *psock, int protocol);
static int inet_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen);
static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf,
size_t len, int flags);
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);
size_t len, int flags, FAR const struct sockaddr *to,
socklen_t tolen);
/****************************************************************************
* Public Data
@@ -71,6 +74,8 @@ static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf,
const struct sock_intf_s g_inet_sockif =
{
inet_setup, /* si_setup */
inet_bind, /* si_bind */
inet_connect, /* si_connect */
inet_send, /* si_send */
inet_sendto, /* si_sendto */
inet_recvfrom /* si_recvfrom */
@@ -152,6 +157,65 @@ static int inet_udp_alloc(FAR struct socket *psock)
}
#endif /* NET_UDP_HAVE_STACK */
/****************************************************************************
* Name: usrsock_socket_setup
*
* Description:
* Special socket setup may be required by user sockets.
*
* Parameters:
* domain (see sys/socket.h)
* type (see sys/socket.h)
* protocol (see sys/socket.h)
* psock A pointer to a user allocated socket structure to be initialized.
*
* Returned Value:
* 0 on success; a negated errno value is returned on failure.
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
static int usrsock_socket_setup(int domain, int type, int protocol,
FAR struct socket *psock)
{
switch (domain)
{
default:
return OK;
case PF_INET:
case PF_INET6:
{
#ifndef CONFIG_NET_USRSOCK_UDP
if (type == SOCK_DGRAM)
{
return OK;
}
#endif
#ifndef CONFIG_NET_USRSOCK_TCP
if (type == SOCK_STREAM)
{
return OK;
}
#endif
psock->s_type = PF_UNSPEC;
psock->s_conn = NULL;
/* Let the user socket logic handle the setup...
*
* A return value of zero means that the operation was
* successfully handled by usrsock. A negative value means that
* an error occurred. The special error value -ENETDOWN means
* that usrsock daemon is not running. The caller should attempt
* to open socket with kernel networking stack in this case.
*/
return usrsock_socket(domain, type, protocol, psock);
}
}
}
#endif /* CONFIG_NET_USRSOCK */
/****************************************************************************
* Name: inet_setup
*
@@ -175,6 +239,27 @@ static int inet_udp_alloc(FAR struct socket *psock)
static int inet_setup(FAR struct socket *psock, int protocol)
{
#ifdef CONFIG_NET_USRSOCK
/* Handle speical setup for user INET sockets */
ret = usrsock_socket_setup(domain, type, protocol, psock);
if (ret < 0)
{
if (ret = -ENETDOWN)
{
/* -ENETDOWN means that usrsock daemon is not running. Attempt to
* open socket with kernel networking stack.
*/
warn("WARNING: usrsock daemon is not running\n");
}
else
{
return ret;
}
}
#endif /* CONFIG_NET_USRSOCK */
/* 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.
@@ -197,6 +282,7 @@ static int inet_setup(FAR struct socket *psock, int protocol)
return inet_tcp_alloc(psock);
#else
warning("WARNING: SOCK_STREAM disabled\n");
return = -ENETDOWN;
#endif
#endif /* CONFIG_NET_TCP */
@@ -214,6 +300,7 @@ static int inet_setup(FAR struct socket *psock, int protocol)
return inet_udp_alloc(psock);
#else
warning("WARNING: SOCK_DGRAM disabled\n");
return -ENETDOWN;
#endif
#endif /* CONFIG_NET_UDP */
@@ -224,6 +311,130 @@ static int inet_setup(FAR struct socket *psock, int protocol)
}
}
/****************************************************************************
* Name: inet_bind
*
* Description:
* inet_bind() gives the socket 'psock' the local address 'addr'. 'addr'
* is 'addrlen' bytes long. Traditionally, this is called "assigning a
* name to a socket." When a socket is created with socket(), it exists
* in a name space (address family) but has no name assigned.
*
* Parameters:
* psock Socket structure of the socket to bind
* addr Socket local address
* addrlen Length of 'addr'
*
* Returned Value:
* 0 on success; A negated errno value is returned on failure. See
* bind() for a list a appropriate error values.
*
****************************************************************************/
static int inet_bind(FAR struct socket *psock,
FAR const struct sockaddr *addr, socklen_t addrlen)
{
int minlen;
int ret;
/* Verify that a valid address has been provided */
switch (addr->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
default:
nerr("ERROR: Unrecognized address family: %d\n", addr->sa_family);
return -EAFNOSUPPORT;
}
if (addrlen < minlen)
{
nerr("ERROR: Invalid address length: %d < %d\n", addrlen, minlen);
return -EBADF;
}
/* Perform the binding depending on the protocol type */
switch (psock->s_type)
{
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
FAR struct usrsock_conn_s *conn = psock->s_conn;
DEBUGASSERT(conn != NULL);
/* Perform the usrsock bind operation */
ret = usrsock_bind(conn, addr, addrlen);
}
break;
#endif
#ifdef CONFIG_NET_TCP
case SOCK_STREAM:
{
#ifdef NET_TCP_HAVE_STACK
/* Bind a TCP/IP stream socket. */
ret = tcp_bind(psock->s_conn, addr);
/* Mark the socket bound */
if (ret >= 0)
{
psock->s_flags |= _SF_BOUND;
}
#else
nwarn("WARNING: TCP/IP stack is not available in this configuration\n");
return -ENOSYS;
#endif
}
break;
#endif /* CONFIG_NET_TCP */
#ifdef CONFIG_NET_UDP
case SOCK_DGRAM:
{
#ifdef NET_UDP_HAVE_STACK
/* Bind a UDP/IP datagram socket */
ret = udp_bind(psock->s_conn, addr);
/* Mark the socket bound */
if (ret >= 0)
{
psock->s_flags |= _SF_BOUND;
}
#else
nwarn("WARNING: UDP stack is not available in this configuration\n");
ret = -ENOSYS;
#endif
}
break;
#endif /* CONFIG_NET_UDP */
default:
nerr("ERROR: Unsupported socket type: %d\n", psock->s_type);
ret = -EBADF;
break;
}
return ret;
}
/****************************************************************************
* Name: inet_send
*
@@ -303,6 +514,16 @@ static ssize_t inet_send(FAR struct socket *psock, FAR const void *buf,
break;
#endif /* CONFIG_NET_UDP */
/* Special case user sockets */
#ifdef CONFIG_NET_USRSOCK
case SOCK_USRSOCK_TYPE:
{
ret = usrsock_sendto(psock, buf, len, NULL, 0);
}
break;
#endif /*CONFIG_NET_USRSOCK*/
default:
{
/* EDESTADDRREQ. Signifies that the socket is not connection-mode
@@ -347,71 +568,78 @@ static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf,
socklen_t minlen;
ssize_t nsent;
/* Verify that a valid address has been provided */
switch (to->sa_family)
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
/* Perform the usrsock sendto operation */
nsent = usrsock_sendto(psock, buf, len, to, tolen);
}
else
#endif
{
/* 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;
case AF_INET:
minlen = sizeof(struct sockaddr_in);
break;
#endif
#ifdef CONFIG_NET_IPv6
case AF_INET6:
minlen = sizeof(struct sockaddr_in6);
break;
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;
}
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;
}
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 this is a connected socket, then return EISCONN */
if (psock->s_type != SOCK_DGRAM)
{
nerr("ERROR: Connected socket\n");
return -EBADF;
}
if (psock->s_type != SOCK_DGRAM)
{
nerr("ERROR: Connected socket\n");
return -EBADF;
}
/* Now handle the INET sendto() operation */
/* Now handle the INET sendto() operation */
#if defined(CONFIG_NET_6LOWPAN)
/* Try 6LoWPAN UDP packet sendto() */
/* Try 6LoWPAN UDP packet sendto() */
nsent = psock_6lowpan_udp_sendto(psock, buf, len, flags, to, tolen);
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 */
if (nsent < 0)
{
/* UDP/IP packet sendto */
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
}
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);
nsent = psock_udp_sendto(psock, buf, len, flags, to, tolen);
#else
nsent = -ENOSYS;
nwarn("WARNING: UDP not available in this configuiration\n")
nsent = -ENOSYS;
#endif /* CONFIG_NET_6LOWPAN */
#else
nsent = -EISCONN;
nwarn("WARNING: UDP not enabled in this configuiration\n")
nsent = -EISCONN;
#endif /* CONFIG_NET_UDP */
}
return nsent;
}
@@ -421,7 +649,7 @@ static ssize_t inet_sendto(FAR struct socket *psock, FAR const void *buf,
****************************************************************************/
/****************************************************************************
* Name:
* Name:
*
* Description:
*
-2
View File
@@ -155,5 +155,3 @@ int net_clone(FAR struct socket *psock1, FAR struct socket *psock2)
}
#endif /* CONFIG_NET */
+6 -26
View File
@@ -46,7 +46,6 @@
#include <nuttx/net/net.h>
#include "socket/socket.h"
#include "usrsock/usrsock.h"
#ifdef CONFIG_NET
@@ -147,33 +146,14 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len,
psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_RECV);
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
{
/* Perform the usrsock recvfrom operation */
/* Let logic specific to this address family handle the recvfrom()
* operation.
*/
ret = usrsock_recvfrom(psock, buf, len, from, fromlen);
}
else
#endif
{
/* Let logic specific to this address family handle the recvfrom()
* operation.
*/
DEBUGASSERT(psock->s_sockif != NULL &&
psock->s_sockif->si_recvfrom != NULL);
DEBUGASSERT(psock->s_sockif != NULL &&
psock->s_sockif->si_recvfrom != NULL);
if (psock->s_sockif->si_recvfrom != NULL)
{
ret = psock->s_sockif->si_recvfrom(psock, buf, len, flags,
from, fromlen);
}
else
{
ret = -ENOSYS;
}
}
ret = psock->s_sockif->si_recvfrom(psock, buf, len, flags, from, fromlen);
/* Set the socket state to idle */
+3 -15
View File
@@ -53,7 +53,6 @@
#include "sixlowpan/sixlowpan.h"
#include "local/local.h"
#include "socket/socket.h"
#include "usrsock/usrsock.h"
/****************************************************************************
* Public Functions
@@ -134,21 +133,10 @@ ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len,
(void)enter_cancellation_point();
/* Special case user sockets */
/* Let the address family's send() method handle the operation */
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type SOCK_USRSOCK_TYPE)
{
ret = usrsock_sendto(psock, buf, len, NULL, 0);
}
else
#endif /*CONFIG_NET_USRSOCK*/
{
/* Let the address family's send() method handle the operation */
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_send != NULL);
ret = psock->s_sockif->si_send(psock, buf, len, flags);
}
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_send != NULL);
ret = psock->s_sockif->si_send(psock, buf, len, flags);
leave_cancellation_point();
if (ret < 0)
+9 -21
View File
@@ -53,7 +53,6 @@
#include "sixlowpan/sixlowpan.h"
#include "local/local.h"
#include "socket/socket.h"
#include "usrsock/usrsock.h"
/****************************************************************************
* Public Functions
@@ -149,30 +148,19 @@ ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf,
#endif
}
#ifdef CONFIG_NET_USRSOCK
if (psock->s_type == SOCK_USRSOCK_TYPE)
/* Verify that the psock corresponds to valid, allocated socket */
if (!psock || psock->s_crefs <= 0)
{
/* Perform the usrsock sendto operation */
nsent = usrsock_sendto(psock, buf, len, to, tolen);
nerr("ERROR: Invalid socket\n");
errcode = EBADF;
goto errout;
}
else
#endif
{
/* Verify that the psock corresponds to valid, allocated socket */
if (!psock || psock->s_crefs <= 0)
{
nerr("ERROR: Invalid socket\n");
errcode = EBADF;
goto errout;
}
/* Let the address family's send() method handle the operation */
/* Let the address family's send() method handle the operation */
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_send != NULL);
nsent = psock->s_sockif->si_send(psock, buf, len, flags);
}
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_send != NULL);
nsent = psock->s_sockif->si_send(psock, buf, len, flags);
/* Check if the domain-specific sendto() logic failed */
-87
View File
@@ -44,87 +44,10 @@
#include <assert.h>
#include <debug.h>
#include "usrsock/usrsock.h"
#include "socket/socket.h"
#ifdef CONFIG_NET
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: usrsock_socket_setup
*
* Description:
* Special socket setup may be required by user sockets.
*
* Parameters:
* domain (see sys/socket.h)
* type (see sys/socket.h)
* protocol (see sys/socket.h)
* psock A pointer to a user allocated socket structure to be initialized.
*
* Returned Value:
* 0 on success; -1 on error with errno set appropriately
*
****************************************************************************/
#ifdef CONFIG_NET_USRSOCK
static int usrsock_socket_setup(int domain, int type, int protocol,
FAR struct socket *psock)
{
int errcode;
int ret;
switch (domain)
{
default:
break;
case PF_INET:
case PF_INET6:
{
#ifndef CONFIG_NET_USRSOCK_UDP
if (type == SOCK_DGRAM)
{
break;
}
#endif
#ifndef CONFIG_NET_USRSOCK_TCP
if (type == SOCK_STREAM)
{
break;
}
#endif
psock->s_type = 0;
psock->s_conn = NULL;
ret = usrsock_socket(domain, type, protocol, psock);
if (ret >= 0)
{
/* Successfully handled and opened by usrsock daemon. */
return OK;
}
else if (ret == -ENETDOWN)
{
/* Net down means that usrsock daemon is not running.
* Attempt to open socket with kernel networking stack.
*/
}
else
{
return ret;
}
}
}
return OK;
}
#else
#endif /* CONFIG_NET_USRSOCK */
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -173,16 +96,6 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
int errcode;
int ret;
#ifdef CONFIG_NET_USRSOCK
ret = usrsock_socket_setup(domain, type, protocol, psock);
if (ret < 0)
{
nerr("ERROR: usrsock_socket_setup() failed: %d\n", ret);
errcode = -ret;
goto errout;
}
#endif /* CONFIG_NET_USRSOCK */
/* Initialize the socket structure */
psock->s_domain = domain;
+35
View File
@@ -436,6 +436,41 @@ int net_timeo(systime_t start_time, socktimeo_t timeo);
ssize_t psock_send(FAR struct socket *psock, FAR const void *buf, size_t len,
int flags);
/****************************************************************************
* Name: inet_connect
*
* Description:
* inet_connect() connects the local socket referred to by the structure
* 'psock' to the address specified by 'addr'. The addrlen argument
* specifies the size of 'addr'. The format of the address in 'addr' is
* determined by the address space of the socket 'psock'.
*
* If the socket 'psock' is of type SOCK_DGRAM then 'addr' is the address
* to which datagrams are sent by default, and the only address from which
* datagrams are received. If the socket is of type SOCK_STREAM or
* SOCK_SEQPACKET, this call attempts to make a connection to the socket
* that is bound to the address specified by 'addr'.
*
* Generally, connection-based protocol sockets may successfully
* inet_connect() only once; connectionless protocol sockets may use
* inet_connect() multiple times to change their association.
* Connectionless sockets may dissolve the association by connecting to
* an address with the sa_family member of sockaddr set to AF_UNSPEC.
*
* Parameters:
* psock Pointer to a socket structure initialized by psock_socket()
* addr Server address (form depends on type of socket)
* addrlen Length of actual 'addr'
*
* Returned Value:
* 0 on success; a negated errno value on failue. See connect() for the
* list of appropriate errno values to be returned.
*
****************************************************************************/
int inet_connect(FAR struct socket *psock, FAR const struct sockaddr *addr,
socklen_t addrlen);
/****************************************************************************
* Name: inet_recvfrom
*