The Pedigree Project 0.1
ip6_addr.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
26/*
27 * Copyright (c) 2010 Inico Technologies Ltd.
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without modification,
31 * are permitted provided that the following conditions are met:
32 *
33 * 1. Redistributions of source code must retain the above copyright notice,
34 * this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following disclaimer in the documentation
37 * and/or other materials provided with the distribution.
38 * 3. The name of the author may not be used to endorse or promote products
39 * derived from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
43 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
44 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
46 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
49 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
50 * OF SUCH DAMAGE.
51 *
52 * This file is part of the lwIP TCP/IP stack.
53 *
54 * Author: Ivan Delamer <delamer@inicotech.com>
55 *
56 * Functions for handling IPv6 addresses.
57 *
58 * Please coordinate changes and requests with Ivan Delamer
59 * <delamer@inicotech.com>
60 */
61
62#include "lwip/opt.h"
63
64#if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
65
66#include "lwip/ip_addr.h"
67#include "lwip/def.h"
68
69/* used by IP6_ADDR_ANY(6) in ip6_addr.h */
70const ip_addr_t ip6_addr_any = IPADDR6_INIT(0ul, 0ul, 0ul, 0ul);
71
72#ifndef isprint
73#define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up)
74#define isprint(c) in_range(c, 0x20, 0x7f)
75#define isdigit(c) in_range(c, '0', '9')
76#define isxdigit(c) (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
77#define islower(c) in_range(c, 'a', 'z')
78#define isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
79#define xchar(i) ((i) < 10 ? '0' + (i) : 'A' + (i) - 10)
80#endif
81
91int
92ip6addr_aton(const char *cp, ip6_addr_t *addr)
93{
94 u32_t addr_index, zero_blocks, current_block_index, current_block_value;
95 const char *s;
96
97 /* Count the number of colons, to count the number of blocks in a "::" sequence
98 zero_blocks may be 1 even if there are no :: sequences */
99 zero_blocks = 8;
100 for (s = cp; *s != 0; s++) {
101 if (*s == ':') {
102 zero_blocks--;
103 } else if (!isxdigit(*s)) {
104 break;
105 }
106 }
107
108 /* parse each block */
109 addr_index = 0;
110 current_block_index = 0;
111 current_block_value = 0;
112 for (s = cp; *s != 0; s++) {
113 if (*s == ':') {
114 if (addr) {
115 if (current_block_index & 0x1) {
116 addr->addr[addr_index++] |= current_block_value;
117 }
118 else {
119 addr->addr[addr_index] = current_block_value << 16;
120 }
121 }
122 current_block_index++;
123 current_block_value = 0;
124 if (current_block_index > 7) {
125 /* address too long! */
126 return 0;
127 }
128 if (s[1] == ':') {
129 if (s[2] == ':') {
130 /* invalid format: three successive colons */
131 return 0;
132 }
133 s++;
134 /* "::" found, set zeros */
135 while (zero_blocks > 0) {
136 zero_blocks--;
137 if (current_block_index & 0x1) {
138 addr_index++;
139 } else {
140 if (addr) {
141 addr->addr[addr_index] = 0;
142 }
143 }
144 current_block_index++;
145 if (current_block_index > 7) {
146 /* address too long! */
147 return 0;
148 }
149 }
150 }
151 } else if (isxdigit(*s)) {
152 /* add current digit */
153 current_block_value = (current_block_value << 4) +
154 (isdigit(*s) ? (u32_t)(*s - '0') :
155 (u32_t)(10 + (islower(*s) ? *s - 'a' : *s - 'A')));
156 } else {
157 /* unexpected digit, space? CRLF? */
158 break;
159 }
160 }
161
162 if (addr) {
163 if (current_block_index & 0x1) {
164 addr->addr[addr_index++] |= current_block_value;
165 }
166 else {
167 addr->addr[addr_index] = current_block_value << 16;
168 }
169 }
170
171 /* convert to network byte order. */
172 if (addr) {
173 for (addr_index = 0; addr_index < 4; addr_index++) {
174 addr->addr[addr_index] = lwip_htonl(addr->addr[addr_index]);
175 }
176 }
177
178 if (current_block_index != 7) {
179 return 0;
180 }
181
182 return 1;
183}
184
193char *
194ip6addr_ntoa(const ip6_addr_t *addr)
195{
196 static char str[40];
197 return ip6addr_ntoa_r(addr, str, 40);
198}
199
209char *
210ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen)
211{
212 u32_t current_block_index, current_block_value, next_block_value;
213 s32_t i;
214 u8_t zero_flag, empty_block_flag;
215
216 i = 0;
217 empty_block_flag = 0; /* used to indicate a zero chain for "::' */
218
219 for (current_block_index = 0; current_block_index < 8; current_block_index++) {
220 /* get the current 16-bit block */
221 current_block_value = lwip_htonl(addr->addr[current_block_index >> 1]);
222 if ((current_block_index & 0x1) == 0) {
223 current_block_value = current_block_value >> 16;
224 }
225 current_block_value &= 0xffff;
226
227 /* Check for empty block. */
228 if (current_block_value == 0) {
229 if (current_block_index == 7 && empty_block_flag == 1) {
230 /* special case, we must render a ':' for the last block. */
231 buf[i++] = ':';
232 if (i >= buflen) {
233 return NULL;
234 }
235 break;
236 }
237 if (empty_block_flag == 0) {
238 /* generate empty block "::", but only if more than one contiguous zero block,
239 * according to current formatting suggestions RFC 5952. */
240 next_block_value = lwip_htonl(addr->addr[(current_block_index + 1) >> 1]);
241 if ((current_block_index & 0x1) == 0x01) {
242 next_block_value = next_block_value >> 16;
243 }
244 next_block_value &= 0xffff;
245 if (next_block_value == 0) {
246 empty_block_flag = 1;
247 buf[i++] = ':';
248 if (i >= buflen) {
249 return NULL;
250 }
251 continue; /* move on to next block. */
252 }
253 } else if (empty_block_flag == 1) {
254 /* move on to next block. */
255 continue;
256 }
257 } else if (empty_block_flag == 1) {
258 /* Set this flag value so we don't produce multiple empty blocks. */
259 empty_block_flag = 2;
260 }
261
262 if (current_block_index > 0) {
263 buf[i++] = ':';
264 if (i >= buflen) {
265 return NULL;
266 }
267 }
268
269 if ((current_block_value & 0xf000) == 0) {
270 zero_flag = 1;
271 } else {
272 buf[i++] = xchar(((current_block_value & 0xf000) >> 12));
273 zero_flag = 0;
274 if (i >= buflen) {
275 return NULL;
276 }
277 }
278
279 if (((current_block_value & 0xf00) == 0) && (zero_flag)) {
280 /* do nothing */
281 } else {
282 buf[i++] = xchar(((current_block_value & 0xf00) >> 8));
283 zero_flag = 0;
284 if (i >= buflen) {
285 return NULL;
286 }
287 }
288
289 if (((current_block_value & 0xf0) == 0) && (zero_flag)) {
290 /* do nothing */
291 }
292 else {
293 buf[i++] = xchar(((current_block_value & 0xf0) >> 4));
294 zero_flag = 0;
295 if (i >= buflen) {
296 return NULL;
297 }
298 }
299
300 buf[i++] = xchar((current_block_value & 0xf));
301 if (i >= buflen) {
302 return NULL;
303 }
304 }
305
306 buf[i] = 0;
307
308 return buf;
309}
310
311#endif /* LWIP_IPV6 */