1*462c32cbSDag-Erling Smørgrav /* $OpenBSD: inet_ntop.c,v 1.8 2008/12/09 19:38:38 otto Exp $ */
283d2307dSDag-Erling Smørgrav
383d2307dSDag-Erling Smørgrav /* Copyright (c) 1996 by Internet Software Consortium.
483d2307dSDag-Erling Smørgrav *
583d2307dSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
683d2307dSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
783d2307dSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
883d2307dSDag-Erling Smørgrav *
983d2307dSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
1083d2307dSDag-Erling Smørgrav * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
1183d2307dSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
1283d2307dSDag-Erling Smørgrav * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
1383d2307dSDag-Erling Smørgrav * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
1483d2307dSDag-Erling Smørgrav * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
1583d2307dSDag-Erling Smørgrav * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
1683d2307dSDag-Erling Smørgrav * SOFTWARE.
1783d2307dSDag-Erling Smørgrav */
1883d2307dSDag-Erling Smørgrav
19021d409fSDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
20021d409fSDag-Erling Smørgrav
214b17dab0SDag-Erling Smørgrav #include "includes.h"
2283d2307dSDag-Erling Smørgrav
2383d2307dSDag-Erling Smørgrav #ifndef HAVE_INET_NTOP
2483d2307dSDag-Erling Smørgrav
2583d2307dSDag-Erling Smørgrav #include <sys/types.h>
2683d2307dSDag-Erling Smørgrav #include <sys/socket.h>
2783d2307dSDag-Erling Smørgrav #include <netinet/in.h>
2883d2307dSDag-Erling Smørgrav #include <arpa/inet.h>
2983d2307dSDag-Erling Smørgrav #include <arpa/nameser.h>
3083d2307dSDag-Erling Smørgrav #include <string.h>
3183d2307dSDag-Erling Smørgrav #include <errno.h>
3283d2307dSDag-Erling Smørgrav #include <stdio.h>
3383d2307dSDag-Erling Smørgrav
3483d2307dSDag-Erling Smørgrav #ifndef IN6ADDRSZ
3583d2307dSDag-Erling Smørgrav #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
3683d2307dSDag-Erling Smørgrav #endif
3783d2307dSDag-Erling Smørgrav
3883d2307dSDag-Erling Smørgrav #ifndef INT16SZ
3983d2307dSDag-Erling Smørgrav #define INT16SZ 2 /* for systems without 16-bit ints */
4083d2307dSDag-Erling Smørgrav #endif
4183d2307dSDag-Erling Smørgrav
4283d2307dSDag-Erling Smørgrav /*
4383d2307dSDag-Erling Smørgrav * WARNING: Don't even consider trying to compile this on a system where
4483d2307dSDag-Erling Smørgrav * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
4583d2307dSDag-Erling Smørgrav */
4683d2307dSDag-Erling Smørgrav
474b17dab0SDag-Erling Smørgrav static const char *inet_ntop4(const u_char *src, char *dst, size_t size);
484b17dab0SDag-Erling Smørgrav static const char *inet_ntop6(const u_char *src, char *dst, size_t size);
4983d2307dSDag-Erling Smørgrav
5083d2307dSDag-Erling Smørgrav /* char *
5183d2307dSDag-Erling Smørgrav * inet_ntop(af, src, dst, size)
5283d2307dSDag-Erling Smørgrav * convert a network format address to presentation format.
5383d2307dSDag-Erling Smørgrav * return:
5483d2307dSDag-Erling Smørgrav * pointer to presentation format address (`dst'), or NULL (see errno).
5583d2307dSDag-Erling Smørgrav * author:
5683d2307dSDag-Erling Smørgrav * Paul Vixie, 1996.
5783d2307dSDag-Erling Smørgrav */
5883d2307dSDag-Erling Smørgrav const char *
inet_ntop(int af,const void * src,char * dst,socklen_t size)59*462c32cbSDag-Erling Smørgrav inet_ntop(int af, const void *src, char *dst, socklen_t size)
6083d2307dSDag-Erling Smørgrav {
6183d2307dSDag-Erling Smørgrav switch (af) {
6283d2307dSDag-Erling Smørgrav case AF_INET:
63*462c32cbSDag-Erling Smørgrav return (inet_ntop4(src, dst, (size_t)size));
6483d2307dSDag-Erling Smørgrav case AF_INET6:
65*462c32cbSDag-Erling Smørgrav return (inet_ntop6(src, dst, (size_t)size));
6683d2307dSDag-Erling Smørgrav default:
6783d2307dSDag-Erling Smørgrav errno = EAFNOSUPPORT;
6883d2307dSDag-Erling Smørgrav return (NULL);
6983d2307dSDag-Erling Smørgrav }
7083d2307dSDag-Erling Smørgrav /* NOTREACHED */
7183d2307dSDag-Erling Smørgrav }
7283d2307dSDag-Erling Smørgrav
7383d2307dSDag-Erling Smørgrav /* const char *
7483d2307dSDag-Erling Smørgrav * inet_ntop4(src, dst, size)
7583d2307dSDag-Erling Smørgrav * format an IPv4 address, more or less like inet_ntoa()
7683d2307dSDag-Erling Smørgrav * return:
7783d2307dSDag-Erling Smørgrav * `dst' (as a const)
7883d2307dSDag-Erling Smørgrav * notes:
7983d2307dSDag-Erling Smørgrav * (1) uses no statics
8083d2307dSDag-Erling Smørgrav * (2) takes a u_char* not an in_addr as input
8183d2307dSDag-Erling Smørgrav * author:
8283d2307dSDag-Erling Smørgrav * Paul Vixie, 1996.
8383d2307dSDag-Erling Smørgrav */
8483d2307dSDag-Erling Smørgrav static const char *
inet_ntop4(const u_char * src,char * dst,size_t size)85021d409fSDag-Erling Smørgrav inet_ntop4(const u_char *src, char *dst, size_t size)
8683d2307dSDag-Erling Smørgrav {
8783d2307dSDag-Erling Smørgrav static const char fmt[] = "%u.%u.%u.%u";
8883d2307dSDag-Erling Smørgrav char tmp[sizeof "255.255.255.255"];
894b17dab0SDag-Erling Smørgrav int l;
9083d2307dSDag-Erling Smørgrav
914b17dab0SDag-Erling Smørgrav l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
924b17dab0SDag-Erling Smørgrav if (l <= 0 || l >= size) {
9383d2307dSDag-Erling Smørgrav errno = ENOSPC;
9483d2307dSDag-Erling Smørgrav return (NULL);
9583d2307dSDag-Erling Smørgrav }
964b17dab0SDag-Erling Smørgrav strlcpy(dst, tmp, size);
9783d2307dSDag-Erling Smørgrav return (dst);
9883d2307dSDag-Erling Smørgrav }
9983d2307dSDag-Erling Smørgrav
10083d2307dSDag-Erling Smørgrav /* const char *
10183d2307dSDag-Erling Smørgrav * inet_ntop6(src, dst, size)
10283d2307dSDag-Erling Smørgrav * convert IPv6 binary address into presentation (printable) format
10383d2307dSDag-Erling Smørgrav * author:
10483d2307dSDag-Erling Smørgrav * Paul Vixie, 1996.
10583d2307dSDag-Erling Smørgrav */
10683d2307dSDag-Erling Smørgrav static const char *
inet_ntop6(const u_char * src,char * dst,size_t size)107021d409fSDag-Erling Smørgrav inet_ntop6(const u_char *src, char *dst, size_t size)
10883d2307dSDag-Erling Smørgrav {
10983d2307dSDag-Erling Smørgrav /*
11083d2307dSDag-Erling Smørgrav * Note that int32_t and int16_t need only be "at least" large enough
11183d2307dSDag-Erling Smørgrav * to contain a value of the specified size. On some systems, like
11283d2307dSDag-Erling Smørgrav * Crays, there is no such thing as an integer variable with 16 bits.
11383d2307dSDag-Erling Smørgrav * Keep this in mind if you think this function should have been coded
11483d2307dSDag-Erling Smørgrav * to use pointer overlays. All the world's not a VAX.
11583d2307dSDag-Erling Smørgrav */
1164b17dab0SDag-Erling Smørgrav char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
1174b17dab0SDag-Erling Smørgrav char *tp, *ep;
11883d2307dSDag-Erling Smørgrav struct { int base, len; } best, cur;
11983d2307dSDag-Erling Smørgrav u_int words[IN6ADDRSZ / INT16SZ];
12083d2307dSDag-Erling Smørgrav int i;
1214b17dab0SDag-Erling Smørgrav int advance;
12283d2307dSDag-Erling Smørgrav
12383d2307dSDag-Erling Smørgrav /*
12483d2307dSDag-Erling Smørgrav * Preprocess:
12583d2307dSDag-Erling Smørgrav * Copy the input (bytewise) array into a wordwise array.
12683d2307dSDag-Erling Smørgrav * Find the longest run of 0x00's in src[] for :: shorthanding.
12783d2307dSDag-Erling Smørgrav */
12883d2307dSDag-Erling Smørgrav memset(words, '\0', sizeof words);
12983d2307dSDag-Erling Smørgrav for (i = 0; i < IN6ADDRSZ; i++)
13083d2307dSDag-Erling Smørgrav words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
13183d2307dSDag-Erling Smørgrav best.base = -1;
13283d2307dSDag-Erling Smørgrav cur.base = -1;
13383d2307dSDag-Erling Smørgrav for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
13483d2307dSDag-Erling Smørgrav if (words[i] == 0) {
13583d2307dSDag-Erling Smørgrav if (cur.base == -1)
13683d2307dSDag-Erling Smørgrav cur.base = i, cur.len = 1;
13783d2307dSDag-Erling Smørgrav else
13883d2307dSDag-Erling Smørgrav cur.len++;
13983d2307dSDag-Erling Smørgrav } else {
14083d2307dSDag-Erling Smørgrav if (cur.base != -1) {
14183d2307dSDag-Erling Smørgrav if (best.base == -1 || cur.len > best.len)
14283d2307dSDag-Erling Smørgrav best = cur;
14383d2307dSDag-Erling Smørgrav cur.base = -1;
14483d2307dSDag-Erling Smørgrav }
14583d2307dSDag-Erling Smørgrav }
14683d2307dSDag-Erling Smørgrav }
14783d2307dSDag-Erling Smørgrav if (cur.base != -1) {
14883d2307dSDag-Erling Smørgrav if (best.base == -1 || cur.len > best.len)
14983d2307dSDag-Erling Smørgrav best = cur;
15083d2307dSDag-Erling Smørgrav }
15183d2307dSDag-Erling Smørgrav if (best.base != -1 && best.len < 2)
15283d2307dSDag-Erling Smørgrav best.base = -1;
15383d2307dSDag-Erling Smørgrav
15483d2307dSDag-Erling Smørgrav /*
15583d2307dSDag-Erling Smørgrav * Format the result.
15683d2307dSDag-Erling Smørgrav */
15783d2307dSDag-Erling Smørgrav tp = tmp;
1584b17dab0SDag-Erling Smørgrav ep = tmp + sizeof(tmp);
1594b17dab0SDag-Erling Smørgrav for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
16083d2307dSDag-Erling Smørgrav /* Are we inside the best run of 0x00's? */
16183d2307dSDag-Erling Smørgrav if (best.base != -1 && i >= best.base &&
16283d2307dSDag-Erling Smørgrav i < (best.base + best.len)) {
1634b17dab0SDag-Erling Smørgrav if (i == best.base) {
1644b17dab0SDag-Erling Smørgrav if (tp + 1 >= ep)
1654b17dab0SDag-Erling Smørgrav return (NULL);
16683d2307dSDag-Erling Smørgrav *tp++ = ':';
1674b17dab0SDag-Erling Smørgrav }
16883d2307dSDag-Erling Smørgrav continue;
16983d2307dSDag-Erling Smørgrav }
17083d2307dSDag-Erling Smørgrav /* Are we following an initial run of 0x00s or any real hex? */
1714b17dab0SDag-Erling Smørgrav if (i != 0) {
1724b17dab0SDag-Erling Smørgrav if (tp + 1 >= ep)
1734b17dab0SDag-Erling Smørgrav return (NULL);
17483d2307dSDag-Erling Smørgrav *tp++ = ':';
1754b17dab0SDag-Erling Smørgrav }
17683d2307dSDag-Erling Smørgrav /* Is this address an encapsulated IPv4? */
17783d2307dSDag-Erling Smørgrav if (i == 6 && best.base == 0 &&
17883d2307dSDag-Erling Smørgrav (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
1794b17dab0SDag-Erling Smørgrav if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
18083d2307dSDag-Erling Smørgrav return (NULL);
18183d2307dSDag-Erling Smørgrav tp += strlen(tp);
18283d2307dSDag-Erling Smørgrav break;
18383d2307dSDag-Erling Smørgrav }
1844b17dab0SDag-Erling Smørgrav advance = snprintf(tp, ep - tp, "%x", words[i]);
1854b17dab0SDag-Erling Smørgrav if (advance <= 0 || advance >= ep - tp)
1864b17dab0SDag-Erling Smørgrav return (NULL);
1874b17dab0SDag-Erling Smørgrav tp += advance;
18883d2307dSDag-Erling Smørgrav }
18983d2307dSDag-Erling Smørgrav /* Was it a trailing run of 0x00's? */
1904b17dab0SDag-Erling Smørgrav if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
1914b17dab0SDag-Erling Smørgrav if (tp + 1 >= ep)
1924b17dab0SDag-Erling Smørgrav return (NULL);
19383d2307dSDag-Erling Smørgrav *tp++ = ':';
1944b17dab0SDag-Erling Smørgrav }
1954b17dab0SDag-Erling Smørgrav if (tp + 1 >= ep)
1964b17dab0SDag-Erling Smørgrav return (NULL);
19783d2307dSDag-Erling Smørgrav *tp++ = '\0';
19883d2307dSDag-Erling Smørgrav
19983d2307dSDag-Erling Smørgrav /*
20083d2307dSDag-Erling Smørgrav * Check for overflow, copy, and we're done.
20183d2307dSDag-Erling Smørgrav */
20283d2307dSDag-Erling Smørgrav if ((size_t)(tp - tmp) > size) {
20383d2307dSDag-Erling Smørgrav errno = ENOSPC;
20483d2307dSDag-Erling Smørgrav return (NULL);
20583d2307dSDag-Erling Smørgrav }
2064b17dab0SDag-Erling Smørgrav strlcpy(dst, tmp, size);
20783d2307dSDag-Erling Smørgrav return (dst);
20883d2307dSDag-Erling Smørgrav }
20983d2307dSDag-Erling Smørgrav
21083d2307dSDag-Erling Smørgrav #endif /* !HAVE_INET_NTOP */
211