annotate lib/inet_ntop.c @ 6841:b2c0fe3ad032

Revert slightly, from Bruno.
author Simon Josefsson <simon@josefsson.org>
date Mon, 19 Jun 2006 12:28:18 +0000
parents 5eb20b30a115
children 8a1a9361108c
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
6838
5eb20b30a115 2006-06-19 Simon Josefsson <jas@extundo.com>
Simon Josefsson <simon@josefsson.org>
parents: 6435
diff changeset
2 Copyright (c) 2005, 2006 Free Software Foundation, Inc.
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
3
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
4 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
5 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
6 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
7 any later version.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
8
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
9 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
10 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
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
12 GNU General Public License for more details.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
13
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
14 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
15 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
16 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
17
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 * Copyright (c) 1996-1999 by Internet Software Consortium.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 * Permission to use, copy, modify, and distribute this software for any
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 * 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
23 * copyright notice and this permission notice appear in all copies.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 * 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
30 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 * 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
32 * SOFTWARE.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34
6259
96c32553b4c6 Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5825
diff changeset
35 #ifdef HAVE_CONFIG_H
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 # include <config.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #endif
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 /* Specification. */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "inet_ntop.h"
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 #include <stdio.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #include <string.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #include <errno.h>
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
46 #ifndef EAFNOSUPPORT
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
47 # define EAFNOSUPPORT EINVAL
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
48 #endif
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
49
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #define NS_IN6ADDRSZ 16
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 #define NS_INT16SZ 2
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 /*
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 * 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
55 * 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
56 */
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
57 typedef int verify_int_size[2 * sizeof (int) - 7];
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 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
60 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 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
62 #endif
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
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 /* char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 * inet_ntop(af, src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 * convert a network format address to presentation format.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 * pointer to presentation format address (`dst'), or NULL (see errno).
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 * Paul Vixie, 1996.
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 const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
74 inet_ntop (int af, const void *restrict src,
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
75 char *restrict dst, socklen_t cnt)
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
77 switch (af)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
78 {
6841
b2c0fe3ad032 Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents: 6838
diff changeset
79 #if HAVE_IPV4
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
80 case AF_INET:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
81 return (inet_ntop4 (src, dst, cnt));
6841
b2c0fe3ad032 Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents: 6838
diff changeset
82 #endif
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 #if HAVE_IPV6
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
85 case AF_INET6:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
86 return (inet_ntop6 (src, dst, cnt));
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 #endif
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
88
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
89 default:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
90 errno = EAFNOSUPPORT;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
91 return (NULL);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
92 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
93 /* NOTREACHED */
5825
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
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 * inet_ntop4(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 * format an IPv4 address
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 * return:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 * `dst' (as a const)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 * notes:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 * (1) uses no statics
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103 * (2) takes a u_char* not an in_addr as input
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
108 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
109 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
110 char tmp[sizeof "255.255.255.255"];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
111 int len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
112
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
113 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
114 if (len < 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
115 return NULL;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
117 if (len > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
118 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
119 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
120 return NULL;
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
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
123 return strcpy (dst, tmp);
5825
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
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 #if HAVE_IPV6
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 /* const char *
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 * inet_ntop6(src, dst, size)
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 * convert IPv6 binary address into presentation (printable) format
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 * author:
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 * Paul Vixie, 1996.
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 */
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 static const char *
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
135 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
136 {
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
137 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
138 * 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
139 * 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
140 * 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
141 * 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
142 * 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
143 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
144 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
145 struct
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
146 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
147 int base, len;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
148 } best, cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
149 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
150 int i;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
152 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
153 * Preprocess:
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
154 * Copy the input (bytewise) array into a wordwise array.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
155 * 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
156 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
157 memset (words, '\0', sizeof words);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
158 for (i = 0; i < NS_IN6ADDRSZ; i += 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
159 words[i / 2] = (src[i] << 8) | src[i + 1];
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
160 best.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
161 cur.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
162 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
163 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
164 if (words[i] == 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
165 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
166 if (cur.base == -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
167 cur.base = i, cur.len = 1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
168 else
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
169 cur.len++;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
170 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
171 else
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
172 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
173 if (cur.base != -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
174 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
175 if (best.base == -1 || cur.len > best.len)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
176 best = cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
177 cur.base = -1;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
178 }
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
180 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
181 if (cur.base != -1)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
182 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
183 if (best.base == -1 || cur.len > best.len)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
184 best = cur;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
185 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
186 if (best.base != -1 && best.len < 2)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
187 best.base = -1;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
189 /*
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
190 * Format the result.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
191 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
192 tp = tmp;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
193 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
194 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
195 /* Are we inside the best run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
196 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
197 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
198 if (i == best.base)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
199 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
200 continue;
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 }
6435
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
202 /* 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
203 if (i != 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
204 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
205 /* Is this address an encapsulated IPv4? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
206 if (i == 6 && best.base == 0 &&
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
207 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
208 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
209 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
210 return (NULL);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
211 tp += strlen (tp);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
212 break;
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 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
215 int len = sprintf (tp, "%x", words[i]);
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
216 if (len < 0)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
217 return NULL;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
218 tp += len;
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 }
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
221 /* Was it a trailing run of 0x00's? */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
222 if (best.base != -1 && (best.base + best.len) ==
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
223 (NS_IN6ADDRSZ / NS_INT16SZ))
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
224 *tp++ = ':';
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
225 *tp++ = '\0';
5825
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226
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 * Check for overflow, copy, and we're done.
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
229 */
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
230 if ((socklen_t) (tp - tmp) > size)
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
231 {
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
232 errno = ENOSPC;
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
233 return NULL;
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
be00741570b7 Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents: 6259
diff changeset
236 return strcpy (dst, tmp);
5825
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
96441be32be1 New module 'inet_ntop'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 #endif