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