1 /* 2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (c) 1998,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 * $Id: inet_cidr_ntop.c,v 1.7 2006/10/11 02:18:18 marka Exp $ 18 */ 19 20 #include "port_before.h" 21 22 #include <sys/types.h> 23 #include <sys/socket.h> 24 #include <netinet/in.h> 25 #include <arpa/nameser.h> 26 #include <arpa/inet.h> 27 28 #include <errno.h> 29 #include <stdio.h> 30 #include <string.h> 31 #include <stdlib.h> 32 33 #include "port_after.h" 34 35 static char * 36 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size); 37 static char * 38 inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size); 39 40 /*% 41 * char * 42 * inet_cidr_ntop(af, src, bits, dst, size) 43 * convert network address from network to presentation format. 44 * "src"'s size is determined from its "af". 45 * return: 46 * pointer to dst, or NULL if an error occurred (check errno). 47 * note: 48 * 192.5.5.1/28 has a nonzero host part, which means it isn't a network 49 * as called for by inet_net_ntop() but it can be a host address with 50 * an included netmask. 51 * author: 52 * Paul Vixie (ISC), October 1998 53 */ 54 char * 55 inet_cidr_ntop(int af, const void *src, int bits, char *dst, size_t size) { 56 switch (af) { 57 case AF_INET: 58 return (inet_cidr_ntop_ipv4(src, bits, dst, size)); 59 case AF_INET6: 60 return (inet_cidr_ntop_ipv6(src, bits, dst, size)); 61 default: 62 errno = EAFNOSUPPORT; 63 return (NULL); 64 } 65 } 66 67 static int 68 decoct(const u_char *src, int bytes, char *dst, size_t size) { 69 char *odst = dst; 70 char *t; 71 int b; 72 73 for (b = 1; b <= bytes; b++) { 74 if (size < sizeof "255.") 75 return (0); 76 t = dst; 77 dst += sprintf(dst, "%u", *src++); 78 if (b != bytes) { 79 *dst++ = '.'; 80 *dst = '\0'; 81 } 82 size -= (size_t)(dst - t); 83 } 84 return (dst - odst); 85 } 86 87 /*% 88 * static char * 89 * inet_cidr_ntop_ipv4(src, bits, dst, size) 90 * convert IPv4 network address from network to presentation format. 91 * "src"'s size is determined from its "af". 92 * return: 93 * pointer to dst, or NULL if an error occurred (check errno). 94 * note: 95 * network byte order assumed. this means 192.5.5.240/28 has 96 * 0b11110000 in its fourth octet. 97 * author: 98 * Paul Vixie (ISC), October 1998 99 */ 100 static char * 101 inet_cidr_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) { 102 char *odst = dst; 103 size_t len = 4; 104 size_t b; 105 size_t bytes; 106 107 if ((bits < -1) || (bits > 32)) { 108 errno = EINVAL; 109 return (NULL); 110 } 111 112 /* Find number of significant bytes in address. */ 113 if (bits == -1) 114 len = 4; 115 else 116 for (len = 1, b = 1 ; b < 4U; b++) 117 if (*(src + b)) 118 len = b + 1; 119 120 /* Format whole octets plus nonzero trailing octets. */ 121 bytes = (((bits <= 0) ? 1 : bits) + 7) / 8; 122 if (len > bytes) 123 bytes = len; 124 b = decoct(src, bytes, dst, size); 125 if (b == 0U) 126 goto emsgsize; 127 dst += b; 128 size -= b; 129 130 if (bits != -1) { 131 /* Format CIDR /width. */ 132 if (size < sizeof "/32") 133 goto emsgsize; 134 dst += sprintf(dst, "/%u", bits); 135 } 136 137 return (odst); 138 139 emsgsize: 140 errno = EMSGSIZE; 141 return (NULL); 142 } 143 144 static char * 145 inet_cidr_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) { 146 /* 147 * Note that int32_t and int16_t need only be "at least" large enough 148 * to contain a value of the specified size. On some systems, like 149 * Crays, there is no such thing as an integer variable with 16 bits. 150 * Keep this in mind if you think this function should have been coded 151 * to use pointer overlays. All the world's not a VAX. 152 */ 153 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128"]; 154 char *tp; 155 struct { int base, len; } best, cur; 156 u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; 157 int i; 158 159 if ((bits < -1) || (bits > 128)) { 160 errno = EINVAL; 161 return (NULL); 162 } 163 164 /* 165 * Preprocess: 166 * Copy the input (bytewise) array into a wordwise array. 167 * Find the longest run of 0x00's in src[] for :: shorthanding. 168 */ 169 memset(words, '\0', sizeof words); 170 for (i = 0; i < NS_IN6ADDRSZ; i++) 171 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 172 best.base = -1; 173 best.len = 0; 174 cur.base = -1; 175 cur.len = 0; 176 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 177 if (words[i] == 0) { 178 if (cur.base == -1) 179 cur.base = i, cur.len = 1; 180 else 181 cur.len++; 182 } else { 183 if (cur.base != -1) { 184 if (best.base == -1 || cur.len > best.len) 185 best = cur; 186 cur.base = -1; 187 } 188 } 189 } 190 if (cur.base != -1) { 191 if (best.base == -1 || cur.len > best.len) 192 best = cur; 193 } 194 if (best.base != -1 && best.len < 2) 195 best.base = -1; 196 197 /* 198 * Format the result. 199 */ 200 tp = tmp; 201 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 202 /* Are we inside the best run of 0x00's? */ 203 if (best.base != -1 && i >= best.base && 204 i < (best.base + best.len)) { 205 if (i == best.base) 206 *tp++ = ':'; 207 continue; 208 } 209 /* Are we following an initial run of 0x00s or any real hex? */ 210 if (i != 0) 211 *tp++ = ':'; 212 /* Is this address an encapsulated IPv4? */ 213 if (i == 6 && best.base == 0 && (best.len == 6 || 214 (best.len == 7 && words[7] != 0x0001) || 215 (best.len == 5 && words[5] == 0xffff))) { 216 int n; 217 218 if (src[15] || bits == -1 || bits > 120) 219 n = 4; 220 else if (src[14] || bits > 112) 221 n = 3; 222 else 223 n = 2; 224 n = decoct(src+12, n, tp, sizeof tmp - (tp - tmp)); 225 if (n == 0) { 226 errno = EMSGSIZE; 227 return (NULL); 228 } 229 tp += strlen(tp); 230 break; 231 } 232 tp += sprintf(tp, "%x", words[i]); 233 } 234 235 /* Was it a trailing run of 0x00's? */ 236 if (best.base != -1 && (best.base + best.len) == 237 (NS_IN6ADDRSZ / NS_INT16SZ)) 238 *tp++ = ':'; 239 *tp = '\0'; 240 241 if (bits != -1) 242 tp += sprintf(tp, "/%u", bits); 243 244 /* 245 * Check for overflow, copy, and we're done. 246 */ 247 if ((size_t)(tp - tmp) > size) { 248 errno = EMSGSIZE; 249 return (NULL); 250 } 251 strcpy(dst, tmp); 252 return (dst); 253 } 254