]>
Commit | Line | Data |
---|---|---|
d9d3f9c1 RG |
1 | |
2 | #include "config.h" | |
3 | #include "rec-protobuf.hh" | |
4 | ||
d6399006 NC |
5 | #ifdef NOD_ENABLED |
6 | void RecProtoBufMessage::setNOD(bool nod) | |
7 | { | |
8 | #ifdef HAVE_PROTOBUF | |
9 | d_message.set_newlyobserveddomain(nod); | |
10 | #endif /* HAVE_PROTOBUF */ | |
11 | } | |
12 | ||
13 | void RecProtoBufMessage::clearUDR() | |
14 | { | |
15 | #ifdef HAVE_PROTOBUF | |
16 | auto response = d_message.mutable_response(); | |
17 | const int count = response->rrs_size(); | |
18 | for (int idx = 0; idx < count; idx++) { | |
19 | auto rr = response->mutable_rrs(idx); | |
20 | rr->set_udr(false); | |
21 | } | |
22 | #endif /* HAVE_PROTOBUF */ | |
23 | } | |
24 | #endif /* NOD_ENABLED */ | |
25 | ||
26 | #ifdef NOD_ENABLED | |
27 | void RecProtoBufMessage::addRR(const DNSRecord& record, const std::set<uint16_t>& exportTypes, bool udr) | |
28 | #else | |
0bd2e252 | 29 | void RecProtoBufMessage::addRR(const DNSRecord& record, const std::set<uint16_t>& exportTypes) |
d6399006 | 30 | #endif /* NOD_ENABLED */ |
d9d3f9c1 RG |
31 | { |
32 | #ifdef HAVE_PROTOBUF | |
33 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
34 | if (!response) { | |
35 | return; | |
36 | } | |
37 | ||
0bd2e252 | 38 | if (record.d_place != DNSResourceRecord::ANSWER || record.d_class != QClass::IN) { |
d9d3f9c1 RG |
39 | return; |
40 | } | |
41 | ||
0bd2e252 RG |
42 | if (exportTypes.count(record.d_type) == 0) { |
43 | return; | |
44 | } | |
45 | ||
46 | PBDNSMessage_DNSResponse_DNSRR* pbRR = response->add_rrs(); | |
47 | if (!pbRR) { | |
48 | return; | |
49 | } | |
50 | ||
51 | pbRR->set_name(record.d_name.toString()); | |
52 | pbRR->set_type(record.d_type); | |
53 | pbRR->set_class_(record.d_class); | |
54 | pbRR->set_ttl(record.d_ttl); | |
d6399006 NC |
55 | #ifdef NOD_ENABLED |
56 | pbRR->set_udr(udr); | |
57 | #endif | |
d9d3f9c1 | 58 | |
0bd2e252 RG |
59 | switch(record.d_type) { |
60 | case QType::A: | |
61 | { | |
62 | const auto& content = dynamic_cast<const ARecordContent&>(*(record.d_content)); | |
63 | ComboAddress data = content.getCA(); | |
64 | pbRR->set_rdata(&data.sin4.sin_addr.s_addr, sizeof(data.sin4.sin_addr.s_addr)); | |
65 | break; | |
66 | } | |
67 | case QType::AAAA: | |
68 | { | |
69 | const auto& content = dynamic_cast<const AAAARecordContent&>(*(record.d_content)); | |
70 | ComboAddress data = content.getCA(); | |
71 | pbRR->set_rdata(&data.sin6.sin6_addr.s6_addr, sizeof(data.sin6.sin6_addr.s6_addr)); | |
72 | break; | |
73 | } | |
74 | case QType::CNAME: | |
75 | { | |
76 | const auto& content = dynamic_cast<const CNAMERecordContent&>(*(record.d_content)); | |
77 | pbRR->set_rdata(content.getTarget().toString()); | |
78 | break; | |
79 | } | |
80 | case QType::TXT: | |
81 | { | |
82 | const auto& content = dynamic_cast<const TXTRecordContent&>(*(record.d_content)); | |
83 | pbRR->set_rdata(content.d_text); | |
84 | break; | |
85 | } | |
86 | case QType::NS: | |
87 | { | |
88 | const auto& content = dynamic_cast<const NSRecordContent&>(*(record.d_content)); | |
89 | pbRR->set_rdata(content.getNS().toString()); | |
90 | break; | |
91 | } | |
92 | case QType::PTR: | |
93 | { | |
94 | const auto& content = dynamic_cast<const PTRRecordContent&>(*(record.d_content)); | |
95 | pbRR->set_rdata(content.getContent().toString()); | |
96 | break; | |
97 | } | |
98 | case QType::MX: | |
99 | { | |
100 | const auto& content = dynamic_cast<const MXRecordContent&>(*(record.d_content)); | |
101 | pbRR->set_rdata(content.d_mxname.toString()); | |
102 | break; | |
103 | } | |
104 | case QType::SPF: | |
105 | { | |
106 | const auto& content = dynamic_cast<const SPFRecordContent&>(*(record.d_content)); | |
107 | pbRR->set_rdata(content.getText()); | |
108 | break; | |
109 | } | |
110 | case QType::SRV: | |
111 | { | |
112 | const auto& content = dynamic_cast<const SRVRecordContent&>(*(record.d_content)); | |
113 | pbRR->set_rdata(content.d_target.toString()); | |
114 | break; | |
115 | } | |
116 | default: | |
117 | break; | |
118 | } | |
d9d3f9c1 RG |
119 | #endif /* HAVE_PROTOBUF */ |
120 | } | |
121 | ||
0bd2e252 | 122 | void RecProtoBufMessage::addRRs(const std::vector<DNSRecord>& records, const std::set<uint16_t>& exportTypes) |
d9d3f9c1 RG |
123 | { |
124 | for (const auto& record : records) { | |
0bd2e252 | 125 | addRR(record, exportTypes); |
d9d3f9c1 RG |
126 | } |
127 | } | |
128 | ||
129 | void RecProtoBufMessage::setAppliedPolicy(const std::string& policy) | |
130 | { | |
131 | #ifdef HAVE_PROTOBUF | |
132 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
133 | if (response && !policy.empty()) { | |
134 | response->set_appliedpolicy(policy); | |
135 | } | |
136 | #endif /* HAVE_PROTOBUF */ | |
137 | } | |
138 | ||
f3da83fe RG |
139 | void RecProtoBufMessage::setAppliedPolicyType(const DNSFilterEngine::PolicyType& type) |
140 | { | |
141 | #ifdef HAVE_PROTOBUF | |
142 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
143 | if (response) { | |
144 | switch(type) { | |
145 | case DNSFilterEngine::PolicyType::None: | |
146 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_UNKNOWN); | |
147 | break; | |
148 | case DNSFilterEngine::PolicyType::QName: | |
149 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_QNAME); | |
150 | break; | |
151 | case DNSFilterEngine::PolicyType::ClientIP: | |
152 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_CLIENTIP); | |
153 | break; | |
154 | case DNSFilterEngine::PolicyType::ResponseIP: | |
155 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_RESPONSEIP); | |
156 | break; | |
157 | case DNSFilterEngine::PolicyType::NSDName: | |
158 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_NSDNAME); | |
159 | break; | |
160 | case DNSFilterEngine::PolicyType::NSIP: | |
161 | response->set_appliedpolicytype(PBDNSMessage_PolicyType_NSIP); | |
162 | break; | |
163 | default: | |
164 | throw std::runtime_error("Unsupported protobuf policy type"); | |
165 | } | |
166 | } | |
167 | #endif /* HAVE_PROTOBUF */ | |
168 | } | |
169 | ||
d9d3f9c1 RG |
170 | void RecProtoBufMessage::setPolicyTags(const std::vector<std::string>& policyTags) |
171 | { | |
172 | #ifdef HAVE_PROTOBUF | |
173 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
174 | if (response) { | |
175 | for (const auto& tag : policyTags) { | |
176 | response->add_tags(tag); | |
177 | } | |
178 | } | |
179 | #endif /* HAVE_PROTOBUF */ | |
180 | } | |
b790ef3d | 181 | |
dc11520c NC |
182 | void RecProtoBufMessage::addPolicyTag(const std::string& policyTag) |
183 | { | |
184 | #ifdef HAVE_PROTOBUF | |
185 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
186 | if (response) { | |
187 | response->add_tags(policyTag); | |
188 | } | |
189 | #endif | |
190 | } | |
191 | ||
192 | void RecProtoBufMessage::removePolicyTag(const std::string& policyTag) | |
193 | { | |
194 | #ifdef HAVE_PROTOBUF | |
195 | PBDNSMessage_DNSResponse* response = d_message.mutable_response(); | |
196 | if (response) { | |
197 | const int count = response->tags_size(); | |
198 | int keep = 0; | |
199 | for (int idx = 0; idx < count; ++idx) { | |
200 | auto tagp = response->mutable_tags(idx); | |
201 | if (tagp->compare(policyTag) == 0) { | |
202 | } | |
203 | else { | |
204 | if (keep < idx) { | |
205 | response->mutable_tags()->SwapElements(idx, keep); | |
206 | } | |
207 | ++keep; | |
208 | } | |
209 | } | |
210 | response->mutable_tags()->DeleteSubrange(keep, count - keep); | |
211 | } | |
212 | #endif | |
213 | } | |
214 | ||
b790ef3d RG |
215 | std::string RecProtoBufMessage::getAppliedPolicy() const |
216 | { | |
217 | std::string result; | |
218 | #ifdef HAVE_PROTOBUF | |
219 | const PBDNSMessage_DNSResponse& response = d_message.response(); | |
220 | result = response.appliedpolicy(); | |
221 | #endif /* HAVE_PROTOBUF */ | |
222 | return result; | |
223 | } | |
224 | ||
225 | std::vector<std::string> RecProtoBufMessage::getPolicyTags() const | |
226 | { | |
227 | std::vector<std::string> result; | |
228 | #ifdef HAVE_PROTOBUF | |
229 | const PBDNSMessage_DNSResponse& response = d_message.response(); | |
230 | const int count = response.tags_size(); | |
231 | for (int idx = 0; idx < count; idx++) { | |
232 | result.push_back(response.tags(idx)); | |
233 | } | |
234 | #endif /* HAVE_PROTOBUF */ | |
235 | return result; | |
236 | } |