2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1996 by Internet Software Consortium.
2N/A *
2N/A * Permission to use, copy, modify, and distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
2N/A * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
2N/A * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
2N/A * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
2N/A * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
2N/A * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
2N/A * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
2N/A * SOFTWARE.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "mt.h"
2N/A#include <stdlib.h>
2N/A#include <ctype.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <netdb.h>
2N/A#include <stdio.h>
2N/A#include <arpa/inet.h>
2N/A#include <netinet/in.h>
2N/A#include <sys/socket.h>
2N/A#include <errno.h>
2N/A
2N/Astatic const char *inet_ntop4(const uchar_t *, char *, socklen_t);
2N/Astatic const char *inet_ntop6(const uchar_t *, char *, socklen_t);
2N/A
2N/A/*
2N/A * char *
2N/A * inet_ntop(af, src, dst, size)
2N/A * convert a network format address to presentation format.
2N/A * return:
2N/A * pointer to presentation format address (`dst'), or NULL (see errno).
2N/A */
2N/Aconst char *
2N/Ainet_ntop(int af, const void *src, char *dst, socklen_t size)
2N/A{
2N/A switch (af) {
2N/A case AF_INET:
2N/A return (inet_ntop4(src, dst, size));
2N/A case AF_INET6:
2N/A return (inet_ntop6(src, dst, size));
2N/A default:
2N/A errno = EAFNOSUPPORT;
2N/A return (NULL);
2N/A }
2N/A /* NOTREACHED */
2N/A}
2N/A
2N/A/*
2N/A * const char *
2N/A * inet_ntop4(src, dst, size)
2N/A * format an IPv4 address, more or less like inet_ntoa()
2N/A * return:
2N/A * `dst' (as a const)
2N/A * notes:
2N/A * (1) uses no statics
2N/A * (2) takes a uchar_t* not an in_addr as input
2N/A */
2N/A
2N/A#ifdef SPRINTF_CHAR
2N/A/* CSTYLED */
2N/A#define SPRINTF(x) strlen(sprintf/**/x)
2N/A#else
2N/A#define SPRINTF(x) ((size_t)sprintf x)
2N/A#endif
2N/A
2N/Astatic const char *
2N/Ainet_ntop4(const uchar_t *src, char *dst, socklen_t size)
2N/A{
2N/A static const char fmt[] = "%u.%u.%u.%u";
2N/A char tmp[sizeof ("255.255.255.255")];
2N/A
2N/A if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
2N/A errno = ENOSPC;
2N/A return (NULL);
2N/A }
2N/A (void) strcpy(dst, tmp);
2N/A return (dst);
2N/A}
2N/A
2N/A/*
2N/A * const char *
2N/A * inet_ntop6(src, dst, size)
2N/A * convert IPv6 binary address into presentation (printable) format
2N/A */
2N/A#define INADDRSZ 4
2N/A#define IN6ADDRSZ 16
2N/A#define INT16SZ 2
2N/Astatic const char *
2N/Ainet_ntop6(const uchar_t *src, char *dst, socklen_t size)
2N/A{
2N/A /*
2N/A * Note that int32_t and int16_t need only be "at least" large enough
2N/A * to contain a value of the specified size. On some systems, like
2N/A * Crays, there is no such thing as an integer variable with 16 bits.
2N/A * Keep this in mind if you think this function should have been coded
2N/A * to use pointer overlays. All the world's not a VAX.
2N/A */
2N/A char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
2N/A struct { int base, len; } best, cur;
2N/A uint_t words[IN6ADDRSZ / INT16SZ];
2N/A int i;
2N/A
2N/A /*
2N/A * Preprocess:
2N/A * Copy the input (bytewise) array into a wordwise array.
2N/A * Find the longest run of 0x00's in src[] for :: shorthanding.
2N/A */
2N/A (void) memset(words, '\0', sizeof (words));
2N/A for (i = 0; i < IN6ADDRSZ; i++)
2N/A words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
2N/A best.base = -1;
2N/A cur.base = -1;
2N/A for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
2N/A if (words[i] == 0) {
2N/A if (cur.base == -1)
2N/A cur.base = i, cur.len = 1;
2N/A else
2N/A cur.len++;
2N/A } else {
2N/A if (cur.base != -1) {
2N/A if (best.base == -1 || cur.len > best.len)
2N/A best = cur;
2N/A cur.base = -1;
2N/A }
2N/A }
2N/A }
2N/A if (cur.base != -1) {
2N/A if (best.base == -1 || cur.len > best.len)
2N/A best = cur;
2N/A }
2N/A if (best.base != -1 && best.len < 2)
2N/A best.base = -1;
2N/A
2N/A /*
2N/A * Format the result.
2N/A */
2N/A tp = tmp;
2N/A for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
2N/A /* Are we inside the best run of 0x00's? */
2N/A if (best.base != -1 && i >= best.base &&
2N/A i < (best.base + best.len)) {
2N/A if (i == best.base)
2N/A *tp++ = ':';
2N/A continue;
2N/A }
2N/A /* Are we following an initial run of 0x00s or any real hex? */
2N/A if (i != 0)
2N/A *tp++ = ':';
2N/A /* Is this address an encapsulated IPv4? */
2N/A if (i == 6 && best.base == 0 &&
2N/A (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
2N/A if (!inet_ntop4(src+12, tp, sizeof (tmp) - (tp - tmp)))
2N/A return (NULL);
2N/A tp += strlen(tp);
2N/A break;
2N/A }
2N/A tp += SPRINTF((tp, "%x", words[i]));
2N/A }
2N/A /* Was it a trailing run of 0x00's? */
2N/A if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
2N/A *tp++ = ':';
2N/A *tp++ = '\0';
2N/A
2N/A /*
2N/A * Check for overflow, copy, and we're done.
2N/A */
2N/A if ((int)(tp - tmp) > size) {
2N/A errno = ENOSPC;
2N/A return (NULL);
2N/A }
2N/A (void) strcpy(dst, tmp);
2N/A return (dst);
2N/A}