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