Home | History | Annotate | Line # | Download | only in netinet6
      1 /*	$NetBSD: in6_print.c,v 1.1 2014/12/02 19:36:58 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 #include <sys/cdefs.h>
     29 
     30 #include <sys/types.h>
     31 #ifdef _KERNEL
     32 __KERNEL_RCSID(0, "$NetBSD: in6_print.c,v 1.1 2014/12/02 19:36:58 christos Exp $");
     33 #include <sys/systm.h>
     34 #else
     35 __RCSID("$NetBSD: in6_print.c,v 1.1 2014/12/02 19:36:58 christos Exp $");
     36 #include <stdio.h>
     37 #define s6_addr32 __u6_addr.__u6_addr32
     38 static const uint8_t hexdigits[] = "0123456789abcdef";
     39 #endif
     40 
     41 #include <netinet/in.h>
     42 
     43 int
     44 in6_print(char *buf, size_t len, const struct in6_addr *ia6)
     45 {
     46 	int i;
     47 	char *bp;
     48 	char *cp, *ecp;
     49 	const uint16_t *a;
     50 	const uint8_t *d;
     51 	int dcolon = 0;
     52 
     53 	if (IN6_IS_ADDR_V4MAPPED(ia6)) {
     54 		char buf4[INET_ADDRSTRLEN];
     55 		struct in_addr ia = { .s_addr = ia6->s6_addr32[3] };
     56 		in_print(buf4, sizeof(buf4), &ia);
     57 		return snprintf(buf, len, "::ffff:%s", buf4);
     58 	}
     59 
     60 #define ADDC(c) do { \
     61 		if (cp >= ecp) {\
     62 			cp++; \
     63 		} else \
     64 			*cp++ = (char)(c); \
     65 	} while (/*CONSTCOND*/0)
     66 #define ADDX(v) do { \
     67 		uint8_t n = hexdigits[(v)]; \
     68 		ADDC(n); \
     69 		if (cp == bp && n == '0') \
     70 			cp--; \
     71 	} while (/*CONSTCOND*/0)
     72 
     73 	cp = buf;
     74 	ecp = buf + len;
     75 	a = (const uint16_t *)ia6;
     76 	for (i = 0; i < 8; i++) {
     77 		if (dcolon == 1) {
     78 			if (*a == 0) {
     79 				if (i == 7)
     80 					ADDC(':');
     81 				a++;
     82 				continue;
     83 			} else
     84 				dcolon = 2;
     85 		}
     86 		if (*a == 0) {
     87 			if (dcolon == 0 && *(a + 1) == 0) {
     88 				if (i == 0)
     89 					ADDC(':');
     90 				ADDC(':');
     91 				dcolon = 1;
     92 			} else {
     93 				ADDC('0');
     94 				ADDC(':');
     95 			}
     96 			a++;
     97 			continue;
     98 		}
     99 		d = (const u_char *)a;
    100 		bp = cp + 1;
    101 
    102 		ADDX((u_int)*d >> 4);
    103 		ADDX(*d & 0xf);
    104 		d++;
    105 		ADDX((u_int)*d >> 4);
    106 		ADDX(*d & 0xf);
    107 		ADDC(':');
    108 		a++;
    109 	}
    110 	if (cp > buf)
    111 		--cp;
    112 	if (ecp > buf) {
    113 		if (cp < ecp)
    114 			*cp = '\0';
    115 		else
    116 			*--ecp = '\0';
    117 	}
    118 	return (int)(cp - buf);
    119 }
    120 
    121 int
    122 sin6_print(char *buf, size_t len, const void *v)
    123 {
    124 	const struct sockaddr_in6 *sin6 = v;
    125 	const struct in6_addr *ia6 = &sin6->sin6_addr;
    126 	char abuf[INET6_ADDRSTRLEN];
    127 
    128 	if (!sin6->sin6_port)
    129 		return in6_print(buf, len, ia6);
    130 	in6_print(abuf, sizeof(abuf), ia6);
    131 	return snprintf(buf, len, "[%s]:%hu", abuf, ntohs(sin6->sin6_port));
    132 }
    133