File: protozero.hh

package info (click to toggle)
dnsdist 2.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,640 kB
  • sloc: cpp: 91,323; javascript: 24,456; sh: 4,744; python: 1,328; makefile: 832; ansic: 816
file content (408 lines) | stat: -rw-r--r-- 11,954 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * 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 "config.h"

#include "iputils.hh"
#include "gettime.hh"
#include "uuid-utils.hh"

#ifndef DISABLE_PROTOBUF

#include <protozero/pbf_writer.hpp>

namespace pdns
{
namespace ProtoZero
{
  class Message
  {
  public:
    enum class MetaValueField : protozero::pbf_tag_type
    {
      stringVal = 1,
      intVal = 2
    };
    enum class HTTPVersion : protozero::pbf_tag_type
    {
      HTTP1 = 1,
      HTTP2 = 2,
      HTTP3 = 3
    };
    enum class MetaField : protozero::pbf_tag_type
    {
      key = 1,
      value = 2
    };
    enum class Event : protozero::pbf_tag_type
    {
      ts = 1,
      event = 2,
      start = 3,
      boolVal = 4,
      intVal = 5,
      stringVal = 6,
      bytesVal = 7,
      custom = 8
    };
    enum class MessageType : int32_t
    {
      DNSQueryType = 1,
      DNSResponseType = 2,
      DNSOutgoingQueryType = 3,
      DNSIncomingResponseType = 4
    };
    enum class Field : protozero::pbf_tag_type
    {
      type = 1,
      messageId = 2,
      serverIdentity = 3,
      socketFamily = 4,
      socketProtocol = 5,
      from = 6,
      to = 7,
      inBytes = 8,
      timeSec = 9,
      timeUsec = 10,
      id = 11,
      question = 12,
      response = 13,
      originalRequestorSubnet = 14,
      requestorId = 15,
      initialRequestId = 16,
      deviceId = 17,
      newlyObservedDomain = 18,
      deviceName = 19,
      fromPort = 20,
      toPort = 21,
      meta = 22,
      trace = 23,
      httpVersion = 24,
      workerId = 25,
      packetCacheHit = 26,
      outgoingQueries = 27,
      headerFlags = 28,
      ednsVersion = 29,
    };
    enum class QuestionField : protozero::pbf_tag_type
    {
      qName = 1,
      qType = 2,
      qClass = 3
    };
    enum class ResponseField : protozero::pbf_tag_type
    {
      rcode = 1,
      rrs = 2,
      appliedPolicy = 3,
      tags = 4,
      queryTimeSec = 5,
      queryTimeUsec = 6,
      appliedPolicyType = 7,
      appliedPolicyTrigger = 8,
      appliedPolicyHit = 9,
      appliedPolicyKind = 10,
      validationState = 11
    };
    enum class RRField : protozero::pbf_tag_type
    {
      name = 1,
      type = 2,
      class_ = 3,
      ttl = 4,
      rdata = 5,
      udr = 6
    };
    enum class TransportProtocol : protozero::pbf_tag_type
    {
      UDP = 1,
      TCP = 2,
      DoT = 3,
      DoH = 4,
      DNSCryptUDP = 5,
      DNSCryptTCP = 6,
      DoQ = 7
    };

    Message(std::string& buffer) :
      d_buffer(buffer), d_message{d_buffer}
    {
    }
    ~Message() = default;
    Message(const Message&) = delete;
    Message(Message&&) = delete;
    Message& operator=(const Message&) = delete;
    Message& operator=(Message&&) = delete;

    void setRequest(const boost::uuids::uuid& uniqueId, const ComboAddress& requestor, const ComboAddress& local, const DNSName& qname, uint16_t qtype, uint16_t qclass, uint16_t qid, TransportProtocol proto, size_t len);
    void setResponse(const DNSName& qname, uint16_t qtype, uint16_t qclass);

    void setType(MessageType mtype)
    {
      add_enum(d_message, Field::type, static_cast<int32_t>(mtype));
    }

