From 70f3e5ed059fc0dc7e69ed9322ed01a6145a9f44 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Fri, 26 Feb 2016 23:04:51 +0100 Subject: [PATCH] Cleanup: Create new file ip.c and move dual-stack code from ipv4 and ipv6 folder in there --- src/Filelists.mk | 1 + src/core/init.c | 4 -- src/core/ip.c | 113 +++++++++++++++++++++++++++++++++++++++ src/core/ipv4/ip4.c | 23 -------- src/core/ipv6/ip6.c | 17 ------ src/core/ipv6/ip6_addr.c | 35 ------------ 6 files changed, 114 insertions(+), 79 deletions(-) create mode 100644 src/core/ip.c diff --git a/src/Filelists.mk b/src/Filelists.mk index 9bfb30e6..41b44db7 100644 --- a/src/Filelists.mk +++ b/src/Filelists.mk @@ -34,6 +34,7 @@ COREFILES=$(LWIPDIR)/core/def.c \ $(LWIPDIR)/core/dns.c \ $(LWIPDIR)/core/inet_chksum.c \ $(LWIPDIR)/core/init.c \ + $(LWIPDIR)/core/ip.c \ $(LWIPDIR)/core/mem.c \ $(LWIPDIR)/core/memp.c \ $(LWIPDIR)/core/netif.c \ diff --git a/src/core/init.c b/src/core/init.c index 349d071d..99419df7 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -61,10 +61,6 @@ #include "lwip/api.h" #include "netif/ppp/ppp_impl.h" -#if LWIP_IPV4 && LWIP_IPV6 -const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT; -#endif - /* Compile-time sanity checks for configuration errors. * These can be done independently of LWIP_DEBUG, without penalty. */ diff --git a/src/core/ip.c b/src/core/ip.c new file mode 100644 index 00000000..4f431be6 --- /dev/null +++ b/src/core/ip.c @@ -0,0 +1,113 @@ +/** + * @file ip.c + * Common IPv4 and IPv6 code + * + */ + +/* + * Copyright (c) 2001-2004 Swedish Institute of Computer Science. + * All rights reserved. + * + * 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. 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. + * + * This file is part of the lwIP TCP/IP stack. + * + * Author: Adam Dunkels + * + */ + +#include "lwip/opt.h" + +#if LWIP_IPV4 || LWIP_IPV6 + +#include "lwip/ip_addr.h" +#include "lwip/ip.h" + +#if LWIP_IPV4 && LWIP_IPV6 +const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT; + +/** Convert IP address string (both versions) to numeric. + * The version is auto-detected from the string. + * + * @param cp IP address string to convert + * @param addr conversion result is stored here + * @return 1 on success, 0 on error + */ +int +ipaddr_aton(const char *cp, ip_addr_t *addr) +{ + if (cp != NULL) { + const char* c; + for (c = cp; *c != 0; c++) { + if (*c == ':') { + /* contains a colon: IPv6 address */ + if (addr) { + IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6); + } + return ip6addr_aton(cp, ip_2_ip6(addr)); + } else if (*c == '.') { + /* contains a dot: IPv4 address */ + break; + } + } + /* call ip4addr_aton as fallback or if IPv4 was found */ + if (addr) { + IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4); + } + return ip4addr_aton(cp, ip_2_ip4(addr)); + } + return 0; +} + +#endif /* LWIP_IPV4 && LWIP_IPV6 */ + +/** Global data for both IPv4 and IPv6 */ +struct ip_globals ip_data; + +/* If both IP versions are enabled, this function can dispatch packets to the correct one. + * If only IPv4 is enabled, this directly maps at ip4_input. + * May be used as netif input function. + */ +err_t +ip_input(struct pbuf *p, struct netif *inp) +{ +#if LWIP_IPV4 && LWIP_IPV6 + if (p != NULL) { + if (IP_HDR_GET_VERSION(p->payload) == 6) { + return ip6_input(p, inp); + } + return ip4_input(p, inp); + } + return ERR_VAL; +#else /* LWIP_IPV4 && LWIP_IPV6 */ + +#if LWIP_IPV4 + return ip4_input(p, inp); +#else /* LWIP_IPV4 */ + return ip6_input(p, inp); +#endif /* LWIP_IPV4 */ + +#endif /* LWIP_IPV4 && LWIP_IPV6 */ +} + +#endif /* LWIP_IPV4 || LWIP_IPV6 */ diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c index 87760c3d..922ea924 100644 --- a/src/core/ipv4/ip4.c +++ b/src/core/ipv4/ip4.c @@ -99,9 +99,6 @@ #define IP_ACCEPT_LINK_LAYER_ADDRESSING 0 #endif /* LWIP_DHCP */ -/** Global data for both IPv4 and IPv6 */ -struct ip_globals ip_data; - /** The IP header ID of the next outgoing IP packet */ static u16_t ip_id; @@ -352,26 +349,6 @@ return_noroute: } #endif /* IP_FORWARD */ -/* If both IP versions are enabled, this function can dispatch packets to the correct one. - * If only IPv4 is enabled, this directly maps at ip4_input. - * May be used as netif input function. - */ -err_t -ip_input(struct pbuf *p, struct netif *inp) -{ -#if LWIP_IPV6 - if (p != NULL) { - if (IP_HDR_GET_VERSION(p->payload) == 6) { - return ip6_input(p, inp); - } - return ip4_input(p, inp); - } - return ERR_VAL; -#else /* LWIP_IPV6 */ - return ip4_input(p, inp); -#endif /* LWIP_IPV6 */ -} - /** * This function is called by the network interface device driver when * an IP packet is received. The function does the basic checks of the diff --git a/src/core/ipv6/ip6.c b/src/core/ipv6/ip6.c index aa2f5969..d9eca758 100644 --- a/src/core/ipv6/ip6.c +++ b/src/core/ipv6/ip6.c @@ -59,11 +59,6 @@ #include "lwip/debug.h" #include "lwip/stats.h" -#if !LWIP_IPV4 -/** Global data for IPv6 only */ -struct ip_globals ip_data; -#endif /* !LWIP_IPV4 */ - /** * Finds the appropriate network interface for a given IPv6 address. It tries to select * a netif following a sequence of heuristics: @@ -374,18 +369,6 @@ ip6_forward(struct pbuf *p, struct ip6_hdr *iphdr, struct netif *inp) } #endif /* LWIP_IPV6_FORWARD */ -#if !LWIP_IPV4 -/* If both IP versions are enabled, this function can dispatch packets to the correct one. - * If only IPv6 is enabled, this directly maps at ip6_input. - * May be used as netif input function. - */ -err_t -ip_input(struct pbuf *p, struct netif *inp) -{ - return ip6_input(p, inp); -} -#endif /* !LWIP_IPV4 */ - /** * This function is called by the network interface device driver when * an IPv6 packet is received. The function does the basic checks of the diff --git a/src/core/ipv6/ip6_addr.c b/src/core/ipv6/ip6_addr.c index a6aba11e..44d57607 100644 --- a/src/core/ipv6/ip6_addr.c +++ b/src/core/ipv6/ip6_addr.c @@ -289,39 +289,4 @@ ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen) return buf; } -#if LWIP_IPV4 -/** Convert IP address string (both versions) to numeric. - * The version is auto-detected from the string. - * - * @param cp IP address string to convert - * @param addr conversion result is stored here - * @return 1 on success, 0 on error - */ -int -ipaddr_aton(const char *cp, ip_addr_t *addr) -{ - if (cp != NULL) { - const char* c; - for (c = cp; *c != 0; c++) { - if (*c == ':') { - /* contains a colon: IPv6 address */ - if (addr) { - IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6); - } - return ip6addr_aton(cp, ip_2_ip6(addr)); - } else if (*c == '.') { - /* contains a dot: IPv4 address */ - break; - } - } - /* call ip4addr_aton as fallback or if IPv4 was found */ - if (addr) { - IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4); - } - return ip4addr_aton(cp, ip_2_ip4(addr)); - } - return 0; -} -#endif /* LWIP_IPV4 */ - #endif /* LWIP_IPV6 */