inet_pton.c 5.3 KB

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