Pdu.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 49 SNMP Interface */
10
11#include "squid.h"
12#include "base/TextException.h"
13#include "ipc/TypedMsgHdr.h"
14#include "snmp/Pdu.h"
15#include "snmp/Var.h"
16#include "snmp_core.h"
17#include "tools.h"
18
19#include <algorithm>
20
22{
23 init();
24}
25
27{
28 init();
29 assign(pdu);
30}
31
33{
34 clear();
35}
36
39{
40 clear();
41 assign(pdu);
42 return *this;
43}
44
45void
47{
48 memset(static_cast<snmp_pdu *>(this), 0, sizeof(snmp_pdu));
49 aggrCount = 0;
50 errstat = SNMP_DEFAULT_ERRSTAT;
51 errindex = SNMP_DEFAULT_ERRINDEX;
52}
53
54void
56{
57 Must(varCount() == pdu.varCount());
58 ++aggrCount;
59 for (variable_list* p_aggr = variables, *p_var = pdu.variables; p_var != nullptr;
60 p_aggr = p_aggr->next_variable, p_var = p_var->next_variable) {
61 Must(p_aggr != nullptr);
62 Var& aggr = static_cast<Var&>(*p_aggr);
63 Var& var = static_cast<Var&>(*p_var);
64 if (aggr.isNull()) {
65 aggr.setName(var.getName());
66 aggr.copyValue(var);
67 } else {
68 switch (snmpAggrType(aggr.name, aggr.name_length)) {
69 case atSum:
70 case atAverage:
71 // The mean-average division is done later
72 // when the Snmp::Pdu::fixAggregate() called
73 aggr += var;
74 break;
75 case atMax:
76 if (var > aggr)
77 aggr.copyValue(var);
78 break;
79 case atMin:
80 if (var < aggr)
81 aggr.copyValue(var);
82 break;
83 default:
84 break;
85 }
86 }
87 }
88}
89
90void
92{
93 clearSystemOid();
94 clearVars();
95 init();
96}
97
98void
100{
101 command = pdu.command;
102 address.sin_addr.s_addr = pdu.address.sin_addr.s_addr;
103 reqid = pdu.reqid;
104 errstat = pdu.errstat;
105 errindex = pdu.errindex;
106 non_repeaters = pdu.non_repeaters;
107 max_repetitions = pdu.max_repetitions;
108 agent_addr.sin_addr.s_addr = pdu.agent_addr.sin_addr.s_addr;
109 trap_type = pdu.trap_type;
110 specific_type = pdu.specific_type;
111 time = pdu.time;
112 aggrCount = pdu.aggrCount;
113 setSystemOid(pdu.getSystemOid());
114 setVars(pdu.variables);
115}
116
117void
119{
120 variable_list* var = variables;
121 while (var != nullptr) {
122 variable_list* tmp = var;
123 var = var->next_variable;
124 snmp_var_free(tmp);
125 }
126 variables = nullptr;
127}
128
129void
131{
132 clearVars();
133 for (variable_list** p_var = &variables; vars != nullptr;
134 vars = vars->next_variable, p_var = &(*p_var)->next_variable) {
135 *p_var = new Var(static_cast<Var&>(*vars));
136 }
137}
138
139void
141{
142 if (enterprise != nullptr) {
143 xfree(enterprise);
144 enterprise = nullptr;
145 }
146 enterprise_length = 0;
147}
148
151{
152 return Range<const oid*>(enterprise, enterprise + enterprise_length);
153}
154
155void
157{
158 clearSystemOid();
159 if (systemOid.start != NULL && systemOid.size() != 0) {
160 enterprise_length = systemOid.size();
161 enterprise = static_cast<oid*>(xmalloc(enterprise_length * sizeof(oid)));
162 std::copy(systemOid.start, systemOid.end, enterprise);
163 }
164}
165
166void
168{
169 msg.putPod(command);
170 msg.putPod(address);
171 msg.putPod(reqid);
172 msg.putPod(errstat);
173 msg.putPod(errindex);
174 msg.putPod(non_repeaters);
175 msg.putPod(max_repetitions);
176 msg.putInt(enterprise_length);
177 if (enterprise_length > 0) {
178 Must(enterprise != nullptr);
179 msg.putFixed(enterprise, enterprise_length * sizeof(oid));
180 }
181 msg.putPod(agent_addr);
182 msg.putPod(trap_type);
183 msg.putPod(specific_type);
184 msg.putPod(time);
185 msg.putInt(varCount());
186 for (variable_list* var = variables; var != nullptr; var = var->next_variable)
187 static_cast<Var*>(var)->pack(msg);
188}
189
190void
192{
193 clear();
194 msg.getPod(command);
195 msg.getPod(address);
196 msg.getPod(reqid);
197 msg.getPod(errstat);
198 msg.getPod(errindex);
199 msg.getPod(non_repeaters);
200 msg.getPod(max_repetitions);
201 enterprise_length = msg.getInt();
202 if (enterprise_length > 0) {
203 enterprise = static_cast<oid*>(xmalloc(enterprise_length * sizeof(oid)));
204 msg.getFixed(enterprise, enterprise_length * sizeof(oid));
205 }
206 msg.getPod(agent_addr);
207 msg.getPod(trap_type);
208 msg.getPod(specific_type);
209 msg.getPod(time);
210 int count = msg.getInt();
211 for (variable_list** p_var = &variables; count > 0;
212 p_var = &(*p_var)->next_variable, --count) {
213 Var* var = new Var();
214 var->unpack(msg);
215 *p_var = var;
216 }
217}
218
219int
221{
222 int count = 0;
223 for (variable_list* var = variables; var != nullptr; var = var->next_variable)
224 ++count;
225 return count;
226}
227
228void
230{
231 if (aggrCount < 2)
232 return;
233 for (variable_list* p_aggr = variables; p_aggr != nullptr; p_aggr = p_aggr->next_variable) {
234 Var& aggr = static_cast<Var&>(*p_aggr);
235 if (snmpAggrType(aggr.name, aggr.name_length) == atAverage) {
236 aggr /= aggrCount;
237 }
238 }
239 aggrCount = 0;
240}
241
#define Must(condition)
Definition: TextException.h:75
u_int oid
Definition: asn1.h:42
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:35
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:151
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:118
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:158
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:126
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:111
Definition: Range.h:19
C start
Definition: Range.h:24
S size() const
Definition: Range.h:61
C end
Definition: Range.h:25
Definition: Pdu.h:24
void assign(const Pdu &pdu)
perform full assignment
Definition: Pdu.cc:99
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Pdu.cc:191
void clear()
clear all internal members
Definition: Pdu.cc:91
void aggregate(const Pdu &pdu)
Definition: Pdu.cc:55
void fixAggregate()
Definition: Pdu.cc:229
Pdu & operator=(const Pdu &pdu)
Definition: Pdu.cc:38
void clearVars()
clear variables list
Definition: Pdu.cc:118
void setSystemOid(const Range< const oid * > &systemOid)
Definition: Pdu.cc:156
void setVars(variable_list *vars)
perform assignment of variables list
Definition: Pdu.cc:130
~Pdu()
Definition: Pdu.cc:32
Pdu()
Definition: Pdu.cc:21
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Pdu.cc:167
int varCount() const
size of variables list
Definition: Pdu.cc:220
void clearSystemOid()
Definition: Pdu.cc:140
void init()
initialize members
Definition: Pdu.cc:46
unsigned int aggrCount
The number of other Pdus merged into.
Definition: Pdu.h:46
Range< const oid * > getSystemOid() const
Definition: Pdu.cc:150
Definition: Var.h:24
void copyValue(const Var &var)
copy variable from another one
Definition: Var.cc:297
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Var.cc:340
bool isNull() const
Definition: Var.cc:191
Range< const oid * > getName() const
returns variable name
Definition: Var.cc:165
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Var.cc:324
void setName(const Range< const oid * > &aName)
set new variable name
Definition: Var.cc:171
#define xfree
#define xmalloc
AggrType snmpAggrType(oid *Current, snint CurrentLen)
Definition: snmp_core.cc:573
@ atSum
Definition: snmp_core.h:29
@ atMin
Definition: snmp_core.h:29
@ atMax
Definition: snmp_core.h:29
@ atAverage
Definition: snmp_core.h:29
#define SNMP_DEFAULT_ERRSTAT
Definition: snmp_pdu.h:97
#define SNMP_DEFAULT_ERRINDEX
Definition: snmp_pdu.h:98
void snmp_var_free(struct variable_list *)
Definition: snmp_vars.c:227
struct sockaddr_in address
Definition: snmp_pdu.h:52
int non_repeaters
Definition: snmp_pdu.h:59
int trap_type
Definition: snmp_pdu.h:68
int errindex
Definition: snmp_pdu.h:56
int reqid
Definition: snmp_pdu.h:54
int command
Definition: snmp_pdu.h:51
struct variable_list * variables
Definition: snmp_pdu.h:62
int max_repetitions
Definition: snmp_pdu.h:60
struct sockaddr_in agent_addr
Definition: snmp_pdu.h:67
int errstat
Definition: snmp_pdu.h:55
int specific_type
Definition: snmp_pdu.h:69
u_int time
Definition: snmp_pdu.h:70
struct variable_list * next_variable
Definition: snmp_vars.h:45
int name_length
Definition: snmp_vars.h:47
oid * name
Definition: snmp_vars.h:46
#define NULL
Definition: types.h:145

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors