1258945Sroberto/* 2280849Scy * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC") 3258945Sroberto * Copyright (C) 1996-2001 Internet Software Consortium. 4258945Sroberto * 5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any 6258945Sroberto * purpose with or without fee is hereby granted, provided that the above 7258945Sroberto * copyright notice and this permission notice appear in all copies. 8258945Sroberto * 9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11258945Sroberto * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15258945Sroberto * PERFORMANCE OF THIS SOFTWARE. 16258945Sroberto */ 17258945Sroberto 18258945Sroberto/*! \file */ 19258945Sroberto 20258945Sroberto#if defined(LIBC_SCCS) && !defined(lint) 21258945Srobertostatic char rcsid[] = 22280849Scy "$Id: inet_ntop.c,v 1.21 2009/07/17 23:47:41 tbox Exp $"; 23258945Sroberto#endif /* LIBC_SCCS and not lint */ 24258945Sroberto 25258945Sroberto#include <config.h> 26258945Sroberto 27258945Sroberto#include <errno.h> 28258945Sroberto#include <stdio.h> 29258945Sroberto#include <string.h> 30258945Sroberto 31258945Sroberto#include <isc/net.h> 32258945Sroberto#include <isc/print.h> 33258945Sroberto 34258945Sroberto#define NS_INT16SZ 2 35258945Sroberto#define NS_IN6ADDRSZ 16 36258945Sroberto 37258945Sroberto/* 38258945Sroberto * WARNING: Don't even consider trying to compile this on a system where 39258945Sroberto * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. 40258945Sroberto */ 41258945Sroberto 42258945Srobertostatic const char *inet_ntop4(const unsigned char *src, char *dst, 43258945Sroberto size_t size); 44258945Sroberto 45258945Sroberto#ifdef AF_INET6 46258945Srobertostatic const char *inet_ntop6(const unsigned char *src, char *dst, 47258945Sroberto size_t size); 48258945Sroberto#endif 49280849Scyconst char *isc_net_ntop(int af, const void *src, char *dst, size_t size); 50258945Sroberto 51258945Sroberto/*! char * 52258945Sroberto * isc_net_ntop(af, src, dst, size) 53258945Sroberto * convert a network format address to presentation format. 54258945Sroberto * \return 55258945Sroberto * pointer to presentation format address (`dst'), or NULL (see errno). 56280849Scy * \author 57258945Sroberto * Paul Vixie, 1996. 58258945Sroberto */ 59258945Srobertoconst char * 60258945Srobertoisc_net_ntop(int af, const void *src, char *dst, size_t size) 61258945Sroberto{ 62258945Sroberto switch (af) { 63258945Sroberto case AF_INET: 64258945Sroberto return (inet_ntop4(src, dst, size)); 65258945Sroberto#ifdef AF_INET6 66258945Sroberto case AF_INET6: 67258945Sroberto return (inet_ntop6(src, dst, size)); 68258945Sroberto#endif 69258945Sroberto default: 70258945Sroberto errno = EAFNOSUPPORT; 71258945Sroberto return (NULL); 72258945Sroberto } 73258945Sroberto /* NOTREACHED */ 74258945Sroberto} 75258945Sroberto 76258945Sroberto/*! const char * 77258945Sroberto * inet_ntop4(src, dst, size) 78258945Sroberto * format an IPv4 address 79258945Sroberto * \return 80258945Sroberto * `dst' (as a const) 81258945Sroberto * \note 82258945Sroberto * (1) uses no statics 83258945Sroberto * \note 84258945Sroberto * (2) takes a unsigned char* not an in_addr as input 85258945Sroberto * \author 86258945Sroberto * Paul Vixie, 1996. 87258945Sroberto */ 88258945Srobertostatic const char * 89258945Srobertoinet_ntop4(const unsigned char *src, char *dst, size_t size) 90258945Sroberto{ 91258945Sroberto static const char *fmt = "%u.%u.%u.%u"; 92258945Sroberto char tmp[sizeof("255.255.255.255")]; 93280849Scy int len; 94258945Sroberto 95280849Scy len = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); 96280849Scy if (len < 0 || (size_t)len >= size) 97258945Sroberto { 98258945Sroberto errno = ENOSPC; 99258945Sroberto return (NULL); 100258945Sroberto } 101280849Scy memcpy(dst, tmp, 1 + len); 102258945Sroberto 103258945Sroberto return (dst); 104258945Sroberto} 105258945Sroberto 106258945Sroberto/*! const char * 107258945Sroberto * isc_inet_ntop6(src, dst, size) 108258945Sroberto * convert IPv6 binary address into presentation (printable) format 109258945Sroberto * \author 110258945Sroberto * Paul Vixie, 1996. 111258945Sroberto */ 112258945Sroberto#ifdef AF_INET6 113258945Srobertostatic const char * 114258945Srobertoinet_ntop6(const unsigned char *src, char *dst, size_t size) 115258945Sroberto{ 116258945Sroberto /* 117258945Sroberto * Note that int32_t and int16_t need only be "at least" large enough 118258945Sroberto * to contain a value of the specified size. On some systems, like 119258945Sroberto * Crays, there is no such thing as an integer variable with 16 bits. 120258945Sroberto * Keep this in mind if you think this function should have been coded 121258945Sroberto * to use pointer overlays. All the world's not a VAX. 122258945Sroberto */ 123258945Sroberto char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp; 124258945Sroberto struct { int base, len; } best, cur; 125258945Sroberto unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ]; 126258945Sroberto int i; 127258945Sroberto 128258945Sroberto /* 129258945Sroberto * Preprocess: 130258945Sroberto * Copy the input (bytewise) array into a wordwise array. 131258945Sroberto * Find the longest run of 0x00's in src[] for :: shorthanding. 132258945Sroberto */ 133258945Sroberto memset(words, '\0', sizeof(words)); 134258945Sroberto for (i = 0; i < NS_IN6ADDRSZ; i++) 135258945Sroberto words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 136258945Sroberto best.base = -1; 137258945Sroberto cur.base = -1; 138285612Sdelphij best.len = cur.len = 0; 139258945Sroberto for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 140258945Sroberto if (words[i] == 0) { 141258945Sroberto if (cur.base == -1) 142258945Sroberto cur.base = i, cur.len = 1; 143258945Sroberto else 144258945Sroberto cur.len++; 145258945Sroberto } else { 146258945Sroberto if (cur.base != -1) { 147258945Sroberto if (best.base == -1 || cur.len > best.len) 148258945Sroberto best = cur; 149258945Sroberto cur.base = -1; 150258945Sroberto } 151258945Sroberto } 152258945Sroberto } 153258945Sroberto if (cur.base != -1) { 154258945Sroberto if (best.base == -1 || cur.len > best.len) 155258945Sroberto best = cur; 156258945Sroberto } 157258945Sroberto if (best.base != -1 && best.len < 2) 158258945Sroberto best.base = -1; 159258945Sroberto 160258945Sroberto /* 161258945Sroberto * Format the result. 162258945Sroberto */ 163258945Sroberto tp = tmp; 164258945Sroberto for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 165258945Sroberto /* Are we inside the best run of 0x00's? */ 166258945Sroberto if (best.base != -1 && i >= best.base && 167258945Sroberto i < (best.base + best.len)) { 168258945Sroberto if (i == best.base) 169258945Sroberto *tp++ = ':'; 170258945Sroberto continue; 171258945Sroberto } 172258945Sroberto /* Are we following an initial run of 0x00s or any real hex? */ 173258945Sroberto if (i != 0) 174258945Sroberto *tp++ = ':'; 175258945Sroberto /* Is this address an encapsulated IPv4? */ 176280849Scy if (i == 6 && best.base == 0 && (best.len == 6 || 177280849Scy (best.len == 7 && words[7] != 0x0001) || 178280849Scy (best.len == 5 && words[5] == 0xffff))) { 179258945Sroberto if (!inet_ntop4(src+12, tp, 180258945Sroberto sizeof(tmp) - (tp - tmp))) 181258945Sroberto return (NULL); 182258945Sroberto tp += strlen(tp); 183258945Sroberto break; 184258945Sroberto } 185280849Scy tp += snprintf(tp, sizeof(tmp) - (tp - tmp), "%x", words[i]); 186258945Sroberto } 187258945Sroberto /* Was it a trailing run of 0x00's? */ 188258945Sroberto if (best.base != -1 && (best.base + best.len) == 189258945Sroberto (NS_IN6ADDRSZ / NS_INT16SZ)) 190258945Sroberto *tp++ = ':'; 191258945Sroberto *tp++ = '\0'; 192258945Sroberto 193258945Sroberto /* 194258945Sroberto * Check for overflow, copy, and we're done. 195258945Sroberto */ 196258945Sroberto if ((size_t)(tp - tmp) > size) { 197258945Sroberto errno = ENOSPC; 198258945Sroberto return (NULL); 199258945Sroberto } 200280849Scy memcpy(dst, tmp, (size_t)(tp - tmp)); 201258945Sroberto return (dst); 202258945Sroberto} 203258945Sroberto#endif /* AF_INET6 */ 204