Go to the source code of this file.
Functions | |
const char * | inet_ntop4 (const unsigned char *src, char *dst, size_t size) |
const char * | inet_ntop6 (const unsigned char *src, char *dst, size_t size) |
const char * | inet_ntop (int af, const void *src, char *dst, size_t size) |
|
Definition at line 74 of file inet_ntop.c. References dst, and sprintf(). Referenced by inet_ntop(), and inet_ntop6().
|
|
Definition at line 97 of file inet_ntop.c. References dst, inet_ntop4(), and sprintf(). Referenced by inet_ntop().
00098 { 00099 /* 00100 * Note that int32_t and int16_t need only be "at least" large enough 00101 * to contain a value of the specified size. On some systems, like 00102 * Crays, there is no such thing as an integer variable with 16 bits. 00103 * Keep this in mind if you think this function should have been coded 00104 * to use pointer overlays. All the world's not a VAX. 00105 */ 00106 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; 00107 struct { int base, len; } best, cur; 00108 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ]; 00109 int i; 00110 00111 /* 00112 * Preprocess: 00113 * Copy the input (bytewise) array into a wordwise array. 00114 * Find the longest run of 0x00's in src[] for :: shorthanding. 00115 */ 00116 memset(words, '\0', sizeof words); 00117 for (i = 0; i < NS_IN6ADDRSZ; i++) 00118 words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 00119 best.base = -1; 00120 cur.base = -1; 00121 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 00122 if (words[i] == 0) { 00123 if (cur.base == -1) 00124 cur.base = i, cur.len = 1; 00125 else 00126 cur.len++; 00127 } else { 00128 if (cur.base != -1) { 00129 if (best.base == -1 || cur.len > best.len) 00130 best = cur; 00131 cur.base = -1; 00132 } 00133 } 00134 } 00135 if (cur.base != -1) { 00136 if (best.base == -1 || cur.len > best.len) 00137 best = cur; 00138 } 00139 if (best.base != -1 && best.len < 2) 00140 best.base = -1; 00141 00142 /* 00143 * Format the result. 00144 */ 00145 tp = tmp; 00146 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 00147 /* Are we inside the best run of 0x00's? */ 00148 if (best.base != -1 && i >= best.base && 00149 i < (best.base + best.len)) { 00150 if (i == best.base) 00151 *tp++ = ':'; 00152 continue; 00153 } 00154 /* Are we following an initial run of 0x00s or any real hex? */ 00155 if (i != 0) 00156 *tp++ = ':'; 00157 /* Is this address an encapsulated IPv4? */ 00158 if (i == 6 && best.base == 0 && 00159 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { 00160 if (!inet_ntop4(src+12, tp, 00161 sizeof tmp - (tp - tmp))) 00162 return (NULL); 00163 tp += strlen(tp); 00164 break; 00165 } 00166 tp += sprintf(tp, "%x", words[i]); 00167 } 00168 /* Was it a trailing run of 0x00's? */ 00169 if (best.base != -1 && (best.base + best.len) == 00170 (NS_IN6ADDRSZ / NS_INT16SZ)) 00171 *tp++ = ':'; 00172 *tp++ = '\0'; 00173 00174 /* 00175 * Check for overflow, copy, and we're done. 00176 */ 00177 if ((size_t)(tp - tmp) > size) { 00178 errno = ENOSPC; 00179 return (NULL); 00180 } 00181 strcpy(dst, tmp); 00182 return (dst); 00183 } |
|
Definition at line 46 of file inet_ntop.c. References af, dst, inet_ntop4(), and inet_ntop6().
00047 { 00048 switch (af) { 00049 case AF_INET: 00050 return (inet_ntop4(src, dst, size)); 00051 #ifdef AF_INET6 00052 case AF_INET6: 00053 return (inet_ntop6(src, dst, size)); 00054 #endif 00055 default: 00056 errno = EAFNOSUPPORT; 00057 return (NULL); 00058 } 00059 /* NOTREACHED */ 00060 } |