---
title: llarp/dns/rr.hpp

---

# llarp/dns/rr.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::dns](Namespaces/namespacellarp_1_1dns.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| class | **[llarp::dns::RData](Classes/classllarp_1_1dns_1_1RData.md)**  |
| class | **[llarp::dns::ResourceRecord](Classes/classllarp_1_1dns_1_1ResourceRecord.md)**  |




## Source code

```cpp
#pragma once

#include "name.hpp"
#include <llarp/net/net_int.hpp>
#include <llarp/util/status.hpp>
#include <llarp/util/underlying.hpp>

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

namespace llarp::dns
{

  enum class RRType : uint16_t
  {
    SRV = 33,
    AAAA = 28,
    TXT = 16,
    MX = 15,
    PTR = 12,
    CNAME = 5,
    NS = 2,
    A = 1
  };

  std::optional<RRType>
  get_rr_type(uint16_t i);

  std::string
  ToString(RRType t);

  class RData
  {
    bstring_t _raw;

   public:
    RData() = default;
    RData(const RData&) = default;
    RData(RData&&) = default;

    RData&
    operator=(const RData&) = default;
    RData&
    operator=(RData&&) = default;

    explicit RData(byte_view_t& view);

    explicit RData(std::vector<std::string_view> dns_labels);
    explicit RData(net::ipv4addr_t ip);
    explicit RData(net::ipv6addr_t ip);

    explicit RData(uint16_t priority, std::vector<std::string_view> dns_labels);

    explicit RData(bstring_t&& raw);

    [[nodiscard]] constexpr const auto&
    data() const
    {
      return _raw;
    }

    std::vector<std::string_view>
    view_dns_labels() const;

    inline std::string
    as_dns_name() const
    {
      std::string name;

      for (auto part : view_dns_labels())
        name += fmt::format("{}.", part);

      return name.substr(0, name.size() - 2);
    }

    util::StatusObject
    to_json() const;

    std::string
    ToString() const;
  };

  class ResourceRecord
  {
    explicit ResourceRecord(bstring_t&&);

   public:
    ResourceRecord() = default;

    ResourceRecord(std::string name, RRType type, RData rdata, uint32_t ttl);

    explicit ResourceRecord(byte_view_t& raw);

    bstring_t
    encode_dns() const;

    util::StatusObject
    to_json() const;

    std::string
    ToString() const;

    bool
    HasCNameForTLD(const std::string& tld) const;

    std::vector<std::string> rr_name_labels;
    RRType rr_type;
    uint32_t rr_ttl;
    RData rr_data;
  };

  [[deprecated]] constexpr auto qTypePTR = to_underlying(RRType::PTR);
  [[deprecated]] constexpr auto qTypeA = to_underlying(RRType::A);
  [[deprecated]] constexpr auto qTypeAAAA = to_underlying(RRType::AAAA);
  [[deprecated]] constexpr auto qTypeSRV = to_underlying(RRType::SRV);
  [[deprecated]] constexpr auto qTypeCNAME = to_underlying(RRType::CNAME);
  [[deprecated]] constexpr auto qTypeMX = to_underlying(RRType::MX);
  [[deprecated]] constexpr auto qTypeNS = to_underlying(RRType::NS);
}  // namespace llarp::dns

template <>
constexpr inline bool llarp::IsToStringFormattable<llarp::dns::ResourceRecord> = true;

template <>
constexpr inline bool llarp::IsToStringFormattable<llarp::dns::RData> = true;

template <>
constexpr inline bool llarp::IsToStringFormattable<llarp::dns::RRType> = true;
```


-------------------------------

Updated on 2023-04-27 at 04:17:17 +0000
