Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /* This is from the BIND 4.9.4 release, modified to compile by itself */ |
| 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 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 19 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 20 | |
| 21 | #ifndef HAVE_INET_PTON |
| 22 | |
| 23 | #ifdef HAVE_SYS_PARAM_H |
| 24 | #include <sys/param.h> |
| 25 | #endif |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 26 | #ifdef HAVE_NETINET_IN_H |
| 27 | #include <netinet/in.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_ARPA_INET_H |
| 30 | #include <arpa/inet.h> |
| 31 | #endif |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 32 | |
| 33 | #include "inet_pton.h" |
| 34 | |
| 35 | #define IN6ADDRSZ 16 |
| 36 | #define INADDRSZ 4 |
| 37 | #define INT16SZ 2 |
| 38 | |
| 39 | /* |
| 40 | * WARNING: Don't even consider trying to compile this on a system where |
| 41 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
| 42 | */ |
| 43 | |
| 44 | static int inet_pton4(const char *src, unsigned char *dst); |
| 45 | #ifdef ENABLE_IPV6 |
| 46 | static int inet_pton6(const char *src, unsigned char *dst); |
| 47 | #endif |
| 48 | |
| 49 | /* int |
| 50 | * inet_pton(af, src, dst) |
| 51 | * convert from presentation format (which usually means ASCII printable) |
| 52 | * to network format (which is usually some kind of binary format). |
| 53 | * return: |
| 54 | * 1 if the address was valid for the specified address family |
| 55 | * 0 if the address wasn't valid (`dst' is untouched in this case) |
| 56 | * -1 if some other error occurred (`dst' is untouched in this case, too) |
| 57 | * notice: |
| 58 | * On Windows we store the error in the thread errno, not |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 59 | * in the winsock error code. This is to avoid losing the |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 60 | * actual last winsock error. So when this function returns |
| 61 | * -1, check errno not SOCKERRNO. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 62 | * author: |
| 63 | * Paul Vixie, 1996. |
| 64 | */ |
| 65 | int |
| 66 | Curl_inet_pton(int af, const char *src, void *dst) |
| 67 | { |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 68 | switch(af) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 69 | case AF_INET: |
| 70 | return (inet_pton4(src, (unsigned char *)dst)); |
| 71 | #ifdef ENABLE_IPV6 |
| 72 | case AF_INET6: |
| 73 | return (inet_pton6(src, (unsigned char *)dst)); |
| 74 | #endif |
| 75 | default: |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 76 | errno = EAFNOSUPPORT; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 77 | return (-1); |
| 78 | } |
| 79 | /* NOTREACHED */ |
| 80 | } |
| 81 | |
| 82 | /* int |
| 83 | * inet_pton4(src, dst) |
| 84 | * like inet_aton() but without all the hexadecimal and shorthand. |
| 85 | * return: |
| 86 | * 1 if `src' is a valid dotted quad, else 0. |
| 87 | * notice: |
| 88 | * does not touch `dst' unless it's returning 1. |
| 89 | * author: |
| 90 | * Paul Vixie, 1996. |
| 91 | */ |
| 92 | static int |
| 93 | inet_pton4(const char *src, unsigned char *dst) |
| 94 | { |
| 95 | static const char digits[] = "0123456789"; |
| 96 | int saw_digit, octets, ch; |
| 97 | unsigned char tmp[INADDRSZ], *tp; |
| 98 | |
| 99 | saw_digit = 0; |
| 100 | octets = 0; |
| 101 | tp = tmp; |
| 102 | *tp = 0; |
| 103 | while((ch = *src++) != '\0') { |
| 104 | const char *pch; |
| 105 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 106 | pch = strchr(digits, ch); |
| 107 | if(pch) { |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 108 | unsigned int val = *tp * 10 + (unsigned int)(pch - digits); |
| 109 | |
| 110 | if(saw_digit && *tp == 0) |
| 111 | return (0); |
| 112 | if(val > 255) |
| 113 | return (0); |
| 114 | *tp = (unsigned char)val; |
| 115 | if(! saw_digit) { |
| 116 | if(++octets > 4) |
| 117 | return (0); |
| 118 | saw_digit = 1; |
| 119 | } |
| 120 | } |
| 121 | else if(ch == '.' && saw_digit) { |
| 122 | if(octets == 4) |
| 123 | return (0); |
| 124 | *++tp = 0; |
| 125 | saw_digit = 0; |
| 126 | } |
| 127 | else |
| 128 | return (0); |
| 129 | } |
| 130 | if(octets < 4) |
| 131 | return (0); |
| 132 | memcpy(dst, tmp, INADDRSZ); |
| 133 | return (1); |
| 134 | } |
| 135 | |
| 136 | #ifdef ENABLE_IPV6 |
| 137 | /* int |
| 138 | * inet_pton6(src, dst) |
| 139 | * convert presentation level address to network order binary form. |
| 140 | * return: |
| 141 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. |
| 142 | * notice: |
| 143 | * (1) does not touch `dst' unless it's returning 1. |
| 144 | * (2) :: in a full address is silently ignored. |
| 145 | * credit: |
| 146 | * inspired by Mark Andrews. |
| 147 | * author: |
| 148 | * Paul Vixie, 1996. |
| 149 | */ |
| 150 | static int |
| 151 | inet_pton6(const char *src, unsigned char *dst) |
| 152 | { |
| 153 | static const char xdigits_l[] = "0123456789abcdef", |
| 154 | xdigits_u[] = "0123456789ABCDEF"; |
| 155 | unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; |
| 156 | const char *xdigits, *curtok; |
| 157 | int ch, saw_xdigit; |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 158 | size_t val; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 159 | |
| 160 | memset((tp = tmp), 0, IN6ADDRSZ); |
| 161 | endp = tp + IN6ADDRSZ; |
| 162 | colonp = NULL; |
| 163 | /* Leading :: requires some special handling. */ |
| 164 | if(*src == ':') |
| 165 | if(*++src != ':') |
| 166 | return (0); |
| 167 | curtok = src; |
| 168 | saw_xdigit = 0; |
| 169 | val = 0; |
| 170 | while((ch = *src++) != '\0') { |
| 171 | const char *pch; |
| 172 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 173 | pch = strchr((xdigits = xdigits_l), ch); |
| 174 | if(!pch) |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 175 | pch = strchr((xdigits = xdigits_u), ch); |
| 176 | if(pch != NULL) { |
| 177 | val <<= 4; |
| 178 | val |= (pch - xdigits); |
| 179 | if(++saw_xdigit > 4) |
| 180 | return (0); |
| 181 | continue; |
| 182 | } |
| 183 | if(ch == ':') { |
| 184 | curtok = src; |
| 185 | if(!saw_xdigit) { |
| 186 | if(colonp) |
| 187 | return (0); |
| 188 | colonp = tp; |
| 189 | continue; |
| 190 | } |
| 191 | if(tp + INT16SZ > endp) |
| 192 | return (0); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 193 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
| 194 | *tp++ = (unsigned char) (val & 0xff); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 195 | saw_xdigit = 0; |
| 196 | val = 0; |
| 197 | continue; |
| 198 | } |
| 199 | if(ch == '.' && ((tp + INADDRSZ) <= endp) && |
| 200 | inet_pton4(curtok, tp) > 0) { |
| 201 | tp += INADDRSZ; |
| 202 | saw_xdigit = 0; |
| 203 | break; /* '\0' was seen by inet_pton4(). */ |
| 204 | } |
| 205 | return (0); |
| 206 | } |
| 207 | if(saw_xdigit) { |
| 208 | if(tp + INT16SZ > endp) |
| 209 | return (0); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 210 | *tp++ = (unsigned char) ((val >> 8) & 0xff); |
| 211 | *tp++ = (unsigned char) (val & 0xff); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 212 | } |
| 213 | if(colonp != NULL) { |
| 214 | /* |
| 215 | * Since some memmove()'s erroneously fail to handle |
| 216 | * overlapping regions, we'll do the shift by hand. |
| 217 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 218 | const ssize_t n = tp - colonp; |
| 219 | ssize_t i; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 220 | |
| 221 | if(tp == endp) |
| 222 | return (0); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 223 | for(i = 1; i <= n; i++) { |
| 224 | *(endp - i) = *(colonp + n - i); |
| 225 | *(colonp + n - i) = 0; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 226 | } |
| 227 | tp = endp; |
| 228 | } |
| 229 | if(tp != endp) |
| 230 | return (0); |
| 231 | memcpy(dst, tmp, IN6ADDRSZ); |
| 232 | return (1); |
| 233 | } |
| 234 | #endif /* ENABLE_IPV6 */ |
| 235 | |
| 236 | #endif /* HAVE_INET_PTON */ |