tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DNSPacket.h (3993B)


      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/. */
      4 
      5 #ifndef mozilla_net_DNSPacket_h__
      6 #define mozilla_net_DNSPacket_h__
      7 
      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"
     14 
     15 #include <functional>
     16 
     17 namespace mozilla {
     18 namespace net {
     19 
     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;
     26 };
     27 
     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
     37 };
     38 
     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;
     44 
     45  DNSPacket() = default;
     46  virtual ~DNSPacket() = default;
     47 
     48  Result<uint8_t, nsresult> GetRCode() const;
     49  Result<bool, nsresult> RecursionAvailable() const;
     50 
     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);
     54 
     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);
     60 
     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,
     68      uint32_t& aTTL);
     69 
     70  void SetOriginHost(const Maybe<nsCString>& aHost) { mOriginHost = aHost; }
     71 
     72  nsresult FillBuffer(std::function<int(unsigned char response[MAX_SIZE])>&&);
     73 
     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; }
     79 
     80  static nsresult GetQname(nsACString& aQname, unsigned int& aIndex,
     81                           const unsigned char* aBuffer,
     82                           unsigned int aBodySize);
     83 
     84 protected:
     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);
     94 
     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;
    103 };
    104 
    105 }  // namespace net
    106 }  // namespace mozilla
    107 
    108 #endif  // mozilla_net_DNSPacket_h__