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