DNS.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef DNS_h_
  6. #define DNS_h_
  7. #include "nscore.h"
  8. #include "prio.h"
  9. #include "prnetdb.h"
  10. #include "plstr.h"
  11. #include "mozilla/LinkedList.h"
  12. #include "mozilla/MemoryReporting.h"
  13. #if !defined(XP_WIN)
  14. #include <arpa/inet.h>
  15. #endif
  16. #ifdef XP_WIN
  17. #include "winsock2.h"
  18. #endif
  19. #ifndef AF_LOCAL
  20. #define AF_LOCAL 1 // used for named pipe
  21. #endif
  22. #define IPv6ADDR_IS_LOOPBACK(a) \
  23. (((a)->u32[0] == 0) && \
  24. ((a)->u32[1] == 0) && \
  25. ((a)->u32[2] == 0) && \
  26. ((a)->u8[12] == 0) && \
  27. ((a)->u8[13] == 0) && \
  28. ((a)->u8[14] == 0) && \
  29. ((a)->u8[15] == 0x1U))
  30. #define IPv6ADDR_IS_V4MAPPED(a) \
  31. (((a)->u32[0] == 0) && \
  32. ((a)->u32[1] == 0) && \
  33. ((a)->u8[8] == 0) && \
  34. ((a)->u8[9] == 0) && \
  35. ((a)->u8[10] == 0xff) && \
  36. ((a)->u8[11] == 0xff))
  37. #define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3])
  38. #define IPv6ADDR_IS_UNSPECIFIED(a) \
  39. (((a)->u32[0] == 0) && \
  40. ((a)->u32[1] == 0) && \
  41. ((a)->u32[2] == 0) && \
  42. ((a)->u32[3] == 0))
  43. namespace mozilla {
  44. namespace net {
  45. // Required buffer size for text form of an IP address.
  46. // Includes space for null termination. We make our own contants
  47. // because we don't want higher-level code depending on things
  48. // like INET6_ADDRSTRLEN and having to include the associated
  49. // platform-specific headers.
  50. #ifdef XP_WIN
  51. // Windows requires longer buffers for some reason.
  52. const int kIPv4CStrBufSize = 22;
  53. const int kIPv6CStrBufSize = 65;
  54. const int kNetAddrMaxCStrBufSize = kIPv6CStrBufSize;
  55. #else
  56. const int kIPv4CStrBufSize = 16;
  57. const int kIPv6CStrBufSize = 46;
  58. const int kLocalCStrBufSize = 108;
  59. const int kNetAddrMaxCStrBufSize = kLocalCStrBufSize;
  60. #endif
  61. // This was all created at a time in which we were using NSPR for host
  62. // resolution and we were propagating NSPR types like "PRAddrInfo" and
  63. // "PRNetAddr" all over Gecko. This made it hard to use another host
  64. // resolver -- we were locked into NSPR. The goal here is to get away
  65. // from that. We'll translate what we get from NSPR or any other host
  66. // resolution library into the types below and use them in Gecko.
  67. union IPv6Addr {
  68. uint8_t u8[16];
  69. uint16_t u16[8];
  70. uint32_t u32[4];
  71. uint64_t u64[2];
  72. };
  73. // This struct is similar to operating system structs like "sockaddr", used for
  74. // things like "connect" and "getsockname". When tempted to cast or do dumb
  75. // copies of this struct to another struct, bear compiler-computed padding
  76. // in mind. The size of this struct, and the layout of the data in it, may
  77. // not be what you expect.
  78. union NetAddr {
  79. struct {
  80. uint16_t family; /* address family (0x00ff maskable) */
  81. char data[14]; /* raw address data */
  82. } raw;
  83. struct {
  84. uint16_t family; /* address family (AF_INET) */
  85. uint16_t port; /* port number */
  86. uint32_t ip; /* The actual 32 bits of address */
  87. } inet;
  88. struct {
  89. uint16_t family; /* address family (AF_INET6) */
  90. uint16_t port; /* port number */
  91. uint32_t flowinfo; /* routing information */
  92. IPv6Addr ip; /* the actual 128 bits of address */
  93. uint32_t scope_id; /* set of interfaces for a scope */
  94. } inet6;
  95. #if defined(XP_UNIX) || defined(XP_WIN)
  96. struct { /* Unix domain socket or
  97. Windows Named Pipes address */
  98. uint16_t family; /* address family (AF_UNIX) */
  99. char path[104]; /* null-terminated pathname */
  100. } local;
  101. #endif
  102. // introduced to support nsTArray<NetAddr> comparisons and sorting
  103. bool operator == (const NetAddr& other) const;
  104. bool operator < (const NetAddr &other) const;
  105. };
  106. // This class wraps a NetAddr union to provide C++ linked list
  107. // capabilities and other methods. It is created from a PRNetAddr,
  108. // which is converted to a mozilla::dns::NetAddr.
  109. class NetAddrElement : public LinkedListElement<NetAddrElement> {
  110. public:
  111. explicit NetAddrElement(const PRNetAddr *prNetAddr);
  112. NetAddrElement(const NetAddrElement& netAddr);
  113. ~NetAddrElement();
  114. NetAddr mAddress;
  115. };
  116. class AddrInfo {
  117. public:
  118. // Creates an AddrInfo object. It calls the AddrInfo(const char*, const char*)
  119. // to initialize the host and the cname.
  120. AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4,
  121. bool filterNameCollision, const char *cname);
  122. // Creates a basic AddrInfo object (initialize only the host and the cname).
  123. AddrInfo(const char *host, const char *cname);
  124. ~AddrInfo();
  125. void AddAddress(NetAddrElement *address);
  126. size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
  127. char *mHostName;
  128. char *mCanonicalName;
  129. uint16_t ttl;
  130. static const uint16_t NO_TTL_DATA = (uint16_t) -1;
  131. LinkedList<NetAddrElement> mAddresses;
  132. private:
  133. void Init(const char *host, const char *cname);
  134. };
  135. // Copies the contents of a PRNetAddr to a NetAddr.
  136. // Does not do a ptr safety check!
  137. void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr);
  138. // Copies the contents of a NetAddr to a PRNetAddr.
  139. // Does not do a ptr safety check!
  140. void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr);
  141. bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize);
  142. bool IsLoopBackAddress(const NetAddr *addr);
  143. bool IsIPAddrAny(const NetAddr *addr);
  144. bool IsIPAddrV4Mapped(const NetAddr *addr);
  145. bool IsIPAddrLocal(const NetAddr *addr);
  146. nsresult GetPort(const NetAddr *aAddr, uint16_t *aResult);
  147. } // namespace net
  148. } // namespace mozilla
  149. #endif // DNS_h_