    void setHTTPVersion(HTTPVersion version)
    {
      add_enum(d_message, Field::httpVersion, static_cast<int32_t>(version));
    }

    void setMessageIdentity(const boost::uuids::uuid& uniqueId)
    {
      add_bytes(d_message, Field::messageId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
    }

    void setServerIdentity(const std::string& serverIdentity)
    {
      add_bytes(d_message, Field::serverIdentity, serverIdentity.data(), serverIdentity.length());
    }

    void setSocketFamily(int family)
    {
      add_enum(d_message, Field::socketFamily, family == AF_INET ? 1 : 2);
    }

    void setSocketProtocol(TransportProtocol proto)
    {
      add_enum(d_message, Field::socketProtocol, static_cast<int32_t>(proto));
    }

    void setFrom(const ComboAddress& address)
    {
      encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::from), address);
    }

    void setTo(const ComboAddress& address)
    {
      encodeComboAddress(static_cast<protozero::pbf_tag_type>(Field::to), address);
    }

    void setInBytes(uint64_t len)
    {
      add_uint64(d_message, Field::inBytes, len);
    }

    void setTime()
    {
      timespec timesp{};
      gettime(&timesp, true);

      setTime(timesp.tv_sec, timesp.tv_nsec / 1000);
    }

    void setTime(time_t sec, uint32_t usec)
    {
      // coverity[store_truncates_time_t]
      add_uint32(d_message, Field::timeSec, sec);
      add_uint32(d_message, Field::timeUsec, usec);
    }

    void setId(uint16_t qid)
    {
      add_uint32(d_message, Field::id, ntohs(qid));
    }

    void setQuestion(const DNSName& qname, uint16_t qtype, uint16_t qclass)
    {
      protozero::pbf_writer pbf_question{d_message, static_cast<protozero::pbf_tag_type>(Field::question)};
      encodeDNSName(pbf_question, d_buffer, static_cast<protozero::pbf_tag_type>(QuestionField::qName), qname);
      pbf_question.add_uint32(static_cast<protozero::pbf_tag_type>(QuestionField::qType), qtype);
      pbf_question.add_uint32(static_cast<protozero::pbf_tag_type>(QuestionField::qClass), qclass);
    }

    void setMeta(const std::string& key, const std::unordered_set<std::string>& stringVal, const std::unordered_set<int64_t>& intVal)
    {
      protozero::pbf_writer pbf_meta{d_message, static_cast<protozero::pbf_tag_type>(Field::meta)};
      pbf_meta.add_string(static_cast<protozero::pbf_tag_type>(MetaField::key), key);
      protozero::pbf_writer pbf_meta_value{pbf_meta, static_cast<protozero::pbf_tag_type>(MetaField::value)};
      for (const auto& str : stringVal) {
        pbf_meta_value.add_string(static_cast<protozero::pbf_tag_type>(MetaValueField::stringVal), str);
      }
      for (const auto& val : intVal) {
        pbf_meta_value.add_uint64(static_cast<protozero::pbf_tag_type>(MetaValueField::intVal), val);
      }
    }

    void setEDNSSubnet(const Netmask& netmask, uint8_t mask)
    {
      encodeNetmask(static_cast<protozero::pbf_tag_type>(Field::originalRequestorSubnet), netmask, mask);
    }

    void setRequestorId(const std::string& req)
    {
      if (!req.empty()) {
        add_string(d_message, Field::requestorId, req);
      }
    }

