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 
26 Snmp::Pdu::Pdu(const Pdu& pdu)
27 {
28  init();
29  assign(pdu);
30 }
31 
33 {
34  clear();
35 }
36 
37 Snmp::Pdu&
39 {
40  clear();
41  assign(pdu);
42  return *this;
43 }
44 
45 void
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 
54 void
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 
90 void
92 {
93  clearSystemOid();
94  clearVars();
95  init();
96 }
97 
98 void
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 
117 void
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 
129 void
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 
139 void
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 
155 void
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 
166 void
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 
190 void
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 
219 int
221 {
222  int count = 0;
223  for (variable_list* var = variables; var != nullptr; var = var->next_variable)
224  ++count;
225  return count;
226 }
227 
228 void
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 SNMP_DEFAULT_ERRSTAT
Definition: snmp_pdu.h:97
Pdu()
Definition: Pdu.cc:21
Definition: Var.h:23
#define xmalloc
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:158
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Var.cc:324
C end
Definition: Range.h:25
int max_repetitions
Definition: snmp_pdu.h:84
bool isNull() const
Definition: Var.cc:191
u_int time
Definition: snmp_pdu.h:94
void clearSystemOid()
Definition: Pdu.cc:140
int varCount() const
size of variables list
Definition: Pdu.cc:220
@ atAverage
Definition: snmp_core.h:29
S size() const
Definition: Range.h:61
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Pdu.cc:191
int name_length
Definition: snmp_vars.h:71
void unpack(const Ipc::TypedMsgHdr &msg)
restore struct from the message
Definition: Var.cc:340
oid * name
Definition: snmp_vars.h:70
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:126
void setSystemOid(const Range< const oid * > &systemOid)
Definition: Pdu.cc:156
Definition: Range.h:18
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
#define NULL
Definition: types.h:145
struct variable_list * variables
Definition: snmp_pdu.h:86
int trap_type
Definition: snmp_pdu.h:92
int errindex
Definition: snmp_pdu.h:80
struct sockaddr_in address
Definition: snmp_pdu.h:76
int non_repeaters
Definition: snmp_pdu.h:83
void fixAggregate()
Definition: Pdu.cc:229
int command
Definition: snmp_pdu.h:75
int specific_type
Definition: snmp_pdu.h:93
int reqid
Definition: snmp_pdu.h:78
void clear()
clear all internal members
Definition: Pdu.cc:91
void clearVars()
clear variables list
Definition: Pdu.cc:118
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:111
unsigned int aggrCount
The number of other Pdus merged into.
Definition: Pdu.h:46
@ atSum
Definition: snmp_core.h:29
void setVars(variable_list *vars)
perform assignment of variables list
Definition: Pdu.cc:130
void init()
initialize members
Definition: Pdu.cc:46
Range< const oid * > getSystemOid() const
Definition: Pdu.cc:150
~Pdu()
Definition: Pdu.cc:32
#define xfree
Definition: Pdu.h:23
void setName(const Range< const oid * > &aName)
set new variable name
Definition: Var.cc:171
void snmp_var_free(struct variable_list *)
Definition: snmp_vars.c:227
void aggregate(const Pdu &pdu)
Definition: Pdu.cc:55
Pdu & operator=(const Pdu &pdu)
Definition: Pdu.cc:38
u_int oid
Definition: asn1.h:42
#define SNMP_DEFAULT_ERRINDEX
Definition: snmp_pdu.h:98
void pack(Ipc::TypedMsgHdr &msg) const
prepare for sendmsg()
Definition: Pdu.cc:167
@ atMin
Definition: snmp_core.h:29
AggrType snmpAggrType(oid *Current, snint CurrentLen)
Definition: snmp_core.cc:573
#define Must(condition)
Definition: TextException.h:75
Range< const oid * > getName() const
returns variable name
Definition: Var.cc:165
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
@ atMax
Definition: snmp_core.h:29
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:118
struct variable_list * next_variable
Definition: snmp_vars.h:69
void assign(const Pdu &pdu)
perform full assignment
Definition: Pdu.cc:99
struct sockaddr_in agent_addr
Definition: snmp_pdu.h:91
void copyValue(const Var &var)
copy variable from another one
Definition: Var.cc:297
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:151
C start
Definition: Range.h:24
int errstat
Definition: snmp_pdu.h:79

 

Introduction

Documentation

Support

Miscellaneous