/*1* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")2* Copyright (c) 1996-1999 by Internet Software Consortium.3*4* Permission to use, copy, modify, and distribute this software for any5* purpose with or without fee is hereby granted, provided that the above6* copyright notice and this permission notice appear in all copies.7*8* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES9* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF10* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR11* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES12* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN13* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT14* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.15*/1617#include <sys/param.h>18#include <sys/socket.h>19#include <sys/systm.h>2021#include <netinet/in.h>2223/*%24* WARNING: Don't even consider trying to compile this on a system where25* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.26*/2728static char *inet_ntop4(const u_char *src, char *dst, socklen_t size);29static char *inet_ntop6(const u_char *src, char *dst, socklen_t size);3031/* char *32* inet_ntop(af, src, dst, size)33* convert a network format address to presentation format.34* return:35* pointer to presentation format address (`dst'), or NULL (see errno).36* author:37* Paul Vixie, 1996.38*/39char *40inet_ntop(int af, const void *src, char *dst, socklen_t size)41{42switch (af) {43case AF_INET:44return (inet_ntop4(src, dst, size));45case AF_INET6:46return (inet_ntop6(src, dst, size));47default:48return (NULL);49}50/* NOTREACHED */51}5253/* const char *54* inet_ntop4(src, dst, size)55* format an IPv4 address56* return:57* `dst' (as a const)58* notes:59* (1) uses no statics60* (2) takes a u_char* not an in_addr as input61* author:62* Paul Vixie, 1996.63*/64static char *65inet_ntop4(const u_char *src, char *dst, socklen_t size)66{67static const char fmt[] = "%u.%u.%u.%u";68char tmp[sizeof "255.255.255.255"];69int l;7071l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);72if (l <= 0 || (socklen_t) l >= size) {73return (NULL);74}75strlcpy(dst, tmp, size);76return (dst);77}7879/* const char *80* inet_ntop6(src, dst, size)81* convert IPv6 binary address into presentation (printable) format82* author:83* Paul Vixie, 1996.84*/85static char *86inet_ntop6(const u_char *src, char *dst, socklen_t size)87{88/*89* Note that int32_t and int16_t need only be "at least" large enough90* to contain a value of the specified size. On some systems, like91* Crays, there is no such thing as an integer variable with 16 bits.92* Keep this in mind if you think this function should have been coded93* to use pointer overlays. All the world's not a VAX.94*/95char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;96struct { int base, len; } best, cur;97#define NS_IN6ADDRSZ 1698#define NS_INT16SZ 299u_int words[NS_IN6ADDRSZ / NS_INT16SZ];100int i;101102/*103* Preprocess:104* Copy the input (bytewise) array into a wordwise array.105* Find the longest run of 0x00's in src[] for :: shorthanding.106*/107memset(words, '\0', sizeof words);108for (i = 0; i < NS_IN6ADDRSZ; i++)109words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));110best.base = -1;111best.len = 0;112cur.base = -1;113cur.len = 0;114for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {115if (words[i] == 0) {116if (cur.base == -1)117cur.base = i, cur.len = 1;118else119cur.len++;120} else {121if (cur.base != -1) {122if (best.base == -1 || cur.len > best.len)123best = cur;124cur.base = -1;125}126}127}128if (cur.base != -1) {129if (best.base == -1 || cur.len > best.len)130best = cur;131}132if (best.base != -1 && best.len < 2)133best.base = -1;134135/*136* Format the result.137*/138tp = tmp;139for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {140/* Are we inside the best run of 0x00's? */141if (best.base != -1 && i >= best.base &&142i < (best.base + best.len)) {143if (i == best.base)144*tp++ = ':';145continue;146}147/* Are we following an initial run of 0x00s or any real hex? */148if (i != 0)149*tp++ = ':';150/* Is this address an encapsulated IPv4? */151if (i == 6 && best.base == 0 && (best.len == 6 ||152(best.len == 7 && words[7] != 0x0001) ||153(best.len == 5 && words[5] == 0xffff))) {154if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))155return (NULL);156tp += strlen(tp);157break;158}159tp += sprintf(tp, "%x", words[i]);160}161/* Was it a trailing run of 0x00's? */162if (best.base != -1 && (best.base + best.len) ==163(NS_IN6ADDRSZ / NS_INT16SZ))164*tp++ = ':';165*tp++ = '\0';166167/*168* Check for overflow, copy, and we're done.169*/170if ((socklen_t)(tp - tmp) > size) {171return (NULL);172}173strcpy(dst, tmp);174return (dst);175}176177/*! \file */178179180