inet_pton.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Copyright (c) 1996 by Internet Software Consortium.
  2. *
  3. * Permission to use, copy, modify, and distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  8. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  9. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  10. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  12. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  13. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. * SOFTWARE.
  15. */
  16. #include "apr_private.h"
  17. #include "apr_arch_networkio.h"
  18. #if APR_HAVE_SYS_TYPES_H
  19. #include <sys/types.h>
  20. #endif
  21. #if APR_HAVE_SYS_SOCKET_H
  22. #include <sys/socket.h>
  23. #endif
  24. #if APR_HAVE_NETINET_IN_H
  25. #include <netinet/in.h>
  26. #endif
  27. #if APR_HAVE_ARPA_INET_H
  28. #include <arpa/inet.h>
  29. #endif
  30. #include <string.h>
  31. #if APR_HAVE_ERRNO_H
  32. #include <errno.h>
  33. #endif
  34. #ifndef IN6ADDRSZ
  35. #define IN6ADDRSZ 16
  36. #endif
  37. #ifndef INT16SZ
  38. #define INT16SZ sizeof(apr_int16_t)
  39. #endif
  40. #ifndef INADDRSZ
  41. #define INADDRSZ 4
  42. #endif
  43. #ifndef __P
  44. #define __P(x) x
  45. #endif
  46. #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
  47. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  48. #endif
  49. /*
  50. * WARNING: Don't even consider trying to compile this on a system where
  51. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  52. */
  53. static int inet_pton4 __P((const char *src, unsigned char *dst));
  54. #if APR_HAVE_IPV6
  55. static int inet_pton6 __P((const char *src, unsigned char *dst));
  56. #endif
  57. /* int
  58. * inet_pton(af, src, dst)
  59. * convert from presentation format (which usually means ASCII printable)
  60. * to network format (which is usually some kind of binary format).
  61. * return:
  62. * 1 if the address was valid for the specified address family
  63. * 0 if the address wasn't valid (`dst' is untouched in this case)
  64. * -1 if some other error occurred (`dst' is untouched in this case, too)
  65. * author:
  66. * Paul Vixie, 1996.
  67. */
  68. int
  69. apr_inet_pton(int af, const char *src, void *dst)
  70. {
  71. switch (af) {
  72. case AF_INET:
  73. return (inet_pton4(src, dst));
  74. #if APR_HAVE_IPV6
  75. case AF_INET6:
  76. return (inet_pton6(src, dst));
  77. #endif
  78. default:
  79. errno = EAFNOSUPPORT;
  80. return (-1);
  81. }
  82. /* NOTREACHED */
  83. }
  84. /* int
  85. * inet_pton4(src, dst)
  86. * like inet_aton() but without all the hexadecimal and shorthand.
  87. * return:
  88. * 1 if `src' is a valid dotted quad, else 0.
  89. * notice:
  90. * does not touch `dst' unless it's returning 1.
  91. * author:
  92. * Paul Vixie, 1996.
  93. */
  94. static int
  95. inet_pton4(const char *src, unsigned char *dst)
  96. {
  97. static const char digits[] = "0123456789";
  98. int saw_digit, octets, ch;
  99. unsigned char tmp[INADDRSZ], *tp;
  100. saw_digit = 0;
  101. octets = 0;
  102. *(tp = tmp) = 0;
  103. while ((ch = *src++) != '\0') {
  104. const char *pch;
  105. if ((pch = strchr(digits, ch)) != NULL) {
  106. unsigned int new = *tp * 10 + (unsigned int)(pch - digits);
  107. if (new > 255)
  108. return (0);
  109. *tp = new;
  110. if (! saw_digit) {
  111. if (++octets > 4)
  112. return (0);
  113. saw_digit = 1;
  114. }
  115. } else if (ch == '.' && saw_digit) {
  116. if (octets == 4)
  117. return (0);
  118. *++tp = 0;
  119. saw_digit = 0;
  120. } else
  121. return (0);
  122. }
  123. if (octets < 4)
  124. return (0);
  125. memcpy(dst, tmp, INADDRSZ);
  126. return (1);
  127. }
  128. #if APR_HAVE_IPV6
  129. /* int
  130. * inet_pton6(src, dst)
  131. * convert presentation level address to network order binary form.
  132. * return:
  133. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  134. * notice:
  135. * (1) does not touch `dst' unless it's returning 1.
  136. * (2) :: in a full address is silently ignored.
  137. * credit:
  138. * inspired by Mark Andrews.
  139. * author:
  140. * Paul Vixie, 1996.
  141. */
  142. static int
  143. inet_pton6(const char *src, unsigned char *dst)
  144. {
  145. static const char xdigits_l[] = "0123456789abcdef",
  146. xdigits_u[] = "0123456789ABCDEF";
  147. unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  148. const char *xdigits, *curtok;
  149. int ch, saw_xdigit;
  150. unsigned int val;
  151. memset((tp = tmp), '\0', IN6ADDRSZ);
  152. endp = tp + IN6ADDRSZ;
  153. colonp = NULL;
  154. /* Leading :: requires some special handling. */
  155. if (*src == ':')
  156. if (*++src != ':')
  157. return (0);
  158. curtok = src;
  159. saw_xdigit = 0;
  160. val = 0;
  161. while ((ch = *src++) != '\0') {
  162. const char *pch;
  163. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  164. pch = strchr((xdigits = xdigits_u), ch);
  165. if (pch != NULL) {
  166. val <<= 4;
  167. val |= (pch - xdigits);
  168. if (val > 0xffff)
  169. return (0);
  170. saw_xdigit = 1;
  171. continue;
  172. }
  173. if (ch == ':') {
  174. curtok = src;
  175. if (!saw_xdigit) {
  176. if (colonp)
  177. return (0);
  178. colonp = tp;
  179. continue;
  180. }
  181. if (tp + INT16SZ > endp)
  182. return (0);
  183. *tp++ = (unsigned char) (val >> 8) & 0xff;
  184. *tp++ = (unsigned char) val & 0xff;
  185. saw_xdigit = 0;
  186. val = 0;
  187. continue;
  188. }
  189. if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
  190. inet_pton4(curtok, tp) > 0) {
  191. tp += INADDRSZ;
  192. saw_xdigit = 0;
  193. break; /* '\0' was seen by inet_pton4(). */
  194. }
  195. return (0);
  196. }
  197. if (saw_xdigit) {
  198. if (tp + INT16SZ > endp)
  199. return (0);
  200. *tp++ = (unsigned char) (val >> 8) & 0xff;
  201. *tp++ = (unsigned char) val & 0xff;
  202. }
  203. if (colonp != NULL) {
  204. /*
  205. * Since some memmove()'s erroneously fail to handle
  206. * overlapping regions, we'll do the shift by hand.
  207. */
  208. const int n = tp - colonp;
  209. int i;
  210. for (i = 1; i <= n; i++) {
  211. endp[- i] = colonp[n - i];
  212. colonp[n - i] = 0;
  213. }
  214. tp = endp;
  215. }
  216. if (tp != endp)
  217. return (0);
  218. memcpy(dst, tmp, IN6ADDRSZ);
  219. return (1);
  220. }
  221. #endif