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"
11 #include "ScopedNSSTypes.h"
12 #include "nsClassHashtable.h"
13 #include "nsIDNSService.h"
15 #include "DNSByTypeRecord.h"
22 nsresult
Add(uint32_t TTL
, unsigned char const* dns
, unsigned int index
,
23 uint16_t len
, bool aLocalAllowed
);
24 nsTArray
<NetAddr
> mAddresses
;
28 // the values map to RFC1035 type identifiers
36 TRRTYPE_HTTPSSVC
= nsIDNSService::RESOLVE_TYPE_HTTPSSVC
, // 65
39 enum class DNSPacketStatus
: uint8_t {
52 DNSPacket() = default;
53 virtual ~DNSPacket() = default;
55 Result
<uint8_t, nsresult
> GetRCode() const;
56 Result
<bool, nsresult
> RecursionAvailable() const;
58 // Called in order to feed data into the buffer.
59 nsresult
OnDataAvailable(nsIRequest
* aRequest
, nsIInputStream
* aInputStream
,
60 uint64_t aOffset
, const uint32_t aCount
);
62 // Encodes the name request into a buffer that represents a DNS packet
63 virtual nsresult
EncodeRequest(nsCString
& aBody
, const nsACString
& aHost
,
64 uint16_t aType
, bool aDisableECS
);
66 // Decodes the DNS response and extracts the responses, additional records,
67 // etc. XXX: This should probably be refactored to reduce the number of
68 // output parameters and have a common format for different record types.
69 virtual nsresult
Decode(
70 nsCString
& aHost
, enum TrrType aType
, nsCString
& aCname
,
71 bool aAllowRFC1918
, DOHresp
& aResp
, TypeRecordResultType
& aTypeResult
,
72 nsClassHashtable
<nsCStringHashKey
, DOHresp
>& aAdditionalRecords
,
75 DNSPacketStatus
PacketStatus() const { return mStatus
; }
76 void SetOriginHost(const Maybe
<nsCString
>& aHost
) { mOriginHost
= aHost
; }
79 // Never accept larger DOH responses than this as that would indicate
80 // something is wrong. Typical ones are much smaller.
81 static const unsigned int MAX_SIZE
= 3200;
83 nsresult
PassQName(unsigned int& index
, const unsigned char* aBuffer
);
84 nsresult
GetQname(nsACString
& aQname
, unsigned int& aIndex
,
85 const unsigned char* aBuffer
);
86 nsresult
ParseSvcParam(unsigned int svcbIndex
, uint16_t key
,
87 SvcFieldValue
& field
, uint16_t length
,
88 const unsigned char* aBuffer
);
89 nsresult
DecodeInternal(
90 nsCString
& aHost
, enum TrrType aType
, nsCString
& aCname
,
91 bool aAllowRFC1918
, DOHresp
& aResp
, TypeRecordResultType
& aTypeResult
,
92 nsClassHashtable
<nsCStringHashKey
, DOHresp
>& aAdditionalRecords
,
93 uint32_t& aTTL
, const unsigned char* aBuffer
, uint32_t aLen
);
95 void SetDNSPacketStatus(DNSPacketStatus aStatus
) {
96 if (mStatus
== DNSPacketStatus::Unknown
||
97 mStatus
== DNSPacketStatus::Success
) {
102 // The response buffer.
103 unsigned char mResponse
[MAX_SIZE
]{};
104 unsigned int mBodySize
= 0;
105 DNSPacketStatus mStatus
= DNSPacketStatus::Unknown
;
106 Maybe
<nsCString
> mOriginHost
;
110 } // namespace mozilla
112 #endif // mozilla_net_DNSPacket_h__