inet_pton_mingw.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /* Portions Copyright (c) Microsoft Open Technologies, Inc. */
  18. /*modifications by Microsoft Open Technologies, Inc. to implement missing inet_pton() from Windows 8 SDK */
  19. #if defined(__MINGW32__)
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <WS2tcpip.h>
  23. #include <Winsock2.h>
  24. #define ERRNO ((int)GetLastError())
  25. #define SET_ERRNO(x) (SetLastError((DWORD)(x)))
  26. #include "platform/win32/inet_pton_mingw.h"
  27. #define ENABLE_IPV6
  28. #define IN6ADDRSZ 16
  29. #define INADDRSZ 4
  30. #define INT16SZ 2
  31. /*
  32. * WARNING: Don't even consider trying to compile this on a system where
  33. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  34. */
  35. static int inet_pton4(const char *src, unsigned char *dst);
  36. #ifdef ENABLE_IPV6
  37. static int inet_pton6(const char *src, unsigned char *dst);
  38. #endif
  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. * notice:
  48. * On Windows we store the error in the thread errno, not
  49. * in the winsock error code. This is to avoid losing the
  50. * actual last winsock error. So use macro ERRNO to fetch the
  51. * errno this function sets when returning (-1), not SOCKERRNO.
  52. * author:
  53. * Paul Vixie, 1996.
  54. */
  55. int inet_pton(int af, const char *src, void *dst)
  56. {
  57. switch (af) {
  58. case AF_INET:
  59. return (inet_pton4(src, (unsigned char *)dst));
  60. #ifdef ENABLE_IPV6
  61. case AF_INET6:
  62. return (inet_pton6(src, (unsigned char *)dst));
  63. #endif
  64. default:
  65. SET_ERRNO(EAFNOSUPPORT);
  66. return (-1);
  67. }
  68. /* NOTREACHED */
  69. }
  70. /* int
  71. * inet_pton4(src, dst)
  72. * like inet_aton() but without all the hexadecimal and shorthand.
  73. * return:
  74. * 1 if `src' is a valid dotted quad, else 0.
  75. * notice:
  76. * does not touch `dst' unless it's returning 1.
  77. * author:
  78. * Paul Vixie, 1996.
  79. */
  80. static int
  81. inet_pton4(const char *src, unsigned char *dst)
  82. {
  83. static const char digits[] = "0123456789";
  84. int saw_digit, octets, ch;
  85. unsigned char tmp[INADDRSZ], *tp;
  86. saw_digit = 0;
  87. octets = 0;
  88. tp = tmp;
  89. *tp = 0;
  90. while((ch = *src++) != '\0') {
  91. const char *pch;
  92. if((pch = strchr(digits, ch)) != NULL) {
  93. unsigned int val = *tp * 10 + (unsigned int)(pch - digits);
  94. if(saw_digit && *tp == 0)
  95. return (0);
  96. if(val > 255)
  97. return (0);
  98. *tp = (unsigned char)val;
  99. if(! saw_digit) {
  100. if(++octets > 4)
  101. return (0);
  102. saw_digit = 1;
  103. }
  104. }
  105. else if(ch == '.' && saw_digit) {
  106. if(octets == 4)
  107. return (0);
  108. *++tp = 0;
  109. saw_digit = 0;
  110. }
  111. else
  112. return (0);
  113. }
  114. if(octets < 4)
  115. return (0);
  116. memcpy(dst, tmp, INADDRSZ);
  117. return (1);
  118. }
  119. #ifdef ENABLE_IPV6
  120. /* int
  121. * inet_pton6(src, dst)
  122. * convert presentation level address to network order binary form.
  123. * return:
  124. * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
  125. * notice:
  126. * (1) does not touch `dst' unless it's returning 1.
  127. * (2) :: in a full address is silently ignored.
  128. * credit:
  129. * inspired by Mark Andrews.
  130. * author:
  131. * Paul Vixie, 1996.
  132. */
  133. static int
  134. inet_pton6(const char *src, unsigned char *dst)
  135. {
  136. static const char xdigits_l[] = "0123456789abcdef",
  137. xdigits_u[] = "0123456789ABCDEF";
  138. unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  139. const char *xdigits, *curtok;
  140. int ch, saw_xdigit;
  141. size_t val;
  142. memset((tp = tmp), 0, IN6ADDRSZ);
  143. endp = tp + IN6ADDRSZ;
  144. colonp = NULL;
  145. /* Leading :: requires some special handling. */
  146. if(*src == ':')
  147. if(*++src != ':')
  148. return (0);
  149. curtok = src;
  150. saw_xdigit = 0;
  151. val = 0;
  152. while((ch = *src++) != '\0') {
  153. const char *pch;
  154. if((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  155. pch = strchr((xdigits = xdigits_u), ch);
  156. if(pch != NULL) {
  157. val <<= 4;
  158. val |= (pch - xdigits);
  159. if(++saw_xdigit > 4)
  160. return (0);
  161. continue;
  162. }
  163. if(ch == ':') {
  164. curtok = src;
  165. if(!saw_xdigit) {
  166. if(colonp)
  167. return (0);
  168. colonp = tp;
  169. continue;
  170. }
  171. if(tp + INT16SZ > endp)
  172. return (0);
  173. *tp++ = (unsigned char) (val >> 8) & 0xff;
  174. *tp++ = (unsigned char) val & 0xff;
  175. saw_xdigit = 0;
  176. val = 0;
  177. continue;
  178. }
  179. if(ch == '.' && ((tp + INADDRSZ) <= endp) &&
  180. inet_pton4(curtok, tp) > 0) {
  181. tp += INADDRSZ;
  182. saw_xdigit = 0;
  183. break; /* '\0' was seen by inet_pton4(). */
  184. }
  185. return (0);
  186. }
  187. if(saw_xdigit) {
  188. if(tp + INT16SZ > endp)
  189. return (0);
  190. *tp++ = (unsigned char) (val >> 8) & 0xff;
  191. *tp++ = (unsigned char) val & 0xff;
  192. }
  193. if(colonp != NULL) {
  194. /*
  195. * Since some memmove()'s erroneously fail to handle
  196. * overlapping regions, we'll do the shift by hand.
  197. */
  198. const ssize_t n = tp - colonp;
  199. ssize_t i;
  200. if(tp == endp)
  201. return (0);
  202. for(i = 1; i <= n; i++) {
  203. *(endp - i) = *(colonp + n - i);
  204. *(colonp + n - i) = 0;
  205. }
  206. tp = endp;
  207. }
  208. if(tp != endp)
  209. return (0);
  210. memcpy(dst, tmp, IN6ADDRSZ);
  211. return (1);
  212. }
  213. #endif /* ENABLE_IPV6 */
  214. #endif /* HAVE_INET_PTON */