aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2015-06-29 22:51:40 -0500
committerBrent Cook <busterb@gmail.com>2015-07-02 00:19:53 -0500
commit545454277a0be1dcb17b64cb10530a881aae760d (patch)
treedebdf3d7ff6eb7a877a813fd01fc414a0316c883
parent8414df69bc5463e13d91b865d8f21c294e2fe18b (diff)
downloadportable-545454277a0be1dcb17b64cb10530a881aae760d.tar.gz
portable-545454277a0be1dcb17b64cb10530a881aae760d.tar.bz2
portable-545454277a0be1dcb17b64cb10530a881aae760d.zip
add check for inet_pton, nudge minimum win32 compat to 0x0501
-rw-r--r--crypto/Makefile.am4
-rw-r--r--crypto/compat/inet_pton.c212
-rw-r--r--include/Makefile.am1
-rw-r--r--include/arpa/inet.h9
-rw-r--r--m4/check-libc.m43
-rw-r--r--m4/check-os-options.m42
6 files changed, 229 insertions, 2 deletions
diff --git a/crypto/Makefile.am b/crypto/Makefile.am
index 16efe88..7a42c3e 100644
--- a/crypto/Makefile.am
+++ b/crypto/Makefile.am
@@ -53,6 +53,10 @@ if !HAVE_ASPRINTF
53libcompat_la_SOURCES += compat/bsd-asprintf.c 53libcompat_la_SOURCES += compat/bsd-asprintf.c
54endif 54endif
55 55
56if !HAVE_INET_PTON
57libcompat_la_SOURCES += compat/inet_pton.c
58endif
59
56if !HAVE_REALLOCARRAY 60if !HAVE_REALLOCARRAY
57libcompat_la_SOURCES += compat/reallocarray.c 61libcompat_la_SOURCES += compat/reallocarray.c
58endif 62endif
diff --git a/crypto/compat/inet_pton.c b/crypto/compat/inet_pton.c
new file mode 100644
index 0000000..f8e299b
--- /dev/null
+++ b/crypto/compat/inet_pton.c
@@ -0,0 +1,212 @@
1/* $OpenBSD: inet_pton.c,v 1.9 2015/01/16 16:48:51 deraadt Exp $ */
2
3/* Copyright (c) 1996 by Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
12 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
16 * SOFTWARE.
17 */
18
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>
24#include <string.h>
25#include <errno.h>
26
27/*
28 * WARNING: Don't even consider trying to compile this on a system where
29 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
30 */
31
32static int inet_pton4(const char *src, u_char *dst);
33static int inet_pton6(const char *src, u_char *dst);
34
35/* int
36 * inet_pton(af, src, dst)
37 * convert from presentation format (which usually means ASCII printable)
38 * to network format (which is usually some kind of binary format).
39 * return:
40 * 1 if the address was valid for the specified address family
41 * 0 if the address wasn't valid (`dst' is untouched in this case)
42 * -1 if some other error occurred (`dst' is untouched in this case, too)
43 * author:
44 * Paul Vixie, 1996.
45 */
46int
47inet_pton(int af, const char *src, void *dst)
48{
49 switch (af) {
50 case AF_INET:
51 return (inet_pton4(src, dst));
52 case AF_INET6:
53 return (inet_pton6(src, dst));
54 default:
55 errno = EAFNOSUPPORT;
56 return (-1);
57 }
58 /* NOTREACHED */
59}
60
61/* int
62 * inet_pton4(src, dst)
63 * like inet_aton() but without all the hexadecimal and shorthand.
64 * return:
65 * 1 if `src' is a valid dotted quad, else 0.
66 * notice:
67 * does not touch `dst' unless it's returning 1.
68 * author:
69 * Paul Vixie, 1996.
70 */
71static int
72inet_pton4(const char *src, u_char *dst)
73{
74 static const char digits[] = "0123456789";
75 int saw_digit, octets, ch;
76 u_char tmp[INADDRSZ], *tp;
77
78 saw_digit = 0;
79 octets = 0;
80 *(tp = tmp) = 0;
81 while ((ch = *src++) != '\0') {
82 const char *pch;
83
84 if ((pch = strchr(digits, ch)) != NULL) {
85 u_int new = *tp * 10 + (pch - digits);
86
87 if (new > 255)
88 return (0);
89 if (! saw_digit) {
90 if (++octets > 4)
91 return (0);
92 saw_digit = 1;
93 }
94 *tp = new;
95 } else if (ch == '.' && saw_digit) {
96 if (octets == 4)
97 return (0);
98 *++tp = 0;
99 saw_digit = 0;
100 } else
101 return (0);
102 }
103 if (octets < 4)
104 return (0);
105
106 memcpy(dst, tmp, INADDRSZ);
107 return (1);
108}
109
110/* int
111 * inet_pton6(src, dst)
112 * convert presentation level address to network order binary form.
113 * return:
114 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
115 * notice:
116 * does not touch `dst' unless it's returning 1.
117 * credit:
118 * inspired by Mark Andrews.
119 * author:
120 * Paul Vixie, 1996.
121 */
122static int
123inet_pton6(const char *src, u_char *dst)
124{
125 static const char xdigits_l[] = "0123456789abcdef",
126 xdigits_u[] = "0123456789ABCDEF";
127 u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
128 const char *xdigits, *curtok;
129 int ch, saw_xdigit, count_xdigit;
130 u_int val;
131
132 memset((tp = tmp), '\0', IN6ADDRSZ);
133 endp = tp + 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 = count_xdigit = 0;
141 val = 0;
142 while ((ch = *src++) != '\0') {
143 const char *pch;
144
145 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
146 pch = strchr((xdigits = xdigits_u), ch);
147 if (pch != NULL) {
148 if (count_xdigit >= 4)
149 return (0);
150 val <<= 4;
151 val |= (pch - xdigits);
152 if (val > 0xffff)
153 return (0);
154 saw_xdigit = 1;
155 count_xdigit++;
156 continue;
157 }
158 if (ch == ':') {
159 curtok = src;
160 if (!saw_xdigit) {
161 if (colonp)
162 return (0);
163 colonp = tp;
164 continue;
165 } else if (*src == '\0') {
166 return (0);
167 }
168 if (tp + INT16SZ > endp)
169 return (0);
170 *tp++ = (u_char) (val >> 8) & 0xff;
171 *tp++ = (u_char) val & 0xff;
172 saw_xdigit = 0;
173 count_xdigit = 0;
174 val = 0;
175 continue;
176 }
177 if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
178 inet_pton4(curtok, tp) > 0) {
179 tp += INADDRSZ;
180 saw_xdigit = 0;
181 count_xdigit = 0;
182 break; /* '\0' was seen by inet_pton4(). */
183 }
184 return (0);
185 }
186 if (saw_xdigit) {
187 if (tp + INT16SZ > endp)
188 return (0);
189 *tp++ = (u_char) (val >> 8) & 0xff;
190 *tp++ = (u_char) val & 0xff;
191 }
192 if (colonp != NULL) {
193 /*
194 * Since some memmove()'s erroneously fail to handle
195 * overlapping regions, we'll do the shift by hand.
196 */
197 const int n = tp - colonp;
198 int i;
199
200 if (tp == endp)
201 return (0);
202 for (i = 1; i <= n; i++) {
203 endp[- i] = colonp[n - i];
204 colonp[n - i] = 0;
205 }
206 tp = endp;
207 }
208 if (tp != endp)
209 return (0);
210 memcpy(dst, tmp, IN6ADDRSZ);
211 return (1);
212}
diff --git a/include/Makefile.am b/include/Makefile.am
index 7e1ece2..b620938 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -14,6 +14,7 @@ noinst_HEADERS += unistd.h
14noinst_HEADERS += win32netcompat.h 14noinst_HEADERS += win32netcompat.h
15 15
16noinst_HEADERS += arpa/inet.h 16noinst_HEADERS += arpa/inet.h
17noinst_HEADERS += arpa/nameser.h
17 18
18noinst_HEADERS += machine/endian.h 19noinst_HEADERS += machine/endian.h
19 20
diff --git a/include/arpa/inet.h b/include/arpa/inet.h
index 5d43e4f..67740c6 100644
--- a/include/arpa/inet.h
+++ b/include/arpa/inet.h
@@ -7,4 +7,13 @@
7#include_next <arpa/inet.h> 7#include_next <arpa/inet.h>
8#else 8#else
9#include <win32netcompat.h> 9#include <win32netcompat.h>
10
11#ifndef AI_ADDRCONFIG
12#define AI_ADDRCONFIG 0x00000400
13#endif
14
15#endif
16
17#ifndef HAVE_INET_PTON
18int inet_pton(int af, const char * restrict src, void * restrict dst);
10#endif 19#endif
diff --git a/m4/check-libc.m4 b/m4/check-libc.m4
index 2bbfb81..f1ba611 100644
--- a/m4/check-libc.m4
+++ b/m4/check-libc.m4
@@ -1,8 +1,9 @@
1AC_DEFUN([CHECK_LIBC_COMPAT], [ 1AC_DEFUN([CHECK_LIBC_COMPAT], [
2# Check for general libc functions 2# Check for general libc functions
3AC_CHECK_FUNCS([asprintf memmem poll reallocarray]) 3AC_CHECK_FUNCS([asprintf inet_pton memmem poll reallocarray])
4AC_CHECK_FUNCS([strlcat strlcpy strndup strnlen strsep strtonum]) 4AC_CHECK_FUNCS([strlcat strlcpy strndup strnlen strsep strtonum])
5AM_CONDITIONAL([HAVE_ASPRINTF], [test "x$ac_cv_func_asprintf" = xyes]) 5AM_CONDITIONAL([HAVE_ASPRINTF], [test "x$ac_cv_func_asprintf" = xyes])
6AM_CONDITIONAL([HAVE_INET_PTON], [test "x$ac_cv_func_inet_pton" = xyes])
6AM_CONDITIONAL([HAVE_MEMMEM], [test "x$ac_cv_func_memmem" = xyes]) 7AM_CONDITIONAL([HAVE_MEMMEM], [test "x$ac_cv_func_memmem" = xyes])
7AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes]) 8AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes])
8AM_CONDITIONAL([HAVE_REALLOCARRAY], [test "x$ac_cv_func_reallocarray" = xyes]) 9AM_CONDITIONAL([HAVE_REALLOCARRAY], [test "x$ac_cv_func_reallocarray" = xyes])
diff --git a/m4/check-os-options.m4 b/m4/check-os-options.m4
index e4e5364..371ffb3 100644
--- a/m4/check-os-options.m4
+++ b/m4/check-os-options.m4
@@ -50,7 +50,7 @@ case $host_os in
50 HOST_OS=win 50 HOST_OS=win
51 CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -D_POSIX -D_POSIX_SOURCE -D__USE_MINGW_ANSI_STDIO" 51 CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -D_POSIX -D_POSIX_SOURCE -D__USE_MINGW_ANSI_STDIO"
52 CPPFLAGS="$CPPFLAGS -D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS" 52 CPPFLAGS="$CPPFLAGS -D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS"
53 CPPFLAGS="$CPPFLAGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0600" 53 CPPFLAGS="$CPPFLAGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0501"
54 CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SPEED -DNO_SYSLOG" 54 CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SPEED -DNO_SYSLOG"
55 CFLAGS="$CFLAGS -static-libgcc" 55 CFLAGS="$CFLAGS -static-libgcc"
56 LDFLAGS="$LDFLAGS -static-libgcc" 56 LDFLAGS="$LDFLAGS -static-libgcc"