File: rec-protozero.hh

package info (click to toggle)
pdns-recursor 5.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 11,116 kB
  • sloc: cpp: 109,672; javascript: 20,651; python: 5,657; sh: 5,114; makefile: 782; ansic: 582; xml: 37
file content (260 lines) | stat: -rw-r--r-- 7,148 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#pragma once

#include "protozero.hh"

#include "filterpo.hh"
#include "rec-eventtrace.hh"
#include "validate.hh"

namespace pdns
{
namespace ProtoZero
{
  class RecMessage : public Message
  {
  public:
    RecMessage() :
      Message(d_msgbuf)
    {
      d_response = protozero::pbf_writer(d_rspbuf);
    }

    RecMessage(std::string& buffer) :
      Message(buffer)
    {
      d_response = protozero::pbf_writer(buffer);
    }

    // Start a new messagebuf, containing separate data for the response part
    RecMessage(std::string::size_type sz1, std::string::size_type sz2) :
      RecMessage()
    {
      reserve(sz1, sz2);
    }

    // Construct a Message with (partially) constructed content
    RecMessage(const std::string& buf1, const std::string& buf2, std::string::size_type sz1, std::string::size_type sz2) :
      Message(d_msgbuf),
      d_msgbuf{buf1},
      d_rspbuf{buf2}
    {
      d_message = protozero::pbf_writer(d_msgbuf);
      d_response = protozero::pbf_writer(d_rspbuf);
      reserve(sz1, sz2);
    }
    RecMessage(const Message&) = delete;
    RecMessage(Message&&) = delete;
    RecMessage& operator=(const Message&) = delete;
    RecMessage& operator=(Message&&) = delete;

    void reserve(std::string::size_type sz1, std::string::size_type sz2)
    {
      // We expect to grow the buffers, in the end the d_message will contains the (grown) d_response
      // This is extra space in addition to what's already there
      // Different from what string.reserve() does
      std::string::size_type extra = sz1 + d_rspbuf.length() + sz2;
      if (d_msgbuf.capacity() < d_msgbuf.size() + extra) {
        d_message.reserve(extra);
      }
      if (d_rspbuf.capacity() < d_rspbuf.size() + sz2) {
        d_response.reserve(sz2);
      }
    }

    const std::string& getMessageBuf() const
    {
      return d_msgbuf;
    }

    const std::string& getResponseBuf() const
    {
      return d_rspbuf;
    }

    [[nodiscard]] size_t size() const
    {
      return d_msgbuf.size() + d_rspbuf.size();
    }

    std::string&& finishAndMoveBuf()
    {
      if (!d_rspbuf.empty()) {
        d_message.add_message(static_cast<protozero::pbf_tag_type>(Field::response), d_rspbuf);
      }
      return std::move(d_msgbuf);
    }

    void addEvents(const RecEventTrace& trace);

    // DNSResponse related fields below

    void addRR(const DNSRecord& record, const std::set<uint16_t>& exportTypes, std::optional<bool> udr);

    void setAppliedPolicyType(const DNSFilterEngine::PolicyType type)
    {
      uint32_t p;

      switch (type) {
      case DNSFilterEngine::PolicyType::None:
        p = 1;
        break;
      case DNSFilterEngine::PolicyType::QName:
        p = 2;
        break;
      case DNSFilterEngine::PolicyType::ClientIP:
        p = 3;
        break;
      case DNSFilterEngine::PolicyType::ResponseIP:
        p = 4;
        break;
      case DNSFilterEngine::PolicyType::NSDName:
        p = 5;
        break;
      case DNSFilterEngine::PolicyType::NSIP:
        p = 6;
        break;
      default:
        throw std::runtime_error("Unsupported protobuf policy type");
      }
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::appliedPolicyType), p);
    }

    void setAppliedPolicyTrigger(const DNSName& trigger)
    {
      encodeDNSName(d_response, d_rspbuf, static_cast<protozero::pbf_tag_type>(ResponseField::appliedPolicyTrigger), trigger);
    }

    void setAppliedPolicyHit(const std::string& hit)
    {
      d_response.add_string(static_cast<protozero::pbf_tag_type>(ResponseField::appliedPolicyHit), hit);
    }

    void setAppliedPolicyKind(const DNSFilterEngine::PolicyKind kind)
    {
      uint32_t k;

      switch (kind) {
      case DNSFilterEngine::PolicyKind::NoAction:
        k = 1;
        break;
      case DNSFilterEngine::PolicyKind::Drop:
        k = 2;
        break;
      case DNSFilterEngine::PolicyKind::NXDOMAIN:
        k = 3;
        break;
      case DNSFilterEngine::PolicyKind::NODATA:
        k = 4;
        break;
      case DNSFilterEngine::PolicyKind::Truncate:
        k = 5;
        break;
      case DNSFilterEngine::PolicyKind::Custom:
        k = 6;
        break;
      default:
        throw std::runtime_error("Unsupported protobuf policy kind");
      }
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::appliedPolicyKind), k);
    }

    void setValidationState(const vState state)
    {
      uint32_t s;

      switch (state) {
      case vState::Indeterminate:
        s = 1;
        break;
      case vState::Insecure:
        s = 2;
        break;
      case vState::Secure:
        s = 3;
        break;
      case vState::BogusNoValidDNSKEY:
        s = 4;
        break;
      case vState::BogusInvalidDenial:
        s = 5;
        break;
      case vState::BogusUnableToGetDSs:
        s = 6;
        break;
      case vState::BogusUnableToGetDNSKEYs:
        s = 7;
        break;
      case vState::BogusSelfSignedDS:
        s = 8;
        break;
      case vState::BogusNoRRSIG:
        s = 9;
        break;
      case vState::BogusNoValidRRSIG:
        s = 10;
        break;
      case vState::BogusMissingNegativeIndication:
        s = 11;
        break;
      case vState::BogusSignatureNotYetValid:
        s = 12;
        break;
      case vState::BogusSignatureExpired:
        s = 13;
        break;
      case vState::BogusUnsupportedDNSKEYAlgo:
        s = 14;
        break;
      case vState::BogusUnsupportedDSDigestType:
        s = 15;
        break;
      case vState::BogusNoZoneKeyBitSet:
        s = 16;
        break;
      case vState::BogusRevokedDNSKEY:
        s = 17;
        break;
      case vState::BogusInvalidDNSKEYProtocol:
        s = 18;
        break;
      default:
        throw std::runtime_error("Unsupported protobuf validation state");
      }
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::validationState), s);
    }

#ifdef NOD_ENABLED
    void clearUDR(std::string&);
#endif

  private:
    std::string d_msgbuf;
    std::string d_rspbuf;

#ifdef NOD_ENABLED
    vector<std::string::size_type> offsets;
#endif
  };
};
};