diff options
Diffstat (limited to 'nuttx/net/uip/uip-udpconn.c')
-rw-r--r-- | nuttx/net/uip/uip-udpconn.c | 232 |
1 files changed, 194 insertions, 38 deletions
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 <spudmonkey@racsa.co.cr> * + * 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 <nuttx/config.h> #if defined(CONFIG_NET) && defined(CONFIG_NET_UDP) +#include <sys/types.h> +#include <string.h> +#include <errno.h> +#include <arch/irq.h> + +#include <net/uip/uipopt.h> +#include <net/uip/uip.h> +#include <net/uip/uip-arch.h> + +#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 */ |