inet_pton.c 6.0 KB

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