]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/remote_logger.hh
Merge pull request #14021 from Habbie/auth-lua-join-whitespace
[thirdparty/pdns.git] / pdns / remote_logger.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <atomic>
28 #include <condition_variable>
29 #include <queue>
30 #include <thread>
31
32 #include "iputils.hh"
33 #include "circular_buffer.hh"
34
35 /* Writes can be submitted and they are atomically accepted. Either the whole write
36 ends up in the buffer or nothing ends up in the buffer.
37 In case nothing ends up in the buffer, an exception is thrown.
38 Similarly, EOF leads to this treatment
39
40 The filedescriptor can be in non-blocking mode.
41
42 This class is not threadsafe.
43 */
44
45 class CircularWriteBuffer
46 {
47 public:
48 explicit CircularWriteBuffer(int fd, size_t size) : d_fd(fd), d_buffer(size)
49 {
50 }
51
52 void write(const std::string& str);
53 void flush();
54 private:
55 int d_fd;
56 boost::circular_buffer<char> d_buffer;
57 };
58
59 class RemoteLoggerInterface
60 {
61 public:
62 virtual ~RemoteLoggerInterface() {};
63 virtual void queueData(const std::string& data) = 0;
64 virtual std::string toString() const = 0;
65
66 bool logQueries(void) const { return d_logQueries; }
67 bool logResponses(void) const { return d_logResponses; }
68 void setLogQueries(bool flag) { d_logQueries = flag; }
69 void setLogResponses(bool flag) { d_logResponses = flag; }
70
71 private:
72 bool d_logQueries{true};
73 bool d_logResponses{true};
74 };
75
76 /* Thread safe. Will connect asynchronously on request.
77 Runs a reconnection thread that also periodicall flushes.
78 Note that the buffer only runs as long as there is a connection.
79 If there is no connection we don't buffer a thing
80 */
81 class RemoteLogger : public RemoteLoggerInterface
82 {
83 public:
84 RemoteLogger(const ComboAddress& remote, uint16_t timeout=2,
85 uint64_t maxQueuedBytes=100000,
86 uint8_t reconnectWaitTime=1,
87 bool asyncConnect=false);
88 ~RemoteLogger();
89 void queueData(const std::string& data) override;
90 std::string toString() const override
91 {
92 return d_remote.toStringWithPort();
93 }
94 void stop()
95 {
96 d_exiting = true;
97 }
98 std::atomic<uint32_t> d_drops{0};
99
100 private:
101 bool reconnect();
102 void maintenanceThread();
103
104 ComboAddress d_remote;
105 uint64_t d_maxQueuedBytes;
106 int d_socket{-1};
107 std::unique_ptr<CircularWriteBuffer> d_writer;
108 uint16_t d_timeout;
109 uint8_t d_reconnectWaitTime;
110 std::atomic<bool> d_exiting{false};
111 bool d_asyncConnect{false};
112
113 std::mutex d_mutex;
114 std::thread d_thread;
115 };