Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/dns/TRR.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set sw=2 ts=8 et 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 mozilla_net_TRR_h
8
#define mozilla_net_TRR_h
9
10
#include "nsIChannel.h"
11
#include "nsIHttpPushListener.h"
12
#include "nsIInterfaceRequestor.h"
13
#include "nsIStreamListener.h"
14
15
namespace mozilla { namespace net {
16
17
// the values map to RFC1035 type identifiers
18
enum TrrType {
19
  TRRTYPE_A = 1,
20
  TRRTYPE_NS = 2,
21
  TRRTYPE_CNAME = 5,
22
  TRRTYPE_AAAA = 28,
23
  TRRTYPE_TXT = 16,
24
};
25
26
class DOHaddr : public LinkedListElement<DOHaddr> {
27
public:
28
  NetAddr mNet;
29
  uint32_t mTtl;
30
};
31
32
class TRRService;
33
extern TRRService *gTRRService;
34
35
class DOHresp {
36
public:
37
0
  ~DOHresp() {
38
0
    DOHaddr *el;
39
0
    while ((el = mAddresses.popLast())) {
40
0
      delete el;
41
0
    }
42
0
  }
43
  nsresult Add(uint32_t TTL, unsigned char *dns, int index, uint16_t len,
44
               bool aLocalAllowed);
45
  LinkedList<DOHaddr> mAddresses;
46
};
47
48
class TRR
49
  : public Runnable
50
  , public nsITimerCallback
51
  , public nsIHttpPushListener
52
  , public nsIInterfaceRequestor
53
  , public nsIStreamListener
54
{
55
public:
56
  NS_DECL_ISUPPORTS_INHERITED
57
  NS_DECL_NSIHTTPPUSHLISTENER
58
  NS_DECL_NSIINTERFACEREQUESTOR
59
  NS_DECL_NSIREQUESTOBSERVER
60
  NS_DECL_NSISTREAMLISTENER
61
  NS_DECL_NSITIMERCALLBACK
62
63
  // Never accept larger DOH responses than this as that would indicate
64
  // something is wrong. Typical ones are much smaller.
65
  static const unsigned int kMaxSize = 3200;
66
67
  // Number of "steps" we follow CNAME chains
68
  static const unsigned int kCnameChaseMax = 64;
69
70
  // when firing off a normal A or AAAA query
71
  explicit TRR(AHostResolver *aResolver,
72
               nsHostRecord *aRec,
73
               enum TrrType aType)
74
    : mozilla::Runnable("TRR")
75
    , mRec(aRec)
76
    , mHostResolver(aResolver)
77
    , mType(aType)
78
    , mBodySize(0)
79
    , mFailed(false)
80
    , mCnameLoop(kCnameChaseMax)
81
    , mAllowRFC1918(false)
82
    , mTxtTtl(UINT32_MAX)
83
0
  {
84
0
    mHost = aRec->host;
85
0
    mPB = aRec->pb;
86
0
  }
87
88
  // when following CNAMEs
89
  explicit TRR(AHostResolver *aResolver,
90
               nsHostRecord *aRec,
91
               nsCString &aHost,
92
               enum TrrType & aType,
93
               unsigned int aLoopCount,
94
               bool aPB)
95
    : mozilla::Runnable("TRR")
96
    , mHost(aHost)
97
    , mRec(aRec)
98
    , mHostResolver(aResolver)
99
    , mType(aType)
100
    , mBodySize(0)
101
    , mFailed(false)
102
    , mPB(aPB)
103
    , mCnameLoop(aLoopCount)
104
    , mAllowRFC1918(false)
105
    , mTxtTtl(UINT32_MAX)
106
0
  {
107
0
108
0
  }
109
110
  // used on push
111
  explicit TRR(AHostResolver *aResolver, bool aPB)
112
    : mozilla::Runnable("TRR")
113
    , mHostResolver(aResolver)
114
    , mType(TRRTYPE_A)
115
    , mBodySize(0)
116
    , mFailed(false)
117
    , mPB(aPB)
118
    , mCnameLoop(kCnameChaseMax)
119
    , mAllowRFC1918(false)
120
    , mTxtTtl(UINT32_MAX)
121
0
  { }
122
123
  // to verify a domain
124
  explicit TRR(AHostResolver *aResolver,
125
               nsACString &aHost,
126
               enum TrrType aType,
127
               bool aPB)
128
    : mozilla::Runnable("TRR")
129
    , mHost(aHost)
130
    , mHostResolver(aResolver)
131
    , mType(aType)
132
    , mBodySize(0)
133
    , mFailed(false)
134
    , mPB(aPB)
135
    , mCnameLoop(kCnameChaseMax)
136
    , mAllowRFC1918(false)
137
    , mTxtTtl(UINT32_MAX)
138
0
  { }
139
140
  NS_IMETHOD Run() override;
141
  void Cancel();
142
0
  enum TrrType Type() { return mType; }
143
  nsCString mHost;
144
  RefPtr<nsHostRecord> mRec;
145
  RefPtr<AHostResolver> mHostResolver;
146
147
private:
148
0
  ~TRR() = default;
149
  nsresult SendHTTPRequest();
150
  nsresult DohEncode(nsCString &target, bool aDisableECS);
151
  nsresult PassQName(unsigned int &index);
152
  nsresult GetQname(nsAutoCString &aQname, unsigned int &aIndex);
153
  nsresult DohDecode(nsCString &aHost);
154
  nsresult ReturnData();
155
156
  // FailData() must be called to signal that the asynch TRR resolve is
157
  // completed. For failed name resolves ("no such host"), the 'error' it
158
  // passses on in its argument must be NS_ERROR_UNKNOWN_HOST. Other errors
159
  // (if host was blacklisted, there as a bad content-type received, etc)
160
  // other error codes must be used. This distinction is important for the
161
  // subsequent logic to separate the error reasons.
162
  nsresult FailData(nsresult error);
163
  nsresult DohDecodeQuery(const nsCString &query,
164
                          nsCString &host, enum TrrType &type);
165
  nsresult ReceivePush(nsIHttpChannel *pushed, nsHostRecord *pushedRec);
166
  nsresult On200Response();
167
168
  nsCOMPtr<nsIChannel> mChannel;
169
  enum TrrType mType;
170
  TimeStamp mStartTime;
171
  unsigned char mResponse[kMaxSize];
172
  unsigned int mBodySize;
173
  bool mFailed;
174
  bool mPB;
175
  DOHresp mDNS;
176
  nsCOMPtr<nsITimer> mTimeout;
177
  nsCString mCname;
178
  uint32_t mCnameLoop; // loop detection counter
179
  bool mAllowRFC1918;
180
  nsTArray<nsCString> mTxt;
181
  uint32_t mTxtTtl;
182
};
183
184
} // namespace net
185
} // namespace mozilla
186
187
#endif // include guard