Coverage Report

Created: 2018-09-25 14:53

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