1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1996-1999 by Internet Software Consortium. 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <stdio.h> 19 #include <string.h> 20 21 #if defined(_MSC_VER) && _MSC_VER < 1600 22 # include "uv/stdint-msvc2008.h" 23 #else 24 # include <stdint.h> 25 #endif 26 27 #include "uv.h" 28 #include "uv-common.h" 29 30 #define UV__INET_ADDRSTRLEN 16 31 #define UV__INET6_ADDRSTRLEN 46 32 33 34 static int inet_ntop4(const unsigned char *src, char *dst, size_t size); 35 static int inet_ntop6(const unsigned char *src, char *dst, size_t size); 36 static int inet_pton4(const char *src, unsigned char *dst); 37 static int inet_pton6(const char *src, unsigned char *dst); 38 39 uv_inet_ntop(int af,const void * src,char * dst,size_t size)40 int uv_inet_ntop(int af, const void* src, char* dst, size_t size) { 41 switch (af) { 42 case AF_INET: 43 return (inet_ntop4(src, dst, size)); 44 case AF_INET6: 45 return (inet_ntop6(src, dst, size)); 46 default: 47 return UV_EAFNOSUPPORT; 48 } 49 /* NOTREACHED */ 50 } 51 52 inet_ntop4(const unsigned char * src,char * dst,size_t size)53 static int inet_ntop4(const unsigned char *src, char *dst, size_t size) { 54 static const char fmt[] = "%u.%u.%u.%u"; 55 char tmp[UV__INET_ADDRSTRLEN]; 56 int l; 57 58 l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); 59 if (l <= 0 || (size_t) l >= size) { 60 return UV_ENOSPC; 61 } 62 uv__strscpy(dst, tmp, size); 63 return 0; 64 } 65 66 inet_ntop6(const unsigned char * src,char * dst,size_t size)67 static int inet_ntop6(const unsigned char *src, char *dst, size_t size) { 68 /* 69 * Note that int32_t and int16_t need only be "at least" large enough 70 * to contain a value of the specified size. On some systems, like 71 * Crays, there is no such thing as an integer variable with 16 bits. 72 * Keep this in mind if you think this function should have been coded 73 * to use pointer overlays. All the world's not a VAX. 74 */ 75 char tmp[UV__INET6_ADDRSTRLEN], *tp; 76 struct { int base, len; } best, cur; 77 unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)]; 78 int i; 79 80 /* 81 * Preprocess: 82 * Copy the input (bytewise) array into a wordwise array. 83 * Find the longest run of 0x00's in src[] for :: shorthanding. 84 */ 85 memset(words, '\0', sizeof words); 86 for (i = 0; i < (int) sizeof(struct in6_addr); i++) 87 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 88 best.base = -1; 89 best.len = 0; 90 cur.base = -1; 91 cur.len = 0; 92 for (i = 0; i < (int) ARRAY_SIZE(words); i++) { 93 if (words[i] == 0) { 94 if (cur.base == -1) 95 cur.base = i, cur.len = 1; 96 else 97 cur.len++; 98 } else { 99 if (cur.base != -1) { 100 if (best.base == -1 || cur.len > best.len) 101 best = cur; 102 cur.base = -1; 103 } 104 } 105 } 106 if (cur.base != -1) { 107 if (best.base == -1 || cur.len > best.len) 108 best = cur; 109 } 110 if (best.base != -1 && best.len < 2) 111 best.base = -1; 112 113 /* 114 * Format the result. 115 */ 116 tp = tmp; 117 for (i = 0; i < (int) ARRAY_SIZE(words); i++) { 118 /* Are we inside the best run of 0x00's? */ 119 if (best.base != -1 && i >= best.base && 120 i < (best.base + best.len)) { 121 if (i == best.base) 122 *tp++ = ':'; 123 continue; 124 } 125 /* Are we following an initial run of 0x00s or any real hex? */ 126 if (i != 0) 127 *tp++ = ':'; 128 /* Is this address an encapsulated IPv4? */ 129 if (i == 6 && best.base == 0 && (best.len == 6 || 130 (best.len == 7 && words[7] != 0x0001) || 131 (best.len == 5 && words[5] == 0xffff))) { 132 int err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)); 133 if (err) 134 return err; 135 tp += strlen(tp); 136 break; 137 } 138 tp += sprintf(tp, "%x", words[i]); 139 } 140 /* Was it a trailing run of 0x00's? */ 141 if (best.base != -1 && (best.base + best.len) == ARRAY_SIZE(words)) 142 *tp++ = ':'; 143 *tp++ = '\0'; 144 if ((size_t) (tp - tmp) > size) 145 return UV_ENOSPC; 146 uv__strscpy(dst, tmp, size); 147 return 0; 148 } 149 150 uv_inet_pton(int af,const char * src,void * dst)151 int uv_inet_pton(int af, const char* src, void* dst) { 152 if (src == NULL || dst == NULL) 153 return UV_EINVAL; 154 155 switch (af) { 156 case AF_INET: 157 return (inet_pton4(src, dst)); 158 case AF_INET6: { 159 int len; 160 char tmp[UV__INET6_ADDRSTRLEN], *s, *p; 161 s = (char*) src; 162 p = strchr(src, '%'); 163 if (p != NULL) { 164 s = tmp; 165 len = p - src; 166 if (len > UV__INET6_ADDRSTRLEN-1) 167 return UV_EINVAL; 168 memcpy(s, src, len); 169 s[len] = '\0'; 170 } 171 return inet_pton6(s, dst); 172 } 173 default: 174 return UV_EAFNOSUPPORT; 175 } 176 /* NOTREACHED */ 177 } 178 179 inet_pton4(const char * src,unsigned char * dst)180 static int inet_pton4(const char *src, unsigned char *dst) { 181 static const char digits[] = "0123456789"; 182 int saw_digit, octets, ch; 183 unsigned char tmp[sizeof(struct in_addr)], *tp; 184 185 saw_digit = 0; 186 octets = 0; 187 *(tp = tmp) = 0; 188 while ((ch = *src++) != '\0') { 189 const char *pch; 190 191 if ((pch = strchr(digits, ch)) != NULL) { 192 unsigned int nw = *tp * 10 + (pch - digits); 193 194 if (saw_digit && *tp == 0) 195 return UV_EINVAL; 196 if (nw > 255) 197 return UV_EINVAL; 198 *tp = nw; 199 if (!saw_digit) { 200 if (++octets > 4) 201 return UV_EINVAL; 202 saw_digit = 1; 203 } 204 } else if (ch == '.' && saw_digit) { 205 if (octets == 4) 206 return UV_EINVAL; 207 *++tp = 0; 208 saw_digit = 0; 209 } else 210 return UV_EINVAL; 211 } 212 if (octets < 4) 213 return UV_EINVAL; 214 memcpy(dst, tmp, sizeof(struct in_addr)); 215 return 0; 216 } 217 218 inet_pton6(const char * src,unsigned char * dst)219 static int inet_pton6(const char *src, unsigned char *dst) { 220 static const char xdigits_l[] = "0123456789abcdef", 221 xdigits_u[] = "0123456789ABCDEF"; 222 unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp; 223 const char *xdigits, *curtok; 224 int ch, seen_xdigits; 225 unsigned int val; 226 227 memset((tp = tmp), '\0', sizeof tmp); 228 endp = tp + sizeof tmp; 229 colonp = NULL; 230 /* Leading :: requires some special handling. */ 231 if (*src == ':') 232 if (*++src != ':') 233 return UV_EINVAL; 234 curtok = src; 235 seen_xdigits = 0; 236 val = 0; 237 while ((ch = *src++) != '\0') { 238 const char *pch; 239 240 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) 241 pch = strchr((xdigits = xdigits_u), ch); 242 if (pch != NULL) { 243 val <<= 4; 244 val |= (pch - xdigits); 245 if (++seen_xdigits > 4) 246 return UV_EINVAL; 247 continue; 248 } 249 if (ch == ':') { 250 curtok = src; 251 if (!seen_xdigits) { 252 if (colonp) 253 return UV_EINVAL; 254 colonp = tp; 255 continue; 256 } else if (*src == '\0') { 257 return UV_EINVAL; 258 } 259 if (tp + sizeof(uint16_t) > endp) 260 return UV_EINVAL; 261 *tp++ = (unsigned char) (val >> 8) & 0xff; 262 *tp++ = (unsigned char) val & 0xff; 263 seen_xdigits = 0; 264 val = 0; 265 continue; 266 } 267 if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) { 268 int err = inet_pton4(curtok, tp); 269 if (err == 0) { 270 tp += sizeof(struct in_addr); 271 seen_xdigits = 0; 272 break; /*%< '\\0' was seen by inet_pton4(). */ 273 } 274 } 275 return UV_EINVAL; 276 } 277 if (seen_xdigits) { 278 if (tp + sizeof(uint16_t) > endp) 279 return UV_EINVAL; 280 *tp++ = (unsigned char) (val >> 8) & 0xff; 281 *tp++ = (unsigned char) val & 0xff; 282 } 283 if (colonp != NULL) { 284 /* 285 * Since some memmove()'s erroneously fail to handle 286 * overlapping regions, we'll do the shift by hand. 287 */ 288 const int n = tp - colonp; 289 int i; 290 291 if (tp == endp) 292 return UV_EINVAL; 293 for (i = 1; i <= n; i++) { 294 endp[- i] = colonp[n - i]; 295 colonp[n - i] = 0; 296 } 297 tp = endp; 298 } 299 if (tp != endp) 300 return UV_EINVAL; 301 memcpy(dst, tmp, sizeof tmp); 302 return 0; 303 } 304