ipv6.h 2.5 KB

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