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 "curl_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_NETINET_IN_H 27 #include <netinet/in.h> 28 #endif 29 #ifdef HAVE_ARPA_INET_H 30 #include <arpa/inet.h> 31 #endif 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 59 * in the winsock error code. This is to avoid losing the 60 * actual last winsock error. So use macro ERRNO to fetch the 61 * errno this function sets when returning (-1), not SOCKERRNO. 62 * author: 63 * Paul Vixie, 1996. 64 */ 65 int 66 Curl_inet_pton(int af, const char *src, void *dst) 67 { 68 switch (af) { 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: 76 SET_ERRNO(EAFNOSUPPORT); 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 106 if((pch = strchr(digits, ch)) != NULL) { 107 unsigned int val = *tp * 10 + (unsigned int)(pch - digits); 108 109 if(saw_digit && *tp == 0) 110 return (0); 111 if(val > 255) 112 return (0); 113 *tp = (unsigned char)val; 114 if(! saw_digit) { 115 if(++octets > 4) 116 return (0); 117 saw_digit = 1; 118 } 119 } 120 else if(ch == '.' && saw_digit) { 121 if(octets == 4) 122 return (0); 123 *++tp = 0; 124 saw_digit = 0; 125 } 126 else 127 return (0); 128 } 129 if(octets < 4) 130 return (0); 131 memcpy(dst, tmp, INADDRSZ); 132 return (1); 133 } 134 135 #ifdef ENABLE_IPV6 136 /* int 137 * inet_pton6(src, dst) 138 * convert presentation level address to network order binary form. 139 * return: 140 * 1 if `src' is a valid [RFC1884 2.2] address, else 0. 141 * notice: 142 * (1) does not touch `dst' unless it's returning 1. 143 * (2) :: in a full address is silently ignored. 144 * credit: 145 * inspired by Mark Andrews. 146 * author: 147 * Paul Vixie, 1996. 148 */ 149 static int 150 inet_pton6(const char *src, unsigned char *dst) 151 { 152 static const char xdigits_l[] = "0123456789abcdef", 153 xdigits_u[] = "0123456789ABCDEF"; 154 unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; 155 const char *xdigits, *curtok; 156 int ch, saw_xdigit; 157 size_t val; 158 159 memset((tp = tmp), 0, IN6ADDRSZ); 160 endp = tp + IN6ADDRSZ; 161 colonp = NULL; 162 /* Leading :: requires some special handling. */ 163 if(*src == ':') 164 if(*++src != ':') 165 return (0); 166 curtok = src; 167 saw_xdigit = 0; 168 val = 0; 169 while((ch = *src++) != '\0') { 170 const char *pch; 171 172 if((pch = strchr((xdigits = xdigits_l), ch)) == NULL) 173 pch = strchr((xdigits = xdigits_u), ch); 174 if(pch != NULL) { 175 val <<= 4; 176 val |= (pch - xdigits); 177 if(++saw_xdigit > 4) 178 return (0); 179 continue; 180 } 181 if(ch == ':') { 182 curtok = src; 183 if(!saw_xdigit) { 184 if(colonp) 185 return (0); 186 colonp = tp; 187 continue; 188 } 189 if(tp + INT16SZ > endp) 190 return (0); 191 *tp++ = (unsigned char) ((val >> 8) & 0xff); 192 *tp++ = (unsigned char) (val & 0xff); 193 saw_xdigit = 0; 194 val = 0; 195 continue; 196 } 197 if(ch == '.' && ((tp + INADDRSZ) <= endp) && 198 inet_pton4(curtok, tp) > 0) { 199 tp += INADDRSZ; 200 saw_xdigit = 0; 201 break; /* '\0' was seen by inet_pton4(). */ 202 } 203 return (0); 204 } 205 if(saw_xdigit) { 206 if(tp + INT16SZ > endp) 207 return (0); 208 *tp++ = (unsigned char) ((val >> 8) & 0xff); 209 *tp++ = (unsigned char) (val & 0xff); 210 } 211 if(colonp != NULL) { 212 /* 213 * Since some memmove()'s erroneously fail to handle 214 * overlapping regions, we'll do the shift by hand. 215 */ 216 const ssize_t n = tp - colonp; 217 ssize_t i; 218 219 if(tp == endp) 220 return (0); 221 for(i = 1; i <= n; i++) { 222 *(endp - i) = *(colonp + n - i); 223 *(colonp + n - i) = 0; 224 } 225 tp = endp; 226 } 227 if(tp != endp) 228 return (0); 229 memcpy(dst, tmp, IN6ADDRSZ); 230 return (1); 231 } 232 #endif /* ENABLE_IPV6 */ 233 234 #endif /* HAVE_INET_PTON */ 235