1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
3 Copyright (C) 2005, 2006, 2008 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /*
20 * Copyright (c) 1996-1999 by Internet Software Consortium.
21 *
22 * Permission to use, copy, modify, and distribute this software for any
23 * purpose with or without fee is hereby granted, provided that the above
24 * copyright notice and this permission notice appear in all copies.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
27 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
29 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
30 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
31 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
32 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
33 * SOFTWARE.
34 */
36 #include <config.h>
38 /* Specification. */
39 #include <arpa/inet.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
45 #ifndef EAFNOSUPPORT
46 # define EAFNOSUPPORT EINVAL
47 #endif
49 #define NS_IN6ADDRSZ 16
50 #define NS_INT16SZ 2
52 /*
53 * WARNING: Don't even consider trying to compile this on a system where
54 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
55 */
56 typedef int verify_int_size[2 * sizeof (int) - 7];
58 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
59 #if HAVE_IPV6
60 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
61 #endif
64 /* char *
65 * inet_ntop(af, src, dst, size)
66 * convert a network format address to presentation format.
67 * return:
68 * pointer to presentation format address (`dst'), or NULL (see errno).
69 * author:
70 * Paul Vixie, 1996.
71 */
72 const char *
73 inet_ntop (int af, const void *restrict src,
74 char *restrict dst, socklen_t cnt)
75 {
76 switch (af)
77 {
78 #if HAVE_IPV4
79 case AF_INET:
80 return (inet_ntop4 (src, dst, cnt));
81 #endif
83 #if HAVE_IPV6
84 case AF_INET6:
85 return (inet_ntop6 (src, dst, cnt));
86 #endif
88 default:
89 errno = EAFNOSUPPORT;
90 return (NULL);
91 }
92 /* NOTREACHED */
93 }
95 /* const char *
96 * inet_ntop4(src, dst, size)
97 * format an IPv4 address
98 * return:
99 * `dst' (as a const)
100 * notes:
101 * (1) uses no statics
102 * (2) takes a u_char* not an in_addr as input
103 * author:
104 * Paul Vixie, 1996.
105 */
106 static const char *
107 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
108 {
109 char tmp[sizeof "255.255.255.255"];
110 int len;
112 len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
113 if (len < 0)
114 return NULL;
116 if (len > size)
117 {
118 errno = ENOSPC;
119 return NULL;
120 }
122 return strcpy (dst, tmp);
123 }
125 #if HAVE_IPV6
127 /* const char *
128 * inet_ntop6(src, dst, size)
129 * convert IPv6 binary address into presentation (printable) format
130 * author:
131 * Paul Vixie, 1996.
132 */
133 static const char *
134 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
135 {
136 /*
137 * Note that int32_t and int16_t need only be "at least" large enough
138 * to contain a value of the specified size. On some systems, like
139 * Crays, there is no such thing as an integer variable with 16 bits.
140 * Keep this in mind if you think this function should have been coded
141 * to use pointer overlays. All the world's not a VAX.
142 */
143 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
144 struct
145 {
146 int base, len;
147 } best, cur;
148 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
149 int i;
151 /*
152 * Preprocess:
153 * Copy the input (bytewise) array into a wordwise array.
154 * Find the longest run of 0x00's in src[] for :: shorthanding.
155 */
156 memset (words, '\0', sizeof words);
157 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
158 words[i / 2] = (src[i] << 8) | src[i + 1];
159 best.base = -1;
160 cur.base = -1;
161 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
162 {
163 if (words[i] == 0)
164 {
165 if (cur.base == -1)
166 cur.base = i, cur.len = 1;
167 else
168 cur.len++;
169 }
170 else
171 {
172 if (cur.base != -1)
173 {
174 if (best.base == -1 || cur.len > best.len)
175 best = cur;
176 cur.base = -1;
177 }
178 }
179 }
180 if (cur.base != -1)
181 {
182 if (best.base == -1 || cur.len > best.len)
183 best = cur;
184 }
185 if (best.base != -1 && best.len < 2)
186 best.base = -1;
188 /*
189 * Format the result.
190 */
191 tp = tmp;
192 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
193 {
194 /* Are we inside the best run of 0x00's? */
195 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
196 {
197 if (i == best.base)
198 *tp++ = ':';
199 continue;
200 }
201 /* Are we following an initial run of 0x00s or any real hex? */
202 if (i != 0)
203 *tp++ = ':';
204 /* Is this address an encapsulated IPv4? */
205 if (i == 6 && best.base == 0 &&
206 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
207 {
208 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
209 return (NULL);
210 tp += strlen (tp);
211 break;
212 }
213 {
214 int len = sprintf (tp, "%x", words[i]);
215 if (len < 0)
216 return NULL;
217 tp += len;
218 }
219 }
220 /* Was it a trailing run of 0x00's? */
221 if (best.base != -1 && (best.base + best.len) ==
222 (NS_IN6ADDRSZ / NS_INT16SZ))
223 *tp++ = ':';
224 *tp++ = '\0';
226 /*
227 * Check for overflow, copy, and we're done.
228 */
229 if ((socklen_t) (tp - tmp) > size)
230 {
231 errno = ENOSPC;
232 return NULL;
233 }
235 return strcpy (dst, tmp);
236 }
238 #endif