Mercurial > hg > octave-shane > gnulib-hg
annotate lib/inet_ntop.c @ 12559:c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Use the same procedure as for 2009, outlined in
http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/20081
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Fri, 01 Jan 2010 10:31:12 +0100 |
parents | e8d2c6fc33ad |
children | f4b0b520e409 |
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 |
12559
c2cbabec01dd
update nearly all FSF copyright year lists to include 2010
Jim Meyering <meyering@redhat.com>
parents:
12421
diff
changeset
|
3 Copyright (C) 2005-2006, 2008-2010 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 | 19 /* |
20 * Copyright (c) 1996-1999 by Internet Software Consortium. | |
21 * | |
22 * Permission to use, copy, modify, and distribute this software for any | |
23 * purpose with or without fee is hereby granted, provided that the above | |
24 * copyright notice and this permission notice appear in all copies. | |
25 * | |
26 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
27 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
28 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
29 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
30 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
31 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
32 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
33 * SOFTWARE. | |
34 */ | |
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 | 37 |
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 | 40 |
41 #include <stdio.h> | |
42 #include <string.h> | |
43 #include <errno.h> | |
44 | |
45 #define NS_IN6ADDRSZ 16 | |
46 #define NS_INT16SZ 2 | |
47 | |
48 /* | |
49 * WARNING: Don't even consider trying to compile this on a system where | |
50 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | |
51 */ | |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
52 typedef int verify_int_size[2 * sizeof (int) - 7]; |
5825 | 53 |
54 static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size); | |
55 #if HAVE_IPV6 | |
56 static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size); | |
57 #endif | |
58 | |
59 | |
60 /* char * | |
61 * inet_ntop(af, src, dst, size) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
62 * convert a network format address to presentation format. |
5825 | 63 * return: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
64 * pointer to presentation format address (`dst'), or NULL (see errno). |
5825 | 65 * author: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
66 * Paul Vixie, 1996. |
5825 | 67 */ |
68 const char * | |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
69 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
|
70 char *restrict dst, socklen_t cnt) |
5825 | 71 { |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
72 switch (af) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
73 { |
6841
b2c0fe3ad032
Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents:
6838
diff
changeset
|
74 #if HAVE_IPV4 |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
75 case AF_INET: |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
76 return (inet_ntop4 (src, dst, cnt)); |
6841
b2c0fe3ad032
Revert slightly, from Bruno.
Simon Josefsson <simon@josefsson.org>
parents:
6838
diff
changeset
|
77 #endif |
5825 | 78 |
79 #if HAVE_IPV6 | |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
80 case AF_INET6: |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
81 return (inet_ntop6 (src, dst, cnt)); |
5825 | 82 #endif |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
83 |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
84 default: |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
85 errno = EAFNOSUPPORT; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
86 return (NULL); |
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 /* NOTREACHED */ |
5825 | 89 } |
90 | |
91 /* const char * | |
92 * inet_ntop4(src, dst, size) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
93 * format an IPv4 address |
5825 | 94 * return: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
95 * `dst' (as a const) |
5825 | 96 * notes: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
97 * (1) uses no statics |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
98 * (2) takes a u_char* not an in_addr as input |
5825 | 99 * author: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
100 * Paul Vixie, 1996. |
5825 | 101 */ |
102 static const char * | |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
103 inet_ntop4 (const unsigned char *src, char *dst, socklen_t size) |
5825 | 104 { |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
105 char tmp[sizeof "255.255.255.255"]; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
106 int len; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
107 |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
108 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
|
109 if (len < 0) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
110 return NULL; |
5825 | 111 |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
112 if (len > size) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
113 { |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
114 errno = ENOSPC; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
115 return NULL; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
116 } |
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 return strcpy (dst, tmp); |
5825 | 119 } |
120 | |
121 #if HAVE_IPV6 | |
122 | |
123 /* const char * | |
124 * inet_ntop6(src, dst, size) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
125 * convert IPv6 binary address into presentation (printable) format |
5825 | 126 * author: |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
127 * Paul Vixie, 1996. |
5825 | 128 */ |
129 static const char * | |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
130 inet_ntop6 (const unsigned char *src, char *dst, socklen_t size) |
5825 | 131 { |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
132 /* |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
133 * 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
|
134 * 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
|
135 * 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
|
136 * 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
|
137 * 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
|
138 */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
139 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
|
140 struct |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
141 { |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
142 int base, len; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
143 } best, cur; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
144 unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ]; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
145 int i; |
5825 | 146 |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
147 /* |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
148 * Preprocess: |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
149 * Copy the input (bytewise) array into a wordwise array. |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
150 * 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
|
151 */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
152 memset (words, '\0', sizeof words); |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
153 for (i = 0; i < NS_IN6ADDRSZ; i += 2) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
154 words[i / 2] = (src[i] << 8) | src[i + 1]; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
155 best.base = -1; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
156 cur.base = -1; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
157 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
158 { |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
159 if (words[i] == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
160 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
161 if (cur.base == -1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
162 cur.base = i, cur.len = 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
163 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
164 cur.len++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
165 } |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
166 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
167 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
168 if (cur.base != -1) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
169 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
170 if (best.base == -1 || cur.len > best.len) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
171 best = cur; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
172 cur.base = -1; |
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 } |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
175 } |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
176 if (cur.base != -1) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
177 { |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
178 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
|
179 best = cur; |
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 (best.base != -1 && best.len < 2) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
182 best.base = -1; |
5825 | 183 |
6435
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 * Format the result. |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
186 */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
187 tp = tmp; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
188 for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) |
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 /* Are we inside the best run of 0x00's? */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
191 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
|
192 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
193 if (i == best.base) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
194 *tp++ = ':'; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
195 continue; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
196 } |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
197 /* 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
|
198 if (i != 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
199 *tp++ = ':'; |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
200 /* Is this address an encapsulated IPv4? */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
201 if (i == 6 && best.base == 0 && |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
202 (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
|
203 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
204 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
|
205 return (NULL); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
206 tp += strlen (tp); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
207 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
208 } |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
209 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
210 int len = sprintf (tp, "%x", words[i]); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
211 if (len < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
212 return NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
213 tp += len; |
6435
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 } |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
216 /* Was it a trailing run of 0x00's? */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
217 if (best.base != -1 && (best.base + best.len) == |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
218 (NS_IN6ADDRSZ / NS_INT16SZ)) |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
219 *tp++ = ':'; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
220 *tp++ = '\0'; |
5825 | 221 |
6435
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
222 /* |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
223 * Check for overflow, copy, and we're done. |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
224 */ |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
225 if ((socklen_t) (tp - tmp) > size) |
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 errno = ENOSPC; |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
228 return NULL; |
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 |
be00741570b7
Fix inet_ntop on mingw32.
Simon Josefsson <simon@josefsson.org>
parents:
6259
diff
changeset
|
231 return strcpy (dst, tmp); |
5825 | 232 } |
233 | |
234 #endif |