Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / lwip / repo / lwip / src / core / ip.c
1 /**
2  * @file
3  * Common IPv4 and IPv6 code
4  *
5  * @defgroup ip IP
6  * @ingroup callbackstyle_api
7  * 
8  * @defgroup ip4 IPv4
9  * @ingroup ip
10  *
11  * @defgroup ip6 IPv6
12  * @ingroup ip
13  * 
14  * @defgroup ipaddr IP address handling
15  * @ingroup infrastructure
16  * 
17  * @defgroup ip4addr IPv4 only
18  * @ingroup ipaddr
19  * 
20  * @defgroup ip6addr IPv6 only
21  * @ingroup ipaddr
22  */
23
24 /*
25  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
26  * All rights reserved.
27  *
28  * Redistribution and use in source and binary forms, with or without modification,
29  * are permitted provided that the following conditions are met:
30  *
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.
38  *
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
48  * OF SUCH DAMAGE.
49  *
50  * This file is part of the lwIP TCP/IP stack.
51  *
52  * Author: Adam Dunkels <adam@sics.se>
53  *
54  */
55
56 #include "lwip/opt.h"
57
58 #if LWIP_IPV4 || LWIP_IPV6
59
60 #include "lwip/ip_addr.h"
61 #include "lwip/ip.h"
62
63 /** Global data for both IPv4 and IPv6 */
64 struct ip_globals ip_data;
65
66 #if LWIP_IP_DEBUG_TARGET
67
68 #define DEBUG_TARGET_MODE_ALLOW_ALL 0
69 #define DEBUG_TARGET_MODE_ALLOW_NONE 1
70 #define DEBUG_TARGET_MODE_FILTER 2
71
72 int debug_target_mode = DEBUG_TARGET_MODE_ALLOW_ALL;
73 int debug_target_is_ipv6;
74 ipX_addr_t debug_target_ip;
75
76 int lwip_set_debug_target(const char *addr)
77 {
78   if (addr == 0) {
79     debug_target_mode = DEBUG_TARGET_MODE_ALLOW_ALL;
80   }
81   else if (*addr == 0) {
82     debug_target_mode = DEBUG_TARGET_MODE_ALLOW_NONE;
83   }
84   else {
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;
88     }
89 #if LWIP_IPV6
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;
93     }
94 #endif
95     else {
96       return 0;
97     }
98   }
99   return 1;
100 }
101
102 int debug_target_match(int is_ipv6, ipX_addr_t *src, ipX_addr_t *dest)
103 {
104   if (debug_target_mode == DEBUG_TARGET_MODE_ALLOW_ALL) {
105     return 1;
106   }
107   if (debug_target_mode == DEBUG_TARGET_MODE_ALLOW_NONE) {
108     return 0;
109   }
110   if (is_ipv6 != debug_target_is_ipv6) {
111     return 0;
112   }
113   if (ipX_addr_isany(is_ipv6, &debug_target_ip)) {
114     return 1;
115   }
116   if (ipX_addr_cmp(is_ipv6, &debug_target_ip, src)) {
117     return 1;
118   }
119   if (ipX_addr_cmp(is_ipv6, &debug_target_ip, dest)) {
120     return 1;
121   }
122   return 0;
123 }
124
125 #endif
126
127 #if LWIP_IPV4 && LWIP_IPV6
128
129 const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;
130
131 /**
132  * @ingroup ipaddr
133  * Convert IP address string (both versions) to numeric.
134  * The version is auto-detected from the string.
135  *
136  * @param cp IP address string to convert
137  * @param addr conversion result is stored here
138  * @return 1 on success, 0 on error
139  */
140 int
141 ipaddr_aton(const char *cp, ip_addr_t *addr)
142 {
143   if (cp != NULL) {
144     const char* c;
145     for (c = cp; *c != 0; c++) {
146       if (*c == ':') {
147         /* contains a colon: IPv6 address */
148         if (addr) {
149           IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
150         }
151         return ip6addr_aton(cp, ip_2_ip6(addr));
152       } else if (*c == '.') {
153         /* contains a dot: IPv4 address */
154         break;
155       }
156     }
157     /* call ip4addr_aton as fallback or if IPv4 was found */
158     if (addr) {
159       IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
160     }
161     return ip4addr_aton(cp, ip_2_ip4(addr));
162   }
163   return 0;
164 }
165
166 /**
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().
170  */
171 err_t
172 ip_input(struct pbuf *p, struct netif *inp)
173 {
174   if (p != NULL) {
175     if (IP_HDR_GET_VERSION(p->payload) == 6) {
176       return ip6_input(p, inp);
177     }
178     return ip4_input(p, inp);
179   }
180   return ERR_VAL;
181 }
182
183 #endif /* LWIP_IPV4 && LWIP_IPV6 */
184
185 #endif /* LWIP_IPV4 || LWIP_IPV6 */