Home | History | Annotate | Line # | Download | only in inet
inet_ntop.c revision 1.5.12.1
      1  1.5.12.1  wrstuden /*	$NetBSD: inet_ntop.c,v 1.5.12.1 2008/06/23 04:29:31 wrstuden Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*
      4       1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5       1.1  christos  * Copyright (c) 1996-1999 by Internet Software Consortium.
      6       1.1  christos  *
      7       1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8       1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9       1.1  christos  * copyright notice and this permission notice appear in all copies.
     10       1.1  christos  *
     11       1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12       1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13       1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14       1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15       1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16       1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17       1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18       1.1  christos  */
     19       1.1  christos 
     20       1.2  christos #include <sys/cdefs.h>
     21       1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     22       1.2  christos #if 0
     23  1.5.12.1  wrstuden static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp";
     24       1.2  christos #else
     25  1.5.12.1  wrstuden __RCSID("$NetBSD: inet_ntop.c,v 1.5.12.1 2008/06/23 04:29:31 wrstuden Exp $");
     26       1.2  christos #endif
     27       1.1  christos #endif /* LIBC_SCCS and not lint */
     28       1.1  christos 
     29       1.1  christos #include "port_before.h"
     30       1.1  christos 
     31       1.2  christos #include "namespace.h"
     32       1.1  christos #include <sys/param.h>
     33       1.1  christos #include <sys/types.h>
     34       1.1  christos #include <sys/socket.h>
     35       1.1  christos 
     36       1.1  christos #include <netinet/in.h>
     37       1.1  christos #include <arpa/inet.h>
     38       1.1  christos #include <arpa/nameser.h>
     39       1.1  christos 
     40       1.2  christos #include <assert.h>
     41       1.1  christos #include <errno.h>
     42       1.1  christos #include <stdio.h>
     43       1.2  christos #include <stdlib.h>
     44       1.1  christos #include <string.h>
     45       1.1  christos 
     46       1.1  christos #include "port_after.h"
     47       1.1  christos 
     48       1.2  christos #ifdef __weak_alias
     49       1.2  christos __weak_alias(inet_ntop,_inet_ntop)
     50       1.1  christos #endif
     51       1.1  christos 
     52       1.4  christos /*%
     53       1.1  christos  * WARNING: Don't even consider trying to compile this on a system where
     54       1.1  christos  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
     55       1.1  christos  */
     56       1.1  christos 
     57       1.2  christos static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
     58       1.2  christos static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
     59       1.1  christos 
     60       1.1  christos /* char *
     61       1.1  christos  * inet_ntop(af, src, dst, size)
     62       1.1  christos  *	convert a network format address to presentation format.
     63       1.1  christos  * return:
     64       1.1  christos  *	pointer to presentation format address (`dst'), or NULL (see errno).
     65       1.1  christos  * author:
     66       1.1  christos  *	Paul Vixie, 1996.
     67       1.1  christos  */
     68       1.1  christos const char *
     69       1.1  christos inet_ntop(af, src, dst, size)
     70       1.1  christos 	int af;
     71       1.1  christos 	const void *src;
     72       1.1  christos 	char *dst;
     73       1.2  christos 	socklen_t size;
     74       1.1  christos {
     75       1.2  christos 
     76       1.2  christos 	_DIAGASSERT(src != NULL);
     77       1.2  christos 	_DIAGASSERT(dst != NULL);
     78       1.2  christos 
     79       1.1  christos 	switch (af) {
     80       1.1  christos 	case AF_INET:
     81       1.1  christos 		return (inet_ntop4(src, dst, size));
     82       1.1  christos 	case AF_INET6:
     83       1.1  christos 		return (inet_ntop6(src, dst, size));
     84       1.1  christos 	default:
     85       1.1  christos 		errno = EAFNOSUPPORT;
     86       1.1  christos 		return (NULL);
     87       1.1  christos 	}
     88       1.1  christos 	/* NOTREACHED */
     89       1.1  christos }
     90       1.1  christos 
     91       1.1  christos /* const char *
     92       1.1  christos  * inet_ntop4(src, dst, size)
     93       1.2  christos  *	format an IPv4 address, more or less like inet_ntoa()
     94       1.1  christos  * return:
     95       1.1  christos  *	`dst' (as a const)
     96       1.1  christos  * notes:
     97       1.1  christos  *	(1) uses no statics
     98       1.1  christos  *	(2) takes a u_char* not an in_addr as input
     99       1.1  christos  * author:
    100       1.1  christos  *	Paul Vixie, 1996.
    101       1.1  christos  */
    102       1.1  christos static const char *
    103       1.1  christos inet_ntop4(src, dst, size)
    104       1.1  christos 	const u_char *src;
    105       1.1  christos 	char *dst;
    106       1.2  christos 	socklen_t size;
    107       1.1  christos {
    108       1.1  christos 	char tmp[sizeof "255.255.255.255"];
    109       1.2  christos 	int l;
    110       1.2  christos 
    111       1.2  christos 	_DIAGASSERT(src != NULL);
    112       1.2  christos 	_DIAGASSERT(dst != NULL);
    113       1.1  christos 
    114       1.2  christos 	l = snprintf(tmp, sizeof(tmp), "%u.%u.%u.%u",
    115       1.2  christos 	    src[0], src[1], src[2], src[3]);
    116       1.2  christos 	if (l <= 0 || (socklen_t) l >= size) {
    117       1.1  christos 		errno = ENOSPC;
    118       1.1  christos 		return (NULL);
    119       1.1  christos 	}
    120       1.2  christos 	strlcpy(dst, tmp, size);
    121       1.1  christos 	return (dst);
    122       1.1  christos }
    123       1.1  christos 
    124       1.1  christos /* const char *
    125       1.1  christos  * inet_ntop6(src, dst, size)
    126       1.1  christos  *	convert IPv6 binary address into presentation (printable) format
    127       1.1  christos  * author:
    128       1.1  christos  *	Paul Vixie, 1996.
    129       1.1  christos  */
    130       1.1  christos static const char *
    131       1.1  christos inet_ntop6(src, dst, size)
    132       1.1  christos 	const u_char *src;
    133       1.1  christos 	char *dst;
    134       1.2  christos 	socklen_t size;
    135       1.1  christos {
    136       1.1  christos 	/*
    137       1.1  christos 	 * Note that int32_t and int16_t need only be "at least" large enough
    138       1.1  christos 	 * to contain a value of the specified size.  On some systems, like
    139       1.1  christos 	 * Crays, there is no such thing as an integer variable with 16 bits.
    140       1.1  christos 	 * Keep this in mind if you think this function should have been coded
    141       1.1  christos 	 * to use pointer overlays.  All the world's not a VAX.
    142       1.1  christos 	 */
    143       1.2  christos 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
    144       1.2  christos 	char *tp, *ep;
    145       1.1  christos 	struct { int base, len; } best, cur;
    146       1.1  christos 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
    147       1.1  christos 	int i;
    148       1.2  christos 	int advance;
    149       1.2  christos 
    150       1.2  christos 	_DIAGASSERT(src != NULL);
    151       1.2  christos 	_DIAGASSERT(dst != NULL);
    152       1.1  christos 
    153       1.1  christos 	/*
    154       1.1  christos 	 * Preprocess:
    155       1.1  christos 	 *	Copy the input (bytewise) array into a wordwise array.
    156       1.1  christos 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
    157       1.1  christos 	 */
    158       1.1  christos 	memset(words, '\0', sizeof words);
    159       1.1  christos 	for (i = 0; i < NS_IN6ADDRSZ; i++)
    160       1.1  christos 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
    161       1.1  christos 	best.base = -1;
    162       1.4  christos 	best.len = 0;
    163       1.1  christos 	cur.base = -1;
    164       1.4  christos 	cur.len = 0;
    165       1.1  christos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    166       1.1  christos 		if (words[i] == 0) {
    167       1.1  christos 			if (cur.base == -1)
    168       1.1  christos 				cur.base = i, cur.len = 1;
    169       1.1  christos 			else
    170       1.1  christos 				cur.len++;
    171       1.1  christos 		} else {
    172       1.1  christos 			if (cur.base != -1) {
    173       1.1  christos 				if (best.base == -1 || cur.len > best.len)
    174       1.1  christos 					best = cur;
    175       1.1  christos 				cur.base = -1;
    176       1.1  christos 			}
    177       1.1  christos 		}
    178       1.1  christos 	}
    179       1.1  christos 	if (cur.base != -1) {
    180       1.1  christos 		if (best.base == -1 || cur.len > best.len)
    181       1.1  christos 			best = cur;
    182       1.1  christos 	}
    183       1.1  christos 	if (best.base != -1 && best.len < 2)
    184       1.1  christos 		best.base = -1;
    185       1.1  christos 
    186       1.1  christos 	/*
    187       1.1  christos 	 * Format the result.
    188       1.1  christos 	 */
    189       1.1  christos 	tp = tmp;
    190       1.2  christos 	ep = tmp + sizeof(tmp);
    191       1.1  christos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    192       1.1  christos 		/* Are we inside the best run of 0x00's? */
    193       1.1  christos 		if (best.base != -1 && i >= best.base &&
    194       1.1  christos 		    i < (best.base + best.len)) {
    195       1.1  christos 			if (i == best.base)
    196       1.1  christos 				*tp++ = ':';
    197       1.1  christos 			continue;
    198       1.1  christos 		}
    199       1.1  christos 		/* Are we following an initial run of 0x00s or any real hex? */
    200       1.2  christos 		if (i != 0) {
    201       1.2  christos 			if (tp + 1 >= ep)
    202       1.2  christos 				return (NULL);
    203       1.1  christos 			*tp++ = ':';
    204       1.2  christos 		}
    205       1.1  christos 		/* Is this address an encapsulated IPv4? */
    206       1.2  christos 		if (i == 6 && best.base == 0 &&
    207       1.2  christos 		    (best.len == 6 ||
    208       1.1  christos 		    (best.len == 7 && words[7] != 0x0001) ||
    209       1.1  christos 		    (best.len == 5 && words[5] == 0xffff))) {
    210  1.5.12.1  wrstuden 			if (!inet_ntop4(src+12, tp,
    211  1.5.12.1  wrstuden 			    (socklen_t)(sizeof(tmp) - (ep - tp))))
    212       1.1  christos 				return (NULL);
    213       1.1  christos 			tp += strlen(tp);
    214       1.1  christos 			break;
    215       1.1  christos 		}
    216       1.2  christos 		advance = snprintf(tp, (size_t)(ep - tp), "%x", words[i]);
    217       1.2  christos 		if (advance <= 0 || advance >= ep - tp)
    218       1.2  christos 			return (NULL);
    219       1.2  christos 		tp += advance;
    220       1.1  christos 	}
    221       1.1  christos 	/* Was it a trailing run of 0x00's? */
    222       1.1  christos 	if (best.base != -1 && (best.base + best.len) ==
    223       1.2  christos 	    (NS_IN6ADDRSZ / NS_INT16SZ)) {
    224       1.2  christos 		if (tp + 1 >= ep)
    225       1.2  christos 			return (NULL);
    226       1.1  christos 		*tp++ = ':';
    227       1.2  christos 	}
    228       1.2  christos 	if (tp + 1 >= ep)
    229       1.2  christos 		return (NULL);
    230       1.1  christos 	*tp++ = '\0';
    231       1.1  christos 
    232       1.1  christos 	/*
    233       1.1  christos 	 * Check for overflow, copy, and we're done.
    234       1.1  christos 	 */
    235       1.1  christos 	if ((size_t)(tp - tmp) > size) {
    236       1.1  christos 		errno = ENOSPC;
    237       1.1  christos 		return (NULL);
    238       1.1  christos 	}
    239       1.2  christos 	strlcpy(dst, tmp, size);
    240       1.1  christos 	return (dst);
    241       1.1  christos }
    242       1.4  christos 
    243       1.4  christos /*! \file */
    244