Bug 1890277: part 4) Add CSPParser support for the `trusted-types` directive, guarded...
[gecko.git] / netwerk / dns / DNSPacket.h
blob943bcfe24678b66f28c530caf998fb3882d76034
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_net_DNSPacket_h__
6 #define mozilla_net_DNSPacket_h__
8 #include "mozilla/Maybe.h"
9 #include "mozilla/Result.h"
10 #include "nsClassHashtable.h"
11 #include "nsIDNSService.h"
12 #include "DNS.h"
13 #include "DNSByTypeRecord.h"
15 #include <functional>
17 namespace mozilla {
18 namespace net {
20 class DOHresp {
21 public:
22 nsresult Add(uint32_t TTL, unsigned char const* dns, unsigned int index,
23 uint16_t len, bool aLocalAllowed);
24 nsTArray<NetAddr> mAddresses;
25 uint32_t mTtl = 0;
28 // the values map to RFC1035 type identifiers
29 enum TrrType {
30 TRRTYPE_A = 1,
31 TRRTYPE_NS = 2,
32 TRRTYPE_CNAME = 5,
33 TRRTYPE_AAAA = 28,
34 TRRTYPE_OPT = 41,
35 TRRTYPE_TXT = 16,
36 TRRTYPE_HTTPSSVC = nsIDNSService::RESOLVE_TYPE_HTTPSSVC, // 65
39 class DNSPacket {
40 public:
41 // Never accept larger DOH responses than this as that would indicate
42 // something is wrong. Typical ones are much smaller.
43 static const unsigned int MAX_SIZE = 3200;
45 DNSPacket() = default;
46 virtual ~DNSPacket() = default;
48 Result<uint8_t, nsresult> GetRCode() const;
49 Result<bool, nsresult> RecursionAvailable() const;
51 // Called in order to feed data into the buffer.
52 nsresult OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
53 uint64_t aOffset, const uint32_t aCount);
55 // Encodes the name request into a buffer that represents a DNS packet
56 virtual nsresult EncodeRequest(nsCString& aBody, const nsACString& aHost,
57 uint16_t aType, bool aDisableECS);
59 // Decodes the DNS response and extracts the responses, additional records,
60 // etc. XXX: This should probably be refactored to reduce the number of
61 // output parameters and have a common format for different record types.
62 virtual nsresult Decode(
63 nsCString& aHost, enum TrrType aType, nsCString& aCname,
64 bool aAllowRFC1918, DOHresp& aResp, TypeRecordResultType& aTypeResult,
65 nsClassHashtable<nsCStringHashKey, DOHresp>& aAdditionalRecords,
66 uint32_t& aTTL);
68 void SetOriginHost(const Maybe<nsCString>& aHost) { mOriginHost = aHost; }
70 nsresult FillBuffer(std::function<int(unsigned char response[MAX_SIZE])>&&);
72 static nsresult ParseHTTPS(uint16_t aRDLen, struct SVCB& aParsed,
73 unsigned int aIndex, const unsigned char* aBuffer,
74 unsigned int aBodySize,
75 const nsACString& aOriginHost);
76 void SetNativePacket(bool aNative) { mNativePacket = aNative; }
78 protected:
79 nsresult PassQName(unsigned int& index, const unsigned char* aBuffer);
80 static nsresult GetQname(nsACString& aQname, unsigned int& aIndex,
81 const unsigned char* aBuffer,
82 unsigned int aBodySize);
83 static nsresult ParseSvcParam(unsigned int svcbIndex, uint16_t key,
84 SvcFieldValue& field, uint16_t length,
85 const unsigned char* aBuffer);
86 nsresult DecodeInternal(
87 nsCString& aHost, enum TrrType aType, nsCString& aCname,
88 bool aAllowRFC1918, DOHresp& aResp, TypeRecordResultType& aTypeResult,
89 nsClassHashtable<nsCStringHashKey, DOHresp>& aAdditionalRecords,
90 uint32_t& aTTL, const unsigned char* aBuffer, uint32_t aLen);
92 // The response buffer.
93 unsigned char mResponse[MAX_SIZE]{};
94 unsigned int mBodySize = 0;
95 // True when decoding a DNS packet received from OS. Decoding will
96 // not panic if packet ID is not zero.
97 bool mNativePacket = false;
98 nsresult mStatus = NS_OK;
99 Maybe<nsCString> mOriginHost;
102 } // namespace net
103 } // namespace mozilla
105 #endif // mozilla_net_DNSPacket_h__