/* $OpenBSD: inet_net_ntop.c,v 1.9 2019/07/03 03:24:04 deraadt Exp $ */12/*3* Copyright (c) 2012 by Gilles Chehade <[email protected]>4* Copyright (c) 1996 by Internet Software Consortium.5*6* SPDX-License-Identifier: ISC7*8* Permission to use, copy, modify, and distribute this software for any9* purpose with or without fee is hereby granted, provided that the above10* copyright notice and this permission notice appear in all copies.11*12* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS13* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES14* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE15* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL16* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR17* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS18* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS19* SOFTWARE.20*/2122#include "port_before.h"2324#include <sys/types.h>25#include <sys/socket.h>26#include <netinet/in.h>27#include <arpa/inet.h>2829#include <errno.h>30#include <stdio.h>31#include <string.h>32#include <stdlib.h>3334#include "port_after.h"3536static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t);37static char *inet_net_ntop_ipv6(const u_char *, int, char *, size_t);3839/*40* char *41* inet_net_ntop(af, src, bits, dst, size)42* convert network number from network to presentation format.43* generates CIDR style result always.44* return:45* pointer to dst, or NULL if an error occurred (check errno).46* author:47* Paul Vixie (ISC), July 199648*/49char *50inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)51{52switch (af) {53case AF_INET:54return (inet_net_ntop_ipv4(src, bits, dst, size));55case AF_INET6:56return (inet_net_ntop_ipv6(src, bits, dst, size));57default:58errno = EAFNOSUPPORT;59return (NULL);60}61}6263/*64* static char *65* inet_net_ntop_ipv4(src, bits, dst, size)66* convert IPv4 network number from network to presentation format.67* generates CIDR style result always.68* return:69* pointer to dst, or NULL if an error occurred (check errno).70* note:71* network byte order assumed. this means 192.5.5.240/28 has72* 0b11110000 in its fourth octet.73* author:74* Paul Vixie (ISC), July 199675*/76static char *77inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size)78{79char *odst = dst;80u_int m;81int b;82char *ep;83int advance;8485ep = dst + size;86if (ep <= dst)87goto emsgsize;8889if (bits < 0 || bits > 32) {90errno = EINVAL;91return (NULL);92}93if (bits == 0) {94if (ep - dst < sizeof "0")95goto emsgsize;96*dst++ = '0';97*dst = '\0';98}99100/* Format whole octets. */101for (b = bits / 8; b > 0; b--) {102if (ep - dst < sizeof "255.")103goto emsgsize;104advance = snprintf(dst, ep - dst, "%u", *src++);105if (advance <= 0 || advance >= ep - dst)106goto emsgsize;107dst += advance;108if (b > 1) {109if (dst + 1 >= ep)110goto emsgsize;111*dst++ = '.';112*dst = '\0';113}114}115116/* Format partial octet. */117b = bits % 8;118if (b > 0) {119if (ep - dst < sizeof ".255")120goto emsgsize;121if (dst != odst)122*dst++ = '.';123m = ((1 << b) - 1) << (8 - b);124advance = snprintf(dst, ep - dst, "%u", *src & m);125if (advance <= 0 || advance >= ep - dst)126goto emsgsize;127dst += advance;128}129130/* Format CIDR /width. */131if (ep - dst < sizeof "/32")132goto emsgsize;133advance = snprintf(dst, ep - dst, "/%u", bits);134if (advance <= 0 || advance >= ep - dst)135goto emsgsize;136dst += advance;137return (odst);138139emsgsize:140errno = EMSGSIZE;141return (NULL);142}143144static char *145inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size)146{147int ret;148char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")];149150if (bits < 0 || bits > 128) {151errno = EINVAL;152return (NULL);153}154155if (inet_ntop(AF_INET6, src, buf, size) == NULL)156return (NULL);157158ret = snprintf(dst, size, "%s/%d", buf, bits);159if (ret < 0 || ret >= size) {160errno = EMSGSIZE;161return (NULL);162}163164return (dst);165}166167/*168* Weak aliases for applications that use certain private entry points,169* and fail to include <arpa/inet.h>.170*/171#undef inet_net_ntop172__weak_reference(__inet_net_ntop, inet_net_ntop);173174175