1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/dns/DNS.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef DNS_h_ 1.11 +#define DNS_h_ 1.12 + 1.13 +#include "nscore.h" 1.14 +#include "prio.h" 1.15 +#include "prnetdb.h" 1.16 +#include "plstr.h" 1.17 +#include "mozilla/LinkedList.h" 1.18 +#include "mozilla/MemoryReporting.h" 1.19 + 1.20 +#if !defined(XP_WIN) 1.21 +#include <arpa/inet.h> 1.22 +#endif 1.23 + 1.24 +#ifdef XP_WIN 1.25 +#include "winsock2.h" 1.26 +#endif 1.27 + 1.28 +#define IPv6ADDR_IS_LOOPBACK(a) \ 1.29 + (((a)->u32[0] == 0) && \ 1.30 + ((a)->u32[1] == 0) && \ 1.31 + ((a)->u32[2] == 0) && \ 1.32 + ((a)->u8[12] == 0) && \ 1.33 + ((a)->u8[13] == 0) && \ 1.34 + ((a)->u8[14] == 0) && \ 1.35 + ((a)->u8[15] == 0x1U)) 1.36 + 1.37 +#define IPv6ADDR_IS_V4MAPPED(a) \ 1.38 + (((a)->u32[0] == 0) && \ 1.39 + ((a)->u32[1] == 0) && \ 1.40 + ((a)->u8[8] == 0) && \ 1.41 + ((a)->u8[9] == 0) && \ 1.42 + ((a)->u8[10] == 0xff) && \ 1.43 + ((a)->u8[11] == 0xff)) 1.44 + 1.45 +#define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3]) 1.46 + 1.47 +#define IPv6ADDR_IS_UNSPECIFIED(a) \ 1.48 + (((a)->u32[0] == 0) && \ 1.49 + ((a)->u32[1] == 0) && \ 1.50 + ((a)->u32[2] == 0) && \ 1.51 + ((a)->u32[3] == 0)) 1.52 + 1.53 +namespace mozilla { 1.54 +namespace net { 1.55 + 1.56 +// Required buffer size for text form of an IP address. 1.57 +// Includes space for null termination. We make our own contants 1.58 +// because we don't want higher-level code depending on things 1.59 +// like INET6_ADDRSTRLEN and having to include the associated 1.60 +// platform-specific headers. 1.61 +#ifdef XP_WIN 1.62 +// Windows requires longer buffers for some reason. 1.63 +const int kIPv4CStrBufSize = 22; 1.64 +const int kIPv6CStrBufSize = 65; 1.65 +const int kNetAddrMaxCStrBufSize = kIPv6CStrBufSize; 1.66 +#else 1.67 +const int kIPv4CStrBufSize = 16; 1.68 +const int kIPv6CStrBufSize = 46; 1.69 +const int kLocalCStrBufSize = 108; 1.70 +const int kNetAddrMaxCStrBufSize = kLocalCStrBufSize; 1.71 +#endif 1.72 + 1.73 +// This was all created at a time in which we were using NSPR for host 1.74 +// resolution and we were propagating NSPR types like "PRAddrInfo" and 1.75 +// "PRNetAddr" all over Gecko. This made it hard to use another host 1.76 +// resolver -- we were locked into NSPR. The goal here is to get away 1.77 +// from that. We'll translate what we get from NSPR or any other host 1.78 +// resolution library into the types below and use them in Gecko. 1.79 + 1.80 +union IPv6Addr { 1.81 + uint8_t u8[16]; 1.82 + uint16_t u16[8]; 1.83 + uint32_t u32[4]; 1.84 + uint64_t u64[2]; 1.85 +}; 1.86 + 1.87 +// This struct is similar to operating system structs like "sockaddr", used for 1.88 +// things like "connect" and "getsockname". When tempted to cast or do dumb 1.89 +// copies of this struct to another struct, bear compiler-computed padding 1.90 +// in mind. The size of this struct, and the layout of the data in it, may 1.91 +// not be what you expect. 1.92 +union NetAddr { 1.93 + struct { 1.94 + uint16_t family; /* address family (0x00ff maskable) */ 1.95 + char data[14]; /* raw address data */ 1.96 + } raw; 1.97 + struct { 1.98 + uint16_t family; /* address family (AF_INET) */ 1.99 + uint16_t port; /* port number */ 1.100 + uint32_t ip; /* The actual 32 bits of address */ 1.101 + } inet; 1.102 + struct { 1.103 + uint16_t family; /* address family (AF_INET6) */ 1.104 + uint16_t port; /* port number */ 1.105 + uint32_t flowinfo; /* routing information */ 1.106 + IPv6Addr ip; /* the actual 128 bits of address */ 1.107 + uint32_t scope_id; /* set of interfaces for a scope */ 1.108 + } inet6; 1.109 +#if defined(XP_UNIX) 1.110 + struct { /* Unix domain socket address */ 1.111 + uint16_t family; /* address family (AF_UNIX) */ 1.112 + char path[104]; /* null-terminated pathname */ 1.113 + } local; 1.114 +#endif 1.115 + // introduced to support nsTArray<NetAddr> (for DNSRequestParent.cpp) 1.116 + bool operator == (const NetAddr& other) const; 1.117 +}; 1.118 + 1.119 +// This class wraps a NetAddr union to provide C++ linked list 1.120 +// capabilities and other methods. It is created from a PRNetAddr, 1.121 +// which is converted to a mozilla::dns::NetAddr. 1.122 +class NetAddrElement : public LinkedListElement<NetAddrElement> { 1.123 +public: 1.124 + NetAddrElement(const PRNetAddr *prNetAddr); 1.125 + NetAddrElement(const NetAddrElement& netAddr); 1.126 + ~NetAddrElement(); 1.127 + 1.128 + NetAddr mAddress; 1.129 +}; 1.130 + 1.131 +class AddrInfo { 1.132 +public: 1.133 + // Creates an AddrInfo object. It calls the AddrInfo(const char*, const char*) 1.134 + // to initialize the host and the cname. 1.135 + AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4, 1.136 + const char *cname); 1.137 + 1.138 + // Creates a basic AddrInfo object (initialize only the host and the cname). 1.139 + AddrInfo(const char *host, const char *cname); 1.140 + ~AddrInfo(); 1.141 + 1.142 + void AddAddress(NetAddrElement *address); 1.143 + 1.144 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.145 + 1.146 + char *mHostName; 1.147 + char *mCanonicalName; 1.148 + LinkedList<NetAddrElement> mAddresses; 1.149 + 1.150 +private: 1.151 + void Init(const char *host, const char *cname); 1.152 +}; 1.153 + 1.154 +// Copies the contents of a PRNetAddr to a NetAddr. 1.155 +// Does not do a ptr safety check! 1.156 +void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr); 1.157 + 1.158 +// Copies the contents of a NetAddr to a PRNetAddr. 1.159 +// Does not do a ptr safety check! 1.160 +void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr); 1.161 + 1.162 +bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize); 1.163 + 1.164 +bool IsLoopBackAddress(const NetAddr *addr); 1.165 + 1.166 +bool IsIPAddrAny(const NetAddr *addr); 1.167 + 1.168 +bool IsIPAddrV4Mapped(const NetAddr *addr); 1.169 + 1.170 +bool IsIPAddrLocal(const NetAddr *addr); 1.171 + 1.172 +nsresult GetPort(const NetAddr *aAddr, uint16_t *aResult); 1.173 + 1.174 +} // namespace net 1.175 +} // namespace mozilla 1.176 + 1.177 +#endif // DNS_h_