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"
13 #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
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 // Encode the input host name into a sequence of labels.
56 static nsresult
EncodeHost(nsCString
& aBody
, const nsACString
& aHost
);
57 // Encodes the name request into a buffer that represents a DNS packet
58 virtual nsresult
EncodeRequest(nsCString
& aBody
, const nsACString
& aHost
,
59 uint16_t aType
, bool aDisableECS
);
61 // Decodes the DNS response and extracts the responses, additional records,
62 // etc. XXX: This should probably be refactored to reduce the number of
63 // output parameters and have a common format for different record types.
64 virtual nsresult
Decode(
65 nsCString
& aHost
, enum TrrType aType
, nsCString
& aCname
,
66 bool aAllowRFC1918
, DOHresp
& aResp
, TypeRecordResultType
& aTypeResult
,
67 nsClassHashtable
<nsCStringHashKey
, DOHresp
>& aAdditionalRecords
,
70 void SetOriginHost(const Maybe
<nsCString
>& aHost
) { mOriginHost
= aHost
; }
72 nsresult
FillBuffer(std::function
<int(unsigned char response
[MAX_SIZE
])>&&);
74 static nsresult
ParseHTTPS(uint16_t aRDLen
, struct SVCB
& aParsed
,
75 unsigned int aIndex
, const unsigned char* aBuffer
,
76 unsigned int aBodySize
,
77 const nsACString
& aOriginHost
);
78 void SetNativePacket(bool aNative
) { mNativePacket
= aNative
; }
80 static nsresult
GetQname(nsACString
& aQname
, unsigned int& aIndex
,
81 const unsigned char* aBuffer
,
82 unsigned int aBodySize
);
85 nsresult
PassQName(unsigned int& index
, const unsigned char* aBuffer
);
86 static 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 // The response buffer.
96 unsigned char mResponse
[MAX_SIZE
]{};
97 unsigned int mBodySize
= 0;
98 // True when decoding a DNS packet received from OS. Decoding will
99 // not panic if packet ID is not zero.
100 bool mNativePacket
= false;
101 nsresult mStatus
= NS_OK
;
102 Maybe
<nsCString
> mOriginHost
;
106 } // namespace mozilla
108 #endif // mozilla_net_DNSPacket_h__