inet_pton.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* This is from the BIND 4.9.4 release, modified to compile by itself */
  2. /* Copyright (c) Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  9. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  10. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  11. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  13. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  15. * SOFTWARE.
  16. *
  17. * SPDX-License-Identifier: ISC
  18. */
  19. #include "curl_setup.h"
  20. #ifndef HAVE_INET_PTON
  21. #ifdef HAVE_SYS_PARAM_H
  22. #include <sys/param.h>
  23. #endif
  24. #ifdef HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #ifdef HAVE_ARPA_INET_H
  28. #include <arpa/inet.h>
  29. #endif
  30. #include "inet_pton.h"
  31. #define IN6ADDRSZ 16
  32. #define INADDRSZ 4
  33. #define INT16SZ 2
  34. /*
  35. * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
  36. * sure we have _some_ value for AF_INET6 without polluting our fake value
  37. * everywhere.
  38. */
  39. #if !defined(USE_IPV6) && !defined(AF_INET6)
  40. #define AF_INET6 (AF_INET + 1)
  41. #endif
  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. static int inet_pton4(const char *src, unsigned char *dst);
  47. static int inet_pton6(const char *src, unsigned char *dst);
  48. /* int
  49. * inet_pton(af, src, dst)
  50. * convert from presentation format (which usually means ASCII printable)
  51. * to network format (which is usually some kind of binary format).
  52. * return:
  53. * 1 if the address was valid for the specified address family
  54. * 0 if the address wasn't valid (`dst' is untouched in this case)
  55. * -1 if some other error occurred (`dst' is untouched in this case, too)
  56. * notice:
  57. * On Windows we store the error in the thread errno, not
  58. * in the winsock error code. This is to avoid losing the
  59. * actual last winsock error. So when this function returns
  60. * -1, check errno not SOCKERRNO.
  61. * author:
  62. * Paul Vixie, 1996.
  63. */
  64. int
  65. Curl_inet_pton(int af, const char *src, void *dst)
  66. {
  67. switch(af) {
  68. case AF_INET:
  69. return (inet_pton4(src, (unsigned char *)dst));
  70. case AF_INET6:
  71. return (inet_pton6(src, (unsigned char *)dst));
  72. default:
  73. errno = EAFNOSUPPORT;
  74. return (-1);
  75. }
  76. /* NOTREACHED */
  77. }
  78. /* int
  79. * inet_pton4(src, dst)
  80. * like inet_aton() but without all the hexadecimal and shorthand.
  81. * return:
  82. * 1 if `src' is a valid dotted quad, else 0.
  83. * notice:
  84. * does not touch `dst' unless it's returning 1.
  85. * author:
  86. * Paul Vixie, 1996.
  87. */
  88. static int
  89. inet_pton4(const char *src, unsigned char *dst)
  90. {
  91. static const char digits[] = "0123456789";
  92. int saw_digit, octets, ch;
  93. unsigned char tmp[INADDRSZ], *tp;
  94. saw_digit = 0;
  95. octets = 0;
  96. tp = tmp;
  97. *tp = 0;
  98. while((ch = *src++) != '\0') {
  99. const char *pch;
  100. pch = strchr(digits, ch);
  101. if(pch) {
  102. unsigned int val = (unsigned int)(*tp * 10) +
  103. (unsigned int)(pch - digits);
  104. if(saw_digit && *tp == 0)
  105. return (0);
  106. if(val > 255)
  107. return (0);
  108. *tp = (unsigned char)val;
  109. if(!saw_digit) {
  110. if(++octets > 4)
  111. return (0);
  112. saw_digit = 1;
  113. }
  114. }
  115. else if(ch == '.' && saw_digit) {
  116. if(octets == 4)
  117. return (0);
  118. *++tp = 0;
  119. saw_digit = 0;
  120. }
  121. else
  122. return (0);
  123. }
  124. if(octets < 4)
  125. return (0);
  126. memcpy(dst, tmp, INADDRSZ);
  127. return (1);
  128. }
  129. /* int
  130. * inet_pton6(src, dst)
  131. * convert presentation level address to network order binary form.
  132. * return:
  133. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  134. * notice:
  135. * (1) does not touch `dst' unless it's returning 1.
  136. * (2) :: in a full address is silently ignored.
  137. * credit:
  138. * inspired by Mark Andrews.
  139. * author:
  140. * Paul Vixie, 1996.
  141. */
  142. static int
  143. inet_pton6(const char *src, unsigned char *dst)
  144. {
  145. static const char xdigits_l[] = "0123456789abcdef",
  146. xdigits_u[] = "0123456789ABCDEF";
  147. unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  148. const char *curtok;
  149. int ch, saw_xdigit;
  150. size_t val;
  151. memset((tp = tmp), 0, IN6ADDRSZ);
  152. endp = tp + IN6ADDRSZ;
  153. colonp = NULL;
  154. /* Leading :: requires some special handling. */
  155. if(*src == ':')
  156. if(*++src != ':')
  157. return (0);
  158. curtok = src;
  159. saw_xdigit = 0;
  160. val = 0;
  161. while((ch = *src++) != '\0') {
  162. const char *xdigits;
  163. const char *pch;
  164. pch = strchr((xdigits = xdigits_l), ch);
  165. if(!pch)
  166. pch = strchr((xdigits = xdigits_u), ch);
  167. if(pch) {
  168. val <<= 4;
  169. val |= (pch - xdigits);
  170. if(++saw_xdigit > 4)
  171. return (0);
  172. continue;
  173. }
  174. if(ch == ':') {
  175. curtok = src;
  176. if(!saw_xdigit) {
  177. if(colonp)
  178. return (0);
  179. colonp = tp;
  180. continue;
  181. }
  182. if(tp + INT16SZ > endp)
  183. return (0);
  184. *tp++ = (unsigned char) ((val >> 8) & 0xff);
  185. *tp++ = (unsigned char) (val & 0xff);
  186. saw_xdigit = 0;
  187. val = 0;
  188. continue;
  189. }
  190. if(ch == '.' && ((tp + INADDRSZ) <= endp) &&
  191. inet_pton4(curtok, tp) > 0) {
  192. tp += INADDRSZ;
  193. saw_xdigit = 0;
  194. break; /* '\0' was seen by inet_pton4(). */
  195. }
  196. return (0);
  197. }
  198. if(saw_xdigit) {
  199. if(tp + INT16SZ > endp)
  200. return (0);
  201. *tp++ = (unsigned char) ((val >> 8) & 0xff);
  202. *tp++ = (unsigned char) (val & 0xff);
  203. }
  204. if(colonp) {
  205. /*
  206. * Since some memmove()'s erroneously fail to handle
  207. * overlapping regions, we'll do the shift by hand.
  208. */
  209. const ssize_t n = tp - colonp;
  210. ssize_t i;
  211. if(tp == endp)
  212. return (0);
  213. for(i = 1; i <= n; i++) {
  214. *(endp - i) = *(colonp + n - i);
  215. *(colonp + n - i) = 0;
  216. }
  217. tp = endp;
  218. }
  219. if(tp != endp)
  220. return (0);
  221. memcpy(dst, tmp, IN6ADDRSZ);
  222. return (1);
  223. }
  224. #endif /* HAVE_INET_PTON */