annotate lib/inet_pton.c @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
1 /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 6856
diff changeset
2
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16235
diff changeset
3 Copyright (C) 2006, 2008-2013 Free Software Foundation, Inc.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
5 This program is free software: you can redistribute it and/or modify
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
8 (at your option) any later version.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
9
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
13 GNU General Public License for more details.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
14
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7302
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
17
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
18 /*
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
19 * Copyright (c) 1996,1999 by Internet Software Consortium.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
20 *
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
21 * Permission to use, copy, modify, and distribute this software for any
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
22 * purpose with or without fee is hereby granted, provided that the above
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
23 * copyright notice and this permission notice appear in all copies.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
24 *
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
25 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
26 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
28 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
29 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
30 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
31 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
32 * SOFTWARE.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
33 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
34
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 6856
diff changeset
35 #include <config.h>
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
36
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
37 /* Specification. */
9993
4d5ba95a0dec Move inet_ntop and inet_pton declarations to arpa/inet.h.
Simon Josefsson <simon@josefsson.org>
parents: 9309
diff changeset
38 #include <arpa/inet.h>
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
39
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
40 #if HAVE_DECL_INET_PTON
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
41
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
42 # undef inet_pton
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
43
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
44 int
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
45 rpl_inet_pton (int af, const char *restrict src, void *restrict dst)
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
46 {
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
47 return inet_pton (af, src, dst);
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
48 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
49
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
50 #else
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
51
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
52 # include <c-ctype.h>
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
53 # include <string.h>
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
54 # include <errno.h>
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
55
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
56 # define NS_INADDRSZ 4
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
57 # define NS_IN6ADDRSZ 16
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
58 # define NS_INT16SZ 2
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
59
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
60 /*
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
61 * WARNING: Don't even consider trying to compile this on a system where
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
62 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
63 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
64
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
65 static int inet_pton4 (const char *src, unsigned char *dst);
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
66 # if HAVE_IPV6
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
67 static int inet_pton6 (const char *src, unsigned char *dst);
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
68 # endif
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
69
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
70 /* int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
71 * inet_pton(af, src, dst)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
72 * convert from presentation format (which usually means ASCII printable)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
73 * to network format (which is usually some kind of binary format).
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
74 * return:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
75 * 1 if the address was valid for the specified address family
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
76 * 0 if the address wasn't valid ('dst' is untouched in this case)
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
77 * -1 if some other error occurred ('dst' is untouched in this case, too)
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.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.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
80 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
81 int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
82 inet_pton (int af, const char *restrict src, void *restrict dst)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
83 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
84 switch (af)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
85 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
86 case AF_INET:
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
87 return (inet_pton4 (src, dst));
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
88
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
89 # if HAVE_IPV6
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
90 case AF_INET6:
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
91 return (inet_pton6 (src, dst));
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
92 # endif
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
93
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
94 default:
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
95 errno = EAFNOSUPPORT;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
96 return (-1);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
97 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
98 /* NOTREACHED */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
99 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
100
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
101 /* int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
102 * inet_pton4(src, dst)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
103 * like inet_aton() but without all the hexadecimal, octal (with the
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
104 * exception of 0) and shorthand.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
105 * return:
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
106 * 1 if 'src' is a valid dotted quad, else 0.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
107 * notice:
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
108 * does not touch 'dst' unless it's returning 1.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
109 * author:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
110 * Paul Vixie, 1996.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
111 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
112 static int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
113 inet_pton4 (const char *restrict src, unsigned char *restrict dst)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
114 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
115 int saw_digit, octets, ch;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
116 unsigned char tmp[NS_INADDRSZ], *tp;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
117
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
118 saw_digit = 0;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
119 octets = 0;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
120 *(tp = tmp) = 0;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
121 while ((ch = *src++) != '\0')
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
122 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
123
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
124 if (ch >= '0' && ch <= '9')
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
125 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
126 unsigned new = *tp * 10 + (ch - '0');
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
127
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
128 if (saw_digit && *tp == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
129 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
130 if (new > 255)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
131 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
132 *tp = new;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
133 if (!saw_digit)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
134 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
135 if (++octets > 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
136 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
137 saw_digit = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
138 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
139 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
140 else if (ch == '.' && saw_digit)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
141 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
142 if (octets == 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
143 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
144 *++tp = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
145 saw_digit = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
146 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
147 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
148 return (0);
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
149 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
150 if (octets < 4)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
151 return (0);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
152 memcpy (dst, tmp, NS_INADDRSZ);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
153 return (1);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
154 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
155
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
156 # if HAVE_IPV6
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
157
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
158 /* int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
159 * inet_pton6(src, dst)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
160 * convert presentation level address to network order binary form.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
161 * return:
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
162 * 1 if 'src' is a valid [RFC1884 2.2] address, else 0.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
163 * notice:
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
164 * (1) does not touch 'dst' unless it's returning 1.
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
165 * (2) :: in a full address is silently ignored.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
166 * credit:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
167 * inspired by Mark Andrews.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
168 * author:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
169 * Paul Vixie, 1996.
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
170 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
171 static int
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
172 inet_pton6 (const char *restrict src, unsigned char *restrict dst)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
173 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
174 static const char xdigits[] = "0123456789abcdef";
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
175 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
176 const char *curtok;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
177 int ch, saw_xdigit;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
178 unsigned val;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
179
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
180 tp = memset (tmp, '\0', NS_IN6ADDRSZ);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
181 endp = tp + NS_IN6ADDRSZ;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
182 colonp = NULL;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
183 /* Leading :: requires some special handling. */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
184 if (*src == ':')
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
185 if (*++src != ':')
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
186 return (0);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
187 curtok = src;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
188 saw_xdigit = 0;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
189 val = 0;
10309
5585c3effb24 inet_pton.c: use locale-independent tolower
Jim Meyering <meyering@redhat.com>
parents: 9993
diff changeset
190 while ((ch = c_tolower (*src++)) != '\0')
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
191 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
192 const char *pch;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
193
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
194 pch = strchr (xdigits, ch);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
195 if (pch != NULL)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
196 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
197 val <<= 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
198 val |= (pch - xdigits);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
199 if (val > 0xffff)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
200 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
201 saw_xdigit = 1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
202 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
203 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
204 if (ch == ':')
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 curtok = src;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
207 if (!saw_xdigit)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
208 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
209 if (colonp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
210 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
211 colonp = tp;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
212 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
213 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
214 else if (*src == '\0')
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
215 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
216 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
217 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
218 if (tp + NS_INT16SZ > endp)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
219 return (0);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
220 *tp++ = (u_char) (val >> 8) & 0xff;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
221 *tp++ = (u_char) val & 0xff;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
222 saw_xdigit = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
223 val = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
224 continue;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
225 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
226 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
227 inet_pton4 (curtok, tp) > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
228 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
229 tp += NS_INADDRSZ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
230 saw_xdigit = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
231 break; /* '\0' was seen by inet_pton4(). */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
232 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
233 return (0);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
234 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
235 if (saw_xdigit)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
236 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
237 if (tp + NS_INT16SZ > endp)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
238 return (0);
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
239 *tp++ = (u_char) (val >> 8) & 0xff;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
240 *tp++ = (u_char) val & 0xff;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
241 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
242 if (colonp != NULL)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
243 {
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
244 /*
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
245 * Since some memmove()'s erroneously fail to handle
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
246 * overlapping regions, we'll do the shift by hand.
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
247 */
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
248 const int n = tp - colonp;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
249 int i;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
250
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
251 if (tp == endp)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
252 return (0);
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
253 for (i = 1; i <= n; i++)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
254 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
255 endp[-i] = colonp[n - i];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
256 colonp[n - i] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11941
diff changeset
257 }
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
258 tp = endp;
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
259 }
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
260 if (tp != endp)
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
261 return (0);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
262 memcpy (dst, tmp, NS_IN6ADDRSZ);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
263 return (1);
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
264 }
15634
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
265
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
266 # endif
bcecbef40a05 inet_pton: Support for MSVC on Windows Vista or newer.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
267
6849
036354241030 Add inet_pton module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
268 #endif