]> git.ipfire.org Git - thirdparty/squid.git/blame - src/snmp/Var.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / snmp / Var.cc
CommitLineData
51ea0904 1/*
bde978a6 2 * Copyright (C) 1996-2015 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{
e297be13 47 memset(this, 0, sizeof(*this));
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:
70 debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
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:
98 debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
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:
120 debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
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:
142 debugs(49, DBG_CRITICAL, HERE << "Unsupported type: " << type);
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{
159 if (name != NULL) {
160 xfree(name);
161 name = NULL;
162 }
163 name_length = 0;
164}
165
166Range<const oid*>
167Snmp::Var::getName() const
168{
169 return Range<const oid*>(name, name + name_length);
170}
171
172void
173Snmp::Var::setName(const Range<const oid*>& aName)
174{
175 clearName();
176 if (aName.start != NULL && aName.size() != 0) {
177 name_length = aName.size();
178 name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
179 std::copy(aName.start, aName.end, name);
180 }
181}
182
183void
184Snmp::Var::clearValue()
185{
186 if (val.string != NULL) {
187 xfree(val.string);
188 val.string = NULL;
189 }
190 val_len = 0;
191 type = 0;
192}
193
194bool
195Snmp::Var::isNull() const
196{
197 return type == SMI_NULLOBJ;
198}
199
200int
201Snmp::Var::asInt() const
202{
203 Must(type == SMI_INTEGER);
204 Must(val.integer != NULL && val_len == sizeof(int));
205 return *val.integer;
206}
207
208unsigned int
209Snmp::Var::asGauge() const
210{
211 Must(type == SMI_GAUGE32);
212 Must(val.integer != NULL && val_len == 4);
213 return *reinterpret_cast<unsigned int*>(val.integer);
214}
215
216int
217Snmp::Var::asCounter() const
218{
219 Must(type == SMI_COUNTER32);
220 Must(val.integer != NULL && val_len == 4);
221 return *reinterpret_cast<int*>(val.integer);
222}
223
224long long int
225Snmp::Var::asCounter64() const
226{
227 Must(type == SMI_COUNTER64);
228 Must(val.integer != NULL && val_len == 8);
229 return *reinterpret_cast<long long int*>(val.integer);
230}
231
579928b6 232unsigned int
51ea0904
CT
233Snmp::Var::asTimeTicks() const
234{
235 Must(type == SMI_TIMETICKS);
579928b6
CT
236 Must(val.integer != NULL && val_len == sizeof(unsigned int));
237 return *reinterpret_cast<unsigned int*>(val.integer);
51ea0904
CT
238}
239
240Range<const oid*>
241Snmp::Var::asObject() const
242{
243 Must(type == SMI_OBJID);
244 Must(val_len % sizeof(oid) == 0);
245 int length = val_len / sizeof(oid);
246 Must(val.objid != NULL && length > 0);
247 return Range<const oid*>(val.objid, val.objid + length);
248}
249
250Range<const u_char*>
251Snmp::Var::asString() const
252{
253 Must(type == SMI_STRING);
254 Must(val.string != NULL && val_len > 0);
255 return Range<const u_char*>(val.string, val.string + val_len);
256}
257
51ea0904
CT
258void
259Snmp::Var::setInt(int value)
260{
261 setValue(&value, sizeof(value), SMI_INTEGER);
262}
263
264void
265Snmp::Var::setCounter(int value)
266{
267 setValue(&value, sizeof(value), SMI_COUNTER32);
268}
269
270void
271Snmp::Var::setGauge(unsigned int value)
272{
273 setValue(&value, sizeof(value), SMI_GAUGE32);
274}
275
276void
277Snmp::Var::setString(const Range<const u_char*>& string)
278{
279 setValue(string.start, string.size(), SMI_STRING);
280}
281
282void
283Snmp::Var::setObject(const Range<const oid*>& object)
284{
285 setValue(object.start, object.size() * sizeof(oid), SMI_OBJID);
286}
287
288void
289Snmp::Var::setCounter64(long long int counter)
290{
291 setValue(&counter, sizeof(counter), SMI_COUNTER64);
292}
293
294void
579928b6 295Snmp::Var::setTimeTicks(unsigned int ticks)
51ea0904
CT
296{
297 setValue(&ticks, sizeof(ticks), SMI_TIMETICKS);
298}
299
51ea0904
CT
300void
301Snmp::Var::copyValue(const Var& var)
302{
303 setValue(var.val.string, var.val_len, var.type);
304}
305
306void
307Snmp::Var::setValue(const void* value, int length, int aType)
308{
309 clearValue();
310 if (value != NULL) {
311 Must(length > 0 && aType > 0);
312 val.string = static_cast<u_char*>(xmalloc(length));
313 memcpy(val.string, value, length);
314 }
315 val_len = length;
316 type = aType;
317}
318
319void
320Snmp::Var::clear()
321{
322 clearName();
323 clearValue();
324 init();
325}
326
327void
328Snmp::Var::pack(Ipc::TypedMsgHdr& msg) const
329{
330 msg.putInt(name_length);
331 if (name_length > 0) {
332 Must(name != NULL);
333 msg.putFixed(name, name_length * sizeof(oid));
334 }
335 msg.putPod(type);
336 msg.putPod(val_len);
337 if (val_len > 0) {
338 Must(val.string != NULL);
339 msg.putFixed(val.string, val_len);
340 }
341}
342
343void
344Snmp::Var::unpack(const Ipc::TypedMsgHdr& msg)
345{
346 clearName();
347 clearValue();
348 name_length = msg.getInt();
349 Must(name_length >= 0);
350 if (name_length > 0) {
351 name = static_cast<oid*>(xmalloc(name_length * sizeof(oid)));
352 msg.getFixed(name, name_length * sizeof(oid));
353 }
354 msg.getPod(type);
355 val_len = msg.getInt();
356 Must(val_len >= 0);
357 if (val_len > 0) {
358 val.string = static_cast<u_char*>(xmalloc(val_len));
359 msg.getFixed(val.string, val_len);
360 }
361}
f53969cc 362