Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / netwerk / dns / DNSPacket.h
blob12df52c1d751e647ad1c15866cf4b3140448070e
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 "pk11pub.h"
11 #include "ScopedNSSTypes.h"
12 #include "nsClassHashtable.h"
13 #include "nsIDNSService.h"
14 #include "DNS.h"
15 #include "DNSByTypeRecord.h"
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 enum class DNSPacketStatus : uint8_t {
40 Unknown = 0,
41 Success,
42 KeyNotAvailable,
43 KeyNotUsable,
44 EncodeError,
45 EncryptError,
46 DecodeError,
47 DecryptError,
50 class DNSPacket {
51 public:
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,
73 uint32_t& aTTL);
75 DNSPacketStatus PacketStatus() const { return mStatus; }
76 void SetOriginHost(const Maybe<nsCString>& aHost) { mOriginHost = aHost; }
78 protected:
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) {
98 mStatus = aStatus;
102 // The response buffer.
103 unsigned char mResponse[MAX_SIZE]{};
104 unsigned int mBodySize = 0;
105 DNSPacketStatus mStatus = DNSPacketStatus::Unknown;
106 Maybe<nsCString> mOriginHost;
109 } // namespace net
110 } // namespace mozilla
112 #endif // mozilla_net_DNSPacket_h__