annotate lib/inet_ntop.c @ 15633:c2c292001fc0

inet_ntop: Support for MSVC on Windows Vista or newer. * lib/arpa_inet.in.h (inet_ntop): Also consider REPLACE_INET_NTOP. * lib/inet_ntop.c (rpl_inet_ntop): Use a simple wrapper if HAVE_DECL_INET_NTOP is defined. * m4/inet_ntop.m4 (gl_FUNC_INET_NTOP): Invoke gl_PREREQ_SYS_H_WINSOCK2. On platforms with <winsock2.h>, test whether inet_ntop is declared in <ws2tcpip.h>. If so, arrange to replace it. * m4/arpa_inet_h.m4 (gl_ARPA_INET_H_DEFAULTS): Initialize REPLACE_INET_NTOP. * modules/arpa_inet (Makefile.am): Substitute REPLACE_INET_NTOP. * modules/inet_ntop (Files): Add m4/sys_socket_h.m4. (Depends-on, configure.ac): Update condition. * doc/posix-functions/inet_ntop.texi: Mention the MSVC problem.
author Bruno Haible <bruno@clisp.org>
date Sat, 17 Sep 2011 14:56:11 +0200
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
1 /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 6841
diff changeset
2
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13787
diff changeset
3 Copyright (C) 2005-2006, 2008-2011 Free Software Foundation, Inc.
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
4
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
5 This program is free software; you can redistribute it and/or modify
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
6 it under the terms of the GNU General Public License as published by
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
8 any later version.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
9
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
10 This program is distributed in the hope that it will be useful,
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
13 GNU General Public License for more details.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
14
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
15 You should have received a copy of the GNU General Public License
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
18
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 * Copyright (c) 1996-1999 by Internet Software Consortium.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 * Permission to use, copy, modify, and distribute this software for any
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 * purpose with or without fee is hereby granted, provided that the above
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 * copyright notice and this permission notice appear in all copies.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 * SOFTWARE.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 6841
diff changeset
36 #include <config.h>
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* Specification. */
9993
4d5ba95a0dec Move inet_ntop and inet_pton declarations to arpa/inet.h.
Simon Josefsson <simon@josefsson.org>
parents: 7302
diff changeset
39 #include <arpa/inet.h>
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41 #if HAVE_DECL_INET_NTOP
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
42
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43 # undef inet_ntop
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 const char *
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
46 rpl_inet_ntop (int af, const void *restrict src,
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
47 char *restrict dst, socklen_t cnt)
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
48 {
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
49 return inet_ntop (af, src, dst, cnt);
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
50 }
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
51
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
52 #else
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
53
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
54 # include <stdio.h>
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
55 # include <string.h>
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
56 # include <errno.h>
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
57
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
58 # define NS_IN6ADDRSZ 16
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
59 # define NS_INT16SZ 2
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 * WARNING: Don't even consider trying to compile this on a system where
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 */
13787
f4b0b520e409 prefer (X ? 1 : -1) when converting from boolean (1,0) to int (1,-1)
Paul Eggert <eggert@cs.ucla.edu>
parents: 12559
diff changeset
65 typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
68 # if HAVE_IPV6
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
70 # endif
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 /* char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 * inet_ntop(af, src, dst, size)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
75 * convert a network format address to presentation format.
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 * return:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
77 * pointer to presentation format address (`dst'), or NULL (see errno).
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 * author:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
79 * Paul Vixie, 1996.
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
82 inet_ntop (int af, const void *restrict src,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
83 char *restrict dst, socklen_t cnt)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
85 switch (af)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
86 {
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
87 # if HAVE_IPV4
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
88 case AF_INET:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
89 return (inet_ntop4 (src, dst, cnt));
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
90 # endif
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
92 # if HAVE_IPV6
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
93 case AF_INET6:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
94 return (inet_ntop6 (src, dst, cnt));
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
95 # endif
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
96
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
97 default:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
98 errno = EAFNOSUPPORT;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
99 return (NULL);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
100 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
101 /* NOTREACHED */
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 * inet_ntop4(src, dst, size)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
106 * format an IPv4 address
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 * return:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
108 * `dst' (as a const)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 * notes:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
110 * (1) uses no statics
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
111 * (2) takes a u_char* not an in_addr as input
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 * author:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
113 * Paul Vixie, 1996.
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
116 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
118 char tmp[sizeof "255.255.255.255"];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
119 int len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
120
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
121 len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
122 if (len < 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
123 return NULL;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
125 if (len > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
126 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
127 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
128 return NULL;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
129 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
130
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
131 return strcpy (dst, tmp);
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
134 # if HAVE_IPV6
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 * inet_ntop6(src, dst, size)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
138 * convert IPv6 binary address into presentation (printable) format
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 * author:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
140 * Paul Vixie, 1996.
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
143 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
145 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
146 * Note that int32_t and int16_t need only be "at least" large enough
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
147 * to contain a value of the specified size. On some systems, like
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
148 * Crays, there is no such thing as an integer variable with 16 bits.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
149 * Keep this in mind if you think this function should have been coded
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
150 * to use pointer overlays. All the world's not a VAX.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
151 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
152 char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
153 struct
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
154 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
155 int base, len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
156 } best, cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
157 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
158 int i;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
160 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
161 * Preprocess:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
162 * Copy the input (bytewise) array into a wordwise array.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
163 * Find the longest run of 0x00's in src[] for :: shorthanding.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
164 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
165 memset (words, '\0', sizeof words);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
166 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
167 words[i / 2] = (src[i] << 8) | src[i + 1];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
168 best.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
169 cur.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
170 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
171 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
172 if (words[i] == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
173 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
174 if (cur.base == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
175 cur.base = i, cur.len = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
176 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
177 cur.len++;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
178 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
179 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
180 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
181 if (cur.base != -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
182 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
183 if (best.base == -1 || cur.len > best.len)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
184 best = cur;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
185 cur.base = -1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
186 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
187 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
188 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
189 if (cur.base != -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
190 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
191 if (best.base == -1 || cur.len > best.len)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
192 best = cur;
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
193 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
194 if (best.base != -1 && best.len < 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
195 best.base = -1;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
197 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
198 * Format the result.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
199 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
200 tp = tmp;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
201 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
202 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
203 /* Are we inside the best run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
204 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
205 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
206 if (i == best.base)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
207 *tp++ = ':';
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
208 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
209 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
210 /* Are we following an initial run of 0x00s or any real hex? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
211 if (i != 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
212 *tp++ = ':';
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
213 /* Is this address an encapsulated IPv4? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
214 if (i == 6 && best.base == 0 &&
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
215 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
216 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
217 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
218 return (NULL);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
219 tp += strlen (tp);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
220 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
221 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
222 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
223 int len = sprintf (tp, "%x", words[i]);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
224 if (len < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
225 return NULL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
226 tp += len;
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
227 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
228 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
229 /* Was it a trailing run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
230 if (best.base != -1 && (best.base + best.len) ==
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
231 (NS_IN6ADDRSZ / NS_INT16SZ))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
232 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
233 *tp++ = '\0';
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
235 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
236 * Check for overflow, copy, and we're done.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
237 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
238 if ((socklen_t) (tp - tmp) > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
239 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
240 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
241 return NULL;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
242 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
243
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
244 return strcpy (dst, tmp);
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246
15633
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
247 # endif
c2c292001fc0 inet_ntop: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
248
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 #endif