1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=4 sw=2 sts=2 et cin: */
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 #include "mozilla/net/DNS.h"
8 
9 #include "mozilla/Assertions.h"
10 #include "mozilla/mozalloc.h"
11 #include "mozilla/ArrayUtils.h"
12 #include "nsString.h"
13 #include <string.h>
14 
15 #ifdef XP_WIN
16 #  include "ws2tcpip.h"
17 #endif
18 
19 namespace mozilla {
20 namespace net {
21 
inet_ntop_internal(int af,const void * src,char * dst,socklen_t size)22 const char* inet_ntop_internal(int af, const void* src, char* dst,
23                                socklen_t size) {
24 #ifdef XP_WIN
25   if (af == AF_INET) {
26     struct sockaddr_in s;
27     memset(&s, 0, sizeof(s));
28     s.sin_family = AF_INET;
29     memcpy(&s.sin_addr, src, sizeof(struct in_addr));
30     int result = getnameinfo((struct sockaddr*)&s, sizeof(struct sockaddr_in),
31                              dst, size, nullptr, 0, NI_NUMERICHOST);
32     if (result == 0) {
33       return dst;
34     }
35   } else if (af == AF_INET6) {
36     struct sockaddr_in6 s;
37     memset(&s, 0, sizeof(s));
38     s.sin6_family = AF_INET6;
39     memcpy(&s.sin6_addr, src, sizeof(struct in_addr6));
40     int result = getnameinfo((struct sockaddr*)&s, sizeof(struct sockaddr_in6),
41                              dst, size, nullptr, 0, NI_NUMERICHOST);
42     if (result == 0) {
43       return dst;
44     }
45   }
46   return nullptr;
47 #else
48   return inet_ntop(af, src, dst, size);
49 #endif
50 }
51 
52 // Copies the contents of a PRNetAddr to a NetAddr.
53 // Does not do a ptr safety check!
PRNetAddrToNetAddr(const PRNetAddr * prAddr,NetAddr * addr)54 void PRNetAddrToNetAddr(const PRNetAddr* prAddr, NetAddr* addr) {
55   if (prAddr->raw.family == PR_AF_INET) {
56     addr->inet.family = AF_INET;
57     addr->inet.port = prAddr->inet.port;
58     addr->inet.ip = prAddr->inet.ip;
59   } else if (prAddr->raw.family == PR_AF_INET6) {
60     addr->inet6.family = AF_INET6;
61     addr->inet6.port = prAddr->ipv6.port;
62     addr->inet6.flowinfo = prAddr->ipv6.flowinfo;
63     memcpy(&addr->inet6.ip, &prAddr->ipv6.ip, sizeof(addr->inet6.ip.u8));
64     addr->inet6.scope_id = prAddr->ipv6.scope_id;
65   }
66 #if defined(XP_UNIX)
67   else if (prAddr->raw.family == PR_AF_LOCAL) {
68     addr->local.family = AF_LOCAL;
69     memcpy(addr->local.path, prAddr->local.path, sizeof(addr->local.path));
70   }
71 #endif
72 }
73 
74 // Copies the contents of a NetAddr to a PRNetAddr.
75 // Does not do a ptr safety check!
NetAddrToPRNetAddr(const NetAddr * addr,PRNetAddr * prAddr)76 void NetAddrToPRNetAddr(const NetAddr* addr, PRNetAddr* prAddr) {
77   if (addr->raw.family == AF_INET) {
78     prAddr->inet.family = PR_AF_INET;
79     prAddr->inet.port = addr->inet.port;
80     prAddr->inet.ip = addr->inet.ip;
81   } else if (addr->raw.family == AF_INET6) {
82     prAddr->ipv6.family = PR_AF_INET6;
83     prAddr->ipv6.port = addr->inet6.port;
84     prAddr->ipv6.flowinfo = addr->inet6.flowinfo;
85     memcpy(&prAddr->ipv6.ip, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
86     prAddr->ipv6.scope_id = addr->inet6.scope_id;
87   }
88 #if defined(XP_UNIX)
89   else if (addr->raw.family == AF_LOCAL) {
90     prAddr->local.family = PR_AF_LOCAL;
91     memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
92   }
93 #elif defined(XP_WIN)
94   else if (addr->raw.family == AF_LOCAL) {
95     prAddr->local.family = PR_AF_LOCAL;
96     memcpy(prAddr->local.path, addr->local.path, sizeof(addr->local.path));
97   }
98 #endif
99 }
100 
NetAddrToString(const NetAddr * addr,char * buf,uint32_t bufSize)101 bool NetAddrToString(const NetAddr* addr, char* buf, uint32_t bufSize) {
102   if (addr->raw.family == AF_INET) {
103     if (bufSize < INET_ADDRSTRLEN) {
104       return false;
105     }
106     struct in_addr nativeAddr = {};
107     nativeAddr.s_addr = addr->inet.ip;
108     return !!inet_ntop_internal(AF_INET, &nativeAddr, buf, bufSize);
109   }
110   if (addr->raw.family == AF_INET6) {
111     if (bufSize < INET6_ADDRSTRLEN) {
112       return false;
113     }
114     struct in6_addr nativeAddr = {};
115     memcpy(&nativeAddr.s6_addr, &addr->inet6.ip, sizeof(addr->inet6.ip.u8));
116     return !!inet_ntop_internal(AF_INET6, &nativeAddr, buf, bufSize);
117   }
118 #if defined(XP_UNIX)
119   else if (addr->raw.family == AF_LOCAL) {
120     if (bufSize < sizeof(addr->local.path)) {
121       // Many callers don't bother checking our return value, so
122       // null-terminate just in case.
123       if (bufSize > 0) {
124         buf[0] = '\0';
125       }
126       return false;
127     }
128 
129     // Usually, the size passed to memcpy should be the size of the
130     // destination. Here, we know that the source is no larger than the
131     // destination, so using the source's size is always safe, whereas
132     // using the destination's size may cause us to read off the end of the
133     // source.
134     memcpy(buf, addr->local.path, sizeof(addr->local.path));
135     return true;
136   }
137 #endif
138   return false;
139 }
140 
IsLoopBackAddress(const NetAddr * addr)141 bool IsLoopBackAddress(const NetAddr* addr) {
142   if (addr->raw.family == AF_INET) {
143     // Consider 127.0.0.1/8 as loopback
144     uint32_t ipv4Addr = ntohl(addr->inet.ip);
145     return (ipv4Addr >> 24) == 127;
146   }
147   if (addr->raw.family == AF_INET6) {
148     if (IPv6ADDR_IS_LOOPBACK(&addr->inet6.ip)) {
149       return true;
150     }
151     if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
152         IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) ==
153             htonl(INADDR_LOOPBACK)) {
154       return true;
155     }
156   }
157   return false;
158 }
159 
IsIPAddrAny(const NetAddr * addr)160 bool IsIPAddrAny(const NetAddr* addr) {
161   if (addr->raw.family == AF_INET) {
162     if (addr->inet.ip == htonl(INADDR_ANY)) {
163       return true;
164     }
165   } else if (addr->raw.family == AF_INET6) {
166     if (IPv6ADDR_IS_UNSPECIFIED(&addr->inet6.ip)) {
167       return true;
168     }
169     if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip) &&
170         IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip) == htonl(INADDR_ANY)) {
171       return true;
172     }
173   }
174   return false;
175 }
176 
IsIPAddrV4(const NetAddr * addr)177 bool IsIPAddrV4(const NetAddr* addr) { return addr->raw.family == AF_INET; }
178 
IsIPAddrV4Mapped(const NetAddr * addr)179 bool IsIPAddrV4Mapped(const NetAddr* addr) {
180   if (addr->raw.family == AF_INET6) {
181     return IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip);
182   }
183   return false;
184 }
185 
isLocalIPv4(uint32_t networkEndianIP)186 static bool isLocalIPv4(uint32_t networkEndianIP) {
187   uint32_t addr32 = ntohl(networkEndianIP);
188   if (addr32 >> 24 == 0x0A ||    // 10/8 prefix (RFC 1918).
189       addr32 >> 20 == 0xAC1 ||   // 172.16/12 prefix (RFC 1918).
190       addr32 >> 16 == 0xC0A8 ||  // 192.168/16 prefix (RFC 1918).
191       addr32 >> 16 == 0xA9FE) {  // 169.254/16 prefix (Link Local).
192     return true;
193   }
194   return false;
195 }
196 
IsIPAddrLocal(const NetAddr * addr)197 bool IsIPAddrLocal(const NetAddr* addr) {
198   MOZ_ASSERT(addr);
199 
200   // IPv4 RFC1918 and Link Local Addresses.
201   if (addr->raw.family == AF_INET) {
202     return isLocalIPv4(addr->inet.ip);
203   }
204   // IPv6 Unique and Link Local Addresses.
205   // or mapped IPv4 addresses
206   if (addr->raw.family == AF_INET6) {
207     uint16_t addr16 = ntohs(addr->inet6.ip.u16[0]);
208     if (addr16 >> 9 == 0xfc >> 1 ||    // fc00::/7 Unique Local Address.
209         addr16 >> 6 == 0xfe80 >> 6) {  // fe80::/10 Link Local Address.
210       return true;
211     }
212     if (IPv6ADDR_IS_V4MAPPED(&addr->inet6.ip)) {
213       return isLocalIPv4(IPv6ADDR_V4MAPPED_TO_IPADDR(&addr->inet6.ip));
214     }
215   }
216 
217   // Not an IPv4/6 local address.
218   return false;
219 }
220 
IsIPAddrShared(const NetAddr * addr)221 bool IsIPAddrShared(const NetAddr* addr) {
222   MOZ_ASSERT(addr);
223 
224   // IPv4 RFC6598.
225   if (addr->raw.family == AF_INET) {
226     uint32_t addr32 = ntohl(addr->inet.ip);
227     if (addr32 >> 22 == 0x644 >> 2) {  // 100.64/10 prefix (RFC 6598).
228       return true;
229     }
230   }
231 
232   // Not an IPv4 shared address.
233   return false;
234 }
235 
GetPort(const NetAddr * aAddr,uint16_t * aResult)236 nsresult GetPort(const NetAddr* aAddr, uint16_t* aResult) {
237   uint16_t port;
238   if (aAddr->raw.family == PR_AF_INET) {
239     port = aAddr->inet.port;
240   } else if (aAddr->raw.family == PR_AF_INET6) {
241     port = aAddr->inet6.port;
242   } else {
243     return NS_ERROR_NOT_INITIALIZED;
244   }
245 
246   *aResult = ntohs(port);
247   return NS_OK;
248 }
249 
operator ==(const NetAddr & other) const250 bool NetAddr::operator==(const NetAddr& other) const {
251   if (this->raw.family != other.raw.family) {
252     return false;
253   }
254   if (this->raw.family == AF_INET) {
255     return (this->inet.port == other.inet.port) &&
256            (this->inet.ip == other.inet.ip);
257   }
258   if (this->raw.family == AF_INET6) {
259     return (this->inet6.port == other.inet6.port) &&
260            (this->inet6.flowinfo == other.inet6.flowinfo) &&
261            (memcmp(&this->inet6.ip, &other.inet6.ip, sizeof(this->inet6.ip)) ==
262             0) &&
263            (this->inet6.scope_id == other.inet6.scope_id);
264 #if defined(XP_UNIX)
265   }
266   if (this->raw.family == AF_LOCAL) {
267     return PL_strncmp(this->local.path, other.local.path,
268                       ArrayLength(this->local.path));
269 #endif
270   }
271   return false;
272 }
273 
operator <(const NetAddr & other) const274 bool NetAddr::operator<(const NetAddr& other) const {
275   if (this->raw.family != other.raw.family) {
276     return this->raw.family < other.raw.family;
277   }
278   if (this->raw.family == AF_INET) {
279     if (this->inet.ip == other.inet.ip) {
280       return this->inet.port < other.inet.port;
281     }
282     return this->inet.ip < other.inet.ip;
283   }
284   if (this->raw.family == AF_INET6) {
285     int cmpResult =
286         memcmp(&this->inet6.ip, &other.inet6.ip, sizeof(this->inet6.ip));
287     if (cmpResult) {
288       return cmpResult < 0;
289     }
290     if (this->inet6.port != other.inet6.port) {
291       return this->inet6.port < other.inet6.port;
292     }
293     return this->inet6.flowinfo < other.inet6.flowinfo;
294   }
295   return false;
296 }
297 
NetAddrElement(const PRNetAddr * prNetAddr)298 NetAddrElement::NetAddrElement(const PRNetAddr* prNetAddr) {
299   this->mAddress.raw.family = 0;
300   this->mAddress.inet = {};
301   PRNetAddrToNetAddr(prNetAddr, &mAddress);
302 }
303 
NetAddrElement(const NetAddrElement & netAddr)304 NetAddrElement::NetAddrElement(const NetAddrElement& netAddr) {
305   mAddress = netAddr.mAddress;
306 }
307 
NetAddrElement(const NetAddr & netAddr)308 NetAddrElement::NetAddrElement(const NetAddr& netAddr) { mAddress = netAddr; }
309 
310 NetAddrElement::~NetAddrElement() = default;
311 
AddrInfo(const nsACString & host,const PRAddrInfo * prAddrInfo,bool disableIPv4,bool filterNameCollision,const nsACString & cname)312 AddrInfo::AddrInfo(const nsACString& host, const PRAddrInfo* prAddrInfo,
313                    bool disableIPv4, bool filterNameCollision,
314                    const nsACString& cname)
315     : mHostName(host),
316       mCanonicalName(cname),
317       ttl(NO_TTL_DATA),
318       mFromTRR(false),
319       mTrrFetchDuration(0),
320       mTrrFetchDurationNetworkOnly(0) {
321   MOZ_ASSERT(prAddrInfo,
322              "Cannot construct AddrInfo with a null prAddrInfo pointer!");
323   const uint32_t nameCollisionAddr = htonl(0x7f003535);  // 127.0.53.53
324 
325   PRNetAddr tmpAddr;
326   void* iter = nullptr;
327   do {
328     iter = PR_EnumerateAddrInfo(iter, prAddrInfo, 0, &tmpAddr);
329     bool addIt = iter && (!disableIPv4 || tmpAddr.raw.family != PR_AF_INET) &&
330                  (!filterNameCollision || tmpAddr.raw.family != PR_AF_INET ||
331                   (tmpAddr.inet.ip != nameCollisionAddr));
332     if (addIt) {
333       auto* addrElement = new NetAddrElement(&tmpAddr);
334       mAddresses.insertBack(addrElement);
335     }
336   } while (iter);
337 }
338 
AddrInfo(const nsACString & host,const nsACString & cname,unsigned int aTRR)339 AddrInfo::AddrInfo(const nsACString& host, const nsACString& cname,
340                    unsigned int aTRR)
341     : mHostName(host),
342       mCanonicalName(cname),
343       ttl(NO_TTL_DATA),
344       mFromTRR(aTRR),
345       mTrrFetchDuration(0),
346       mTrrFetchDurationNetworkOnly(0) {}
347 
AddrInfo(const nsACString & host,unsigned int aTRR)348 AddrInfo::AddrInfo(const nsACString& host, unsigned int aTRR)
349     : mHostName(host),
350       mCanonicalName(EmptyCString()),
351       ttl(NO_TTL_DATA),
352       mFromTRR(aTRR),
353       mTrrFetchDuration(0),
354       mTrrFetchDurationNetworkOnly(0) {}
355 
356 // deep copy constructor
AddrInfo(const AddrInfo * src)357 AddrInfo::AddrInfo(const AddrInfo* src) {
358   mHostName = src->mHostName;
359   mCanonicalName = src->mCanonicalName;
360   ttl = src->ttl;
361   mFromTRR = src->mFromTRR;
362   mTrrFetchDuration = src->mTrrFetchDuration;
363   mTrrFetchDurationNetworkOnly = src->mTrrFetchDurationNetworkOnly;
364 
365   for (auto element = src->mAddresses.getFirst(); element;
366        element = element->getNext()) {
367     AddAddress(new NetAddrElement(*element));
368   }
369 }
370 
371 AddrInfo::~AddrInfo() = default;
372 
AddAddress(NetAddrElement * address)373 void AddrInfo::AddAddress(NetAddrElement* address) {
374   MOZ_ASSERT(address, "Cannot add the address to an uninitialized list");
375 
376   mAddresses.insertBack(address);
377 }
378 
SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const379 size_t AddrInfo::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const {
380   size_t n = mallocSizeOf(this);
381   n += mHostName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
382   n += mCanonicalName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
383   n += mAddresses.sizeOfExcludingThis(mallocSizeOf);
384   return n;
385 }
386 
387 }  // namespace net
388 }  // namespace mozilla
389