/contrib/bind9/lib/isc/inet_pton.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 214 lines · 134 code · 17 blank · 63 comment · 47 complexity · 494e70d2c4f086680ce3111dbd6a743a MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1996-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*! \file */
  18. #if defined(LIBC_SCCS) && !defined(lint)
  19. static char rcsid[] =
  20. "$Id: inet_pton.c,v 1.19 2007/06/19 23:47:17 tbox Exp $";
  21. #endif /* LIBC_SCCS and not lint */
  22. #include <config.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <isc/net.h>
  26. /*% INT16 Size */
  27. #define NS_INT16SZ 2
  28. /*% IPv4 Address Size */
  29. #define NS_INADDRSZ 4
  30. /*% IPv6 Address Size */
  31. #define NS_IN6ADDRSZ 16
  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. static int inet_pton6(const char *src, unsigned char *dst);
  38. /*%
  39. * convert from presentation format (which usually means ASCII printable)
  40. * to network format (which is usually some kind of binary format).
  41. * \return
  42. * 1 if the address was valid for the specified address family
  43. * 0 if the address wasn't valid (`dst' is untouched in this case)
  44. * -1 if some other error occurred (`dst' is untouched in this case, too)
  45. * \author
  46. * Paul Vixie, 1996.
  47. */
  48. int
  49. isc_net_pton(int af, const char *src, void *dst) {
  50. switch (af) {
  51. case AF_INET:
  52. return (inet_pton4(src, dst));
  53. case AF_INET6:
  54. return (inet_pton6(src, dst));
  55. default:
  56. errno = EAFNOSUPPORT;
  57. return (-1);
  58. }
  59. /* NOTREACHED */
  60. }
  61. /*!\fn static int inet_pton4(const char *src, unsigned char *dst)
  62. * \brief
  63. * like inet_aton() but without all the hexadecimal and shorthand.
  64. * \return
  65. * 1 if `src' is a valid dotted quad, else 0.
  66. * \note
  67. * does not touch `dst' unless it's returning 1.
  68. * \author
  69. * Paul Vixie, 1996.
  70. */
  71. static int
  72. inet_pton4(const char *src, unsigned char *dst) {
  73. static const char digits[] = "0123456789";
  74. int saw_digit, octets, ch;
  75. unsigned char tmp[NS_INADDRSZ], *tp;
  76. saw_digit = 0;
  77. octets = 0;
  78. *(tp = tmp) = 0;
  79. while ((ch = *src++) != '\0') {
  80. const char *pch;
  81. if ((pch = strchr(digits, ch)) != NULL) {
  82. unsigned int new = *tp * 10 + (pch - digits);
  83. if (saw_digit && *tp == 0)
  84. return (0);
  85. if (new > 255)
  86. return (0);
  87. *tp = new;
  88. if (!saw_digit) {
  89. if (++octets > 4)
  90. return (0);
  91. saw_digit = 1;
  92. }
  93. } else if (ch == '.' && saw_digit) {
  94. if (octets == 4)
  95. return (0);
  96. *++tp = 0;
  97. saw_digit = 0;
  98. } else
  99. return (0);
  100. }
  101. if (octets < 4)
  102. return (0);
  103. memcpy(dst, tmp, NS_INADDRSZ);
  104. return (1);
  105. }
  106. /*%
  107. * convert presentation level address to network order binary form.
  108. * \return
  109. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  110. * \note
  111. * (1) does not touch `dst' unless it's returning 1.
  112. * \note
  113. * (2) :: in a full address is silently ignored.
  114. * \author
  115. * inspired by Mark Andrews.
  116. * \author
  117. * Paul Vixie, 1996.
  118. */
  119. static int
  120. inet_pton6(const char *src, unsigned char *dst) {
  121. static const char xdigits_l[] = "0123456789abcdef",
  122. xdigits_u[] = "0123456789ABCDEF";
  123. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  124. const char *xdigits, *curtok;
  125. int ch, seen_xdigits;
  126. unsigned int val;
  127. memset((tp = tmp), '\0', NS_IN6ADDRSZ);
  128. endp = tp + NS_IN6ADDRSZ;
  129. colonp = NULL;
  130. /* Leading :: requires some special handling. */
  131. if (*src == ':')
  132. if (*++src != ':')
  133. return (0);
  134. curtok = src;
  135. seen_xdigits = 0;
  136. val = 0;
  137. while ((ch = *src++) != '\0') {
  138. const char *pch;
  139. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  140. pch = strchr((xdigits = xdigits_u), ch);
  141. if (pch != NULL) {
  142. val <<= 4;
  143. val |= (pch - xdigits);
  144. if (++seen_xdigits > 4)
  145. return (0);
  146. continue;
  147. }
  148. if (ch == ':') {
  149. curtok = src;
  150. if (!seen_xdigits) {
  151. if (colonp)
  152. return (0);
  153. colonp = tp;
  154. continue;
  155. }
  156. if (tp + NS_INT16SZ > endp)
  157. return (0);
  158. *tp++ = (unsigned char) (val >> 8) & 0xff;
  159. *tp++ = (unsigned char) val & 0xff;
  160. seen_xdigits = 0;
  161. val = 0;
  162. continue;
  163. }
  164. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  165. inet_pton4(curtok, tp) > 0) {
  166. tp += NS_INADDRSZ;
  167. seen_xdigits = 0;
  168. break; /* '\0' was seen by inet_pton4(). */
  169. }
  170. return (0);
  171. }
  172. if (seen_xdigits) {
  173. if (tp + NS_INT16SZ > endp)
  174. return (0);
  175. *tp++ = (unsigned char) (val >> 8) & 0xff;
  176. *tp++ = (unsigned char) val & 0xff;
  177. }
  178. if (colonp != NULL) {
  179. /*
  180. * Since some memmove()'s erroneously fail to handle
  181. * overlapping regions, we'll do the shift by hand.
  182. */
  183. const int n = tp - colonp;
  184. int i;
  185. if (tp == endp)
  186. return (0);
  187. for (i = 1; i <= n; i++) {
  188. endp[- i] = colonp[n - i];
  189. colonp[n - i] = 0;
  190. }
  191. tp = endp;
  192. }
  193. if (tp != endp)
  194. return (0);
  195. memcpy(dst, tmp, NS_IN6ADDRSZ);
  196. return (1);
  197. }