]> git.ipfire.org Git - thirdparty/squid.git/blame - src/snmp/Var.cc
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / snmp / Var.cc
CommitLineData
51ea0904 1/*
bf95c10a 2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
51ea0904 3 *
bbc27441
AJ
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.
51ea0904
CT
7 */
8
bbc27441
AJ
9/* DEBUG: section 49 SNMP Interface */
10
f7f3304a 11#include "squid.h"
51ea0904 12#include "base/TextException.h"
3b5c25e8 13#include "Debug.h"
51ea0904 14#include "ipc/TypedMsgHdr.h"
d6e3ad20 15#include "snmp/Var.h"
5bed43d6 16#include "tools.h"
074d6a40 17
b167f1b8 18#include <algorithm>
51ea0904 19
51ea0904
CT
20Snmp::Var::Var()
21{
22 init();
23}
24
25Snmp::Var::Var(const Var& var)
26{
27 init();
28 assign(var);
29}
30
31Snmp::Var::~Var()
32{
33 clear();
34}
35
36Snmp::Var&
37Snmp::Var::operator = (const Var& var)
38{
39 clear();
40 assign(var);
41 return *this;
42}
43
44void
45Snmp::Var::init()
46{
b56b37cf 47 memset(static_cast<variable_list *>(this), 0, sizeof(variable_list));
51ea0904
CT
48}
49
50Snmp::Var&
51Snmp::Var::operator += (const Var& var)
52{
8fb5a96c 53 switch (type) {
51ea0904
CT
54 case SMI_INTEGER:
55 setInt(asInt() + var.asInt());
56 break;
57 case SMI_GAUGE32:
58 setGauge(asGauge() + var.asGauge());
59 break;
60 case SMI_COUNTER32:
61 setCounter(asCounter() + var.asCounter());
62 break;
63 case SMI_COUNTER64:
64 setCounter64(asCounter64() + var.asCounter64());
65 break;
66 case SMI_TIMETICKS:
67 setTimeTicks(asTimeTicks() + var.asTimeTicks());
68 break;
69 default:
d816f28d 70 debugs(49, DBG_CRITICAL, "ERROR: Unsupported type: " << type);
51ea0904
CT
71 throw TexcHere("Unsupported type");
72 break;
73 }
74 return *this;
75}
76
77Snmp::Var&
78Snmp::Var::operator /= (int num)
79{
80 Must(num != 0);
8fb5a96c 81 switch (type) {
51ea0904
CT
82 case SMI_INTEGER:
83 setInt(asInt() / num);
84 break;
85 case SMI_GAUGE32:
86 setGauge(asGauge() / num);
87 break;
88 case SMI_COUNTER32:
89 setCounter(asCounter() / num);
90 break;
91 case SMI_COUNTER64:
92 setCounter64(asCounter64() / num);
93 break;
94 case SMI_TIMETICKS:
95 setTimeTicks(asTimeTicks() / num);
96 break;
97 default:
d816f28d 98 debugs(49, DBG_CRITICAL, "ERROR: Unsupported type: " << type);
51ea0904
CT
99 throw TexcHere("Unsupported type");
100 break;
101 }
102 return *this;
103}
104
105bool
106Snmp::Var::operator < (const Var& var) const
107{
8fb5a96c 108 switch (type) {
51ea0904
CT
109 case SMI_INTEGER:
110 return asInt() < var.asInt();
111 case SMI_GAUGE32:
112 return asGauge() < var.asGauge();
113 case SMI_COUNTER32:
114 return asCounter() < var.asCounter();
115 case SMI_COUNTER64:
116 return asCounter64() < var.asCounter64();
117 case SMI_TIMETICKS:
118 return asTimeTicks() < var.asTimeTicks();
119 default:
d816f28d 120 debugs(49, DBG_CRITICAL, "ERROR: Unsupported type: " << type);
51ea0904
CT
121 throw TexcHere("Unsupported type");
122 break;
123 }
124 return false; // unreachable
125}
126
127bool
128Snmp::Var::operator > (const Var& var) const
129{
8fb5a96c 130 switch (type) {
51ea0904
CT
131 case SMI_INTEGER:
132 return asInt() > var.asInt();
133 case SMI_GAUGE32:
134 return asGauge() > var.asGauge();
135 case SMI_COUNTER32:
136 return asCounter() > var.asCounter();
137 case SMI_COUNTER64:
138 return asCounter64() > var.asCounter64();
139 case SMI_TIMETICKS:
140 return asTimeTicks() > var.asTimeTicks();
141 default:
d816f28d 142 debugs(49, DBG_CRITICAL, "ERROR: Unsupported type: " << type);
51ea0904
CT
143 throw TexcHere("Unsupported type");
144 break;
145 }
146 return false; // unreachable
147}
148
149void
150Snmp::Var::assign(const Var& var)
151{
152 setName(var.getName());
153 copyValue(var);
154}
155
156void
157Snmp::Var::clearName()
158{
b56b37cf
AJ
159 xfree(name);
160 name = nullptr;
51ea0904
CT
161 name_length = 0;
162}
163
164Range<const oid*>
165Snmp::Var::getName() const
166{
167 return Range<const oid*>(name, name + name_length);
168}
169
170void
171Snmp::Var::setName(const Range<const oid*>& aName)
172{
173 clearName();
174 if (aName.start != NULL && aName.size() != 0) {
175 name_length = aName.size();
176 name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
177 std::copy(aName.start, aName.end, name);
178 }
179}
180
181void
182Snmp::Var::clearValue()
183{
b56b37cf
AJ
184 xfree(val.string);
185 val.string = nullptr;
51ea0904
CT
186 val_len = 0;
187 type = 0;
188}
189
190bool
191Snmp::Var::isNull() const
192{
193 return type == SMI_NULLOBJ;
194}
195
196int
197Snmp::Var::asInt() const
198{
199 Must(type == SMI_INTEGER);
200 Must(val.integer != NULL && val_len == sizeof(int));
201 return *val.integer;
202}
203
204unsigned int
205Snmp::Var::asGauge() const
206{
207 Must(type == SMI_GAUGE32);
208 Must(val.integer != NULL && val_len == 4);
209 return *reinterpret_cast<unsigned int*>(val.integer);
210}
211
212int
213Snmp::Var::asCounter() const
214{
215 Must(type == SMI_COUNTER32);
216 Must(val.integer != NULL && val_len == 4);
217 return *reinterpret_cast<int*>(val.integer);
218}
219
220long long int
221Snmp::Var::asCounter64() const
222{
223 Must(type == SMI_COUNTER64);
224 Must(val.integer != NULL && val_len == 8);
225 return *reinterpret_cast<long long int*>(val.integer);
226}
227
579928b6 228unsigned int
51ea0904
CT
229Snmp::Var::asTimeTicks() const
230{
231 Must(type == SMI_TIMETICKS);
579928b6
CT
232 Must(val.integer != NULL && val_len == sizeof(unsigned int));
233 return *reinterpret_cast<unsigned int*>(val.integer);
51ea0904
CT
234}
235
236Range<const oid*>
237Snmp::Var::asObject() const
238{
239 Must(type == SMI_OBJID);
240 Must(val_len % sizeof(oid) == 0);
241 int length = val_len / sizeof(oid);
242 Must(val.objid != NULL && length > 0);
243 return Range<const oid*>(val.objid, val.objid + length);
244}
245
246Range<const u_char*>
247Snmp::Var::asString() const
248{
249 Must(type == SMI_STRING);
250 Must(val.string != NULL && val_len > 0);
251 return Range<const u_char*>(val.string, val.string + val_len);
252}
253
51ea0904
CT
254void
255Snmp::Var::setInt(int value)
256{
257 setValue(&value, sizeof(value), SMI_INTEGER);
258}
259
260void
261Snmp::Var::setCounter(int value)
262{
263 setValue(&value, sizeof(value), SMI_COUNTER32);
264}
265
266void
267Snmp::Var::setGauge(unsigned int value)
268{
269 setValue(&value, sizeof(value), SMI_GAUGE32);
270}
271
272void
273Snmp::Var::setString(const Range<const u_char*>& string)
274{
275 setValue(string.start, string.size(), SMI_STRING);
276}
277
278void
279Snmp::Var::setObject(const Range<const oid*>& object)
280{
281 setValue(object.start, object.size() * sizeof(oid), SMI_OBJID);
282}
283
284void
285Snmp::Var::setCounter64(long long int counter)
286{
287 setValue(&counter, sizeof(counter), SMI_COUNTER64);
288}
289
290void
579928b6 291Snmp::Var::setTimeTicks(unsigned int ticks)
51ea0904
CT
292{
293 setValue(&ticks, sizeof(ticks), SMI_TIMETICKS);
294}
295
51ea0904
CT
296void
297Snmp::Var::copyValue(const Var& var)
298{
299 setValue(var.val.string, var.val_len, var.type);
300}
301
302void
303Snmp::Var::setValue(const void* value, int length, int aType)
304{
305 clearValue();
306 if (value != NULL) {
307 Must(length > 0 && aType > 0);
308 val.string = static_cast<u_char*>(xmalloc(length));
309 memcpy(val.string, value, length);
310 }
311 val_len = length;
312 type = aType;
313}
314
315void
316Snmp::Var::clear()
317{
318 clearName();
319 clearValue();
320 init();
321}
322
323void
324Snmp::Var::pack(Ipc::TypedMsgHdr& msg) const
325{
326 msg.putInt(name_length);
327 if (name_length > 0) {
328 Must(name != NULL);
329 msg.putFixed(name, name_length * sizeof(oid));
330 }
331 msg.putPod(type);
332 msg.putPod(val_len);
333 if (val_len > 0) {
334 Must(val.string != NULL);
335 msg.putFixed(val.string, val_len);
336 }
337}
338
339void
340Snmp::Var::unpack(const Ipc::TypedMsgHdr& msg)
341{
342 clearName();
343 clearValue();
344 name_length = msg.getInt();
345 Must(name_length >= 0);
346 if (name_length > 0) {
347 name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
348 msg.getFixed(name, name_length * sizeof(oid));
349 }
350 msg.getPod(type);
351 val_len = msg.getInt();
352 Must(val_len >= 0);
353 if (val_len > 0) {
354 val.string = static_cast<u_char*>(xmalloc(val_len));
355 msg.getFixed(val.string, val_len);
356 }
357}
f53969cc 358