inet_ntop_winrt.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 1996-2001 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
  9. * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Original code by Paul Vixie. "curlified" by Gisle Vanem.
  19. */
  20. /* Portions Copyright (c) Microsoft Open Technologies, Inc. */
  21. /*modifications by Microsoft Open Technologies, Inc. to implement missing inet_ntop() from Windows Phone 8 SDK */
  22. #include <errno.h>
  23. #include <assert.h>
  24. #if defined(_DEBUG)
  25. #define DEBUGASSERT(x) assert(x)
  26. #else
  27. #define DEBUGASSERT(x)
  28. #endif
  29. #define ERRNO ((int)GetLastError())
  30. #define SET_ERRNO(x) (SetLastError((DWORD)(x)))
  31. #define ENABLE_IPV6
  32. #ifndef HAVE_INET_NTOP
  33. #ifdef HAVE_SYS_PARAM_H
  34. #include <sys/param.h>
  35. #endif
  36. #ifdef HAVE_NETINET_IN_H
  37. #include <netinet/in.h>
  38. #endif
  39. #ifdef HAVE_ARPA_INET_H
  40. #include <arpa/inet.h>
  41. #endif
  42. #if 0
  43. #define _MPRINTF_REPLACE /* use our functions only */
  44. #include <curl/mprintf.h>
  45. #endif // 0
  46. //#include "inet_ntop.h"
  47. #define IN6ADDRSZ 16
  48. #define INADDRSZ 4
  49. #define INT16SZ 2
  50. /*
  51. * Format an IPv4 address, more or less like inet_ntoa().
  52. *
  53. * Returns `dst' (as a const)
  54. * Note:
  55. * - uses no statics
  56. * - takes a unsigned char* not an in_addr as input
  57. */
  58. static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
  59. {
  60. char tmp[sizeof "255.255.255.255"];
  61. size_t len;
  62. DEBUGASSERT(size >= 16);
  63. tmp[0] = '\0';
  64. (void)snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
  65. ((int)((unsigned char)src[0])) & 0xff,
  66. ((int)((unsigned char)src[1])) & 0xff,
  67. ((int)((unsigned char)src[2])) & 0xff,
  68. ((int)((unsigned char)src[3])) & 0xff);
  69. len = strlen(tmp);
  70. if(len == 0 || len >= size) {
  71. SET_ERRNO(ENOSPC);
  72. return (NULL);
  73. }
  74. strcpy(dst, tmp);
  75. return dst;
  76. }
  77. #ifdef ENABLE_IPV6
  78. /*
  79. * Convert IPv6 binary address into presentation (printable) format.
  80. */
  81. static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
  82. {
  83. /*
  84. * Note that int32_t and int16_t need only be "at least" large enough
  85. * to contain a value of the specified size. On some systems, like
  86. * Crays, there is no such thing as an integer variable with 16 bits.
  87. * Keep this in mind if you think this function should have been coded
  88. * to use pointer overlays. All the world's not a VAX.
  89. */
  90. char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  91. char *tp;
  92. struct {
  93. long base;
  94. long len;
  95. } best, cur;
  96. unsigned long words[IN6ADDRSZ / INT16SZ];
  97. int i;
  98. /* Preprocess:
  99. * Copy the input (bytewise) array into a wordwise array.
  100. * Find the longest run of 0x00's in src[] for :: shorthanding.
  101. */
  102. memset(words, '\0', sizeof(words));
  103. for(i = 0; i < IN6ADDRSZ; i++)
  104. words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
  105. best.base = -1;
  106. cur.base = -1;
  107. best.len = 0;
  108. cur.len = 0;
  109. for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  110. if(words[i] == 0) {
  111. if(cur.base == -1)
  112. cur.base = i, cur.len = 1;
  113. else
  114. cur.len++;
  115. }
  116. else if(cur.base != -1) {
  117. if(best.base == -1 || cur.len > best.len)
  118. best = cur;
  119. cur.base = -1;
  120. }
  121. }
  122. if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  123. best = cur;
  124. if(best.base != -1 && best.len < 2)
  125. best.base = -1;
  126. /* Format the result. */
  127. tp = tmp;
  128. for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  129. /* Are we inside the best run of 0x00's? */
  130. if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
  131. if(i == best.base)
  132. *tp++ = ':';
  133. continue;
  134. }
  135. /* Are we following an initial run of 0x00s or any real hex?
  136. */
  137. if(i != 0)
  138. *tp++ = ':';
  139. /* Is this address an encapsulated IPv4?
  140. */
  141. if(i == 6 && best.base == 0 &&
  142. (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  143. if(!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp))) {
  144. SET_ERRNO(ENOSPC);
  145. return (NULL);
  146. }
  147. tp += strlen(tp);
  148. break;
  149. }
  150. tp += snprintf(tp, 5, "%lx", words[i]);
  151. }
  152. /* Was it a trailing run of 0x00's?
  153. */
  154. if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  155. *tp++ = ':';
  156. *tp++ = '\0';
  157. /* Check for overflow, copy, and we're done.
  158. */
  159. if((size_t)(tp - tmp) > size) {
  160. SET_ERRNO(ENOSPC);
  161. return (NULL);
  162. }
  163. strcpy(dst, tmp);
  164. return dst;
  165. }
  166. #endif /* ENABLE_IPV6 */
  167. /*
  168. * Convert a network format address to presentation format.
  169. *
  170. * Returns pointer to presentation format address (`buf').
  171. * Returns NULL on error and errno set with the specific
  172. * error, EAFNOSUPPORT or ENOSPC.
  173. *
  174. * On Windows we store the error in the thread errno, not
  175. * in the winsock error code. This is to avoid losing the
  176. * actual last winsock error. So use macro ERRNO to fetch the
  177. * errno this function sets when returning NULL, not SOCKERRNO.
  178. */
  179. char *inet_ntop(int af, const void *src, char *buf, size_t size)
  180. {
  181. switch (af) {
  182. case AF_INET:
  183. return inet_ntop4((const unsigned char*)src, buf, size);
  184. #ifdef ENABLE_IPV6
  185. case AF_INET6:
  186. return inet_ntop6((const unsigned char*)src, buf, size);
  187. #endif
  188. default:
  189. SET_ERRNO(EAFNOSUPPORT);
  190. return NULL;
  191. }
  192. }
  193. #endif /* HAVE_INET_NTOP */