/* $OpenBSD: inet_ntop.c,v 1.9 2014/02/05 14:20:43 millert Exp $ */12/*3* SPDX-License-Identifier: ISC4*5* Copyright (c) 1996 by Internet Software Consortium.6*7* Permission to use, copy, modify, and distribute this software for any8* purpose with or without fee is hereby granted, provided that the above9* copyright notice and this permission notice appear in all copies.10*11* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS12* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES13* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE14* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL15* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR16* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS17* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS18* SOFTWARE.19*/2021#include <config.h>2223#if !defined(HAVE_INET_NTOP)2425#include <sys/types.h>26#include <sys/socket.h>27#include <netinet/in.h>28#include <arpa/inet.h>29#include <arpa/nameser.h>30#include <string.h>31#include <errno.h>32#include <stdio.h>3334#include <sudo_compat.h>3536#ifndef EAFNOSUPPORT37# define EAFNOSUPPORT EINVAL38#endif3940#ifndef NS_IN6ADDRSZ41# ifdef IN6ADDRSZ42# define NS_IN6ADDRSZ IN6ADDRSZ43# else44# define NS_IN6ADDRSZ 1645# endif46#endif47#ifndef NS_INT16SZ48# ifdef INT16SZ49# define NS_INT16SZ INT16SZ50# else51# define NS_INT16SZ 252# endif53#endif5455/*56* WARNING: Don't even consider trying to compile this on a system where57* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.58*/5960/* const char *61* inet_ntop4(src, dst, size)62* format an IPv4 address, more or less like inet_ntoa()63* return:64* `dst' (as a const)65* notes:66* (1) uses no statics67* (2) takes a unsigned char* not an in_addr as input68* author:69* Paul Vixie, 1996.70*/71static const char *72inet_ntop4(const unsigned char * restrict src, char * restrict dst, socklen_t size)73{74const char fmt[] = "%u.%u.%u.%u";75int len;7677len = snprintf(dst, size, fmt, src[0], src[1], src[2], src[3]);78if (len < 0 || (socklen_t)len >= size) {79errno = ENOSPC;80return (NULL);81}82return (dst);83}8485#ifdef HAVE_STRUCT_IN6_ADDR86/* const char *87* inet_ntop6(src, dst, size)88* convert IPv6 binary address into presentation (printable) format89* author:90* Paul Vixie, 1996.91*/92static const char *93inet_ntop6(const unsigned char * restrict src, char * restrict dst, socklen_t size)94{95/*96* Note that int32_t and int16_t need only be "at least" large enough97* to contain a value of the specified size. On some systems, like98* Crays, there is no such thing as an integer variable with 16 bits.99* Keep this in mind if you think this function should have been coded100* to use pointer overlays. All the world's not a VAX.101*/102char *cp, *ep;103struct { int base, len; } best, cur;104unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];105int i;106int advance;107108/*109* Preprocess:110* Copy the input (bytewise) array into a wordwise array.111* Find the longest run of 0x00's in src[] for :: shorthanding.112*/113memset(words, 0, sizeof(words));114for (i = 0; i < NS_IN6ADDRSZ; i++)115words[i / 2] |= (src[i] << ((1 - (i & 1)) << 3));116best.base = -1;117best.len = 0;118cur.base = -1;119cur.len = 0;120for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {121if (words[i] == 0) {122if (cur.base == -1)123cur.base = i, cur.len = 1;124else125cur.len++;126} else {127if (cur.base != -1) {128if (best.base == -1 || cur.len > best.len)129best = cur;130cur.base = -1;131}132}133}134if (cur.base != -1) {135if (best.base == -1 || cur.len > best.len)136best = cur;137}138if (best.base != -1 && best.len < 2)139best.base = -1;140141/*142* Format the result.143*/144cp = dst;145ep = dst + size;146for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ) && cp < ep; i++) {147/* Are we inside the best run of 0x00's? */148if (best.base != -1 && i >= best.base &&149i < (best.base + best.len)) {150if (i == best.base) {151if (cp + 1 >= ep) {152errno = ENOSPC;153return (NULL);154}155*cp++ = ':';156}157continue;158}159/* Are we following an initial run of 0x00s or any real hex? */160if (i != 0) {161if (cp + 1 >= ep) {162errno = ENOSPC;163return (NULL);164}165*cp++ = ':';166}167/* Is this address an encapsulated IPv4? */168if (i == 6 && best.base == 0 &&169(best.len == 6 ||170(best.len == 7 && words[7] != 0x0001) ||171(best.len == 5 && words[5] == 0xffff))) {172if (!inet_ntop4(src + 12, cp, (socklen_t)(ep - cp)))173return (NULL);174cp += strlen(cp);175break;176}177advance = snprintf(cp, (size_t)(ep - cp), "%x", words[i]);178if (advance <= 0 || advance >= ep - cp) {179errno = ENOSPC;180return (NULL);181}182cp += advance;183}184/* Was it a trailing run of 0x00's? */185if (best.base != -1 &&186(best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ)) {187if (cp + 1 >= ep) {188errno = ENOSPC;189return (NULL);190}191*cp++ = ':';192}193if (cp + 1 >= ep) {194errno = ENOSPC;195return (NULL);196}197*cp++ = '\0';198199return (dst);200}201#endif /* HAVE_STRUCT_IN6_ADDR */202203/* const char *204* inet_ntop(af, src, dst, size)205* convert a network format address to presentation format.206* return:207* pointer to presentation format address (`dst'), or NULL (see errno).208* author:209* Paul Vixie, 1996.210*/211const char *212sudo_inet_ntop(int af, const void * restrict src, char * restrict dst, socklen_t size)213{214switch (af) {215case AF_INET:216return (inet_ntop4(src, dst, size));217#ifdef HAVE_STRUCT_IN6_ADDR218case AF_INET6:219return (inet_ntop6(src, dst, size));220#endif221default:222errno = EAFNOSUPPORT;223return (NULL);224}225/* NOTREACHED */226}227228#endif /* !HAVE_INET_NTOP */229230231