1 /* $OpenBSD: inet_ntop.c,v 1.11 2015/01/16 16:48:51 deraadt Exp $ */ 2 3 /* Copyright (c) 1996 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 INTERNET SOFTWARE CONSORTIUM DISCLAIMS 10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 16 * SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <netinet/in.h> 22 #include <arpa/inet.h> 23 #include <arpa/nameser.h> 24 #include <string.h> 25 #include <errno.h> 26 #include <stdio.h> 27 28 /* 29 * WARNING: Don't even consider trying to compile this on a system where 30 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. 31 */ 32 33 static const char *inet_ntop4(const u_char *src, char *dst, size_t size); 34 static const char *inet_ntop6(const u_char *src, char *dst, size_t size); 35 36 /* const char * 37 * inet_ntop(af, src, dst, size) 38 * convert a network format address to presentation format. 39 * return: 40 * pointer to presentation format address (`dst'), or NULL (see errno). 41 * author: 42 * Paul Vixie, 1996. 43 */ 44 const char * 45 inet_ntop(int af, const void *src, char *dst, socklen_t size) 46 { 47 switch (af) { 48 case AF_INET: 49 return (inet_ntop4(src, dst, (size_t)size)); 50 case AF_INET6: 51 return (inet_ntop6(src, dst, (size_t)size)); 52 default: 53 errno = EAFNOSUPPORT; 54 return (NULL); 55 } 56 /* NOTREACHED */ 57 } 58 59 /* const char * 60 * inet_ntop4(src, dst, size) 61 * format an IPv4 address, more or less like inet_ntoa() 62 * return: 63 * `dst' (as a const) 64 * notes: 65 * (1) uses no statics 66 * (2) takes a u_char* not an in_addr as input 67 * author: 68 * Paul Vixie, 1996. 69 */ 70 static const char * 71 inet_ntop4(const u_char *src, char *dst, size_t size) 72 { 73 char tmp[sizeof "255.255.255.255"]; 74 int l; 75 76 l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u", 77 src[0], src[1], src[2], src[3]); 78 if (l <= 0 || l >= size) { 79 errno = ENOSPC; 80 return (NULL); 81 } 82 strlcpy(dst, tmp, size); 83 return (dst); 84 } 85 86 /* const char * 87 * inet_ntop6(src, dst, size) 88 * convert IPv6 binary address into presentation (printable) format 89 * author: 90 * Paul Vixie, 1996. 91 */ 92 static const char * 93 inet_ntop6(const u_char *src, char *dst, size_t size) 94 { 95 /* 96 * Note that int32_t and int16_t need only be "at least" large enough 97 * to contain a value of the specified size. On some systems, like 98 * Crays, there is no such thing as an integer variable with 16 bits. 99 * Keep this in mind if you think this function should have been coded 100 * to use pointer overlays. All the world's not a VAX. 101 */ 102 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; 103 char *tp, *ep; 104 struct { int base, len; } best, cur; 105 u_int words[IN6ADDRSZ / INT16SZ]; 106 int i; 107 int advance; 108 109 /* 110 * Preprocess: 111 * Copy the input (bytewise) array into a wordwise array. 112 * Find the longest run of 0x00's in src[] for :: shorthanding. 113 */ 114 memset(words, '\0', sizeof words); 115 for (i = 0; i < IN6ADDRSZ; i++) 116 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 117 best.base = -1; 118 cur.base = -1; 119 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { 120 if (words[i] == 0) { 121 if (cur.base == -1) 122 cur.base = i, cur.len = 1; 123 else 124 cur.len++; 125 } else { 126 if (cur.base != -1) { 127 if (best.base == -1 || cur.len > best.len) 128 best = cur; 129 cur.base = -1; 130 } 131 } 132 } 133 if (cur.base != -1) { 134 if (best.base == -1 || cur.len > best.len) 135 best = cur; 136 } 137 if (best.base != -1 && best.len < 2) 138 best.base = -1; 139 140 /* 141 * Format the result. 142 */ 143 tp = tmp; 144 ep = tmp + sizeof(tmp); 145 for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { 146 /* Are we inside the best run of 0x00's? */ 147 if (best.base != -1 && i >= best.base && 148 i < (best.base + best.len)) { 149 if (i == best.base) { 150 if (tp + 1 >= ep) { 151 errno = ENOSPC; 152 return (NULL); 153 } 154 *tp++ = ':'; 155 } 156 continue; 157 } 158 /* Are we following an initial run of 0x00s or any real hex? */ 159 if (i != 0) { 160 if (tp + 1 >= ep) { 161 errno = ENOSPC; 162 return (NULL); 163 } 164 *tp++ = ':'; 165 } 166 /* Is this address an encapsulated IPv4? */ 167 if (i == 6 && best.base == 0 && 168 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { 169 if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) 170 return (NULL); 171 tp += strlen(tp); 172 break; 173 } 174 advance = snprintf(tp, ep - tp, "%x", words[i]); 175 if (advance <= 0 || advance >= ep - tp) { 176 errno = ENOSPC; 177 return (NULL); 178 } 179 tp += advance; 180 } 181 /* Was it a trailing run of 0x00's? */ 182 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { 183 if (tp + 1 >= ep) { 184 errno = ENOSPC; 185 return (NULL); 186 } 187 *tp++ = ':'; 188 } 189 if (tp + 1 >= ep) { 190 errno = ENOSPC; 191 return (NULL); 192 } 193 *tp++ = '\0'; 194 195 /* 196 * Check for overflow, copy, and we're done. 197 */ 198 if ((size_t)(tp - tmp) > size) { 199 errno = ENOSPC; 200 return (NULL); 201 } 202 strlcpy(dst, tmp, size); 203 return (dst); 204 } 205