]> git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Var.cc
Sync with trunk rev.13542
[thirdparty/squid.git] / src / snmp / Var.cc
1 /*
2 * DEBUG: section 49 SNMP Interface
3 *
4 */
5
6 #include "squid.h"
7 #include "base/TextException.h"
8 #include "Debug.h"
9 #include "ipc/TypedMsgHdr.h"
10 #include "snmp/Var.h"
11 #include "tools.h"
12
13 #include <algorithm>
14
15 Snmp::Var::Var()
16 {
17 init();
18 }
19
20 Snmp::Var::Var(const Var& var)
21 {
22 init();
23 assign(var);
24 }
25
26 Snmp::Var::~Var()
27 {
28 clear();
29 }
30
31 Snmp::Var&
32 Snmp::Var::operator = (const Var& var)
33 {
34 clear();
35 assign(var);
36 return *this;
37 }
38
39 void
40 Snmp::Var::init()
41 {
42 memset(this, 0, sizeof(*this));
43 }
44
45 Snmp::Var&
46 Snmp::Var::operator += (const Var& var)
47 {
48 switch (type) {
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
72 Snmp::Var&
73 Snmp::Var::operator /= (int num)
74 {
75 Must(num != 0);
76 switch (type) {
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
100 bool
101 Snmp::Var::operator < (const Var& var) const
102 {
103 switch (type) {
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
122 bool
123 Snmp::Var::operator > (const Var& var) const
124 {
125 switch (type) {
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
144 void
145 Snmp::Var::assign(const Var& var)
146 {
147 setName(var.getName());
148 copyValue(var);
149 }
150
151 void
152 Snmp::Var::clearName()
153 {
154 if (name != NULL) {
155 xfree(name);
156 name = NULL;
157 }
158 name_length = 0;
159 }
160
161 Range<const oid*>
162 Snmp::Var::getName() const
163 {
164 return Range<const oid*>(name, name + name_length);
165 }
166
167 void
168 Snmp::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
178 void
179 Snmp::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
189 bool
190 Snmp::Var::isNull() const
191 {
192 return type == SMI_NULLOBJ;
193 }
194
195 int
196 Snmp::Var::asInt() const
197 {
198 Must(type == SMI_INTEGER);
199 Must(val.integer != NULL && val_len == sizeof(int));
200 return *val.integer;
201 }
202
203 unsigned int
204 Snmp::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
211 int
212 Snmp::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
219 long long int
220 Snmp::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
227 unsigned int
228 Snmp::Var::asTimeTicks() const
229 {
230 Must(type == SMI_TIMETICKS);
231 Must(val.integer != NULL && val_len == sizeof(unsigned int));
232 return *reinterpret_cast<unsigned int*>(val.integer);
233 }
234
235 Range<const oid*>
236 Snmp::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
245 Range<const u_char*>
246 Snmp::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
253 void
254 Snmp::Var::setInt(int value)
255 {
256 setValue(&value, sizeof(value), SMI_INTEGER);
257 }
258
259 void
260 Snmp::Var::setCounter(int value)
261 {
262 setValue(&value, sizeof(value), SMI_COUNTER32);
263 }
264
265 void
266 Snmp::Var::setGauge(unsigned int value)
267 {
268 setValue(&value, sizeof(value), SMI_GAUGE32);
269 }
270
271 void
272 Snmp::Var::setString(const Range<const u_char*>& string)
273 {
274 setValue(string.start, string.size(), SMI_STRING);
275 }
276
277 void
278 Snmp::Var::setObject(const Range<const oid*>& object)
279 {
280 setValue(object.start, object.size() * sizeof(oid), SMI_OBJID);
281 }
282
283 void
284 Snmp::Var::setCounter64(long long int counter)
285 {
286 setValue(&counter, sizeof(counter), SMI_COUNTER64);
287 }
288
289 void
290 Snmp::Var::setTimeTicks(unsigned int ticks)
291 {
292 setValue(&ticks, sizeof(ticks), SMI_TIMETICKS);
293 }
294
295 void
296 Snmp::Var::copyValue(const Var& var)
297 {
298 setValue(var.val.string, var.val_len, var.type);
299 }
300
301 void
302 Snmp::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
314 void
315 Snmp::Var::clear()
316 {
317 clearName();
318 clearValue();
319 init();
320 }
321
322 void
323 Snmp::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
338 void
339 Snmp::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 }