inaddr_st.h 2.6 KB

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