]> git.ipfire.org Git - thirdparty/glibc.git/blame - resolv/inet_pton.c
inet: Add IPv6 getaddrinfo coverage to tst-inet6_scopeid_pton.c
[thirdparty/glibc.git] / resolv / inet_pton.c
CommitLineData
e685e07d
UD
1/*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
fa0bc87c
RM
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
16 */
17
fa0bc87c
RM
18#include <sys/param.h>
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <arpa/nameser.h>
8a40ed68 24#include <ctype.h>
fa0bc87c
RM
25#include <string.h>
26#include <errno.h>
fa0bc87c 27
d53b8652
FW
28static int inet_pton4 (const char *src, unsigned char *dst);
29static int inet_pton6 (const char *src, unsigned char *dst);
fa0bc87c 30
d53b8652
FW
31/* Convert from presentation format (which usually means ASCII printable)
32 * to network format (which is usually some kind of binary format).
33 *
fa0bc87c 34 * return:
d53b8652
FW
35 * 1 if the address was valid for the specified address family
36 * 0 if the address wasn't valid (`dst' is untouched in this case)
37 * -1 if some other error occurred (`dst' is untouched in this case, too)
fa0bc87c 38 * author:
d53b8652 39 * Paul Vixie, 1996.
fa0bc87c
RM
40 */
41int
9d46370c 42__inet_pton (int af, const char *src, void *dst)
fa0bc87c 43{
d53b8652
FW
44 switch (af)
45 {
46 case AF_INET:
47 return inet_pton4 (src, dst);
48 case AF_INET6:
49 return inet_pton6 (src, dst);
50 default:
51 __set_errno (EAFNOSUPPORT);
52 return -1;
53 }
fa0bc87c 54}
8ac5a76a
JM
55libc_hidden_def (__inet_pton)
56weak_alias (__inet_pton, inet_pton)
57libc_hidden_weak (inet_pton)
fa0bc87c 58
d53b8652
FW
59/* Like inet_atonbut without all the hexadecimal, octal and shorthand.
60 *
fa0bc87c 61 * return:
d53b8652 62 * 1 if `src' is a valid dotted quad, else 0.
fa0bc87c 63 * notice:
d53b8652 64 * does not touch `dst' unless it's returning 1.
fa0bc87c 65 * author:
d53b8652 66 * Paul Vixie, 1996.
fa0bc87c
RM
67 */
68static int
d53b8652 69inet_pton4 (const char *src, unsigned char *dst)
fa0bc87c 70{
d53b8652
FW
71 int saw_digit, octets, ch;
72 unsigned char tmp[NS_INADDRSZ], *tp;
fa0bc87c 73
d53b8652
FW
74 saw_digit = 0;
75 octets = 0;
76 *(tp = tmp) = 0;
77 while ((ch = *src++) != '\0')
78 {
79 if (ch >= '0' && ch <= '9')
80 {
81 unsigned int new = *tp * 10 + (ch - '0');
fa0bc87c 82
d53b8652
FW
83 if (saw_digit && *tp == 0)
84 return 0;
85 if (new > 255)
86 return 0;
87 *tp = new;
88 if (! saw_digit)
89 {
90 if (++octets > 4)
91 return 0;
92 saw_digit = 1;
93 }
94 }
95 else if (ch == '.' && saw_digit)
96 {
97 if (octets == 4)
98 return 0;
99 *++tp = 0;
100 saw_digit = 0;
101 }
102 else
103 return 0;
104 }
105 if (octets < 4)
106 return 0;
107 memcpy (dst, tmp, NS_INADDRSZ);
108 return 1;
fa0bc87c
RM
109}
110
d53b8652
FW
111/* Cconvert presentation level address to network order binary form.
112 *
fa0bc87c 113 * return:
d53b8652 114 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
fa0bc87c 115 * notice:
d53b8652
FW
116 * (1) does not touch `dst' unless it's returning 1.
117 * (2) :: in a full address is silently ignored.
fa0bc87c 118 * credit:
d53b8652 119 * inspired by Mark Andrews.
fa0bc87c 120 * author:
d53b8652 121 * Paul Vixie, 1996.
fa0bc87c
RM
122 */
123static int
d53b8652 124inet_pton6 (const char *src, unsigned char *dst)
fa0bc87c 125{
d53b8652
FW
126 static const char xdigits[] = "0123456789abcdef";
127 unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
128 const char *curtok;
129 int ch, saw_xdigit;
130 unsigned int val;
fa0bc87c 131
d53b8652
FW
132 tp = memset (tmp, '\0', NS_IN6ADDRSZ);
133 endp = tp + NS_IN6ADDRSZ;
134 colonp = NULL;
135 /* Leading :: requires some special handling. */
136 if (*src == ':')
137 if (*++src != ':')
138 return 0;
139 curtok = src;
140 saw_xdigit = 0;
141 val = 0;
142 while ((ch = tolower (*src++)) != '\0')
143 {
144 const char *pch;
fa0bc87c 145
d53b8652
FW
146 pch = strchr (xdigits, ch);
147 if (pch != NULL)
148 {
149 val <<= 4;
150 val |= (pch - xdigits);
151 if (val > 0xffff)
152 return 0;
153 saw_xdigit = 1;
154 continue;
155 }
156 if (ch == ':')
157 {
158 curtok = src;
159 if (!saw_xdigit)
160 {
161 if (colonp)
162 return 0;
163 colonp = tp;
164 continue;
165 }
166 else if (*src == '\0')
167 {
168 return 0;
169 }
170 if (tp + NS_INT16SZ > endp)
171 return 0;
172 *tp++ = (unsigned char) (val >> 8) & 0xff;
173 *tp++ = (unsigned char) val & 0xff;
174 saw_xdigit = 0;
175 val = 0;
176 continue;
fa0bc87c 177 }
d53b8652
FW
178 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
179 inet_pton4 (curtok, tp) > 0)
180 {
181 tp += NS_INADDRSZ;
182 saw_xdigit = 0;
183 break; /* '\0' was seen by inet_pton4. */
fa0bc87c 184 }
d53b8652
FW
185 return 0;
186 }
187 if (saw_xdigit)
188 {
189 if (tp + NS_INT16SZ > endp)
190 return 0;
191 *tp++ = (unsigned char) (val >> 8) & 0xff;
192 *tp++ = (unsigned char) val & 0xff;
193 }
194 if (colonp != NULL)
195 {
196 /*
197 * Since some memmove's erroneously fail to handle
198 * overlapping regions, we'll do the shift by hand.
199 */
200 const int n = tp - colonp;
201 int i;
fa0bc87c 202
d53b8652
FW
203 if (tp == endp)
204 return 0;
205 for (i = 1; i <= n; i++)
206 {
207 endp[- i] = colonp[n - i];
208 colonp[n - i] = 0;
fa0bc87c 209 }
d53b8652
FW
210 tp = endp;
211 }
212 if (tp != endp)
213 return 0;
214 memcpy (dst, tmp, NS_IN6ADDRSZ);
215 return 1;
fa0bc87c 216}