1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
2 Copyright (c) 2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 * Copyright (c) 1996-1999 by Internet Software Consortium.
21 * Permission to use, copy, modify, and distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
26 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
28 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
29 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
30 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
31 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
40 #include "inet_ntop.h"
47 # define EAFNOSUPPORT EINVAL
50 #define NS_IN6ADDRSZ 16
54 * WARNING: Don't even consider trying to compile this on a system where
55 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
57 typedef int verify_int_size[2 * sizeof (int) - 7];
60 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
63 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
68 * inet_ntop(af, src, dst, size)
69 * convert a network format address to presentation format.
71 * pointer to presentation format address (`dst'), or NULL (see errno).
76 inet_ntop (int af, const void *restrict src,
77 char *restrict dst, socklen_t cnt)
83 return (inet_ntop4 (src, dst, cnt));
88 return (inet_ntop6 (src, dst, cnt));
101 * inet_ntop4(src, dst, size)
102 * format an IPv4 address
106 * (1) uses no statics
107 * (2) takes a u_char* not an in_addr as input
112 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
114 char tmp[sizeof "255.255.255.255"];
117 len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
127 return strcpy (dst, tmp);
135 * inet_ntop6(src, dst, size)
136 * convert IPv6 binary address into presentation (printable) format
141 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
144 * Note that int32_t and int16_t need only be "at least" large enough
145 * to contain a value of the specified size. On some systems, like
146 * Crays, there is no such thing as an integer variable with 16 bits.
147 * Keep this in mind if you think this function should have been coded
148 * to use pointer overlays. All the world's not a VAX.
150 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
155 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
160 * Copy the input (bytewise) array into a wordwise array.
161 * Find the longest run of 0x00's in src[] for :: shorthanding.
163 memset (words, '\0', sizeof words);
164 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
165 words[i / 2] = (src[i] << 8) | src[i + 1];
168 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
173 cur.base = i, cur.len = 1;
181 if (best.base == -1 || cur.len > best.len)
189 if (best.base == -1 || cur.len > best.len)
192 if (best.base != -1 && best.len < 2)
199 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
201 /* Are we inside the best run of 0x00's? */
202 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
208 /* Are we following an initial run of 0x00s or any real hex? */
211 /* Is this address an encapsulated IPv4? */
212 if (i == 6 && best.base == 0 &&
213 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
215 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
221 int len = sprintf (tp, "%x", words[i]);
227 /* Was it a trailing run of 0x00's? */
228 if (best.base != -1 && (best.base + best.len) ==
229 (NS_IN6ADDRSZ / NS_INT16SZ))
234 * Check for overflow, copy, and we're done.
236 if ((socklen_t) (tp - tmp) > size)
242 return strcpy (dst, tmp);