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