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