From 927e1d0a2ff0796ea72dc4a35ecc24863868e4c0 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 13 Mar 2012 13:02:45 +0000 Subject: [PATCH] Add psock_connect() git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4483 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 4 +- include/nuttx/net/net.h | 12 +++-- net/connect.c | 97 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 100 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8261881406..2f80f84a37 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2544,7 +2544,7 @@ the USB (device) driver on the PIC32 Ethernet Starter Kit. * arch/mips/src/pic32mx/pic32mx_ethernet.c: Verifed the PIC32 Ethernet driver on the PIC32 Starter Kit. Status: It is occasionally functional - but no stable enough for use. + but not stable enough for use. * arch/arm/include/stm32, arch/arm/src/stm32: Add general support for the STM32 F2 family. Contributed by Gary Teravskis, * configs/stm3220g-eval: Add support for the STMicro STM3220G-EVAL board. @@ -2559,3 +2559,5 @@ USB host driver with name changes only). * arch/arm/src/stm32/chip/stm32_otgfs.h: STM32 USB OTG FS register definitions (not complete on initial check-in). + * net/connect.c: Add another low level, thread-independent socket interface + for use within the OS. diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index a78ce1251b..584d887efc 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -168,10 +168,16 @@ EXTERN int net_close(int sockfd); EXTERN int psock_close(FAR struct socket *psock); /* net_close.c ***************************************************************/ -/* Performs the bind operation of a socket instance */ +/* Performs the bind() operation on a socket instance */ -EXTERN int psock_bind(FAR struct socket *psock, const struct sockaddr *addr, - socklen_t addrlen); +EXTERN int psock_bind(FAR struct socket *psock, + FAR const struct sockaddr *addr, socklen_t addrlen); + +/* connect.c *****************************************************************/ +/* Performs the connect() operation on a socket instance */ + +EXTERN int psock_connect(FAR struct socket *psock, + FAR const struct sockaddr *addr, socklen_t addrlen); /* send.c ********************************************************************/ /* Send using underlying socket structure */ diff --git a/net/connect.c b/net/connect.c index cffdb12e5b..fdbb34f55f 100644 --- a/net/connect.c +++ b/net/connect.c @@ -354,15 +354,15 @@ static inline int tcp_connect(FAR struct socket *psock, const struct sockaddr_in * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: connect + * Name: psock_connect * * Description: - * connect() connects the socket referred to by the file descriptor sockfd + * connect() connects the 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 sockfd. + * determined by the address space of the socket 'psock'. * - * If the socket sockfd is of type SOCK_DGRAM then 'addr' is the address + * 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 @@ -375,7 +375,7 @@ static inline int tcp_connect(FAR struct socket *psock, const struct sockaddr_in * sockaddr set to AF_UNSPEC. * * Parameters: - * sockfd Socket descriptor returned by socket() + * psock Pointer to a socket structure initialized by psock_socket() * addr Server address (form depends on type of socket) * addrlen Length of actual 'addr' * @@ -422,10 +422,9 @@ static inline int tcp_connect(FAR struct socket *psock, const struct sockaddr_in * ****************************************************************************/ -int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) +int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, + socklen_t addrlen) { - FAR struct socket *psock = sockfd_socket(sockfd); - #if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP) #ifdef CONFIG_NET_IPv6 FAR const struct sockaddr_in6 *inaddr = (const struct sockaddr_in6 *)addr; @@ -437,7 +436,7 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) int err; - /* Verify that the sockfd corresponds to valid, allocated socket */ + /* Verify that the psock corresponds to valid, allocated socket */ if (!psock || psock->s_crefs <= 0) { @@ -509,4 +508,84 @@ errout: return ERROR; } +/**************************************************************************** + * Name: connect + * + * Description: + * connect() connects the socket referred to by the file descriptor 'sockfd' + * 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 'sockfd'. + * + * If the socket 'sockfd' 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 connect() + * only once; connectionless protocol sockets may use 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: + * sockfd Socket descriptor returned by socket() + * addr Server address (form depends on type of socket) + * addrlen Length of actual 'addr' + * + * Returned Value: + * 0 on success; -1 on error with errno set appropriately + * + * EACCES, EPERM + * The user tried to connect to a broadcast address without having the + * socket broadcast flag enabled or the connection request failed + * because of a local firewall rule. + * EADDRINUSE + * Local address is already in use. + * EAFNOSUPPORT + * The passed address didn't have the correct address family in its + * sa_family field. + * EAGAIN + * No more free local ports or insufficient entries in the routing + * cache. + * EALREADY + * The socket is non-blocking and a previous connection attempt has + * not yet been completed. + * EBADF + * The file descriptor is not a valid index in the descriptor table. + * ECONNREFUSED + * No one listening on the remote address. + * EFAULT + * The socket structure address is outside the user's address space. + * EINPROGRESS + * The socket is non-blocking and the connection cannot be completed + * immediately. + * EINTR + * The system call was interrupted by a signal that was caught. + * EISCONN + * The socket is already connected. + * ENETUNREACH + * Network is unreachable. + * ENOTSOCK + * The file descriptor is not associated with a socket. + * ETIMEDOUT + * Timeout while attempting connection. The server may be too busy + * to accept new connections. + * + * Assumptions: + * + ****************************************************************************/ + +int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen) +{ + /* Get the underlying socket structure */ + + FAR struct socket *psock = sockfd_socket(sockfd); + + /* Then let psock_connect() do all of the work */ + + return psock_connect(psock, addr, addrlen); +} + #endif /* CONFIG_NET */