Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/util/inet_pton.c
1532 views
1
/* $OpenBSD: inet_pton.c,v 1.8 2010/05/06 15:47:14 claudio Exp $ */
2
3
/*
4
* SPDX-License-Identifier: ISC
5
*
6
* Copyright (c) 1996 by Internet Software Consortium.
7
*
8
* Permission to use, copy, modify, and distribute this software for any
9
* purpose with or without fee is hereby granted, provided that the above
10
* copyright notice and this permission notice appear in all copies.
11
*
12
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
13
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
14
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19
* SOFTWARE.
20
*/
21
22
#include <config.h>
23
24
#if !defined(HAVE_INET_PTON)
25
26
#include <sys/types.h>
27
#include <sys/socket.h>
28
#include <netinet/in.h>
29
#include <arpa/inet.h>
30
#include <arpa/nameser.h>
31
#include <string.h>
32
#include <errno.h>
33
34
#include <sudo_compat.h>
35
36
#ifndef EAFNOSUPPORT
37
# define EAFNOSUPPORT EINVAL
38
#endif
39
40
#ifndef NS_INADDRSZ
41
# ifdef INADDRSZ
42
# define NS_INADDRSZ INADDRSZ
43
# else
44
# define NS_INADDRSZ 4
45
# endif
46
#endif
47
#ifndef NS_IN6ADDRSZ
48
# ifdef IN6ADDRSZ
49
# define NS_IN6ADDRSZ IN6ADDRSZ
50
# else
51
# define NS_IN6ADDRSZ 16
52
# endif
53
#endif
54
#ifndef NS_INT16SZ
55
# ifdef INT16SZ
56
# define NS_INT16SZ INT16SZ
57
# else
58
# define NS_INT16SZ 2
59
# endif
60
#endif
61
62
/*
63
* WARNING: Don't even consider trying to compile this on a system where
64
* sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
65
*/
66
67
/* int
68
* inet_pton4(src, dst)
69
* like inet_aton() but without all the hexadecimal and shorthand.
70
* return:
71
* 1 if `src' is a valid dotted quad, else 0.
72
* notice:
73
* does not touch `dst' unless it's returning 1.
74
* author:
75
* Paul Vixie, 1996.
76
*/
77
static int
78
inet_pton4(const char *src, u_char *dst)
79
{
80
const char digits[] = "0123456789";
81
int saw_digit, octets, ch;
82
u_char tmp[NS_INADDRSZ], *tp;
83
84
saw_digit = 0;
85
octets = 0;
86
/* cppcheck-suppress uninitvar */
87
*(tp = tmp) = '\0';
88
while ((ch = (unsigned char)*src++) != '\0') {
89
const char *pch;
90
91
if ((pch = strchr(digits, ch)) != NULL) {
92
unsigned int new = *tp * 10 + (pch - digits);
93
94
if (new > 255)
95
return (0);
96
if (!saw_digit) {
97
if (++octets > 4)
98
return (0);
99
saw_digit = 1;
100
}
101
*tp = new;
102
} else if (ch == '.' && saw_digit) {
103
if (octets == 4)
104
return (0);
105
*++tp = 0;
106
saw_digit = 0;
107
} else
108
return (0);
109
}
110
if (octets < 4)
111
return (0);
112
113
memcpy(dst, tmp, NS_INADDRSZ);
114
return (1);
115
}
116
117
#ifdef HAVE_STRUCT_IN6_ADDR
118
/* int
119
* inet_pton6(src, dst)
120
* convert presentation level address to network order binary form.
121
* return:
122
* 1 if `src' is a valid [RFC1884 2.2] address, else 0.
123
* notice:
124
* does not touch `dst' unless it's returning 1.
125
* credit:
126
* inspired by Mark Andrews.
127
* author:
128
* Paul Vixie, 1996.
129
*/
130
static int
131
inet_pton6(const char *src, u_char *dst)
132
{
133
const char xdigits_l[] = "0123456789abcdef",
134
xdigits_u[] = "0123456789ABCDEF";
135
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
136
const char *xdigits, *curtok;
137
int ch, saw_xdigit, count_xdigit;
138
unsigned int val;
139
140
/* cppcheck-suppress uninitvar */
141
memset((tp = tmp), 0, NS_IN6ADDRSZ);
142
endp = tp + NS_IN6ADDRSZ;
143
colonp = NULL;
144
/* Leading :: requires some special handling. */
145
if (*src == ':')
146
if (*++src != ':')
147
return (0);
148
curtok = src;
149
saw_xdigit = count_xdigit = 0;
150
val = 0;
151
while ((ch = (unsigned char)*src++) != '\0') {
152
const char *pch;
153
154
if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
155
pch = strchr((xdigits = xdigits_u), ch);
156
if (pch != NULL) {
157
if (count_xdigit >= 4)
158
return (0);
159
val <<= 4;
160
val |= (pch - xdigits);
161
if (val > 0xffff)
162
return (0);
163
saw_xdigit = 1;
164
count_xdigit++;
165
continue;
166
}
167
if (ch == ':') {
168
curtok = src;
169
if (!saw_xdigit) {
170
if (colonp)
171
return (0);
172
colonp = tp;
173
continue;
174
} else if (*src == '\0') {
175
return (0);
176
}
177
if (tp + NS_INT16SZ > endp)
178
return (0);
179
*tp++ = (u_char) (val >> 8) & 0xff;
180
*tp++ = (u_char) val & 0xff;
181
saw_xdigit = 0;
182
count_xdigit = 0;
183
val = 0;
184
continue;
185
}
186
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
187
inet_pton4(curtok, tp) > 0) {
188
tp += NS_INADDRSZ;
189
saw_xdigit = 0;
190
count_xdigit = 0;
191
break; /* '\0' was seen by inet_pton4(). */
192
}
193
return (0);
194
}
195
if (saw_xdigit) {
196
if (tp + NS_INT16SZ > endp)
197
return (0);
198
*tp++ = (u_char) (val >> 8) & 0xff;
199
*tp++ = (u_char) val & 0xff;
200
}
201
if (colonp != NULL) {
202
/*
203
* Since some memmove()'s erroneously fail to handle
204
* overlapping regions, we'll do the shift by hand.
205
*/
206
const long n = tp - colonp;
207
long i;
208
209
if (tp == endp)
210
return (0);
211
for (i = 1; i <= n; i++) {
212
endp[- i] = colonp[n - i];
213
colonp[n - i] = 0;
214
}
215
tp = endp;
216
}
217
if (tp != endp)
218
return (0);
219
memcpy(dst, tmp, NS_IN6ADDRSZ);
220
return (1);
221
}
222
#endif /* HAVE_STRUCT_IN6_ADDR */
223
224
/* int
225
* inet_pton(af, src, dst)
226
* convert from presentation format (which usually means ASCII printable)
227
* to network format (which is usually some kind of binary format).
228
* return:
229
* 1 if the address was valid for the specified address family
230
* 0 if the address wasn't valid (`dst' is untouched in this case)
231
* -1 if some other error occurred (`dst' is untouched in this case, too)
232
* author:
233
* Paul Vixie, 1996.
234
*/
235
int
236
sudo_inet_pton(int af, const char * restrict src, void * restrict dst)
237
{
238
switch (af) {
239
case AF_INET:
240
return (inet_pton4(src, dst));
241
#ifdef HAVE_STRUCT_IN6_ADDR
242
case AF_INET6:
243
return (inet_pton6(src, dst));
244
#endif /* HAVE_STRUCT_IN6_ADDR */
245
default:
246
errno = EAFNOSUPPORT;
247
return (-1);
248
}
249
/* NOTREACHED */
250
}
251
252
#endif /* HAVE_INET_PTON */
253
254