/* From openssh 4.3p2 compat/inet_ntop.c */1/* Copyright (c) 1996 by Internet Software Consortium.2*3* Permission to use, copy, modify, and distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS8* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES9* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE10* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL11* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR12* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS13* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS14* SOFTWARE.15*/1617/* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */1819#include <ldns/config.h>2021#ifndef HAVE_INET_NTOP2223#include <sys/param.h>24#include <sys/types.h>25#ifdef HAVE_SYS_SOCKET_H26#include <sys/socket.h>27#endif28#ifdef HAVE_NETINET_IN_H29#include <netinet/in.h>30#endif31#include <string.h>32#include <errno.h>33#include <stdio.h>3435#ifndef IN6ADDRSZ36#define IN6ADDRSZ 16 /* IPv6 T_AAAA */37#endif3839#ifndef INT16SZ40#define INT16SZ 2 /* for systems without 16-bit ints */41#endif4243/*44* WARNING: Don't even consider trying to compile this on a system where45* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.46*/4748static const char *inet_ntop4(const u_char *src, char *dst, size_t size);49static const char *inet_ntop6(const u_char *src, char *dst, size_t size);5051/* char *52* inet_ntop(af, src, dst, size)53* convert a network format address to presentation format.54* return:55* pointer to presentation format address (`dst'), or NULL (see errno).56* author:57* Paul Vixie, 1996.58*/59const char *60inet_ntop(int af, const void *src, char *dst, size_t size)61{62switch (af) {63case AF_INET:64return (inet_ntop4(src, dst, size));65case AF_INET6:66return (inet_ntop6(src, dst, size));67default:68#ifdef EAFNOSUPPORT69errno = EAFNOSUPPORT;70#else71errno = ENOSYS;72#endif73return (NULL);74}75/* NOTREACHED */76}7778/* const char *79* inet_ntop4(src, dst, size)80* format an IPv4 address, more or less like inet_ntoa()81* return:82* `dst' (as a const)83* notes:84* (1) uses no statics85* (2) takes a u_char* not an in_addr as input86* author:87* Paul Vixie, 1996.88*/89static const char *90inet_ntop4(const u_char *src, char *dst, size_t size)91{92static const char fmt[] = "%u.%u.%u.%u";93char tmp[sizeof "255.255.255.255"];94int l;9596l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);97if (l <= 0 || l >= (int)size) {98errno = ENOSPC;99return (NULL);100}101strlcpy(dst, tmp, size);102return (dst);103}104105/* const char *106* inet_ntop6(src, dst, size)107* convert IPv6 binary address into presentation (printable) format108* author:109* Paul Vixie, 1996.110*/111static const char *112inet_ntop6(const u_char *src, char *dst, size_t size)113{114/*115* Note that int32_t and int16_t need only be "at least" large enough116* to contain a value of the specified size. On some systems, like117* Crays, there is no such thing as an integer variable with 16 bits.118* Keep this in mind if you think this function should have been coded119* to use pointer overlays. All the world's not a VAX.120*/121char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];122char *tp, *ep;123struct { int base, len; } best, cur;124u_int words[IN6ADDRSZ / INT16SZ];125int i;126int advance;127128/*129* Preprocess:130* Copy the input (bytewise) array into a wordwise array.131* Find the longest run of 0x00's in src[] for :: shorthanding.132*/133memset(words, '\0', sizeof words);134for (i = 0; i < IN6ADDRSZ; i++)135words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));136best.base = -1;137best.len = 0;138cur.base = -1;139cur.len = 0;140for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {141if (words[i] == 0) {142if (cur.base == -1)143cur.base = i, cur.len = 1;144else145cur.len++;146} else {147if (cur.base != -1) {148if (best.base == -1 || cur.len > best.len)149best = cur;150cur.base = -1;151}152}153}154if (cur.base != -1) {155if (best.base == -1 || cur.len > best.len)156best = cur;157}158if (best.base != -1 && best.len < 2)159best.base = -1;160161/*162* Format the result.163*/164tp = tmp;165ep = tmp + sizeof(tmp);166for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {167/* Are we inside the best run of 0x00's? */168if (best.base != -1 && i >= best.base &&169i < (best.base + best.len)) {170if (i == best.base) {171if (tp + 1 >= ep)172return (NULL);173*tp++ = ':';174}175continue;176}177/* Are we following an initial run of 0x00s or any real hex? */178if (i != 0) {179if (tp + 1 >= ep)180return (NULL);181*tp++ = ':';182}183/* Is this address an encapsulated IPv4? */184if (i == 6 && best.base == 0 &&185(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {186if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))187return (NULL);188tp += strlen(tp);189break;190}191advance = snprintf(tp, ep - tp, "%x", words[i]);192if (advance <= 0 || advance >= ep - tp)193return (NULL);194tp += advance;195}196/* Was it a trailing run of 0x00's? */197if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {198if (tp + 1 >= ep)199return (NULL);200*tp++ = ':';201}202if (tp + 1 >= ep)203return (NULL);204*tp++ = '\0';205206/*207* Check for overflow, copy, and we're done.208*/209if ((size_t)(tp - tmp) > size) {210errno = ENOSPC;211return (NULL);212}213strlcpy(dst, tmp, size);214return (dst);215}216217#endif /* !HAVE_INET_NTOP */218219220