ipv6.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.h
  7. * \brief Header for ipv6.c
  8. **/
  9. #ifndef TOR_IPV6_H
  10. #define TOR_IPV6_H
  11. #include "orconfig.h"
  12. #include <stddef.h>
  13. #ifdef HAVE_NETINET_IN6_H
  14. #include <netinet/in6.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <winsock2.h>
  18. #include <ws2tcpip.h>
  19. #include <windows.h>
  20. #endif
  21. #include "lib/cc/torint.h"
  22. /** Implementation of struct in6_addr for platforms that do not have it.
  23. * Generally, these platforms are ones without IPv6 support, but we want to
  24. * have a working in6_addr there anyway, so we can use it to parse IPv6
  25. * addresses. */
  26. #if !defined(HAVE_STRUCT_IN6_ADDR)
  27. struct in6_addr
  28. {
  29. union {
  30. uint8_t u6_addr8[16];
  31. uint16_t u6_addr16[8];
  32. uint32_t u6_addr32[4];
  33. } in6_u;
  34. #define s6_addr in6_u.u6_addr8
  35. #define s6_addr16 in6_u.u6_addr16
  36. #define s6_addr32 in6_u.u6_addr32
  37. };
  38. #endif /* !defined(HAVE_STRUCT_IN6_ADDR) */
  39. /** @{ */
  40. /** Many BSD variants seem not to define these. */
  41. #if defined(__APPLE__) || defined(__darwin__) || \
  42. defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
  43. #ifndef s6_addr16
  44. #define s6_addr16 __u6_addr.__u6_addr16
  45. #endif
  46. #ifndef s6_addr32
  47. #define s6_addr32 __u6_addr.__u6_addr32
  48. #endif
  49. #endif /* defined(__APPLE__) || defined(__darwin__) || ... */
  50. /** @} */
  51. #ifndef HAVE_SA_FAMILY_T
  52. typedef uint16_t sa_family_t;
  53. #endif
  54. /** @{ */
  55. /** Apparently, MS and Solaris don't define s6_addr16 or s6_addr32; these
  56. * macros get you a pointer to s6_addr32 or local equivalent. */
  57. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR32
  58. #define S6_ADDR32(x) ((uint32_t*)(x).s6_addr32)
  59. #else
  60. #define S6_ADDR32(x) ((uint32_t*)((char*)&(x).s6_addr))
  61. #endif
  62. #ifdef HAVE_STRUCT_IN6_ADDR_S6_ADDR16
  63. #define S6_ADDR16(x) ((uint16_t*)(x).s6_addr16)
  64. #else
  65. #define S6_ADDR16(x) ((uint16_t*)((char*)&(x).s6_addr))
  66. #endif
  67. /** @} */
  68. /** Implementation of struct sockaddr_in6 on platforms that do not have
  69. * it. See notes on struct in6_addr. */
  70. #if !defined(HAVE_STRUCT_SOCKADDR_IN6)
  71. struct sockaddr_in6 {
  72. sa_family_t sin6_family;
  73. uint16_t sin6_port;
  74. // uint32_t sin6_flowinfo;
  75. struct in6_addr sin6_addr;
  76. // uint32_t sin6_scope_id;
  77. };
  78. #endif /* !defined(HAVE_STRUCT_SOCKADDR_IN6) */
  79. const char *tor_inet_ntop(int af, const void *src, char *dst, size_t len);
  80. int tor_inet_pton(int af, const char *src, void *dst);
  81. #endif