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)
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. */
20 * Copyright (c) 1996-1999 by Internet Software Consortium.
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.
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
39 #include <arpa/inet.h>
46 # define EAFNOSUPPORT EINVAL
49 #define NS_IN6ADDRSZ 16
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.
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
);
60 static const char *inet_ntop6 (const unsigned char *src
, char *dst
, socklen_t size
);
65 * inet_ntop(af, src, dst, size)
66 * convert a network format address to presentation format.
68 * pointer to presentation format address (`dst'), or NULL (see errno).
73 inet_ntop (int af
, const void *restrict src
,
74 char *restrict dst
, socklen_t cnt
)
80 return (inet_ntop4 (src
, dst
, cnt
));
85 return (inet_ntop6 (src
, dst
, cnt
));
96 * inet_ntop4(src, dst, size)
97 * format an IPv4 address
101 * (1) uses no statics
102 * (2) takes a u_char* not an in_addr as input
107 inet_ntop4 (const unsigned char *src
, char *dst
, socklen_t size
)
109 char tmp
[sizeof "255.255.255.255"];
112 len
= sprintf (tmp
, "%u.%u.%u.%u", src
[0], src
[1], src
[2], src
[3]);
122 return strcpy (dst
, tmp
);
128 * inet_ntop6(src, dst, size)
129 * convert IPv6 binary address into presentation (printable) format
134 inet_ntop6 (const unsigned char *src
, char *dst
, socklen_t size
)
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.
143 char tmp
[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp
;
148 unsigned int words
[NS_IN6ADDRSZ
/ NS_INT16SZ
];
153 * Copy the input (bytewise) array into a wordwise array.
154 * Find the longest run of 0x00's in src[] for :: shorthanding.
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];
161 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++)
166 cur
.base
= i
, cur
.len
= 1;
174 if (best
.base
== -1 || cur
.len
> best
.len
)
182 if (best
.base
== -1 || cur
.len
> best
.len
)
185 if (best
.base
!= -1 && best
.len
< 2)
192 for (i
= 0; i
< (NS_IN6ADDRSZ
/ NS_INT16SZ
); i
++)
194 /* Are we inside the best run of 0x00's? */
195 if (best
.base
!= -1 && i
>= best
.base
&& i
< (best
.base
+ best
.len
))
201 /* Are we following an initial run of 0x00s or any real hex? */
204 /* Is this address an encapsulated IPv4? */
205 if (i
== 6 && best
.base
== 0 &&
206 (best
.len
== 6 || (best
.len
== 5 && words
[5] == 0xffff)))
208 if (!inet_ntop4 (src
+ 12, tp
, sizeof tmp
- (tp
- tmp
)))
214 int len
= sprintf (tp
, "%x", words
[i
]);
220 /* Was it a trailing run of 0x00's? */
221 if (best
.base
!= -1 && (best
.base
+ best
.len
) ==
222 (NS_IN6ADDRSZ
/ NS_INT16SZ
))
227 * Check for overflow, copy, and we're done.
229 if ((socklen_t
) (tp
- tmp
) > size
)
235 return strcpy (dst
, tmp
);