Bug 850713 - Bump the required NDK version to 9. r=blassey.bugs,mh+mozilla
[gecko.git] / netwerk / dns / DNS.h
blobddedb913d0c01b9dca47c66274d57ddbb0979bf9
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef DNS_h_
6 #define DNS_h_
8 #include "nscore.h"
9 #include "prio.h"
10 #include "prnetdb.h"
11 #include "mozilla/LinkedList.h"
13 #if !defined(XP_WIN) && !defined(XP_OS2)
14 #include <arpa/inet.h>
15 #endif
17 #ifdef XP_WIN
18 #include "winsock2.h"
19 #endif
21 #define IPv6ADDR_IS_LOOPBACK(a) \
22 (((a)->u32[0] == 0) && \
23 ((a)->u32[1] == 0) && \
24 ((a)->u32[2] == 0) && \
25 ((a)->u8[12] == 0) && \
26 ((a)->u8[13] == 0) && \
27 ((a)->u8[14] == 0) && \
28 ((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))
38 #define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3])
40 #define IPv6ADDR_IS_UNSPECIFIED(a) \
41 (((a)->u32[0] == 0) && \
42 ((a)->u32[1] == 0) && \
43 ((a)->u32[2] == 0) && \
44 ((a)->u32[3] == 0))
46 namespace mozilla {
47 namespace net {
49 // Required buffer size for text form of an IP address.
50 // Includes space for null termination. We make our own contants
51 // because we don't want higher-level code depending on things
52 // like INET6_ADDRSTRLEN and having to include the associated
53 // platform-specific headers.
54 #ifdef XP_WIN
55 // Windows requires longer buffers for some reason.
56 const int kIPv4CStrBufSize = 22;
57 const int kIPv6CStrBufSize = 65;
58 #else
59 const int kIPv4CStrBufSize = 16;
60 const int kIPv6CStrBufSize = 46;
61 #endif
63 // This was all created at a time in which we were using NSPR for host
64 // resolution and we were propagating NSPR types like "PRAddrInfo" and
65 // "PRNetAddr" all over Gecko. This made it hard to use another host
66 // resolver -- we were locked into NSPR. The goal here is to get away
67 // from that. We'll translate what we get from NSPR or any other host
68 // resolution library into the types below and use them in Gecko.
70 union IPv6Addr {
71 uint8_t u8[16];
72 uint16_t u16[8];
73 uint32_t u32[4];
74 uint64_t u64[2];
77 // This struct is similar to operating system structs like "sockaddr", used for
78 // things like "connect" and "getsockname". When tempted to cast or do dumb
79 // copies of this struct to another struct, bear compiler-computed padding
80 // in mind. The size of this struct, and the layout of the data in it, may
81 // not be what you expect.
82 union NetAddr {
83 struct {
84 uint16_t family; /* address family (0x00ff maskable) */
85 char data[14]; /* raw address data */
86 } raw;
87 struct {
88 uint16_t family; /* address family (AF_INET) */
89 uint16_t port; /* port number */
90 uint32_t ip; /* The actual 32 bits of address */
91 } inet;
92 struct {
93 uint16_t family; /* address family (AF_INET6) */
94 uint16_t port; /* port number */
95 uint32_t flowinfo; /* routing information */
96 IPv6Addr ip; /* the actual 128 bits of address */
97 uint32_t scope_id; /* set of interfaces for a scope */
98 } inet6;
99 #if defined(XP_UNIX) || defined(XP_OS2)
100 struct { /* Unix domain socket address */
101 uint16_t family; /* address family (AF_UNIX) */
102 #ifdef XP_OS2
103 char path[108]; /* null-terminated pathname */
104 #else
105 char path[104]; /* null-terminated pathname */
106 #endif
107 } local;
108 #endif
111 // This class wraps a NetAddr union to provide C++ linked list
112 // capabilities and other methods. It is created from a PRNetAddr,
113 // which is converted to a mozilla::dns::NetAddr.
114 class NetAddrElement : public LinkedListElement<NetAddrElement> {
115 public:
116 NetAddrElement(const PRNetAddr *prNetAddr);
117 ~NetAddrElement();
119 NetAddr mAddress;
122 class AddrInfo {
123 public:
124 AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4);
125 ~AddrInfo();
127 char *mHostName;
128 LinkedList<NetAddrElement> mAddresses;
131 // Copies the contents of a PRNetAddr to a NetAddr.
132 // Does not do a ptr safety check!
133 void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr);
135 // Copies the contents of a NetAddr to a PRNetAddr.
136 // Does not do a ptr safety check!
137 void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr);
139 bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize);
141 bool IsLoopBackAddress(const NetAddr *addr);
143 bool IsIPAddrAny(const NetAddr *addr);
145 bool IsIPAddrV4Mapped(const NetAddr *addr);
147 } // namespace net
148 } // namespace mozilla
150 #endif // DNS_h_