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/. */
14 #include "mozilla/LinkedList.h"
15 #include "mozilla/MemoryReporting.h"
18 #include <arpa/inet.h>
25 #define IPv6ADDR_IS_LOOPBACK(a) \
26 (((a)->u32[0] == 0) && \
27 ((a)->u32[1] == 0) && \
28 ((a)->u32[2] == 0) && \
29 ((a)->u8[12] == 0) && \
30 ((a)->u8[13] == 0) && \
31 ((a)->u8[14] == 0) && \
32 ((a)->u8[15] == 0x1U))
34 #define IPv6ADDR_IS_V4MAPPED(a) \
35 (((a)->u32[0] == 0) && \
36 ((a)->u32[1] == 0) && \
37 ((a)->u8[8] == 0) && \
38 ((a)->u8[9] == 0) && \
39 ((a)->u8[10] == 0xff) && \
40 ((a)->u8[11] == 0xff))
42 #define IPv6ADDR_V4MAPPED_TO_IPADDR(a) ((a)->u32[3])
44 #define IPv6ADDR_IS_UNSPECIFIED(a) \
45 (((a)->u32[0] == 0) && \
46 ((a)->u32[1] == 0) && \
47 ((a)->u32[2] == 0) && \
53 // Required buffer size for text form of an IP address.
54 // Includes space for null termination. We make our own contants
55 // because we don't want higher-level code depending on things
56 // like INET6_ADDRSTRLEN and having to include the associated
57 // platform-specific headers.
59 // Windows requires longer buffers for some reason.
60 const int kIPv4CStrBufSize
= 22;
61 const int kIPv6CStrBufSize
= 65;
62 const int kNetAddrMaxCStrBufSize
= kIPv6CStrBufSize
;
64 const int kIPv4CStrBufSize
= 16;
65 const int kIPv6CStrBufSize
= 46;
66 const int kLocalCStrBufSize
= 108;
67 const int kNetAddrMaxCStrBufSize
= kLocalCStrBufSize
;
70 // This was all created at a time in which we were using NSPR for host
71 // resolution and we were propagating NSPR types like "PRAddrInfo" and
72 // "PRNetAddr" all over Gecko. This made it hard to use another host
73 // resolver -- we were locked into NSPR. The goal here is to get away
74 // from that. We'll translate what we get from NSPR or any other host
75 // resolution library into the types below and use them in Gecko.
84 // This struct is similar to operating system structs like "sockaddr", used for
85 // things like "connect" and "getsockname". When tempted to cast or do dumb
86 // copies of this struct to another struct, bear compiler-computed padding
87 // in mind. The size of this struct, and the layout of the data in it, may
88 // not be what you expect.
91 uint16_t family
; /* address family (0x00ff maskable) */
92 char data
[14]; /* raw address data */
95 uint16_t family
; /* address family (AF_INET) */
96 uint16_t port
; /* port number */
97 uint32_t ip
; /* The actual 32 bits of address */
100 uint16_t family
; /* address family (AF_INET6) */
101 uint16_t port
; /* port number */
102 uint32_t flowinfo
; /* routing information */
103 IPv6Addr ip
; /* the actual 128 bits of address */
104 uint32_t scope_id
; /* set of interfaces for a scope */
107 struct { /* Unix domain socket address */
108 uint16_t family
; /* address family (AF_UNIX) */
109 char path
[104]; /* null-terminated pathname */
112 // introduced to support nsTArray<NetAddr> (for DNSRequestParent.cpp)
113 bool operator == (const NetAddr
& other
) const;
116 // This class wraps a NetAddr union to provide C++ linked list
117 // capabilities and other methods. It is created from a PRNetAddr,
118 // which is converted to a mozilla::dns::NetAddr.
119 class NetAddrElement
: public LinkedListElement
<NetAddrElement
> {
121 explicit NetAddrElement(const PRNetAddr
*prNetAddr
);
122 NetAddrElement(const NetAddrElement
& netAddr
);
130 // Creates an AddrInfo object. It calls the AddrInfo(const char*, const char*)
131 // to initialize the host and the cname.
132 AddrInfo(const char *host
, const PRAddrInfo
*prAddrInfo
, bool disableIPv4
,
135 // Creates a basic AddrInfo object (initialize only the host and the cname).
136 AddrInfo(const char *host
, const char *cname
);
139 void AddAddress(NetAddrElement
*address
);
141 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
144 char *mCanonicalName
;
146 static const uint16_t NO_TTL_DATA
= (uint16_t) -1;
148 LinkedList
<NetAddrElement
> mAddresses
;
151 void Init(const char *host
, const char *cname
);
154 // Copies the contents of a PRNetAddr to a NetAddr.
155 // Does not do a ptr safety check!
156 void PRNetAddrToNetAddr(const PRNetAddr
*prAddr
, NetAddr
*addr
);
158 // Copies the contents of a NetAddr to a PRNetAddr.
159 // Does not do a ptr safety check!
160 void NetAddrToPRNetAddr(const NetAddr
*addr
, PRNetAddr
*prAddr
);
162 bool NetAddrToString(const NetAddr
*addr
, char *buf
, uint32_t bufSize
);
164 bool IsLoopBackAddress(const NetAddr
*addr
);
166 bool IsIPAddrAny(const NetAddr
*addr
);
168 bool IsIPAddrV4Mapped(const NetAddr
*addr
);
170 bool IsIPAddrLocal(const NetAddr
*addr
);
172 nsresult
GetPort(const NetAddr
*aAddr
, uint16_t *aResult
);
175 } // namespace mozilla