ipv6.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file ipv6.c
  7. * \brief Functions for encoding and decoding IPv6 addresses
  8. *
  9. * (Because these functions are generic, they can also handle IPv4 addresses).
  10. **/
  11. #include "lib/net/ipv6.h"
  12. #include "lib/net/ipv4.h"
  13. #include "lib/string/util_string.h"
  14. #include "lib/string/compat_string.h"
  15. #include "lib/string/compat_ctype.h"
  16. #include "lib/string/printf.h"
  17. #include "lib/string/scanf.h"
  18. #include "lib/log/util_bug.h"
  19. #ifdef HAVE_ARPA_INET_H
  20. #include <arpa/inet.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <string.h>
  24. /** Given <b>af</b>==AF_INET and <b>src</b> a struct in_addr, or
  25. * <b>af</b>==AF_INET6 and <b>src</b> a struct in6_addr, try to format the
  26. * address and store it in the <b>len</b>-byte buffer <b>dst</b>. Returns
  27. * <b>dst</b> on success, NULL on failure.
  28. *
  29. * (Like inet_ntop(af,src,dst,len), but works on platforms that don't have it:
  30. * Tor sometimes needs to format ipv6 addresses even on platforms without ipv6
  31. * support.) */
  32. const char *
  33. tor_inet_ntop(int af, const void *src, char *dst, size_t len)
  34. {
  35. if (af == AF_INET) {
  36. if (tor_inet_ntoa(src, dst, len) < 0)
  37. return NULL;
  38. else
  39. return dst;
  40. } else if (af == AF_INET6) {
  41. const struct in6_addr *addr = src;
  42. char buf[64], *cp;
  43. int longestGapLen = 0, longestGapPos = -1, i,
  44. curGapPos = -1, curGapLen = 0;
  45. uint16_t words[8];
  46. for (i = 0; i < 8; ++i) {
  47. words[i] = (((uint16_t)addr->s6_addr[2*i])<<8) + addr->s6_addr[2*i+1];
  48. }
  49. if (words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 &&
  50. words[4] == 0 && ((words[5] == 0 && words[6] && words[7]) ||
  51. (words[5] == 0xffff))) {
  52. /* This is an IPv4 address. */
  53. if (words[5] == 0) {
  54. tor_snprintf(buf, sizeof(buf), "::%d.%d.%d.%d",
  55. addr->s6_addr[12], addr->s6_addr[13],
  56. addr->s6_addr[14], addr->s6_addr[15]);
  57. } else {
  58. tor_snprintf(buf, sizeof(buf), "::%x:%d.%d.%d.%d", words[5],
  59. addr->s6_addr[12], addr->s6_addr[13],
  60. addr->s6_addr[14], addr->s6_addr[15]);
  61. }
  62. if ((strlen(buf) + 1) > len) /* +1 for \0 */
  63. return NULL;
  64. strlcpy(dst, buf, len);
  65. return dst;
  66. }
  67. i = 0;
  68. while (i < 8) {
  69. if (words[i] == 0) {
  70. curGapPos = i++;
  71. curGapLen = 1;
  72. while (i<8 && words[i] == 0) {
  73. ++i; ++curGapLen;
  74. }
  75. if (curGapLen > longestGapLen) {
  76. longestGapPos = curGapPos;
  77. longestGapLen = curGapLen;
  78. }
  79. } else {
  80. ++i;
  81. }
  82. }
  83. if (longestGapLen<=1)
  84. longestGapPos = -1;
  85. cp = buf;
  86. for (i = 0; i < 8; ++i) {
  87. if (words[i] == 0 && longestGapPos == i) {
  88. if (i == 0)
  89. *cp++ = ':';
  90. *cp++ = ':';
  91. while (i < 8 && words[i] == 0)
  92. ++i;
  93. --i; /* to compensate for loop increment. */
  94. } else {
  95. tor_snprintf(cp, sizeof(buf)-(cp-buf), "%x", (unsigned)words[i]);
  96. cp += strlen(cp);
  97. if (i != 7)
  98. *cp++ = ':';
  99. }
  100. }
  101. *cp = '\0';
  102. if ((strlen(buf) + 1) > len) /* +1 for \0 */
  103. return NULL;
  104. strlcpy(dst, buf, len);
  105. return dst;
  106. } else {
  107. return NULL;
  108. }
  109. }
  110. /** Given <b>af</b>==AF_INET or <b>af</b>==AF_INET6, and a string <b>src</b>
  111. * encoding an IPv4 address or IPv6 address correspondingly, try to parse the
  112. * address and store the result in <b>dst</b> (which must have space for a
  113. * struct in_addr or a struct in6_addr, as appropriate). Return 1 on success,
  114. * 0 on a bad parse, and -1 on a bad <b>af</b>.
  115. *
  116. * (Like inet_pton(af,src,dst) but works on platforms that don't have it: Tor
  117. * sometimes needs to format ipv6 addresses even on platforms without ipv6
  118. * support.) */
  119. int
  120. tor_inet_pton(int af, const char *src, void *dst)
  121. {
  122. if (af == AF_INET) {
  123. return tor_inet_aton(src, dst);
  124. } else if (af == AF_INET6) {
  125. struct in6_addr *out = dst;
  126. uint16_t words[8];
  127. int gapPos = -1, i, setWords=0;
  128. const char *dot = strchr(src, '.');
  129. const char *eow; /* end of words. */
  130. memset(words, 0xf8, sizeof(words));
  131. if (dot == src)
  132. return 0;
  133. else if (!dot)
  134. eow = src+strlen(src);
  135. else {
  136. unsigned byte1,byte2,byte3,byte4;
  137. char more;
  138. for (eow = dot-1; eow > src && TOR_ISDIGIT(*eow); --eow)
  139. ;
  140. if (*eow != ':')
  141. return 0;
  142. ++eow;
  143. /* We use "scanf" because some platform inet_aton()s are too lax
  144. * about IPv4 addresses of the form "1.2.3" */
  145. if (tor_sscanf(eow, "%3u.%3u.%3u.%3u%c",
  146. &byte1,&byte2,&byte3,&byte4,&more) != 4)
  147. return 0;
  148. if (byte1 > 255 || byte2 > 255 || byte3 > 255 || byte4 > 255)
  149. return 0;
  150. words[6] = (byte1<<8) | byte2;
  151. words[7] = (byte3<<8) | byte4;
  152. setWords += 2;
  153. }
  154. i = 0;
  155. while (src < eow) {
  156. if (i > 7)
  157. return 0;
  158. if (TOR_ISXDIGIT(*src)) {
  159. char *next;
  160. ssize_t len;
  161. long r = strtol(src, &next, 16);
  162. if (next == NULL || next == src) {
  163. /* The 'next == src' error case can happen on versions of openbsd
  164. * which treat "0xfoo" as an error, rather than as "0" followed by
  165. * "xfoo". */
  166. return 0;
  167. }
  168. len = *next == '\0' ? eow - src : next - src;
  169. if (len > 4)
  170. return 0;
  171. if (len > 1 && !TOR_ISXDIGIT(src[1]))
  172. return 0; /* 0x is not valid */
  173. tor_assert(r >= 0);
  174. tor_assert(r < 65536);
  175. words[i++] = (uint16_t)r;
  176. setWords++;
  177. src = next;
  178. if (*src != ':' && src != eow)
  179. return 0;
  180. ++src;
  181. } else if (*src == ':' && i > 0 && gapPos == -1) {
  182. gapPos = i;
  183. ++src;
  184. } else if (*src == ':' && i == 0 && src+1 < eow && src[1] == ':' &&
  185. gapPos == -1) {
  186. gapPos = i;
  187. src += 2;
  188. } else {
  189. return 0;
  190. }
  191. }
  192. if (setWords > 8 ||
  193. (setWords == 8 && gapPos != -1) ||
  194. (setWords < 8 && gapPos == -1))
  195. return 0;
  196. if (gapPos >= 0) {
  197. int nToMove = setWords - (dot ? 2 : 0) - gapPos;
  198. int gapLen = 8 - setWords;
  199. tor_assert(nToMove >= 0);
  200. memmove(&words[gapPos+gapLen], &words[gapPos],
  201. sizeof(uint16_t)*nToMove);
  202. memset(&words[gapPos], 0, sizeof(uint16_t)*gapLen);
  203. }
  204. for (i = 0; i < 8; ++i) {
  205. out->s6_addr[2*i ] = words[i] >> 8;
  206. out->s6_addr[2*i+1] = words[i] & 0xff;
  207. }
  208. return 1;
  209. } else {
  210. return -1;
  211. }
  212. }