inet_pton_winrt.cpp 6.2 KB

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