summaryrefslogtreecommitdiffstats
path: root/netwerk/dns/HTTPSSVC.h
blob: 792f894a0202496c1b24ace4047a820bb754018f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef HTTPSSVC_h__
#define HTTPSSVC_h__

#include "nsIDNSByTypeRecord.h"
#include "mozilla/net/DNS.h"
#include "mozilla/Variant.h"
#include "mozilla/Maybe.h"
#include "nsHttp.h"

namespace mozilla {
namespace net {

class DNSHTTPSSVCRecordBase;

enum SvcParamKey : uint16_t {
  SvcParamKeyMandatory = 0,
  SvcParamKeyAlpn = 1,
  SvcParamKeyNoDefaultAlpn = 2,
  SvcParamKeyPort = 3,
  SvcParamKeyIpv4Hint = 4,
  SvcParamKeyEchConfig = 5,
  SvcParamKeyIpv6Hint = 6,
  SvcParamKeyODoHConfig = 32769,
};

inline bool IsValidSvcParamKey(uint16_t aKey) {
  return aKey <= SvcParamKeyIpv6Hint || aKey == SvcParamKeyODoHConfig;
}

struct SvcParamAlpn {
  bool operator==(const SvcParamAlpn& aOther) const {
    return mValue == aOther.mValue;
  }
  CopyableTArray<nsCString> mValue;
};

struct SvcParamNoDefaultAlpn {
  bool operator==(const SvcParamNoDefaultAlpn& aOther) const { return true; }
};

struct SvcParamPort {
  bool operator==(const SvcParamPort& aOther) const {
    return mValue == aOther.mValue;
  }
  uint16_t mValue;
};

struct SvcParamIpv4Hint {
  bool operator==(const SvcParamIpv4Hint& aOther) const {
    return mValue == aOther.mValue;
  }
  CopyableTArray<mozilla::net::NetAddr> mValue;
};

struct SvcParamEchConfig {
  bool operator==(const SvcParamEchConfig& aOther) const {
    return mValue == aOther.mValue;
  }
  nsCString mValue;
};

struct SvcParamIpv6Hint {
  bool operator==(const SvcParamIpv6Hint& aOther) const {
    return mValue == aOther.mValue;
  }
  CopyableTArray<mozilla::net::NetAddr> mValue;
};

struct SvcParamODoHConfig {
  bool operator==(const SvcParamODoHConfig& aOther) const {
    return mValue == aOther.mValue;
  }
  nsCString mValue;
};

using SvcParamType =
    mozilla::Variant<Nothing, SvcParamAlpn, SvcParamNoDefaultAlpn, SvcParamPort,
                     SvcParamIpv4Hint, SvcParamEchConfig, SvcParamIpv6Hint,
                     SvcParamODoHConfig>;

struct SvcFieldValue {
  bool operator==(const SvcFieldValue& aOther) const {
    return mValue == aOther.mValue;
  }
  SvcFieldValue() : mValue(AsVariant(Nothing{})) {}
  SvcParamType mValue;
};

struct SVCB {
  bool operator==(const SVCB& aOther) const {
    return mSvcFieldPriority == aOther.mSvcFieldPriority &&
           mSvcDomainName == aOther.mSvcDomainName &&
           mSvcFieldValue == aOther.mSvcFieldValue;
  }
  bool operator<(const SVCB& aOther) const;
  Maybe<uint16_t> GetPort() const;
  bool NoDefaultAlpn() const;
  void GetIPHints(CopyableTArray<mozilla::net::NetAddr>& aAddresses) const;
  nsTArray<std::tuple<nsCString, SupportedAlpnRank>> GetAllAlpn() const;
  uint16_t mSvcFieldPriority = 0;
  nsCString mSvcDomainName;
  nsCString mEchConfig;
  nsCString mODoHConfig;
  bool mHasIPHints = false;
  bool mHasEchConfig = false;
  CopyableTArray<SvcFieldValue> mSvcFieldValue;
};

struct SVCBWrapper {
  explicit SVCBWrapper(const SVCB& aRecord) : mRecord(aRecord) {}
  Maybe<std::tuple<nsCString, SupportedAlpnRank>> mAlpn;
  const SVCB& mRecord;
};

class SVCBRecord : public nsISVCBRecord {
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSISVCBRECORD
 public:
  explicit SVCBRecord(const SVCB& data)
      : mData(data), mPort(Nothing()), mAlpn(Nothing()) {}
  explicit SVCBRecord(const SVCB& data,
                      Maybe<std::tuple<nsCString, SupportedAlpnRank>> aAlpn);

 private:
  friend class DNSHTTPSSVCRecordBase;

  virtual ~SVCBRecord() = default;

  SVCB mData;
  Maybe<uint16_t> mPort;
  Maybe<std::tuple<nsCString, SupportedAlpnRank>> mAlpn;
};

class DNSHTTPSSVCRecordBase {
 public:
  explicit DNSHTTPSSVCRecordBase(const nsACString& aHost) : mHost(aHost) {}

 protected:
  virtual ~DNSHTTPSSVCRecordBase() = default;

  already_AddRefed<nsISVCBRecord> GetServiceModeRecordInternal(
      bool aNoHttp2, bool aNoHttp3, const nsTArray<SVCB>& aRecords,
      bool& aRecordsAllExcluded, bool aCheckHttp3ExcludedList = true);

  bool HasIPAddressesInternal(const nsTArray<SVCB>& aRecords);

  void GetAllRecordsWithEchConfigInternal(
      bool aNoHttp2, bool aNoHttp3, const nsTArray<SVCB>& aRecords,
      bool* aAllRecordsHaveEchConfig, bool* aAllRecordsInH3ExcludedList,
      nsTArray<RefPtr<nsISVCBRecord>>& aResult,
      bool aCheckHttp3ExcludedList = true);

  // The owner name of this HTTPS RR.
  nsCString mHost;
};

}  // namespace net
}  // namespace mozilla

#endif  // HTTPSSVC_h__