inet_pton.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include "../curl_ctype.h"
  21. #include "strparse.h"
  22. #ifndef HAVE_INET_PTON
  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. #include "inet_pton.h"
  33. #define IN6ADDRSZ 16
  34. #define INADDRSZ 4
  35. #define INT16SZ 2
  36. /*
  37. * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
  38. * sure we have _some_ value for AF_INET6 without polluting our fake value
  39. * everywhere.
  40. */
  41. #if !defined(USE_IPV6) && !defined(AF_INET6)
  42. #define AF_INET6 (AF_INET + 1)
  43. #endif
  44. /*
  45. * WARNING: Do not 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. static int inet_pton4(const char *src, unsigned char *dst);
  49. static int inet_pton6(const char *src, unsigned char *dst);
  50. /* int
  51. * inet_pton(af, src, dst)
  52. * convert from presentation format (which usually means ASCII printable)
  53. * to network format (which is usually some kind of binary format).
  54. * return:
  55. * 1 if the address was valid for the specified address family
  56. * 0 if the address was not valid (`dst' is untouched in this case)
  57. * -1 if some other error occurred (`dst' is untouched in this case, too)
  58. * notice:
  59. * On Windows we store the error in the thread errno, not
  60. * in the Winsock error code. This is to avoid losing the
  61. * actual last Winsock error. When this function returns
  62. * -1, check errno not SOCKERRNO.
  63. * author:
  64. * Paul Vixie, 1996.
  65. */
  66. int
  67. curlx_inet_pton(int af, const char *src, void *dst)
  68. {
  69. switch(af) {
  70. case AF_INET:
  71. return inet_pton4(src, (unsigned char *)dst);
  72. case AF_INET6:
  73. return inet_pton6(src, (unsigned char *)dst);
  74. default:
  75. CURL_SETERRNO(SOCKEAFNOSUPPORT);
  76. return -1;
  77. }
  78. /* NOTREACHED */
  79. }
  80. /* int
  81. * inet_pton4(src, dst)
  82. * like inet_aton() but without all the hexadecimal and shorthand.
  83. * return:
  84. * 1 if `src' is a valid dotted quad, else 0.
  85. * notice:
  86. * does not touch `dst' unless it is returning 1.
  87. * author:
  88. * Paul Vixie, 1996.
  89. */
  90. static int
  91. inet_pton4(const char *src, unsigned char *dst)
  92. {
  93. int saw_digit, octets, ch;
  94. unsigned char tmp[INADDRSZ], *tp;
  95. saw_digit = 0;
  96. octets = 0;
  97. tp = tmp;
  98. *tp = 0;
  99. while((ch = *src++) != '\0') {
  100. if(ISDIGIT(ch)) {
  101. unsigned int val = (*tp * 10) + (ch - '0');
  102. if(saw_digit && *tp == 0)
  103. return 0;
  104. if(val > 255)
  105. return 0;
  106. *tp = (unsigned char)val;
  107. if(!saw_digit) {
  108. if(++octets > 4)
  109. return 0;
  110. saw_digit = 1;
  111. }
  112. }
  113. else if(ch == '.' && saw_digit) {
  114. if(octets == 4)
  115. return 0;
  116. *++tp = 0;
  117. saw_digit = 0;
  118. }
  119. else
  120. return 0;
  121. }
  122. if(octets < 4)
  123. return 0;
  124. memcpy(dst, tmp, INADDRSZ);
  125. return 1;
  126. }
  127. /* int
  128. * inet_pton6(src, dst)
  129. * convert presentation level address to network order binary form.
  130. * return:
  131. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  132. * notice:
  133. * (1) does not touch `dst' unless it is returning 1.
  134. * (2) :: in a full address is silently ignored.
  135. * credit:
  136. * inspired by Mark Andrews.
  137. * author:
  138. * Paul Vixie, 1996.
  139. */
  140. static int
  141. inet_pton6(const char *src, unsigned char *dst)
  142. {
  143. unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  144. const char *curtok;
  145. int ch, saw_xdigit;
  146. size_t val;
  147. memset((tp = tmp), 0, IN6ADDRSZ);
  148. endp = tp + IN6ADDRSZ;
  149. colonp = NULL;
  150. /* Leading :: requires some special handling. */
  151. if(*src == ':')
  152. if(*++src != ':')
  153. return 0;
  154. curtok = src;
  155. saw_xdigit = 0;
  156. val = 0;
  157. while((ch = *src++) != '\0') {
  158. if(ISXDIGIT(ch)) {
  159. val <<= 4;
  160. val |= Curl_hexval(ch);
  161. if(++saw_xdigit > 4)
  162. return 0;
  163. continue;
  164. }
  165. if(ch == ':') {
  166. curtok = src;
  167. if(!saw_xdigit) {
  168. if(colonp)
  169. return 0;
  170. colonp = tp;
  171. continue;
  172. }
  173. if(tp + INT16SZ > endp)
  174. return 0;
  175. *tp++ = (unsigned char) ((val >> 8) & 0xff);
  176. *tp++ = (unsigned char) (val & 0xff);
  177. saw_xdigit = 0;
  178. val = 0;
  179. continue;
  180. }
  181. if(ch == '.' && ((tp + INADDRSZ) <= endp) &&
  182. inet_pton4(curtok, tp) > 0) {
  183. tp += INADDRSZ;
  184. saw_xdigit = 0;
  185. break; /* '\0' was seen by inet_pton4(). */
  186. }
  187. return 0;
  188. }
  189. if(saw_xdigit) {
  190. if(tp + INT16SZ > endp)
  191. return 0;
  192. *tp++ = (unsigned char) ((val >> 8) & 0xff);
  193. *tp++ = (unsigned char) (val & 0xff);
  194. }
  195. if(colonp) {
  196. /*
  197. * Since some memmove()'s erroneously fail to handle
  198. * overlapping regions, we will do the shift by hand.
  199. */
  200. const ssize_t n = tp - colonp;
  201. ssize_t i;
  202. if(tp == endp)
  203. return 0;
  204. for(i = 1; i <= n; i++) {
  205. *(endp - i) = *(colonp + n - i);
  206. *(colonp + n - i) = 0;
  207. }
  208. tp = endp;
  209. }
  210. if(tp != endp)
  211. return 0;
  212. memcpy(dst, tmp, IN6ADDRSZ);
  213. return 1;
  214. }
  215. #endif /* HAVE_INET_PTON */