annotate lib/inet_ntop.c @ 9993:4d5ba95a0dec

Move inet_ntop and inet_pton declarations to arpa/inet.h.
author Simon Josefsson <simon@josefsson.org>
date Mon, 28 Apr 2008 17:40:25 +0200
parents 8a1a9361108c
children 909daff94315
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
9993
4d5ba95a0dec Move inet_ntop and inet_pton declarations to arpa/inet.h.
Simon Josefsson <simon@josefsson.org>
parents: 7302
diff changeset
3 Copyright (C) 2005, 2006, 2008 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
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include <stdio.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include <string.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #include <errno.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
45 #ifndef EAFNOSUPPORT
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
46 # define EAFNOSUPPORT EINVAL
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
47 #endif
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
48
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 #define NS_IN6ADDRSZ 16
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #define NS_INT16SZ 2
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 * 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
54 * 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
55 */
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
56 typedef int verify_int_size[2 * sizeof (int) - 7];
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 /* char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 * inet_ntop(af, src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 * convert a network format address to presentation format.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 * pointer to presentation format address (`dst'), or NULL (see errno).
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 * Paul Vixie, 1996.
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 const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
73 inet_ntop (int af, const void *restrict src,
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
74 char *restrict dst, socklen_t cnt)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
76 switch (af)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
77 {
6841
b2c0fe3ad032 Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents: 6838
diff changeset
78 #if HAVE_IPV4
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
79 case AF_INET:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
80 return (inet_ntop4 (src, dst, cnt));
6841
b2c0fe3ad032 Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents: 6838
diff changeset
81 #endif
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 #if HAVE_IPV6
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
84 case AF_INET6:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
85 return (inet_ntop6 (src, dst, cnt));
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 #endif
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
87
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
88 default:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
89 errno = EAFNOSUPPORT;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
90 return (NULL);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
91 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
92 /* NOTREACHED */
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 * inet_ntop4(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 * format an IPv4 address
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 * `dst' (as a const)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 * notes:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 * (1) uses no statics
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 * (2) takes a u_char* not an in_addr as input
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
107 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
108 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
109 char tmp[sizeof "255.255.255.255"];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
110 int len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
111
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
112 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
113 if (len < 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
114 return NULL;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
116 if (len > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
117 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
118 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
119 return NULL;
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
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
122 return strcpy (dst, tmp);
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 * inet_ntop6(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 * convert IPv6 binary address into presentation (printable) format
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 * Paul Vixie, 1996.
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 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
134 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
135 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
136 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
137 * 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
138 * 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
139 * 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
140 * 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
141 * 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
142 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
143 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
144 struct
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 int base, len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
147 } best, cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
148 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
149 int i;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150
6435
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 * Preprocess:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
153 * Copy the input (bytewise) array into a wordwise array.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
154 * 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
155 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
156 memset (words, '\0', sizeof words);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
157 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
158 words[i / 2] = (src[i] << 8) | src[i + 1];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
159 best.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
160 cur.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
161 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
162 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
163 if (words[i] == 0)
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 if (cur.base == -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
166 cur.base = i, cur.len = 1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
167 else
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
168 cur.len++;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
169 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
170 else
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 (cur.base != -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
173 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
174 if (best.base == -1 || cur.len > best.len)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
175 best = cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
176 cur.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
177 }
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
179 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
180 if (cur.base != -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
181 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
182 if (best.base == -1 || cur.len > best.len)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
183 best = cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
184 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
185 if (best.base != -1 && best.len < 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
186 best.base = -1;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
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 * Format the result.
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 tp = tmp;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
192 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
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 /* Are we inside the best run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
195 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
196 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
197 if (i == best.base)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
198 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
199 continue;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
201 /* 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
202 if (i != 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
203 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
204 /* Is this address an encapsulated IPv4? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
205 if (i == 6 && best.base == 0 &&
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
206 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
207 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
208 if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
209 return (NULL);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
210 tp += strlen (tp);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
211 break;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
212 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
213 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
214 int len = sprintf (tp, "%x", words[i]);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
215 if (len < 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
216 return NULL;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
217 tp += len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
218 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
219 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
220 /* Was it a trailing run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
221 if (best.base != -1 && (best.base + best.len) ==
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
222 (NS_IN6ADDRSZ / NS_INT16SZ))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
223 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
224 *tp++ = '\0';
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
226 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
227 * Check for overflow, copy, and we're done.
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 if ((socklen_t) (tp - tmp) > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
230 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
231 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
232 return NULL;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
233 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
234
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
235 return strcpy (dst, tmp);
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 }
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 #endif