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