    void setInitialRequestID(const boost::uuids::uuid& uniqueId)
    {
      add_bytes(d_message, Field::initialRequestId, reinterpret_cast<const char*>(uniqueId.begin()), uniqueId.size()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
    }

    void setDeviceId(const std::string& deviceId)
    {
      if (!deviceId.empty()) {
        add_string(d_message, Field::deviceId, deviceId);
      }
    }

    void setNewlyObservedDomain(bool nod)
    {
      add_bool(d_message, Field::newlyObservedDomain, nod);
    }

    void setDeviceName(const std::string& name)
    {
      if (!name.empty()) {
        add_string(d_message, Field::deviceName, name);
      }
    }

    void setFromPort(in_port_t port)
    {
      add_uint32(d_message, Field::fromPort, port);
    }

    void setToPort(in_port_t port)
    {
      add_uint32(d_message, Field::toPort, port);
    }

    void setWorkerId(uint64_t wid)
    {
      add_uint64(d_message, Field::workerId, wid);
    }

    void setPacketCacheHit(bool hit)
    {
      add_bool(d_message, Field::packetCacheHit, hit);
    }

    void setOutgoingQueries(uint32_t num)
    {
      add_uint32(d_message, Field::outgoingQueries, num);
    }

    void setHeaderFlags(uint16_t flags)
    {
      add_uint32(d_message, Field::headerFlags, flags);
    }

    void setEDNSVersion(uint32_t version)
    {
      add_uint32(d_message, Field::ednsVersion, version);
    }

    void startResponse()
    {
      d_response = protozero::pbf_writer{d_message, static_cast<protozero::pbf_tag_type>(Field::response)};
    }

    void commitResponse()
    {
      d_response.commit();
    }

    void setResponseCode(uint8_t rcode)
    {
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::rcode), rcode);
    }

    void setNetworkErrorResponseCode()
    {
      /* special code meaning 'network error', like a timeout */
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::rcode), 65536);
    }

    void setAppliedPolicy(const std::string& policy)
    {
      d_response.add_string(static_cast<protozero::pbf_tag_type>(ResponseField::appliedPolicy), policy);
    }

    void addPolicyTags(const std::unordered_set<std::string>& tags)
    {
      for (const auto& tag : tags) {
        addPolicyTag(tag);
      }
    }

    void addPolicyTag(const string& tag)
    {
      d_response.add_string(static_cast<protozero::pbf_tag_type>(ResponseField::tags), tag);
    }

    void setQueryTime(uint32_t sec, uint32_t usec)
    {
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::queryTimeSec), sec);
      d_response.add_uint32(static_cast<protozero::pbf_tag_type>(ResponseField::queryTimeUsec), usec);
    }

    void addRRsFromPacket(const char* packet, size_t len, bool includeCNAME = false);
    void addRR(const DNSName& name, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& blob);

  protected:
    void encodeComboAddress(protozero::pbf_tag_type type, const ComboAddress& address);
    void encodeNetmask(protozero::pbf_tag_type type, const Netmask& subnet, uint8_t mask);
    static void encodeDNSName(protozero::pbf_writer& pbf, std::string& buffer, protozero::pbf_tag_type type, const DNSName& name);

    static void add_enum(protozero::pbf_writer& writer, Field type, int32_t value)
    {
      writer.add_enum(static_cast<protozero::pbf_tag_type>(type), value);
    }

    static void add_bool(protozero::pbf_writer& writer, Field type, bool value)
    {
      writer.add_bool(static_cast<protozero::pbf_tag_type>(type), value);
    }

    static void add_uint32(protozero::pbf_writer& writer, Field type, uint32_t value)
    {
      writer.add_uint32(static_cast<protozero::pbf_tag_type>(type), value);
    }

    static void add_uint64(protozero::pbf_writer& writer, Field type, uint64_t value)
    {
      writer.add_uint64(static_cast<protozero::pbf_tag_type>(type), value);
    }

    static void add_bytes(protozero::pbf_writer& writer, Field type, const char* data, size_t len)
    {
      writer.add_bytes(static_cast<protozero::pbf_tag_type>(type), data, len);
    }

    static void add_string(protozero::pbf_writer& writer, Field type, const std::string& str)
    {
      writer.add_string(static_cast<protozero::pbf_tag_type>(type), str);
    }

    // NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes)
    std::string& d_buffer;
    protozero::pbf_writer d_message;
    protozero::pbf_writer d_response;
    // NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes)
  };
};
};

#endif /* DISABLE_PROTOBUF */