/contrib/bind9/lib/lwres/lwinetpton.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 214 lines · 130 code · 17 blank · 67 comment · 42 complexity · 6f38a306d82ec1175de584f4ea41b6d1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1996-2001 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 lwinetpton.c
  18. */
  19. #if defined(LIBC_SCCS) && !defined(lint)
  20. static char rcsid[] = "$Id$";
  21. #endif /* LIBC_SCCS and not lint */
  22. #include <config.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <lwres/net.h>
  26. #define NS_INT16SZ 2
  27. #define NS_INADDRSZ 4
  28. #define NS_IN6ADDRSZ 16
  29. /*
  30. * WARNING: Don't even consider trying to compile this on a system where
  31. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  32. */
  33. static int inet_pton4(const char *src, unsigned char *dst);
  34. static int inet_pton6(const char *src, unsigned char *dst);
  35. /*!
  36. * int
  37. * lwres_net_pton(af, src, dst)
  38. * convert from presentation format (which usually means ASCII printable)
  39. * to network format (which is usually some kind of binary format).
  40. * return:
  41. * 1 if the address was valid for the specified address family
  42. * 0 if the address wasn't valid (`dst' is untouched in this case)
  43. * -1 if some other error occurred (`dst' is untouched in this case, too)
  44. * author:
  45. * Paul Vixie, 1996.
  46. */
  47. int
  48. lwres_net_pton(int af, const char *src, void *dst) {
  49. switch (af) {
  50. case AF_INET:
  51. return (inet_pton4(src, dst));
  52. case AF_INET6:
  53. return (inet_pton6(src, dst));
  54. default:
  55. errno = EAFNOSUPPORT;
  56. return (-1);
  57. }
  58. /* NOTREACHED */
  59. }
  60. /*! int
  61. * inet_pton4(src, dst)
  62. * like inet_aton() but without all the hexadecimal and shorthand.
  63. * return:
  64. * 1 if `src' is a valid dotted quad, else 0.
  65. * notice:
  66. * does not touch `dst' unless it's returning 1.
  67. * author:
  68. * Paul Vixie, 1996.
  69. */
  70. static int
  71. inet_pton4(const char *src, unsigned char *dst) {
  72. static const char digits[] = "0123456789";
  73. int saw_digit, octets, ch;
  74. unsigned char tmp[NS_INADDRSZ], *tp;
  75. saw_digit = 0;
  76. octets = 0;
  77. *(tp = tmp) = 0;
  78. while ((ch = *src++) != '\0') {
  79. const char *pch;
  80. if ((pch = strchr(digits, ch)) != NULL) {
  81. unsigned int new = *tp * 10 + (pch - digits);
  82. if (new > 255)
  83. return (0);
  84. *tp = new;
  85. if (! saw_digit) {
  86. if (++octets > 4)
  87. return (0);
  88. saw_digit = 1;
  89. }
  90. } else if (ch == '.' && saw_digit) {
  91. if (octets == 4)
  92. return (0);
  93. /*
  94. * "clang --analyse" generates warnings using:
  95. * *++tp = 0;
  96. */
  97. tp++;
  98. *tp = 0;
  99. saw_digit = 0;
  100. } else
  101. return (0);
  102. }
  103. if (octets < 4)
  104. return (0);
  105. memcpy(dst, tmp, NS_INADDRSZ);
  106. return (1);
  107. }
  108. /*! int
  109. * inet_pton6(src, dst)
  110. * convert presentation level address to network order binary form.
  111. * return:
  112. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  113. * notice:
  114. * (1) does not touch `dst' unless it's returning 1.
  115. * (2) :: in a full address is silently ignored.
  116. * credit:
  117. * inspired by Mark Andrews.
  118. * author:
  119. * Paul Vixie, 1996.
  120. */
  121. static int
  122. inet_pton6(const char *src, unsigned char *dst) {
  123. static const char xdigits_l[] = "0123456789abcdef",
  124. xdigits_u[] = "0123456789ABCDEF";
  125. unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  126. const char *xdigits, *curtok;
  127. int ch, seen_xdigits;
  128. unsigned int val;
  129. memset((tp = tmp), '\0', NS_IN6ADDRSZ);
  130. endp = tp + NS_IN6ADDRSZ;
  131. colonp = NULL;
  132. /* Leading :: requires some special handling. */
  133. if (*src == ':')
  134. if (*++src != ':')
  135. return (0);
  136. curtok = src;
  137. seen_xdigits = 0;
  138. val = 0;
  139. while ((ch = *src++) != '\0') {
  140. const char *pch;
  141. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  142. pch = strchr((xdigits = xdigits_u), ch);
  143. if (pch != NULL) {
  144. val <<= 4;
  145. val |= (pch - xdigits);
  146. if (++seen_xdigits > 4)
  147. return (0);
  148. continue;
  149. }
  150. if (ch == ':') {
  151. curtok = src;
  152. if (!seen_xdigits) {
  153. if (colonp)
  154. return (0);
  155. colonp = tp;
  156. continue;
  157. }
  158. if (tp + NS_INT16SZ > endp)
  159. return (0);
  160. *tp++ = (unsigned char) (val >> 8) & 0xff;
  161. *tp++ = (unsigned char) val & 0xff;
  162. seen_xdigits = 0;
  163. val = 0;
  164. continue;
  165. }
  166. if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  167. inet_pton4(curtok, tp) > 0) {
  168. tp += NS_INADDRSZ;
  169. seen_xdigits = 0;
  170. break; /* '\0' was seen by inet_pton4(). */
  171. }
  172. return (0);
  173. }
  174. if (seen_xdigits) {
  175. if (tp + NS_INT16SZ > endp)
  176. return (0);
  177. *tp++ = (unsigned char) (val >> 8) & 0xff;
  178. *tp++ = (unsigned char) val & 0xff;
  179. }
  180. if (colonp != NULL) {
  181. /*
  182. * Since some memmove()'s erroneously fail to handle
  183. * overlapping regions, we'll do the shift by hand.
  184. */
  185. const int n = tp - colonp;
  186. int i;
  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. }