inet_ntop.c revision 177633
1177633Sdfr/* 2177633Sdfr * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 3177633Sdfr * Copyright (c) 1996-1999 by Internet Software Consortium. 4177633Sdfr * 5177633Sdfr * Permission to use, copy, modify, and distribute this software for any 6177633Sdfr * purpose with or without fee is hereby granted, provided that the above 7177633Sdfr * copyright notice and this permission notice appear in all copies. 8177633Sdfr * 9177633Sdfr * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 10177633Sdfr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11177633Sdfr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 12177633Sdfr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13177633Sdfr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14177633Sdfr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 15177633Sdfr * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16177633Sdfr */ 17177633Sdfr 18177633Sdfr#if defined(LIBC_SCCS) && !defined(lint) 19177633Sdfrstatic const char rcsid[] = "$Id: inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp $"; 20177633Sdfr#endif /* LIBC_SCCS and not lint */ 21177633Sdfr#include <sys/cdefs.h> 22177633Sdfr__FBSDID("$FreeBSD: head/sys/rpc/inet_ntop.c 177633 2008-03-26 15:23:12Z dfr $"); 23177633Sdfr 24177633Sdfr#include <sys/param.h> 25177633Sdfr#include <sys/types.h> 26177633Sdfr#include <sys/socket.h> 27177633Sdfr#include <sys/systm.h> 28177633Sdfr 29177633Sdfr#include <rpc/types.h> 30177633Sdfr#include "rpc_com.h" 31177633Sdfr 32177633Sdfr/*% 33177633Sdfr * WARNING: Don't even consider trying to compile this on a system where 34177633Sdfr * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. 35177633Sdfr */ 36177633Sdfr 37177633Sdfrstatic const char *inet_ntop4(const u_char *src, char *dst, socklen_t size); 38177633Sdfrstatic const char *inet_ntop6(const u_char *src, char *dst, socklen_t size); 39177633Sdfr 40177633Sdfr/* char * 41177633Sdfr * inet_ntop(af, src, dst, size) 42177633Sdfr * convert a network format address to presentation format. 43177633Sdfr * return: 44177633Sdfr * pointer to presentation format address (`dst'), or NULL (see errno). 45177633Sdfr * author: 46177633Sdfr * Paul Vixie, 1996. 47177633Sdfr */ 48177633Sdfrconst char * 49177633Sdfr__rpc_inet_ntop(int af, const void * __restrict src, char * __restrict dst, 50177633Sdfr socklen_t size) 51177633Sdfr{ 52177633Sdfr switch (af) { 53177633Sdfr case AF_INET: 54177633Sdfr return (inet_ntop4(src, dst, size)); 55177633Sdfr case AF_INET6: 56177633Sdfr return (inet_ntop6(src, dst, size)); 57177633Sdfr default: 58177633Sdfr return (NULL); 59177633Sdfr } 60177633Sdfr /* NOTREACHED */ 61177633Sdfr} 62177633Sdfr 63177633Sdfr/* const char * 64177633Sdfr * inet_ntop4(src, dst, size) 65177633Sdfr * format an IPv4 address 66177633Sdfr * return: 67177633Sdfr * `dst' (as a const) 68177633Sdfr * notes: 69177633Sdfr * (1) uses no statics 70177633Sdfr * (2) takes a u_char* not an in_addr as input 71177633Sdfr * author: 72177633Sdfr * Paul Vixie, 1996. 73177633Sdfr */ 74177633Sdfrstatic const char * 75177633Sdfrinet_ntop4(const u_char *src, char *dst, socklen_t size) 76177633Sdfr{ 77177633Sdfr static const char fmt[] = "%u.%u.%u.%u"; 78177633Sdfr char tmp[sizeof "255.255.255.255"]; 79177633Sdfr int l; 80177633Sdfr 81177633Sdfr l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]); 82177633Sdfr if (l <= 0 || (socklen_t) l >= size) { 83177633Sdfr return (NULL); 84177633Sdfr } 85177633Sdfr strlcpy(dst, tmp, size); 86177633Sdfr return (dst); 87177633Sdfr} 88177633Sdfr 89177633Sdfr/* const char * 90177633Sdfr * inet_ntop6(src, dst, size) 91177633Sdfr * convert IPv6 binary address into presentation (printable) format 92177633Sdfr * author: 93177633Sdfr * Paul Vixie, 1996. 94177633Sdfr */ 95177633Sdfrstatic const char * 96177633Sdfrinet_ntop6(const u_char *src, char *dst, socklen_t size) 97177633Sdfr{ 98177633Sdfr /* 99177633Sdfr * Note that int32_t and int16_t need only be "at least" large enough 100177633Sdfr * to contain a value of the specified size. On some systems, like 101177633Sdfr * Crays, there is no such thing as an integer variable with 16 bits. 102177633Sdfr * Keep this in mind if you think this function should have been coded 103177633Sdfr * to use pointer overlays. All the world's not a VAX. 104177633Sdfr */ 105177633Sdfr char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp; 106177633Sdfr struct { int base, len; } best, cur; 107177633Sdfr#define NS_IN6ADDRSZ 16 108177633Sdfr#define NS_INT16SZ 2 109177633Sdfr u_int words[NS_IN6ADDRSZ / NS_INT16SZ]; 110177633Sdfr int i; 111177633Sdfr 112177633Sdfr /* 113177633Sdfr * Preprocess: 114177633Sdfr * Copy the input (bytewise) array into a wordwise array. 115177633Sdfr * Find the longest run of 0x00's in src[] for :: shorthanding. 116177633Sdfr */ 117177633Sdfr memset(words, '\0', sizeof words); 118177633Sdfr for (i = 0; i < NS_IN6ADDRSZ; i++) 119177633Sdfr words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); 120177633Sdfr best.base = -1; 121177633Sdfr best.len = 0; 122177633Sdfr cur.base = -1; 123177633Sdfr cur.len = 0; 124177633Sdfr for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 125177633Sdfr if (words[i] == 0) { 126177633Sdfr if (cur.base == -1) 127177633Sdfr cur.base = i, cur.len = 1; 128177633Sdfr else 129177633Sdfr cur.len++; 130177633Sdfr } else { 131177633Sdfr if (cur.base != -1) { 132177633Sdfr if (best.base == -1 || cur.len > best.len) 133177633Sdfr best = cur; 134177633Sdfr cur.base = -1; 135177633Sdfr } 136177633Sdfr } 137177633Sdfr } 138177633Sdfr if (cur.base != -1) { 139177633Sdfr if (best.base == -1 || cur.len > best.len) 140177633Sdfr best = cur; 141177633Sdfr } 142177633Sdfr if (best.base != -1 && best.len < 2) 143177633Sdfr best.base = -1; 144177633Sdfr 145177633Sdfr /* 146177633Sdfr * Format the result. 147177633Sdfr */ 148177633Sdfr tp = tmp; 149177633Sdfr for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) { 150177633Sdfr /* Are we inside the best run of 0x00's? */ 151177633Sdfr if (best.base != -1 && i >= best.base && 152177633Sdfr i < (best.base + best.len)) { 153177633Sdfr if (i == best.base) 154177633Sdfr *tp++ = ':'; 155177633Sdfr continue; 156177633Sdfr } 157177633Sdfr /* Are we following an initial run of 0x00s or any real hex? */ 158177633Sdfr if (i != 0) 159177633Sdfr *tp++ = ':'; 160177633Sdfr /* Is this address an encapsulated IPv4? */ 161177633Sdfr if (i == 6 && best.base == 0 && (best.len == 6 || 162177633Sdfr (best.len == 7 && words[7] != 0x0001) || 163177633Sdfr (best.len == 5 && words[5] == 0xffff))) { 164177633Sdfr if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) 165177633Sdfr return (NULL); 166177633Sdfr tp += strlen(tp); 167177633Sdfr break; 168177633Sdfr } 169177633Sdfr tp += sprintf(tp, "%x", words[i]); 170177633Sdfr } 171177633Sdfr /* Was it a trailing run of 0x00's? */ 172177633Sdfr if (best.base != -1 && (best.base + best.len) == 173177633Sdfr (NS_IN6ADDRSZ / NS_INT16SZ)) 174177633Sdfr *tp++ = ':'; 175177633Sdfr *tp++ = '\0'; 176177633Sdfr 177177633Sdfr /* 178177633Sdfr * Check for overflow, copy, and we're done. 179177633Sdfr */ 180177633Sdfr if ((socklen_t)(tp - tmp) > size) { 181177633Sdfr return (NULL); 182177633Sdfr } 183177633Sdfr strcpy(dst, tmp); 184177633Sdfr return (dst); 185177633Sdfr} 186177633Sdfr 187177633Sdfr/*! \file */ 188