inet.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1996-1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #if defined(_MSC_VER) && _MSC_VER < 1600
  20. # include "uv/stdint-msvc2008.h"
  21. #else
  22. # include <stdint.h>
  23. #endif
  24. #include "uv.h"
  25. #include "uv-common.h"
  26. #define UV__INET_ADDRSTRLEN 16
  27. #define UV__INET6_ADDRSTRLEN 46
  28. static int inet_ntop4(const unsigned char *src, char *dst, size_t size);
  29. static int inet_ntop6(const unsigned char *src, char *dst, size_t size);
  30. static int inet_pton4(const char *src, unsigned char *dst);
  31. static int inet_pton6(const char *src, unsigned char *dst);
  32. int uv_inet_ntop(int af, const void* src, char* dst, size_t size) {
  33. switch (af) {
  34. case AF_INET:
  35. return (inet_ntop4(src, dst, size));
  36. case AF_INET6:
  37. return (inet_ntop6(src, dst, size));
  38. default:
  39. return UV_EAFNOSUPPORT;
  40. }
  41. /* NOTREACHED */
  42. }
  43. static int inet_ntop4(const unsigned char *src, char *dst, size_t size) {
  44. static const char fmt[] = "%u.%u.%u.%u";
  45. char tmp[UV__INET_ADDRSTRLEN];
  46. int l;
  47. l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
  48. if (l <= 0 || (size_t) l >= size) {
  49. return UV_ENOSPC;
  50. }
  51. uv__strscpy(dst, tmp, size);
  52. return 0;
  53. }
  54. static int inet_ntop6(const unsigned char *src, char *dst, size_t size) {
  55. /*
  56. * Note that int32_t and int16_t need only be "at least" large enough
  57. * to contain a value of the specified size. On some systems, like
  58. * Crays, there is no such thing as an integer variable with 16 bits.
  59. * Keep this in mind if you think this function should have been coded
  60. * to use pointer overlays. All the world's not a VAX.
  61. */
  62. char tmp[UV__INET6_ADDRSTRLEN], *tp;
  63. struct { int base, len; } best, cur;
  64. unsigned int words[sizeof(struct in6_addr) / sizeof(uint16_t)];
  65. int i;
  66. /*
  67. * Preprocess:
  68. * Copy the input (bytewise) array into a wordwise array.
  69. * Find the longest run of 0x00's in src[] for :: shorthanding.
  70. */
  71. memset(words, '\0', sizeof words);
  72. for (i = 0; i < (int) sizeof(struct in6_addr); i++)
  73. words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  74. best.base = -1;
  75. best.len = 0;
  76. cur.base = -1;
  77. cur.len = 0;
  78. for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
  79. if (words[i] == 0) {
  80. if (cur.base == -1)
  81. cur.base = i, cur.len = 1;
  82. else
  83. cur.len++;
  84. } else {
  85. if (cur.base != -1) {
  86. if (best.base == -1 || cur.len > best.len)
  87. best = cur;
  88. cur.base = -1;
  89. }
  90. }
  91. }
  92. if (cur.base != -1) {
  93. if (best.base == -1 || cur.len > best.len)
  94. best = cur;
  95. }
  96. if (best.base != -1 && best.len < 2)
  97. best.base = -1;
  98. /*
  99. * Format the result.
  100. */
  101. tp = tmp;
  102. for (i = 0; i < (int) ARRAY_SIZE(words); i++) {
  103. /* Are we inside the best run of 0x00's? */
  104. if (best.base != -1 && i >= best.base &&
  105. i < (best.base + best.len)) {
  106. if (i == best.base)
  107. *tp++ = ':';
  108. continue;
  109. }
  110. /* Are we following an initial run of 0x00s or any real hex? */
  111. if (i != 0)
  112. *tp++ = ':';
  113. /* Is this address an encapsulated IPv4? */
  114. if (i == 6 && best.base == 0 && (best.len == 6 ||
  115. (best.len == 7 && words[7] != 0x0001) ||
  116. (best.len == 5 && words[5] == 0xffff))) {
  117. int err = inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp));
  118. if (err)
  119. return err;
  120. tp += strlen(tp);
  121. break;
  122. }
  123. tp += sprintf(tp, "%x", words[i]);
  124. }
  125. /* Was it a trailing run of 0x00's? */
  126. if (best.base != -1 && (best.base + best.len) == ARRAY_SIZE(words))
  127. *tp++ = ':';
  128. *tp++ = '\0';
  129. if (UV_E2BIG == uv__strscpy(dst, tmp, size))
  130. return UV_ENOSPC;
  131. return 0;
  132. }
  133. int uv_inet_pton(int af, const char* src, void* dst) {
  134. if (src == NULL || dst == NULL)
  135. return UV_EINVAL;
  136. switch (af) {
  137. case AF_INET:
  138. return (inet_pton4(src, dst));
  139. case AF_INET6: {
  140. int len;
  141. char tmp[UV__INET6_ADDRSTRLEN], *s, *p;
  142. s = (char*) src;
  143. p = strchr(src, '%');
  144. if (p != NULL) {
  145. s = tmp;
  146. len = p - src;
  147. if (len > UV__INET6_ADDRSTRLEN-1)
  148. return UV_EINVAL;
  149. memcpy(s, src, len);
  150. s[len] = '\0';
  151. }
  152. return inet_pton6(s, dst);
  153. }
  154. default:
  155. return UV_EAFNOSUPPORT;
  156. }
  157. /* NOTREACHED */
  158. }
  159. static int inet_pton4(const char *src, unsigned char *dst) {
  160. static const char digits[] = "0123456789";
  161. int saw_digit, octets, ch;
  162. unsigned char tmp[sizeof(struct in_addr)], *tp;
  163. saw_digit = 0;
  164. octets = 0;
  165. *(tp = tmp) = 0;
  166. while ((ch = *src++) != '\0') {
  167. const char *pch;
  168. if ((pch = strchr(digits, ch)) != NULL) {
  169. unsigned int nw = *tp * 10 + (pch - digits);
  170. if (saw_digit && *tp == 0)
  171. return UV_EINVAL;
  172. if (nw > 255)
  173. return UV_EINVAL;
  174. *tp = nw;
  175. if (!saw_digit) {
  176. if (++octets > 4)
  177. return UV_EINVAL;
  178. saw_digit = 1;
  179. }
  180. } else if (ch == '.' && saw_digit) {
  181. if (octets == 4)
  182. return UV_EINVAL;
  183. *++tp = 0;
  184. saw_digit = 0;
  185. } else
  186. return UV_EINVAL;
  187. }
  188. if (octets < 4)
  189. return UV_EINVAL;
  190. memcpy(dst, tmp, sizeof(struct in_addr));
  191. return 0;
  192. }
  193. static int inet_pton6(const char *src, unsigned char *dst) {
  194. static const char xdigits_l[] = "0123456789abcdef",
  195. xdigits_u[] = "0123456789ABCDEF";
  196. unsigned char tmp[sizeof(struct in6_addr)], *tp, *endp, *colonp;
  197. const char *xdigits, *curtok;
  198. int ch, seen_xdigits;
  199. unsigned int val;
  200. memset((tp = tmp), '\0', sizeof tmp);
  201. endp = tp + sizeof tmp;
  202. colonp = NULL;
  203. /* Leading :: requires some special handling. */
  204. if (*src == ':')
  205. if (*++src != ':')
  206. return UV_EINVAL;
  207. curtok = src;
  208. seen_xdigits = 0;
  209. val = 0;
  210. while ((ch = *src++) != '\0') {
  211. const char *pch;
  212. if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  213. pch = strchr((xdigits = xdigits_u), ch);
  214. if (pch != NULL) {
  215. val <<= 4;
  216. val |= (pch - xdigits);
  217. if (++seen_xdigits > 4)
  218. return UV_EINVAL;
  219. continue;
  220. }
  221. if (ch == ':') {
  222. curtok = src;
  223. if (!seen_xdigits) {
  224. if (colonp)
  225. return UV_EINVAL;
  226. colonp = tp;
  227. continue;
  228. } else if (*src == '\0') {
  229. return UV_EINVAL;
  230. }
  231. if (tp + sizeof(uint16_t) > endp)
  232. return UV_EINVAL;
  233. *tp++ = (unsigned char) (val >> 8) & 0xff;
  234. *tp++ = (unsigned char) val & 0xff;
  235. seen_xdigits = 0;
  236. val = 0;
  237. continue;
  238. }
  239. if (ch == '.' && ((tp + sizeof(struct in_addr)) <= endp)) {
  240. int err = inet_pton4(curtok, tp);
  241. if (err == 0) {
  242. tp += sizeof(struct in_addr);
  243. seen_xdigits = 0;
  244. break; /*%< '\\0' was seen by inet_pton4(). */
  245. }
  246. }
  247. return UV_EINVAL;
  248. }
  249. if (seen_xdigits) {
  250. if (tp + sizeof(uint16_t) > endp)
  251. return UV_EINVAL;
  252. *tp++ = (unsigned char) (val >> 8) & 0xff;
  253. *tp++ = (unsigned char) val & 0xff;
  254. }
  255. if (colonp != NULL) {
  256. /*
  257. * Since some memmove()'s erroneously fail to handle
  258. * overlapping regions, we'll do the shift by hand.
  259. */
  260. const int n = tp - colonp;
  261. int i;
  262. if (tp == endp)
  263. return UV_EINVAL;
  264. for (i = 1; i <= n; i++) {
  265. endp[- i] = colonp[n - i];
  266. colonp[n - i] = 0;
  267. }
  268. tp = endp;
  269. }
  270. if (tp != endp)
  271. return UV_EINVAL;
  272. memcpy(dst, tmp, sizeof tmp);
  273. return 0;
  274. }