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_IPV4 && LWIP_IPV6
67
68 const ip_addr_t ip_addr_any_type = IPADDR_ANY_TYPE_INIT;
69
70 /**
71 * @ingroup ipaddr
72 * Convert numeric IP address (both versions) into ASCII representation.
73 * returns ptr to static buffer; not reentrant!
74 *
75 * @param addr ip address in network order to convert
76 * @return pointer to a global static (!) buffer that holds the ASCII
77 * representation of addr
78 */
ipaddr_ntoa(const ip_addr_t * addr)79 char *ipaddr_ntoa(const ip_addr_t *addr)
80 {
81 if (addr == NULL) {
82 return NULL;
83 }
84 if (IP_IS_V6(addr)) {
85 return ip6addr_ntoa(ip_2_ip6(addr));
86 } else {
87 return ip4addr_ntoa(ip_2_ip4(addr));
88 }
89 }
90
91 /**
92 * @ingroup ipaddr
93 * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
94 *
95 * @param addr ip address in network order to convert
96 * @param buf target buffer where the string is stored
97 * @param buflen length of buf
98 * @return either pointer to buf which now holds the ASCII
99 * representation of addr or NULL if buf was too small
100 */
ipaddr_ntoa_r(const ip_addr_t * addr,char * buf,int buflen)101 char *ipaddr_ntoa_r(const ip_addr_t *addr, char *buf, int buflen)
102 {
103 if (addr == NULL) {
104 return NULL;
105 }
106 if (IP_IS_V6(addr)) {
107 return ip6addr_ntoa_r(ip_2_ip6(addr), buf, buflen);
108 } else {
109 return ip4addr_ntoa_r(ip_2_ip4(addr), buf, buflen);
110 }
111 }
112
113 /**
114 * @ingroup ipaddr
115 * Convert IP address string (both versions) to numeric.
116 * The version is auto-detected from the string.
117 *
118 * @param cp IP address string to convert
119 * @param addr conversion result is stored here
120 * @return 1 on success, 0 on error
121 */
122 int
ipaddr_aton(const char * cp,ip_addr_t * addr)123 ipaddr_aton(const char *cp, ip_addr_t *addr)
124 {
125 if (cp != NULL && addr != NULL) {
126 const char *c;
127 for (c = cp; *c != 0; c++) {
128 if (*c == ':') {
129 /* contains a colon: IPv6 address */
130 IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V6);
131 return ip6addr_aton(cp, ip_2_ip6(addr));
132 } else if (*c == '.') {
133 /* contains a dot: IPv4 address */
134 break;
135 }
136 }
137 /* call ip4addr_aton as fallback or if IPv4 was found */
138 IP_SET_TYPE_VAL(*addr, IPADDR_TYPE_V4);
139 return ip4addr_aton(cp, ip_2_ip4(addr));
140 }
141 return 0;
142 }
143
144 /**
145 * @ingroup lwip_nosys
146 * If both IP versions are enabled, this function can dispatch packets to the correct one.
147 * Don't call directly, pass to netif_add() and call netif->input().
148 */
149 err_t
ip_input(struct pbuf * p,struct netif * inp)150 ip_input(struct pbuf *p, struct netif *inp)
151 {
152 if (p != NULL) {
153 if (IP_HDR_GET_VERSION(p->payload) == 6) {
154 return ip6_input(p, inp);
155 }
156 return ip4_input(p, inp);
157 }
158 return ERR_VAL;
159 }
160
161 #endif /* LWIP_IPV4 && LWIP_IPV6 */
162
163 struct netif*
ip_route_pcb(const ip_addr_t * dest,const struct ip_pcb * pcb)164 ip_route_pcb(const ip_addr_t *dest, const struct ip_pcb *pcb)
165 {
166 struct netif *netif = NULL;
167 #if LWIP_SO_DONTROUTE
168 rt_scope_t scope = RT_SCOPE_UNIVERSAL;
169
170 LWIP_ASSERT("Expecting ipaddr to be not NULL ", dest != NULL);
171 if (pcb != NULL) {
172 scope = ip_get_option(pcb, SOF_DONTROUTE) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSAL;
173 }
174 #endif
175
176 if ((pcb != NULL) && (pcb->netif_idx != NETIF_NO_INDEX)) {
177 return netif_find_by_ifindex(pcb->netif_idx);
178 }
179
180 if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
181 /* Don't call ip_route() with IP_ANY_TYPE */
182 netif = ip_route(IP46_ADDR_ANY(IP_GET_TYPE(dest)), dest);
183 } else {
184 netif = ip_route(&pcb->local_ip, dest);
185 }
186
187 if (netif == NULL) {
188 return NULL;
189 }
190
191 #if LWIP_SO_DONTROUTE
192 if (netif->scope < scope) {
193 return NULL;
194 }
195 #endif
196 return netif;
197 }
198
199 #endif /* LWIP_IPV4 || LWIP_IPV6 */
200