1/* 2 * Copyright (c) 1999 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the Kungliga Tekniska 20 * Högskolan and its contributors. 21 * 22 * 4. Neither the name of the Institute nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39/* $Id: inet_ntop.c,v 1.8 2005/02/09 02:25:46 guy Exp $ */ 40 41#ifndef lint 42static const char rcsid[] _U_ = 43 "@(#) $Header: /tcpdump/master/tcpdump/missing/inet_ntop.c,v 1.8 2005/02/09 02:25:46 guy Exp $"; 44#endif 45 46#include <tcpdump-stdinc.h> 47 48#include <stdio.h> 49#include <errno.h> 50 51/* 52 * 53 */ 54 55#ifndef IN6ADDRSZ 56#define IN6ADDRSZ 16 /* IPv6 T_AAAA */ 57#endif 58 59#ifndef INT16SZ 60#define INT16SZ 2 /* word size */ 61#endif 62 63static const char * 64inet_ntop_v4 (const void *src, char *dst, size_t size) 65{ 66 const char digits[] = "0123456789"; 67 int i; 68 struct in_addr *addr = (struct in_addr *)src; 69 u_long a = ntohl(addr->s_addr); 70 const char *orig_dst = dst; 71 72 if (size < INET_ADDRSTRLEN) { 73 errno = ENOSPC; 74 return NULL; 75 } 76 for (i = 0; i < 4; ++i) { 77 int n = (a >> (24 - i * 8)) & 0xFF; 78 int non_zerop = 0; 79 80 if (non_zerop || n / 100 > 0) { 81 *dst++ = digits[n / 100]; 82 n %= 100; 83 non_zerop = 1; 84 } 85 if (non_zerop || n / 10 > 0) { 86 *dst++ = digits[n / 10]; 87 n %= 10; 88 non_zerop = 1; 89 } 90 *dst++ = digits[n]; 91 if (i != 3) 92 *dst++ = '.'; 93 } 94 *dst++ = '\0'; 95 return orig_dst; 96} 97 98#ifdef INET6 99/* 100 * Convert IPv6 binary address into presentation (printable) format. 101 */ 102static const char * 103inet_ntop_v6 (const u_char *src, char *dst, size_t size) 104{ 105 /* 106 * Note that int32_t and int16_t need only be "at least" large enough 107 * to contain a value of the specified size. On some systems, like 108 * Crays, there is no such thing as an integer variable with 16 bits. 109 * Keep this in mind if you think this function should have been coded 110 * to use pointer overlays. All the world's not a VAX. 111 */ 112 char tmp [INET6_ADDRSTRLEN+1]; 113 char *tp; 114 struct { 115 long base; 116 long len; 117 } best, cur; 118 u_long words [IN6ADDRSZ / INT16SZ]; 119 int i; 120 121 /* Preprocess: 122 * Copy the input (bytewise) array into a wordwise array. 123 * Find the longest run of 0x00's in src[] for :: shorthanding. 124 */ 125 memset (words, 0, sizeof(words)); 126 for (i = 0; i < IN6ADDRSZ; i++) 127 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3)); 128 129 best.base = -1; 130 cur.base = -1; 131 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) 132 { 133 if (words[i] == 0) 134 { 135 if (cur.base == -1) 136 cur.base = i, cur.len = 1; 137 else cur.len++; 138 } 139 else if (cur.base != -1) 140 { 141 if (best.base == -1 || cur.len > best.len) 142 best = cur; 143 cur.base = -1; 144 } 145 } 146 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len)) 147 best = cur; 148 if (best.base != -1 && best.len < 2) 149 best.base = -1; 150 151 /* Format the result. 152 */ 153 tp = tmp; 154 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) 155 { 156 /* Are we inside the best run of 0x00's? 157 */ 158 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) 159 { 160 if (i == best.base) 161 *tp++ = ':'; 162 continue; 163 } 164 165 /* Are we following an initial run of 0x00s or any real hex? 166 */ 167 if (i != 0) 168 *tp++ = ':'; 169 170 /* Is this address an encapsulated IPv4? 171 */ 172 if (i == 6 && best.base == 0 && 173 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) 174 { 175 if (!inet_ntop_v4(src+12, tp, sizeof(tmp) - (tp - tmp))) 176 { 177 errno = ENOSPC; 178 return (NULL); 179 } 180 tp += strlen(tp); 181 break; 182 } 183 tp += sprintf (tp, "%lX", words[i]); 184 } 185 186 /* Was it a trailing run of 0x00's? 187 */ 188 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) 189 *tp++ = ':'; 190 *tp++ = '\0'; 191 192 /* Check for overflow, copy, and we're done. 193 */ 194 if ((size_t)(tp - tmp) > size) 195 { 196 errno = ENOSPC; 197 return (NULL); 198 } 199 return strcpy (dst, tmp); 200 return (NULL); 201} 202#endif /* INET6 */ 203 204 205const char * 206inet_ntop(int af, const void *src, char *dst, size_t size) 207{ 208 switch (af) { 209 case AF_INET : 210 return inet_ntop_v4 (src, dst, size); 211#ifdef INET6 212 case AF_INET6: 213 return inet_ntop_v6 ((const u_char*)src, dst, size); 214#endif 215 default : 216 errno = EAFNOSUPPORT; 217 return NULL; 218 } 219} 220