3 * Common IPv4 and IPv6 code
6 * @ingroup callbackstyle_api
14 * @defgroup ipaddr IP address handling
15 * @ingroup infrastructure
17 * @defgroup ip4addr IPv4 only
20 * @defgroup ip6addr IPv6 only
25 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without modification,
29 * are permitted provided that the following conditions are met:
31 * 1. Redistributions of source code must retain the above copyright notice,
32 * this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright notice,
34 * this list of conditions and the following disclaimer in the documentation
35 * and/or other materials provided with the distribution.
36 * 3. The name of the author may not be used to endorse or promote products
37 * derived from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
40 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
42 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
43 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
44 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
47 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50 * This file is part of the lwIP TCP/IP stack.
52 * Author: Adam Dunkels <adam@sics.se>
58 #if LWIP_IPV4 || LWIP_IPV6
60 #include "lwip/ip_addr.h"
63 /** Global data for both IPv4 and IPv6 */
64 struct ip_globals ip_data;
66 #if LWIP_IP_DEBUG_TARGET
68 #define DEBUG_TARGET_MODE_ALLOW_ALL 0
69 #define DEBUG_TARGET_MODE_ALLOW_NONE 1
70 #define DEBUG_TARGET_MODE_FILTER 2
72 int debug_target_mode = DEBUG_TARGET_MODE_ALLOW_ALL;
73 int debug_target_is_ipv6;
74 ipX_addr_t debug_target_ip;
76 int lwip_set_debug_target(const char *addr)
79 debug_target_mode = DEBUG_TARGET_MODE_ALLOW_ALL;
81 else if (*addr == 0) {
82 debug_target_mode = DEBUG_TARGET_MODE_ALLOW_NONE;
85 if (ipaddr_aton(addr, ipX_2_ip(&debug_target_ip))) {
86 debug_target_is_ipv6 = 0;
87 debug_target_mode = DEBUG_TARGET_MODE_FILTER;
90 else if (ip6addr_aton(addr, ipX_2_ip6(&debug_target_ip))) {
91 debug_target_is_ipv6 = 1;
92 debug_target_mode = DEBUG_TARGET_MODE_FILTER;
102 int debug_target_match(int is_ipv6, ipX_addr_t *src, ipX_addr_t *dest)
104 if (debug_target_mode == DEBUG_TARGET_MODE_ALLOW_ALL) {
107 if (debug_target_mode == DEBUG_TARGET_MODE_ALLOW_NONE) {
110 if (is_ipv6 != debug_target_is_ipv6) {
113 if (ipX_addr_isany(is_ipv6, &debug_target_ip)) {
116 if (ipX_addr_cmp(is_ipv6, &debug_target_ip, src)) {
119 if (ipX_addr_cmp(is_ipv6, &debug_target_ip, dest)) {
127 #if LWIP_IPV4 && LWIP_IPV6
129 const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;
133 * Convert IP address string (both versions) to numeric.
134 * The version is auto-detected from the string.
136 * @param cp IP address string to convert
137 * @param addr conversion result is stored here
138 * @return 1 on success, 0 on error
141 ipaddr_aton(const char *cp, ip_addr_t *addr)
145 for (c = cp; *c != 0; c++) {
147 /* contains a colon: IPv6 address */
149 IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
151 return ip6addr_aton(cp, ip_2_ip6(addr));
152 } else if (*c == '.') {
153 /* contains a dot: IPv4 address */
157 /* call ip4addr_aton as fallback or if IPv4 was found */
159 IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
161 return ip4addr_aton(cp, ip_2_ip4(addr));
167 * @ingroup lwip_nosys
168 * If both IP versions are enabled, this function can dispatch packets to the correct one.
169 * Don't call directly, pass to netif_add() and call netif->input().
172 ip_input(struct pbuf *p, struct netif *inp)
175 if (IP_HDR_GET_VERSION(p->payload) == 6) {
176 return ip6_input(p, inp);
178 return ip4_input(p, inp);
183 #endif /* LWIP_IPV4 && LWIP_IPV6 */
185 #endif /* LWIP_IPV4 || LWIP_IPV6 */