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