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