File: rec-protobuf.cc

package info (click to toggle)
pdns-recursor 4.0.4-1+deb9u3~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,484 kB
  • sloc: cpp: 36,380; sh: 11,771; makefile: 305; xml: 37
file content (74 lines) | stat: -rw-r--r-- 2,198 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#include "config.h"
#include "rec-protobuf.hh"

void RecProtoBufMessage::addRR(const DNSRecord& record)
{
#ifdef HAVE_PROTOBUF
  PBDNSMessage_DNSResponse* response = d_message.mutable_response();
  if (!response) {
    return;
  }

  if (record.d_place != DNSResourceRecord::ANSWER ||
      record.d_class != QClass::IN ||
      (record.d_type != QType::A &&
       record.d_type != QType::AAAA &&
       record.d_type != QType::CNAME)) {
    return;
  }

   PBDNSMessage_DNSResponse_DNSRR* pbRR = response->add_rrs();
   if (!pbRR) {
     return;
   }

   pbRR->set_name(record.d_name.toString());
   pbRR->set_type(record.d_type);
   pbRR->set_class_(record.d_class);
   pbRR->set_ttl(record.d_ttl);
   if (record.d_type == QType::A) {
     const ARecordContent& arc = dynamic_cast<const ARecordContent&>(*(record.d_content));
     ComboAddress data = arc.getCA();
     pbRR->set_rdata(&data.sin4.sin_addr.s_addr, sizeof(data.sin4.sin_addr.s_addr));
   }
   else if (record.d_type == QType::AAAA) {
     const AAAARecordContent& arc = dynamic_cast<const AAAARecordContent&>(*(record.d_content));
     ComboAddress data = arc.getCA();
     pbRR->set_rdata(&data.sin6.sin6_addr.s6_addr, sizeof(data.sin6.sin6_addr.s6_addr));
   } else if (record.d_type == QType::CNAME) {
     const CNAMERecordContent& crc = dynamic_cast<const CNAMERecordContent&>(*(record.d_content));
     DNSName data = crc.getTarget();
     pbRR->set_rdata(data.toString());
   }
#endif /* HAVE_PROTOBUF */
}

void RecProtoBufMessage::addRRs(const std::vector<DNSRecord>& records)
{
  for (const auto& record : records) {
    addRR(record);
  }
}

void RecProtoBufMessage::setAppliedPolicy(const std::string& policy)
{
#ifdef HAVE_PROTOBUF
  PBDNSMessage_DNSResponse* response = d_message.mutable_response();
  if (response && !policy.empty()) {
    response->set_appliedpolicy(policy);
  }
#endif /* HAVE_PROTOBUF */
}

void RecProtoBufMessage::setPolicyTags(const std::vector<std::string>& policyTags)
{
#ifdef HAVE_PROTOBUF
  PBDNSMessage_DNSResponse* response = d_message.mutable_response();
  if (response) {
    for (const auto& tag : policyTags) {
      response->add_tags(tag);
    }
  }
#endif /* HAVE_PROTOBUF */
}