• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 18743945403

23 Oct 2025 09:29AM UTC coverage: 65.845% (+0.02%) from 65.829%
18743945403

Pull #16356

github

web-flow
Merge 8a2027ef1 into efa3637e8
Pull Request #16356: auth 5.0: backport "pdnsutil: fix b2b-migrate to from sql to non-sql"

42073 of 92452 branches covered (45.51%)

Branch coverage included in aggregate %.

128008 of 165855 relevant lines covered (77.18%)

6379935.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.6
/pdns/recursordist/rec-snmp.cc
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

23
#include <unordered_map>
24

25
#include "rec-snmp.hh"
26
#include "rec_channel.hh"
27

28
#include "logging.hh"
29

30
#ifdef HAVE_NET_SNMP
31

32
#include <net-snmp/net-snmp-config.h>
33
#include <net-snmp/definitions.h>
34
#include <net-snmp/types.h>
35
#include <net-snmp/utilities.h>
36
#include <net-snmp/config_api.h>
37
#include <net-snmp/agent/net-snmp-agent-includes.h>
38
#undef INET6 /* SRSLY? */
39

40
#define RECURSOR_OID 1, 3, 6, 1, 4, 1, 43315, 2
41
#define RECURSOR_STATS_OID RECURSOR_OID, 1
42
#define RECURSOR_TRAPS_OID RECURSOR_OID, 10, 0
43
#define RECURSOR_TRAP_OBJECTS_OID RECURSOR_OID, 11
44

45
using oid10 = std::array<oid, 10>;
46
using oid11 = std::array<oid, 11>;
47

48
static const oid11 trapReasonOID = {RECURSOR_TRAP_OBJECTS_OID, 1, 0};
49
static const oid11 customTrapOID = {RECURSOR_TRAPS_OID, 1};
50

51
#include "rec-oids-gen.h"
52

53
static std::unordered_map<oid, std::string> s_statsMap;
54

55
/* We are never called for a GETNEXT if it's registered as a
56
   "instance", as it's "magically" handled for us.  */
57
/* a instance handler also only hands us one request at a time, so
58
   we don't need to loop over a list of requests; we'll only get one. */
59

60
static int handleCounter64Stats(netsnmp_mib_handler* /* handler */,
61
                                netsnmp_handler_registration* reginfo,
62
                                netsnmp_agent_request_info* reqinfo,
63
                                netsnmp_request_info* requests)
64
{
598✔
65
  if (reqinfo->mode != MODE_GET) {
598!
66
    return SNMP_ERR_GENERR;
×
67
  }
×
68

69
  if (reginfo->rootoid_len != OID_LENGTH(questionsOID) + 1) {
598!
70
    return SNMP_ERR_GENERR;
×
71
  }
×
72

73
  const auto& iter = s_statsMap.find(reginfo->rootoid[reginfo->rootoid_len - 2]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) it's the API
598✔
74
  if (iter == s_statsMap.end()) {
598!
75
    return SNMP_ERR_GENERR;
×
76
  }
×
77

78
  auto value = getStatByName(iter->second);
598✔
79
  if (value) {
598✔
80
    return RecursorSNMPAgent::setCounter64Value(requests, *value);
590✔
81
  }
590✔
82
  return RecursorSNMPAgent::setCounter64Value(requests, 0);
8✔
83
}
598✔
84

85
static int handleDisabledCounter64Stats(netsnmp_mib_handler* /* handler */,
86
                                        netsnmp_handler_registration* reginfo,
87
                                        netsnmp_agent_request_info* reqinfo,
88
                                        netsnmp_request_info* requests)
89
{
12✔
90
  if (reqinfo->mode != MODE_GET) {
12!
91
    return SNMP_ERR_GENERR;
×
92
  }
×
93

94
  if (reginfo->rootoid_len != OID_LENGTH(questionsOID) + 1) {
12!
95
    return SNMP_ERR_GENERR;
×
96
  }
×
97

98
  return RecursorSNMPAgent::setCounter64Value(requests, 0);
12✔
99
}
12✔
100

101
static void registerCounter64Stat(const std::string& name, const oid10& statOID)
102
{
153✔
103
  if (statOID.size() != OID_LENGTH(questionsOID)) {
153!
104
    SLOG(g_log << Logger::Error << "Invalid OID for SNMP Counter64 statistic " << name << endl,
×
105
         g_slog->withName("snmp")->info(Logr::Error, "Invalid OID for SNMP Counter64 statistic", "name", Logging::Loggable(name)));
×
106
    return;
×
107
  }
×
108

109
  if (s_statsMap.find(statOID.at(statOID.size() - 1)) != s_statsMap.end()) {
153!
110
    SLOG(g_log << Logger::Error << "OID for SNMP Counter64 statistic " << name << " has already been registered" << endl,
×
111
         g_slog->withName("snmp")->info(Logr::Error, "OID for SNMP Counter64 statistic has already been registered", "name", Logging::Loggable(name)));
×
112
    return;
×
113
  }
×
114

115
  s_statsMap[statOID.at(statOID.size() - 1)] = name;
153✔
116
  netsnmp_register_scalar(netsnmp_create_handler_registration(name.c_str(),
153✔
117
                                                              isStatDisabled(StatComponent::SNMP, name) ? handleDisabledCounter64Stats : handleCounter64Stats,
153✔
118
                                                              statOID.data(),
153✔
119
                                                              statOID.size(),
153✔
120
                                                              HANDLER_CAN_RONLY));
153✔
121
}
153✔
122

123
#endif /* HAVE_NET_SNMP */
124

125
std::shared_ptr<RecursorSNMPAgent> g_snmpAgent{nullptr};
126

127
bool RecursorSNMPAgent::sendCustomTrap([[maybe_unused]] const std::string& reason)
128
{
×
129
#ifdef HAVE_NET_SNMP
×
130
  netsnmp_variable_list* varList = nullptr;
×
131

132
  addSNMPTrapOID(&varList,
×
133
                 customTrapOID.data(),
×
134
                 customTrapOID.size() * sizeof(oid));
×
135

136
  snmp_varlist_add_variable(&varList,
×
137
                            trapReasonOID.data(),
×
138
                            trapReasonOID.size(),
×
139
                            ASN_OCTET_STR,
×
140
                            reason.c_str(),
×
141
                            reason.size());
×
142

143
  return sendTrap(d_sender, varList);
×
144
#endif /* HAVE_NET_SNMP */
×
145
  return true;
×
146
}
×
147

148
RecursorSNMPAgent::RecursorSNMPAgent(const std::string& name, const std::string& daemonSocket) :
149
  SNMPAgent(name, daemonSocket)
150
{
1✔
151
#ifdef HAVE_NET_SNMP
1✔
152

153
#include "rec-snmp-gen.h"
1✔
154

155
#endif /* HAVE_NET_SNMP */
1✔
156
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc