inet_ntop.c revision 16a68807e13caea3183a41a5292f1b3f48b81a26
5784N/A/*
5784N/A * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5784N/A * Copyright (C) 1996-2001 Internet Software Consortium.
5784N/A *
5784N/A * Permission to use, copy, modify, and distribute this software for any
5784N/A * purpose with or without fee is hereby granted, provided that the above
5784N/A * copyright notice and this permission notice appear in all copies.
5784N/A *
5784N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
6982N/A * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
6982N/A * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
5784N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
5784N/A * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
5784N/A * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
6982N/A * PERFORMANCE OF THIS SOFTWARE.
5784N/A */
6982N/A
6982N/A#if defined(LIBC_SCCS) && !defined(lint)
6982N/Astatic char rcsid[] =
6982N/A "$Id: inet_ntop.c,v 1.15 2004/08/28 06:16:57 marka Exp $";
5784N/A#endif /* LIBC_SCCS and not lint */
5784N/A
5784N/A#include <config.h>
5784N/A
5784N/A#include <errno.h>
5784N/A#include <stdio.h>
5784N/A#include <string.h>
5784N/A
5784N/A#include <isc/net.h>
5784N/A#include <isc/print.h>
5784N/A
5784N/A#define NS_INT16SZ 2
5784N/A#define NS_IN6ADDRSZ 16
5784N/A
5784N/A/*
5784N/A * WARNING: Don't even consider trying to compile this on a system where
5784N/A * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
5784N/A */
5784N/A
5784N/Astatic const char *inet_ntop4(const unsigned char *src, char *dst,
5784N/A size_t size);
5784N/A
5784N/A#ifdef AF_INET6
5784N/Astatic const char *inet_ntop6(const unsigned char *src, char *dst,
5784N/A size_t size);
5784N/A#endif
5784N/A
5784N/A/* char *
5784N/A * isc_net_ntop(af, src, dst, size)
5784N/A * convert a network format address to presentation format.
5784N/A * return:
5784N/A * pointer to presentation format address (`dst'), or NULL (see errno).
5784N/A * author:
5784N/A * Paul Vixie, 1996.
5784N/A */
5784N/Aconst char *
5784N/Aisc_net_ntop(int af, const void *src, char *dst, size_t size)
5784N/A{
5784N/A switch (af) {
5784N/A case AF_INET:
5784N/A return (inet_ntop4(src, dst, size));
5784N/A#ifdef AF_INET6
5784N/A case AF_INET6:
5784N/A return (inet_ntop6(src, dst, size));
5784N/A#endif
5784N/A default:
5784N/A errno = EAFNOSUPPORT;
5784N/A return (NULL);
5784N/A }
5784N/A /* NOTREACHED */
5784N/A}
5784N/A
5784N/A/* const char *
5784N/A * inet_ntop4(src, dst, size)
5784N/A * format an IPv4 address
5784N/A * return:
5784N/A * `dst' (as a const)
5784N/A * notes:
5784N/A * (1) uses no statics
5784N/A * (2) takes a unsigned char* not an in_addr as input
5784N/A * author:
5784N/A * Paul Vixie, 1996.
5784N/A */
5784N/Astatic const char *
5784N/Ainet_ntop4(const unsigned char *src, char *dst, size_t size)
5784N/A{
5784N/A static const char *fmt = "%u.%u.%u.%u";
5784N/A char tmp[sizeof("255.255.255.255")];
5784N/A
5784N/A if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size)
5784N/A {
5784N/A errno = ENOSPC;
5784N/A return (NULL);
5784N/A }
5784N/A strcpy(dst, tmp);
5784N/A
5784N/A return (dst);
5784N/A}
5784N/A
5784N/A/* const char *
5784N/A * isc_inet_ntop6(src, dst, size)
5784N/A * convert IPv6 binary address into presentation (printable) format
5784N/A * author:
5784N/A * Paul Vixie, 1996.
5784N/A */
5784N/A#ifdef AF_INET6
5784N/Astatic const char *
5784N/Ainet_ntop6(const unsigned char *src, char *dst, size_t size)
5784N/A{
5784N/A /*
5784N/A * Note that int32_t and int16_t need only be "at least" large enough
5784N/A * to contain a value of the specified size. On some systems, like
5784N/A * Crays, there is no such thing as an integer variable with 16 bits.
5784N/A * Keep this in mind if you think this function should have been coded
5784N/A * to use pointer overlays. All the world's not a VAX.
5784N/A */
5784N/A char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
5784N/A struct { int base, len; } best, cur;
5784N/A unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
5784N/A int i;
5784N/A
5784N/A /*
5784N/A * Preprocess:
5784N/A * Copy the input (bytewise) array into a wordwise array.
5784N/A * Find the longest run of 0x00's in src[] for :: shorthanding.
5784N/A */
5784N/A memset(words, '\0', sizeof(words));
5784N/A for (i = 0; i < NS_IN6ADDRSZ; i++)
5784N/A words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
5784N/A best.base = -1;
5784N/A cur.base = -1;
5784N/A for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
5784N/A if (words[i] == 0) {
5784N/A if (cur.base == -1)
5784N/A cur.base = i, cur.len = 1;
5784N/A else
5784N/A cur.len++;
5784N/A } else {
5784N/A if (cur.base != -1) {
5784N/A if (best.base == -1 || cur.len > best.len)
5784N/A best = cur;
5784N/A cur.base = -1;
5784N/A }
5784N/A }
5784N/A }
5784N/A if (cur.base != -1) {
5784N/A if (best.base == -1 || cur.len > best.len)
5784N/A best = cur;
5784N/A }
5784N/A if (best.base != -1 && best.len < 2)
5784N/A best.base = -1;
5784N/A
5784N/A /*
5784N/A * Format the result.
5784N/A */
5784N/A tp = tmp;
5784N/A for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
5784N/A /* Are we inside the best run of 0x00's? */
5784N/A if (best.base != -1 && i >= best.base &&
5784N/A i < (best.base + best.len)) {
5784N/A if (i == best.base)
5784N/A *tp++ = ':';
5784N/A continue;
5784N/A }
5784N/A /* Are we following an initial run of 0x00s or any real hex? */
5784N/A if (i != 0)
5784N/A *tp++ = ':';
5784N/A /* Is this address an encapsulated IPv4? */
5784N/A if (i == 6 && best.base == 0 &&
5784N/A (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
5784N/A if (!inet_ntop4(src+12, tp,
5784N/A sizeof(tmp) - (tp - tmp)))
5784N/A return (NULL);
5784N/A tp += strlen(tp);
5784N/A break;
5784N/A }
5784N/A tp += sprintf(tp, "%x", words[i]);
5784N/A }
5784N/A /* Was it a trailing run of 0x00's? */
5784N/A if (best.base != -1 && (best.base + best.len) ==
5784N/A (NS_IN6ADDRSZ / NS_INT16SZ))
5784N/A *tp++ = ':';
5784N/A *tp++ = '\0';
5784N/A
5784N/A /*
5784N/A * Check for overflow, copy, and we're done.
5784N/A */
5784N/A if ((size_t)(tp - tmp) > size) {
5784N/A errno = ENOSPC;
5784N/A return (NULL);
5784N/A }
5784N/A strcpy(dst, tmp);
5784N/A return (dst);
5784N/A}
5784N/A#endif /* AF_INET6 */
5784N/A