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