/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")4* Copyright (c) 1996-1999 by Internet Software Consortium.5*6* Permission to use, copy, modify, and distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT16* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/1819#include "port_before.h"2021#include <sys/param.h>22#include <sys/socket.h>2324#include <netinet/in.h>25#include <arpa/inet.h>26#include <arpa/nameser.h>2728#include <errno.h>29#include <stdio.h>30#include <string.h>3132#include "port_after.h"3334/*%35* WARNING: Don't even consider trying to compile this on a system where36* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.37*/3839static const char *inet_ntop4(const u_char *src, char *dst, socklen_t size);40static const char *inet_ntop6(const u_char *src, char *dst, socklen_t size);4142/* const char *43* inet_ntop(af, src, dst, size)44* convert a network format address to presentation format.45* return:46* pointer to presentation format address (`dst'), or NULL (see errno).47* author:48* Paul Vixie, 1996.49*/50const char *51inet_ntop(int af, const void * __restrict src, char * __restrict dst,52socklen_t size)53{54switch (af) {55case AF_INET:56return (inet_ntop4(src, dst, size));57case AF_INET6:58return (inet_ntop6(src, dst, size));59default:60errno = EAFNOSUPPORT;61return (NULL);62}63/* NOTREACHED */64}6566/* const char *67* inet_ntop4(src, dst, size)68* format an IPv4 address69* return:70* `dst' (as a const)71* notes:72* (1) uses no statics73* (2) takes a u_char* not an in_addr as input74* author:75* Paul Vixie, 1996.76*/77static const char *78inet_ntop4(const u_char *src, char *dst, socklen_t size)79{80static const char fmt[] = "%u.%u.%u.%u";81char tmp[sizeof "255.255.255.255"];82int l;8384l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);85if (l <= 0 || (socklen_t) l >= size) {86errno = ENOSPC;87return (NULL);88}89strlcpy(dst, tmp, size);90return (dst);91}9293/* const char *94* inet_ntop6(src, dst, size)95* convert IPv6 binary address into presentation (printable) format96* author:97* Paul Vixie, 1996.98*/99static const char *100inet_ntop6(const u_char *src, char *dst, socklen_t size)101{102/*103* Note that int32_t and int16_t need only be "at least" large enough104* to contain a value of the specified size. On some systems, like105* Crays, there is no such thing as an integer variable with 16 bits.106* Keep this in mind if you think this function should have been coded107* to use pointer overlays. All the world's not a VAX.108*/109char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;110struct { int base, len; } best, cur;111u_int words[NS_IN6ADDRSZ / NS_INT16SZ];112int i;113114/*115* Preprocess:116* Copy the input (bytewise) array into a wordwise array.117* Find the longest run of 0x00's in src[] for :: shorthanding.118*/119memset(words, '\0', sizeof words);120for (i = 0; i < NS_IN6ADDRSZ; i++)121words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));122best.base = -1;123best.len = 0;124cur.base = -1;125cur.len = 0;126for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {127if (words[i] == 0) {128if (cur.base == -1)129cur.base = i, cur.len = 1;130else131cur.len++;132} else {133if (cur.base != -1) {134if (best.base == -1 || cur.len > best.len)135best = cur;136cur.base = -1;137}138}139}140if (cur.base != -1) {141if (best.base == -1 || cur.len > best.len)142best = cur;143}144if (best.base != -1 && best.len < 2)145best.base = -1;146147/*148* Format the result.149*/150tp = tmp;151for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {152/* Are we inside the best run of 0x00's? */153if (best.base != -1 && i >= best.base &&154i < (best.base + best.len)) {155if (i == best.base)156*tp++ = ':';157continue;158}159/* Are we following an initial run of 0x00s or any real hex? */160if (i != 0)161*tp++ = ':';162/* Is this address an encapsulated IPv4? */163if (i == 6 && best.base == 0 && (best.len == 6 ||164(best.len == 7 && words[7] != 0x0001) ||165(best.len == 5 && words[5] == 0xffff))) {166if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) {167errno = ENOSPC;168return (NULL);169}170tp += strlen(tp);171break;172}173tp += sprintf(tp, "%x", words[i]);174}175/* Was it a trailing run of 0x00's? */176if (best.base != -1 && (best.base + best.len) ==177(NS_IN6ADDRSZ / NS_INT16SZ))178*tp++ = ':';179*tp++ = '\0';180181/*182* Check for overflow, copy, and we're done.183*/184if ((socklen_t)(tp - tmp) > size) {185errno = ENOSPC;186return (NULL);187}188strcpy(dst, tmp);189return (dst);190}191192/*193* Weak aliases for applications that use certain private entry points,194* and fail to include <arpa/inet.h>.195*/196#undef inet_ntop197__weak_reference(__inet_ntop, inet_ntop);198199/*! \file */200201202