ipv6.h 2.4 KB

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