inet_pton.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #ifdef HAVE_SYS_SOCKET_H
  26. #include <sys/socket.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. #include <string.h>
  35. #include <errno.h>
  36. #include "inet_pton.h"
  37. #define IN6ADDRSZ 16
  38. #define INADDRSZ 4
  39. #define INT16SZ 2
  40. #ifdef WIN32
  41. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  42. #endif
  43. /*
  44. * WARNING: Don't even consider trying to compile this on a system where
  45. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  46. */
  47. static int inet_pton4(const char *src, unsigned char *dst);
  48. #ifdef ENABLE_IPV6
  49. static int inet_pton6(const char *src, unsigned char *dst);
  50. #endif
  51. /* int
  52. * inet_pton(af, src, dst)
  53. * convert from presentation format (which usually means ASCII printable)
  54. * to network format (which is usually some kind of binary format).
  55. * return:
  56. * 1 if the address was valid for the specified address family
  57. * 0 if the address wasn't valid (`dst' is untouched in this case)
  58. * -1 if some other error occurred (`dst' is untouched in this case, too)
  59. * author:
  60. * Paul Vixie, 1996.
  61. */
  62. int
  63. Curl_inet_pton(int af, const char *src, void *dst)
  64. {
  65. switch (af) {
  66. case AF_INET:
  67. return (inet_pton4(src, dst));
  68. #ifdef ENABLE_IPV6
  69. #ifndef AF_INET6
  70. #define AF_INET6 AF_MAX+1 /* just to let this compile */
  71. #endif
  72. case AF_INET6:
  73. return (inet_pton6(src, dst));
  74. #endif
  75. default:
  76. errno = EAFNOSUPPORT;
  77. return (-1);
  78. }
  79. /* NOTREACHED */
  80. }
  81. /* int
  82. * inet_pton4(src, dst)
  83. * like inet_aton() but without all the hexadecimal and shorthand.
  84. * return:
  85. * 1 if `src' is a valid dotted quad, else 0.
  86. * notice:
  87. * does not touch `dst' unless it's returning 1.
  88. * author:
  89. * Paul Vixie, 1996.
  90. */
  91. static int
  92. inet_pton4(const char *src, unsigned char *dst)
  93. {
  94. static const char digits[] = "0123456789";
  95. int saw_digit, octets, ch;
  96. unsigned char tmp[INADDRSZ], *tp;
  97. saw_digit = 0;
  98. octets = 0;
  99. *(tp = tmp) = 0;
  100. while ((ch = *src++) != '\0') {
  101. const char *pch;
  102. if ((pch = strchr(digits, ch)) != NULL) {
  103. u_int new = *tp * 10 + (pch - digits);
  104. if (new > 255)
  105. return (0);
  106. *tp = new;
  107. if (! saw_digit) {
  108. if (++octets > 4)
  109. return (0);
  110. saw_digit = 1;
  111. }
  112. } else if (ch == '.' && saw_digit) {
  113. if (octets == 4)
  114. return (0);
  115. *++tp = 0;
  116. saw_digit = 0;
  117. } else
  118. return (0);
  119. }
  120. if (octets < 4)
  121. return (0);
  122. /* bcopy(tmp, dst, INADDRSZ); */
  123. memcpy(dst, tmp, INADDRSZ);
  124. return (1);
  125. }
  126. #ifdef ENABLE_IPV6
  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's 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. static const char xdigits_l[] = "0123456789abcdef",
  144. xdigits_u[] = "0123456789ABCDEF";
  145. unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  146. const char *xdigits, *curtok;
  147. int ch, saw_xdigit;
  148. u_int val;
  149. memset((tp = tmp), 0, IN6ADDRSZ);
  150. endp = tp + IN6ADDRSZ;
  151. colonp = NULL;
  152. /* Leading :: requires some special handling. */
  153. if (*src == ':')
  154. if (*++src != ':')
  155. return (0);
  156. curtok = src;
  157. saw_xdigit = 0;
  158. val = 0;
  159. while ((ch = *src++) != '\0') {
  160. const char *pch;
  161. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  162. pch = strchr((xdigits = xdigits_u), ch);
  163. if (pch != NULL) {
  164. val <<= 4;
  165. val |= (pch - xdigits);
  166. if (val > 0xffff)
  167. return (0);
  168. saw_xdigit = 1;
  169. continue;
  170. }
  171. if (ch == ':') {
  172. curtok = src;
  173. if (!saw_xdigit) {
  174. if (colonp)
  175. return (0);
  176. colonp = tp;
  177. continue;
  178. }
  179. if (tp + INT16SZ > endp)
  180. return (0);
  181. *tp++ = (unsigned char) (val >> 8) & 0xff;
  182. *tp++ = (unsigned char) val & 0xff;
  183. saw_xdigit = 0;
  184. val = 0;
  185. continue;
  186. }
  187. if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
  188. inet_pton4(curtok, tp) > 0) {
  189. tp += INADDRSZ;
  190. saw_xdigit = 0;
  191. break; /* '\0' was seen by inet_pton4(). */
  192. }
  193. return (0);
  194. }
  195. if (saw_xdigit) {
  196. if (tp + INT16SZ > endp)
  197. return (0);
  198. *tp++ = (unsigned char) (val >> 8) & 0xff;
  199. *tp++ = (unsigned char) val & 0xff;
  200. }
  201. if (colonp != NULL) {
  202. /*
  203. * Since some memmove()'s erroneously fail to handle
  204. * overlapping regions, we'll do the shift by hand.
  205. */
  206. const int n = tp - colonp;
  207. int i;
  208. for (i = 1; i <= n; i++) {
  209. endp[- i] = colonp[n - i];
  210. colonp[n - i] = 0;
  211. }
  212. tp = endp;
  213. }
  214. if (tp != endp)
  215. return (0);
  216. /* bcopy(tmp, dst, IN6ADDRSZ); */
  217. memcpy(dst, tmp, IN6ADDRSZ);
  218. return (1);
  219. }
  220. #endif /* ENABLE_IPV6 */
  221. #endif /* HAVE_INET_PTON */