Home | History | Annotate | Line # | Download | only in inet
inet_ntop.c revision 1.1.1.4
      1  1.1.1.4  christos /*	$NetBSD: inet_ntop.c,v 1.1.1.4 2009/04/12 16:35:45 christos 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.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.4  christos static const char rcsid[] = "Id: inet_ntop.c,v 1.5 2005/11/03 22:59:52 marka Exp";
     22      1.1  christos #endif /* LIBC_SCCS and not lint */
     23      1.1  christos 
     24      1.1  christos #include "port_before.h"
     25      1.1  christos 
     26      1.1  christos #include <sys/param.h>
     27      1.1  christos #include <sys/types.h>
     28      1.1  christos #include <sys/socket.h>
     29      1.1  christos 
     30      1.1  christos #include <netinet/in.h>
     31      1.1  christos #include <arpa/inet.h>
     32      1.1  christos #include <arpa/nameser.h>
     33      1.1  christos 
     34      1.1  christos #include <errno.h>
     35      1.1  christos #include <stdio.h>
     36      1.1  christos #include <string.h>
     37      1.1  christos 
     38      1.1  christos #include "port_after.h"
     39      1.1  christos 
     40      1.1  christos #ifdef SPRINTF_CHAR
     41      1.1  christos # define SPRINTF(x) strlen(sprintf/**/x)
     42      1.1  christos #else
     43      1.1  christos # define SPRINTF(x) ((size_t)sprintf x)
     44      1.1  christos #endif
     45      1.1  christos 
     46  1.1.1.2  christos /*%
     47      1.1  christos  * WARNING: Don't even consider trying to compile this on a system where
     48      1.1  christos  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
     49      1.1  christos  */
     50      1.1  christos 
     51      1.1  christos static const char *inet_ntop4 __P((const u_char *src, char *dst, size_t size));
     52      1.1  christos static const char *inet_ntop6 __P((const u_char *src, char *dst, size_t size));
     53      1.1  christos 
     54      1.1  christos /* char *
     55      1.1  christos  * inet_ntop(af, src, dst, size)
     56      1.1  christos  *	convert a network format address to presentation format.
     57      1.1  christos  * return:
     58      1.1  christos  *	pointer to presentation format address (`dst'), or NULL (see errno).
     59      1.1  christos  * author:
     60      1.1  christos  *	Paul Vixie, 1996.
     61      1.1  christos  */
     62      1.1  christos const char *
     63      1.1  christos inet_ntop(af, src, dst, size)
     64      1.1  christos 	int af;
     65      1.1  christos 	const void *src;
     66      1.1  christos 	char *dst;
     67      1.1  christos 	size_t size;
     68      1.1  christos {
     69      1.1  christos 	switch (af) {
     70      1.1  christos 	case AF_INET:
     71      1.1  christos 		return (inet_ntop4(src, dst, size));
     72      1.1  christos 	case AF_INET6:
     73      1.1  christos 		return (inet_ntop6(src, dst, size));
     74      1.1  christos 	default:
     75      1.1  christos 		errno = EAFNOSUPPORT;
     76      1.1  christos 		return (NULL);
     77      1.1  christos 	}
     78      1.1  christos 	/* NOTREACHED */
     79      1.1  christos }
     80      1.1  christos 
     81      1.1  christos /* const char *
     82      1.1  christos  * inet_ntop4(src, dst, size)
     83      1.1  christos  *	format an IPv4 address
     84      1.1  christos  * return:
     85      1.1  christos  *	`dst' (as a const)
     86      1.1  christos  * notes:
     87      1.1  christos  *	(1) uses no statics
     88      1.1  christos  *	(2) takes a u_char* not an in_addr as input
     89      1.1  christos  * author:
     90      1.1  christos  *	Paul Vixie, 1996.
     91      1.1  christos  */
     92      1.1  christos static const char *
     93      1.1  christos inet_ntop4(src, dst, size)
     94      1.1  christos 	const u_char *src;
     95      1.1  christos 	char *dst;
     96      1.1  christos 	size_t size;
     97      1.1  christos {
     98      1.1  christos 	static const char fmt[] = "%u.%u.%u.%u";
     99      1.1  christos 	char tmp[sizeof "255.255.255.255"];
    100      1.1  christos 
    101      1.1  christos 	if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) >= size) {
    102      1.1  christos 		errno = ENOSPC;
    103      1.1  christos 		return (NULL);
    104      1.1  christos 	}
    105      1.1  christos 	strcpy(dst, tmp);
    106      1.1  christos 	return (dst);
    107      1.1  christos }
    108      1.1  christos 
    109      1.1  christos /* const char *
    110      1.1  christos  * inet_ntop6(src, dst, size)
    111      1.1  christos  *	convert IPv6 binary address into presentation (printable) format
    112      1.1  christos  * author:
    113      1.1  christos  *	Paul Vixie, 1996.
    114      1.1  christos  */
    115      1.1  christos static const char *
    116      1.1  christos inet_ntop6(src, dst, size)
    117      1.1  christos 	const u_char *src;
    118      1.1  christos 	char *dst;
    119      1.1  christos 	size_t size;
    120      1.1  christos {
    121      1.1  christos 	/*
    122      1.1  christos 	 * Note that int32_t and int16_t need only be "at least" large enough
    123      1.1  christos 	 * to contain a value of the specified size.  On some systems, like
    124      1.1  christos 	 * Crays, there is no such thing as an integer variable with 16 bits.
    125      1.1  christos 	 * Keep this in mind if you think this function should have been coded
    126      1.1  christos 	 * to use pointer overlays.  All the world's not a VAX.
    127      1.1  christos 	 */
    128      1.1  christos 	char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
    129      1.1  christos 	struct { int base, len; } best, cur;
    130      1.1  christos 	u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
    131      1.1  christos 	int i;
    132      1.1  christos 
    133      1.1  christos 	/*
    134      1.1  christos 	 * Preprocess:
    135      1.1  christos 	 *	Copy the input (bytewise) array into a wordwise array.
    136      1.1  christos 	 *	Find the longest run of 0x00's in src[] for :: shorthanding.
    137      1.1  christos 	 */
    138      1.1  christos 	memset(words, '\0', sizeof words);
    139      1.1  christos 	for (i = 0; i < NS_IN6ADDRSZ; i++)
    140      1.1  christos 		words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
    141      1.1  christos 	best.base = -1;
    142  1.1.1.2  christos 	best.len = 0;
    143      1.1  christos 	cur.base = -1;
    144  1.1.1.2  christos 	cur.len = 0;
    145      1.1  christos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    146      1.1  christos 		if (words[i] == 0) {
    147      1.1  christos 			if (cur.base == -1)
    148      1.1  christos 				cur.base = i, cur.len = 1;
    149      1.1  christos 			else
    150      1.1  christos 				cur.len++;
    151      1.1  christos 		} else {
    152      1.1  christos 			if (cur.base != -1) {
    153      1.1  christos 				if (best.base == -1 || cur.len > best.len)
    154      1.1  christos 					best = cur;
    155      1.1  christos 				cur.base = -1;
    156      1.1  christos 			}
    157      1.1  christos 		}
    158      1.1  christos 	}
    159      1.1  christos 	if (cur.base != -1) {
    160      1.1  christos 		if (best.base == -1 || cur.len > best.len)
    161      1.1  christos 			best = cur;
    162      1.1  christos 	}
    163      1.1  christos 	if (best.base != -1 && best.len < 2)
    164      1.1  christos 		best.base = -1;
    165      1.1  christos 
    166      1.1  christos 	/*
    167      1.1  christos 	 * Format the result.
    168      1.1  christos 	 */
    169      1.1  christos 	tp = tmp;
    170      1.1  christos 	for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
    171      1.1  christos 		/* Are we inside the best run of 0x00's? */
    172      1.1  christos 		if (best.base != -1 && i >= best.base &&
    173      1.1  christos 		    i < (best.base + best.len)) {
    174      1.1  christos 			if (i == best.base)
    175      1.1  christos 				*tp++ = ':';
    176      1.1  christos 			continue;
    177      1.1  christos 		}
    178      1.1  christos 		/* Are we following an initial run of 0x00s or any real hex? */
    179      1.1  christos 		if (i != 0)
    180      1.1  christos 			*tp++ = ':';
    181      1.1  christos 		/* Is this address an encapsulated IPv4? */
    182      1.1  christos 		if (i == 6 && best.base == 0 && (best.len == 6 ||
    183      1.1  christos 		    (best.len == 7 && words[7] != 0x0001) ||
    184      1.1  christos 		    (best.len == 5 && words[5] == 0xffff))) {
    185      1.1  christos 			if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
    186      1.1  christos 				return (NULL);
    187      1.1  christos 			tp += strlen(tp);
    188      1.1  christos 			break;
    189      1.1  christos 		}
    190      1.1  christos 		tp += SPRINTF((tp, "%x", words[i]));
    191      1.1  christos 	}
    192      1.1  christos 	/* Was it a trailing run of 0x00's? */
    193      1.1  christos 	if (best.base != -1 && (best.base + best.len) ==
    194      1.1  christos 	    (NS_IN6ADDRSZ / NS_INT16SZ))
    195      1.1  christos 		*tp++ = ':';
    196      1.1  christos 	*tp++ = '\0';
    197      1.1  christos 
    198      1.1  christos 	/*
    199      1.1  christos 	 * Check for overflow, copy, and we're done.
    200      1.1  christos 	 */
    201      1.1  christos 	if ((size_t)(tp - tmp) > size) {
    202      1.1  christos 		errno = ENOSPC;
    203      1.1  christos 		return (NULL);
    204      1.1  christos 	}
    205      1.1  christos 	strcpy(dst, tmp);
    206      1.1  christos 	return (dst);
    207      1.1  christos }
    208  1.1.1.2  christos 
    209  1.1.1.2  christos /*! \file */
    210