17c478bd9Sstevel@tonic-gate /*
2*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
661961e0fSrobinson /*
761961e0fSrobinson * Copyright (c) 1996 by Internet Software Consortium.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * Permission to use, copy, modify, and distribute this software for any
107c478bd9Sstevel@tonic-gate * purpose with or without fee is hereby granted, provided that the above
117c478bd9Sstevel@tonic-gate * copyright notice and this permission notice appear in all copies.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
147c478bd9Sstevel@tonic-gate * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
167c478bd9Sstevel@tonic-gate * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
177c478bd9Sstevel@tonic-gate * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
187c478bd9Sstevel@tonic-gate * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
197c478bd9Sstevel@tonic-gate * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
207c478bd9Sstevel@tonic-gate * SOFTWARE.
217c478bd9Sstevel@tonic-gate */
2261961e0fSrobinson
237c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
247c478bd9Sstevel@tonic-gate
25*e8031f0aSraf #include "mt.h"
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <ctype.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <netdb.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
337c478bd9Sstevel@tonic-gate #include <netinet/in.h>
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate
3761961e0fSrobinson static const char *inet_ntop4(const uchar_t *, char *, socklen_t);
3861961e0fSrobinson static const char *inet_ntop6(const uchar_t *, char *, socklen_t);
397c478bd9Sstevel@tonic-gate
4061961e0fSrobinson /*
4161961e0fSrobinson * char *
427c478bd9Sstevel@tonic-gate * inet_ntop(af, src, dst, size)
437c478bd9Sstevel@tonic-gate * convert a network format address to presentation format.
447c478bd9Sstevel@tonic-gate * return:
457c478bd9Sstevel@tonic-gate * pointer to presentation format address (`dst'), or NULL (see errno).
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)487c478bd9Sstevel@tonic-gate inet_ntop(int af, const void *src, char *dst, socklen_t size)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate switch (af) {
517c478bd9Sstevel@tonic-gate case AF_INET:
527c478bd9Sstevel@tonic-gate return (inet_ntop4(src, dst, size));
537c478bd9Sstevel@tonic-gate case AF_INET6:
547c478bd9Sstevel@tonic-gate return (inet_ntop6(src, dst, size));
557c478bd9Sstevel@tonic-gate default:
567c478bd9Sstevel@tonic-gate errno = EAFNOSUPPORT;
577c478bd9Sstevel@tonic-gate return (NULL);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate /* NOTREACHED */
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate
6261961e0fSrobinson /*
6361961e0fSrobinson * const char *
647c478bd9Sstevel@tonic-gate * inet_ntop4(src, dst, size)
657c478bd9Sstevel@tonic-gate * format an IPv4 address, more or less like inet_ntoa()
667c478bd9Sstevel@tonic-gate * return:
677c478bd9Sstevel@tonic-gate * `dst' (as a const)
687c478bd9Sstevel@tonic-gate * notes:
697c478bd9Sstevel@tonic-gate * (1) uses no statics
7061961e0fSrobinson * (2) takes a uchar_t* not an in_addr as input
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
7461961e0fSrobinson /* CSTYLED */
757c478bd9Sstevel@tonic-gate #define SPRINTF(x) strlen(sprintf/**/x)
767c478bd9Sstevel@tonic-gate #else
777c478bd9Sstevel@tonic-gate #define SPRINTF(x) ((size_t)sprintf x)
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate static const char *
inet_ntop4(const uchar_t * src,char * dst,socklen_t size)8161961e0fSrobinson inet_ntop4(const uchar_t *src, char *dst, socklen_t size)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate static const char fmt[] = "%u.%u.%u.%u";
8461961e0fSrobinson char tmp[sizeof ("255.255.255.255")];
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate if (SPRINTF((tmp, fmt, src[0], src[1], src[2], src[3])) > size) {
877c478bd9Sstevel@tonic-gate errno = ENOSPC;
887c478bd9Sstevel@tonic-gate return (NULL);
897c478bd9Sstevel@tonic-gate }
9061961e0fSrobinson (void) strcpy(dst, tmp);
917c478bd9Sstevel@tonic-gate return (dst);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
9461961e0fSrobinson /*
9561961e0fSrobinson * const char *
967c478bd9Sstevel@tonic-gate * inet_ntop6(src, dst, size)
977c478bd9Sstevel@tonic-gate * convert IPv6 binary address into presentation (printable) format
987c478bd9Sstevel@tonic-gate */
997c478bd9Sstevel@tonic-gate #define INADDRSZ 4
1007c478bd9Sstevel@tonic-gate #define IN6ADDRSZ 16
1017c478bd9Sstevel@tonic-gate #define INT16SZ 2
1027c478bd9Sstevel@tonic-gate static const char *
inet_ntop6(const uchar_t * src,char * dst,socklen_t size)10361961e0fSrobinson inet_ntop6(const uchar_t *src, char *dst, socklen_t size)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * Note that int32_t and int16_t need only be "at least" large enough
1077c478bd9Sstevel@tonic-gate * to contain a value of the specified size. On some systems, like
1087c478bd9Sstevel@tonic-gate * Crays, there is no such thing as an integer variable with 16 bits.
1097c478bd9Sstevel@tonic-gate * Keep this in mind if you think this function should have been coded
1107c478bd9Sstevel@tonic-gate * to use pointer overlays. All the world's not a VAX.
1117c478bd9Sstevel@tonic-gate */
11261961e0fSrobinson char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
1137c478bd9Sstevel@tonic-gate struct { int base, len; } best, cur;
11461961e0fSrobinson uint_t words[IN6ADDRSZ / INT16SZ];
1157c478bd9Sstevel@tonic-gate int i;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * Preprocess:
1197c478bd9Sstevel@tonic-gate * Copy the input (bytewise) array into a wordwise array.
1207c478bd9Sstevel@tonic-gate * Find the longest run of 0x00's in src[] for :: shorthanding.
1217c478bd9Sstevel@tonic-gate */
12261961e0fSrobinson (void) memset(words, '\0', sizeof (words));
1237c478bd9Sstevel@tonic-gate for (i = 0; i < IN6ADDRSZ; i++)
1247c478bd9Sstevel@tonic-gate words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
1257c478bd9Sstevel@tonic-gate best.base = -1;
1267c478bd9Sstevel@tonic-gate cur.base = -1;
1277c478bd9Sstevel@tonic-gate for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1287c478bd9Sstevel@tonic-gate if (words[i] == 0) {
1297c478bd9Sstevel@tonic-gate if (cur.base == -1)
1307c478bd9Sstevel@tonic-gate cur.base = i, cur.len = 1;
1317c478bd9Sstevel@tonic-gate else
1327c478bd9Sstevel@tonic-gate cur.len++;
1337c478bd9Sstevel@tonic-gate } else {
1347c478bd9Sstevel@tonic-gate if (cur.base != -1) {
1357c478bd9Sstevel@tonic-gate if (best.base == -1 || cur.len > best.len)
1367c478bd9Sstevel@tonic-gate best = cur;
1377c478bd9Sstevel@tonic-gate cur.base = -1;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate if (cur.base != -1) {
1427c478bd9Sstevel@tonic-gate if (best.base == -1 || cur.len > best.len)
1437c478bd9Sstevel@tonic-gate best = cur;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (best.base != -1 && best.len < 2)
1467c478bd9Sstevel@tonic-gate best.base = -1;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * Format the result.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate tp = tmp;
1527c478bd9Sstevel@tonic-gate for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
1537c478bd9Sstevel@tonic-gate /* Are we inside the best run of 0x00's? */
1547c478bd9Sstevel@tonic-gate if (best.base != -1 && i >= best.base &&
1557c478bd9Sstevel@tonic-gate i < (best.base + best.len)) {
1567c478bd9Sstevel@tonic-gate if (i == best.base)
1577c478bd9Sstevel@tonic-gate *tp++ = ':';
1587c478bd9Sstevel@tonic-gate continue;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate /* Are we following an initial run of 0x00s or any real hex? */
1617c478bd9Sstevel@tonic-gate if (i != 0)
1627c478bd9Sstevel@tonic-gate *tp++ = ':';
1637c478bd9Sstevel@tonic-gate /* Is this address an encapsulated IPv4? */
1647c478bd9Sstevel@tonic-gate if (i == 6 && best.base == 0 &&
1657c478bd9Sstevel@tonic-gate (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
16661961e0fSrobinson if (!inet_ntop4(src+12, tp, sizeof (tmp) - (tp - tmp)))
1677c478bd9Sstevel@tonic-gate return (NULL);
1687c478bd9Sstevel@tonic-gate tp += strlen(tp);
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate tp += SPRINTF((tp, "%x", words[i]));
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate /* Was it a trailing run of 0x00's? */
1747c478bd9Sstevel@tonic-gate if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
1757c478bd9Sstevel@tonic-gate *tp++ = ':';
1767c478bd9Sstevel@tonic-gate *tp++ = '\0';
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * Check for overflow, copy, and we're done.
1807c478bd9Sstevel@tonic-gate */
1817c478bd9Sstevel@tonic-gate if ((int)(tp - tmp) > size) {
1827c478bd9Sstevel@tonic-gate errno = ENOSPC;
1837c478bd9Sstevel@tonic-gate return (NULL);
1847c478bd9Sstevel@tonic-gate }
18561961e0fSrobinson (void) strcpy(dst, tmp);
1867c478bd9Sstevel@tonic-gate return (dst);
1877c478bd9Sstevel@tonic-gate }
188