michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef DNS_h_ michael@0: #define DNS_h_ michael@0: michael@0: #include "nscore.h" michael@0: #include "prio.h" michael@0: #include "prnetdb.h" michael@0: #include "plstr.h" michael@0: #include "mozilla/LinkedList.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: #if !defined(XP_WIN) michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef XP_WIN michael@0: #include "winsock2.h" michael@0: #endif michael@0: michael@0: #define IPv6ADDR_IS_LOOPBACK(a) \ michael@0: (((a)->u32[0] == 0) && \ michael@0: ((a)->u32[1] == 0) && \ michael@0: ((a)->u32[2] == 0) && \ michael@0: ((a)->u8[12] == 0) && \ michael@0: ((a)->u8[13] == 0) && \ michael@0: ((a)->u8[14] == 0) && \ michael@0: ((a)->u8[15] == 0x1U)) michael@0: michael@0: #define IPv6ADDR_IS_V4MAPPED(a) \ michael@0: (((a)->u32[0] == 0) && \ michael@0: ((a)->u32[1] == 0) && \ michael@0: ((a)->u8[8] == 0) && \ michael@0: ((a)->u8[9] == 0) && \ michael@0: ((a)->u8[10] == 0xff) && \ michael@0: ((a)->u8[11] == 0xff)) michael@0: michael@0: #define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3]) michael@0: michael@0: #define IPv6ADDR_IS_UNSPECIFIED(a) \ michael@0: (((a)->u32[0] == 0) && \ michael@0: ((a)->u32[1] == 0) && \ michael@0: ((a)->u32[2] == 0) && \ michael@0: ((a)->u32[3] == 0)) michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: // Required buffer size for text form of an IP address. michael@0: // Includes space for null termination. We make our own contants michael@0: // because we don't want higher-level code depending on things michael@0: // like INET6_ADDRSTRLEN and having to include the associated michael@0: // platform-specific headers. michael@0: #ifdef XP_WIN michael@0: // Windows requires longer buffers for some reason. michael@0: const int kIPv4CStrBufSize = 22; michael@0: const int kIPv6CStrBufSize = 65; michael@0: const int kNetAddrMaxCStrBufSize = kIPv6CStrBufSize; michael@0: #else michael@0: const int kIPv4CStrBufSize = 16; michael@0: const int kIPv6CStrBufSize = 46; michael@0: const int kLocalCStrBufSize = 108; michael@0: const int kNetAddrMaxCStrBufSize = kLocalCStrBufSize; michael@0: #endif michael@0: michael@0: // This was all created at a time in which we were using NSPR for host michael@0: // resolution and we were propagating NSPR types like "PRAddrInfo" and michael@0: // "PRNetAddr" all over Gecko. This made it hard to use another host michael@0: // resolver -- we were locked into NSPR. The goal here is to get away michael@0: // from that. We'll translate what we get from NSPR or any other host michael@0: // resolution library into the types below and use them in Gecko. michael@0: michael@0: union IPv6Addr { michael@0: uint8_t u8[16]; michael@0: uint16_t u16[8]; michael@0: uint32_t u32[4]; michael@0: uint64_t u64[2]; michael@0: }; michael@0: michael@0: // This struct is similar to operating system structs like "sockaddr", used for michael@0: // things like "connect" and "getsockname". When tempted to cast or do dumb michael@0: // copies of this struct to another struct, bear compiler-computed padding michael@0: // in mind. The size of this struct, and the layout of the data in it, may michael@0: // not be what you expect. michael@0: union NetAddr { michael@0: struct { michael@0: uint16_t family; /* address family (0x00ff maskable) */ michael@0: char data[14]; /* raw address data */ michael@0: } raw; michael@0: struct { michael@0: uint16_t family; /* address family (AF_INET) */ michael@0: uint16_t port; /* port number */ michael@0: uint32_t ip; /* The actual 32 bits of address */ michael@0: } inet; michael@0: struct { michael@0: uint16_t family; /* address family (AF_INET6) */ michael@0: uint16_t port; /* port number */ michael@0: uint32_t flowinfo; /* routing information */ michael@0: IPv6Addr ip; /* the actual 128 bits of address */ michael@0: uint32_t scope_id; /* set of interfaces for a scope */ michael@0: } inet6; michael@0: #if defined(XP_UNIX) michael@0: struct { /* Unix domain socket address */ michael@0: uint16_t family; /* address family (AF_UNIX) */ michael@0: char path[104]; /* null-terminated pathname */ michael@0: } local; michael@0: #endif michael@0: // introduced to support nsTArray (for DNSRequestParent.cpp) michael@0: bool operator == (const NetAddr& other) const; michael@0: }; michael@0: michael@0: // This class wraps a NetAddr union to provide C++ linked list michael@0: // capabilities and other methods. It is created from a PRNetAddr, michael@0: // which is converted to a mozilla::dns::NetAddr. michael@0: class NetAddrElement : public LinkedListElement { michael@0: public: michael@0: NetAddrElement(const PRNetAddr *prNetAddr); michael@0: NetAddrElement(const NetAddrElement& netAddr); michael@0: ~NetAddrElement(); michael@0: michael@0: NetAddr mAddress; michael@0: }; michael@0: michael@0: class AddrInfo { michael@0: public: michael@0: // Creates an AddrInfo object. It calls the AddrInfo(const char*, const char*) michael@0: // to initialize the host and the cname. michael@0: AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4, michael@0: const char *cname); michael@0: michael@0: // Creates a basic AddrInfo object (initialize only the host and the cname). michael@0: AddrInfo(const char *host, const char *cname); michael@0: ~AddrInfo(); michael@0: michael@0: void AddAddress(NetAddrElement *address); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; michael@0: michael@0: char *mHostName; michael@0: char *mCanonicalName; michael@0: LinkedList mAddresses; michael@0: michael@0: private: michael@0: void Init(const char *host, const char *cname); michael@0: }; michael@0: michael@0: // Copies the contents of a PRNetAddr to a NetAddr. michael@0: // Does not do a ptr safety check! michael@0: void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr); michael@0: michael@0: // Copies the contents of a NetAddr to a PRNetAddr. michael@0: // Does not do a ptr safety check! michael@0: void NetAddrToPRNetAddr(const NetAddr *addr, PRNetAddr *prAddr); michael@0: michael@0: bool NetAddrToString(const NetAddr *addr, char *buf, uint32_t bufSize); michael@0: michael@0: bool IsLoopBackAddress(const NetAddr *addr); michael@0: michael@0: bool IsIPAddrAny(const NetAddr *addr); michael@0: michael@0: bool IsIPAddrV4Mapped(const NetAddr *addr); michael@0: michael@0: bool IsIPAddrLocal(const NetAddr *addr); michael@0: michael@0: nsresult GetPort(const NetAddr *aAddr, uint16_t *aResult); michael@0: michael@0: } // namespace net michael@0: } // namespace mozilla michael@0: michael@0: #endif // DNS_h_