From 0792c58515fae8507fcd6de41ca7db89fd2734d4 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 2 Sep 2007 23:11:10 +0000 Subject: Refactoring to provide socket support for UDP git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@327 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/net/uip/Make.defs | 3 +- nuttx/net/uip/uip-internal.h | 8 ++ nuttx/net/uip/uip-tcpconn.c | 62 +++++++----- nuttx/net/uip/uip-udpconn.c | 232 ++++++++++++++++++++++++++++++++++++------- nuttx/net/uip/uip.c | 132 ++---------------------- 5 files changed, 249 insertions(+), 188 deletions(-) (limited to 'nuttx/net') diff --git a/nuttx/net/uip/Make.defs b/nuttx/net/uip/Make.defs index 716682547..96691792d 100644 --- a/nuttx/net/uip/Make.defs +++ b/nuttx/net/uip/Make.defs @@ -34,5 +34,6 @@ ############################################################################ UIP_ASRCS = -UIP_CSRCS = psock.c uip-arp.c uip.c uip-fw.c uip-neighbor.c uip-split.c uip-tcpconn.c uip-wait.c +UIP_CSRCS = psock.c uip-arp.c uip.c uip-fw.c uip-neighbor.c uip-split.c \ + uip-tcpconn.c uip-udpconn.c uip-wait.c diff --git a/nuttx/net/uip/uip-internal.h b/nuttx/net/uip/uip-internal.h index 987b650c7..0f47fbc52 100644 --- a/nuttx/net/uip/uip-internal.h +++ b/nuttx/net/uip/uip-internal.h @@ -61,6 +61,9 @@ extern uint8 g_tcp_sequence[4]; +extern const uip_ipaddr_t all_ones_addr; +extern const uip_ipaddr_t all_zeroes_addr; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -78,6 +81,11 @@ EXTERN void uip_tcpinit(void); EXTERN struct uip_conn *uip_tcpactive(struct uip_tcpip_hdr *buf); EXTERN void uip_tcpnextsequence(void); +/* Defined in uip_udpconn.c *************************************************/ + +EXTERN void uip_udpinit(void); +EXTERN struct uip_udp_conn *uip_udpactive(struct uip_udpip_hdr *buf); + #undef EXTERN #ifdef __cplusplus } diff --git a/nuttx/net/uip/uip-tcpconn.c b/nuttx/net/uip/uip-tcpconn.c index c877de2c4..2c6d0cd02 100644 --- a/nuttx/net/uip/uip-tcpconn.c +++ b/nuttx/net/uip/uip-tcpconn.c @@ -1,9 +1,14 @@ /**************************************************************************** - * uip_tcpbind.c + * uip_tcpconn.c * * Copyright (C) 2007 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * + * Large parts of this file were leveraged from uIP logic: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -11,25 +16,23 @@ * 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 Gregory Nutt nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not 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. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. * ****************************************************************************/ @@ -46,6 +49,7 @@ #include #include +#include #include #include @@ -66,7 +70,7 @@ uint8 g_tcp_sequence[4]; * Private Data ****************************************************************************/ -/* The array containing all uIP connections. */ +/* The array containing all uIP TCP connections. */ static struct uip_conn g_tcp_connections[UIP_CONNS]; @@ -113,8 +117,8 @@ static struct uip_conn *uip_find_conn(uint16 portno) * Name: uip_tcpinit() * * Description: - * Initialize the TCP/IP connection structures. Called only from the UIP - * layer. + * Initialize the TCP/IP connection structures. Called only once and only + * from the UIP layer. * ****************************************************************************/ @@ -237,9 +241,14 @@ struct uip_conn *uip_tcpactive(struct uip_tcpip_hdr *buf) buf->destport == conn->lport && buf->srcport == conn->rport && uip_ipaddr_cmp(buf->srcipaddr, conn->ripaddr)) { + /* Matching connection found.. return a reference to it */ + return conn; } } + + /* No match found */ + return NULL; } @@ -247,7 +256,7 @@ struct uip_conn *uip_tcpactive(struct uip_tcpip_hdr *buf) * Name: uip_tcppoll() * * Description: - * Periodic processing for a connection identified by its number. + * Periodic processing for a TCP connection identified by its number. * This function does the necessary periodic processing (timers, * polling) for a uIP TCP conneciton, and should be called by the UIP * device driver when the periodic uIP timer goes off. It should be @@ -267,7 +276,7 @@ void uip_tcppoll(unsigned int conn) } /**************************************************************************** - * Name: uip_tcpactive() + * Name: uip_tcpnextsequence() * * Description: * Increment the TCP/IP sequence number @@ -312,11 +321,11 @@ int uip_tcpbind(struct uip_conn *conn, const struct sockaddr_in *addr) #endif { #warning "Need to implement bind logic" - return ERROR; + return -ENOSYS; } /**************************************************************************** - * Name: uip_tcpbind() + * Name: uip_tcpconnect() * * Description: * This function implements the UIP specific parts of the standard @@ -342,7 +351,6 @@ int uip_tcpconnect(struct uip_conn *conn, const struct sockaddr_in *addr ) #endif { uint16 port; - int i; /* If the TCP port has not alread been bound to a local port, then select * one now. diff --git a/nuttx/net/uip/uip-udpconn.c b/nuttx/net/uip/uip-udpconn.c index 3b7f06fa6..1738e5d5a 100644 --- a/nuttx/net/uip/uip-udpconn.c +++ b/nuttx/net/uip/uip-udpconn.c @@ -4,6 +4,11 @@ * Copyright (C) 2007 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * + * Large parts of this file were leveraged from uIP logic: + * + * Copyright (c) 2001-2003, Adam Dunkels. + * All rights reserved. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -11,25 +16,23 @@ * 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 Gregory Nutt 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. + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. * ************************************************************/ @@ -44,37 +47,190 @@ #include #if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) +#include +#include +#include +#include + +#include +#include +#include + +#include "uip-internal.h" + +/************************************************************ + * Private Data + ************************************************************/ + +/* The array containing all uIP UDP connections. */ + +struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS]; + +/* Last port used by a UDP connection connection. */ + +static uint16 g_last_udp_port; + /************************************************************ * Private Functions ************************************************************/ +#ifdef CONFIG_NET_UDP +struct uip_udp_conn *uip_find_udp_conn( uint16 portno ) +{ + struct uip_udp_conn *conn; + int i; + + for (i = 0; i < UIP_UDP_CONNS; i++) + { + if (uip_udp_conns[i].lport == htons(g_last_udp_port)) + { + return conn; + } + } + + return NULL; +} +#endif /* CONFIG_NET_UDP */ + /************************************************************ * Public Functions ************************************************************/ -struct uip_udp_conn *uip_udpalloc(void); -void uip_udpfree(struct uip_udp_conn *conn); +/**************************************************************************** + * Name: uip_udpinit() + * + * Description: + * Initialize the UDP connection structures. Called once and only from + * the UIP layer. + * + ****************************************************************************/ -#ifdef CONFIG_NET_IPv6 -int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in6 *addr); -#else -int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in *addr); -#endif +void uip_udpinit(void) +{ + int i; + for (i = 0; i < UIP_UDP_CONNS; i++) + { + uip_udp_conns[i].lport = 0; + } + + g_last_udp_port = 1024; +} + +/**************************************************************************** + * Name: uip_udpalloc() + * + * Description: + * Find a free UDP connection structure and allocate it for use. This is + * normally something done by the implementation of the socket() API. + * + ****************************************************************************/ + +struct uip_udp_conn *uip_udpalloc(void) +{ +#warning "Need to implement allocation logic" + return NULL; +} + +/**************************************************************************** + * Name: uip_udpfree() + * + * Description: + * Free a UDP connection structure that is no longer in use. This should be + * done by the implementation of close() + * + ****************************************************************************/ + +void uip_udpfree(struct uip_udp_conn *conn) +{ +#warning "Need to implement release logic" +} + +/**************************************************************************** + * Name: uip_udpactive() + * + * Description: + * Find a connection structure that is the appropriate + * connection to be used withi the provided TCP/IP header + * + * Assumptions: + * This function is called from UIP logic at interrupt level + * + ****************************************************************************/ + +struct uip_udp_conn *uip_udpactive(struct uip_udpip_hdr *buf) +{ + struct uip_udp_conn *conn; + for (conn = &uip_udp_conns[0]; conn < &uip_udp_conns[UIP_UDP_CONNS]; conn++) + { + /* If the local UDP port is non-zero, the connection is considered + * to be used. If so, the local port number is checked against the + * destination port number in the received packet. If the two port + * numbers match, the remote port number is checked if the + * connection is bound to a remote port. Finally, if the + * connection is bound to a remote IP address, the source IP + * address of the packet is checked. + */ + + if (conn->lport != 0 && buf->destport == conn->lport && + (conn->rport == 0 || buf->srcport == conn->rport) && + (uip_ipaddr_cmp(conn->ripaddr, all_zeroes_addr) || + uip_ipaddr_cmp(conn->ripaddr, all_ones_addr) || + uip_ipaddr_cmp(buf->srcipaddr, conn->ripaddr))) + { + /* Matching connection found.. return a reference to it */ + + return conn; + } + } + + /* No match found */ + + return NULL; +} + +/**************************************************************************** + * Name: uip_udppoll() + * + * Description: + * Periodic processing for a UDP connection identified by its number. + * This function does the necessary periodic processing (timers, + * polling) for a uIP TCP conneciton, and should be called by the UIP + * device driver when the periodic uIP timer goes off. It should be + * called for every connection, regardless of whether they are open of + * closed. + * + * Assumptions: + * This function is called from the CAN device driver may be called from + * the timer interrupt/watchdog handle level. + * + ****************************************************************************/ + +void uip_udppoll(unsigned int conn) +{ + uip_udp_conn = &uip_udp_conns[conn]; + uip_interrupt(UIP_UDP_TIMER); +} + +/**************************************************************************** + * Name: uip_tcpbind() + * + * Description: + * This function implements the UIP specific parts of the standard TCP + * bind() operation. + * + * Assumptions: + * This function is called from normal user level code. + * + ****************************************************************************/ #ifdef CONFIG_NET_IPv6 -int uip_udpconnect(struct uip_udp_conn *conn, const struct sockaddr_in6 *addr ) +int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in6 *addr) #else int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in *addr) #endif { - uint16 ipaddr[2]; - - if (pdhcpc->state == STATE_INITIAL) - { - uip_ipaddr(ipaddr, 0,0,0,0); - uip_sethostaddr(ipaddr); - } - return OK; +#warning "Need to implement bind logic" + return -ENOSYS; } /* Set up a new UDP connection. @@ -134,9 +290,9 @@ struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, uint16 rport) conn = 0; for (i = 0; i < UIP_UDP_CONNS; i++) { - if (uip_udp_conns[c].lport == 0) + if (uip_udp_conns[i].lport == 0) { - conn = &uip_udp_conns[c]; + conn = &uip_udp_conns[i]; break; } } @@ -167,4 +323,4 @@ struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, uint16 rport) return conn; } -#endif /* CONFIG_NET */ +#endif /* CONFIG_NET && CONFIG_NET_UDP */ diff --git a/nuttx/net/uip/uip.c b/nuttx/net/uip/uip.c index 830f34551..e45e0e560 100644 --- a/nuttx/net/uip/uip.c +++ b/nuttx/net/uip/uip.c @@ -181,7 +181,6 @@ uint16 uip_listenports[UIP_LISTENPORTS]; /* The uip_listenports list all currently listning ports. */ #ifdef CONFIG_NET_UDP struct uip_udp_conn *uip_udp_conn; -struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS]; #endif /* CONFIG_NET_UDP */ /* Temporary variables. */ @@ -194,24 +193,24 @@ struct uip_stats uip_stat; # define UIP_STAT(s) #endif /* UIP_STATISTICS == 1 */ -/**************************************************************************** - * Private Variables - ****************************************************************************/ - -static const uip_ipaddr_t all_ones_addr = +const uip_ipaddr_t all_ones_addr = #ifdef CONFIG_NET_IPv6 {0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff}; #else /* CONFIG_NET_IPv6 */ {0xffff,0xffff}; #endif /* CONFIG_NET_IPv6 */ -static const uip_ipaddr_t all_zeroes_addr = +const uip_ipaddr_t all_zeroes_addr = #ifdef CONFIG_NET_IPv6 {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; #else /* CONFIG_NET_IPv6 */ {0x0000,0x0000}; #endif /* CONFIG_NET_IPv6 */ +/**************************************************************************** + * Private Variables + ****************************************************************************/ + #if UIP_FIXEDETHADDR const struct uip_eth_addr uip_ethaddr = {{ UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5 }}; @@ -222,11 +221,6 @@ struct uip_eth_addr uip_ethaddr = {{ 0,0,0,0,0,0 }}; static uint16 ipid; /* Ths ipid variable is an increasing number that is * used for the IP ID field. */ -#ifdef CONFIG_NET_UDP -static uint16 g_last_udp_port; /* Keeps track of the last port used for a new - * connection. */ -#endif - /* Temporary variables. */ static uint8 c, opt; static uint16 tmp16; @@ -307,24 +301,6 @@ static uint16 uip_icmp6chksum(void) #endif /* UIP_ARCH_CHKSUM */ -#ifdef CONFIG_NET_UDP -struct uip_udp_conn *uip_find_udp_conn( uint16 portno ) -{ - struct uip_udp_conn *conn; - int i; - - for (i = 0; i < UIP_UDP_CONNS; i++) - { - if (uip_udp_conns[i].lport == htons(g_last_udp_port)) - { - return conn; - } - } - - return NULL; -} -#endif /* CONFIG_NET_UDP */ - /**************************************************************************** * Public Functions ****************************************************************************/ @@ -419,14 +395,7 @@ void uip_init(void) /* Initialize the UDP connection structures */ -#ifdef CONFIG_NET_UDP - for (c = 0; c < UIP_UDP_CONNS; ++c) - { - uip_udp_conns[c].lport = 0; - } - - g_last_udp_port = 1024; -#endif /* CONFIG_NET_UDP */ + uip_udpinit(); /* IPv4 initialization. */ #if UIP_FIXEDADDR == 0 @@ -434,71 +403,6 @@ void uip_init(void) #endif /* UIP_FIXEDADDR */ } -#ifdef CONFIG_NET_UDP -struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, uint16 rport) -{ - struct uip_udp_conn *conn; - int i; - - /* Find an unused local port number. Loop until we find a valid listen port - * number that is not being used by any other connection. - */ - - do - { - /* Guess that the next available port number will be the one after - * the last port number assigned. - */ - - ++g_last_udp_port; - - /* Make sure that the port number is within range */ - if (g_last_udp_port >= 32000) - { - g_last_udp_port = 4096; - } - } - while (uip_find_udp_conn(g_last_udp_port)); - - /* Now find an available UDP connection structure */ - - conn = 0; - for (i = 0; i < UIP_UDP_CONNS; i++) - { - if (uip_udp_conns[c].lport == 0) - { - conn = &uip_udp_conns[c]; - break; - } - } - - /* Return an error if no connection is available */ - - if (conn == 0) - { - return 0; - } - - /* Initialize and return the connection structure, bind it to the port number */ - - conn->lport = HTONS(g_last_udp_port); - conn->rport = rport; - - if (ripaddr == NULL) - { - memset(conn->ripaddr, 0, sizeof(uip_ipaddr_t)); - } - else - { - uip_ipaddr_copy(&conn->ripaddr, ripaddr); - } - - conn->ttl = UIP_TTL; - - return conn; -} -#endif /* CONFIG_NET_UDP */ - void uip_unlisten(uint16 port) { for (c = 0; c < UIP_LISTENPORTS; ++c) @@ -1180,26 +1084,10 @@ void uip_interrupt(uint8 flag) /* Demultiplex this UDP packet between the UDP "connections". */ - for (uip_udp_conn = &uip_udp_conns[0]; uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS]; uip_udp_conn++) + uip_udp_conn = uip_udpactive(UDPBUF); + if (uip_udp_conn) { - /* If the local UDP port is non-zero, the connection is considered - * to be used. If so, the local port number is checked against the - * destination port number in the received packet. If the two port - * numbers match, the remote port number is checked if the - * connection is bound to a remote port. Finally, if the - * connection is bound to a remote IP address, the source IP - * address of the packet is checked. - */ - - if (uip_udp_conn->lport != 0 && UDPBUF->destport == uip_udp_conn->lport && - (uip_udp_conn->rport == 0 || - UDPBUF->srcport == uip_udp_conn->rport) && - (uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) || - uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) || - uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) - { - goto udp_found; - } + goto udp_found; } UIP_LOG("udp: no matching connection found"); -- cgit v1.2.3