2
0

inet_pton.c 6.0 KB

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