]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Var.cc
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 49 SNMP Interface */
12 #include "base/TextException.h"
14 #include "ipc/TypedMsgHdr.h"
25 Snmp::Var::Var(const Var
& var
)
37 Snmp::Var::operator = (const Var
& var
)
47 memset(this, 0, sizeof(*this));
51 Snmp::Var::operator += (const Var
& var
)
55 setInt(asInt() + var
.asInt());
58 setGauge(asGauge() + var
.asGauge());
61 setCounter(asCounter() + var
.asCounter());
64 setCounter64(asCounter64() + var
.asCounter64());
67 setTimeTicks(asTimeTicks() + var
.asTimeTicks());
70 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
71 throw TexcHere("Unsupported type");
78 Snmp::Var::operator /= (int num
)
83 setInt(asInt() / num
);
86 setGauge(asGauge() / num
);
89 setCounter(asCounter() / num
);
92 setCounter64(asCounter64() / num
);
95 setTimeTicks(asTimeTicks() / num
);
98 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
99 throw TexcHere("Unsupported type");
106 Snmp::Var::operator < (const Var
& var
) const
110 return asInt() < var
.asInt();
112 return asGauge() < var
.asGauge();
114 return asCounter() < var
.asCounter();
116 return asCounter64() < var
.asCounter64();
118 return asTimeTicks() < var
.asTimeTicks();
120 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
121 throw TexcHere("Unsupported type");
124 return false; // unreachable
128 Snmp::Var::operator > (const Var
& var
) const
132 return asInt() > var
.asInt();
134 return asGauge() > var
.asGauge();
136 return asCounter() > var
.asCounter();
138 return asCounter64() > var
.asCounter64();
140 return asTimeTicks() > var
.asTimeTicks();
142 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
143 throw TexcHere("Unsupported type");
146 return false; // unreachable
150 Snmp::Var::assign(const Var
& var
)
152 setName(var
.getName());
157 Snmp::Var::clearName()
167 Snmp::Var::getName() const
169 return Range
<const oid
*>(name
, name
+ name_length
);
173 Snmp::Var::setName(const Range
<const oid
*>& aName
)
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
);
184 Snmp::Var::clearValue()
186 if (val
.string
!= NULL
) {
195 Snmp::Var::isNull() const
197 return type
== SMI_NULLOBJ
;
201 Snmp::Var::asInt() const
203 Must(type
== SMI_INTEGER
);
204 Must(val
.integer
!= NULL
&& val_len
== sizeof(int));
209 Snmp::Var::asGauge() const
211 Must(type
== SMI_GAUGE32
);
212 Must(val
.integer
!= NULL
&& val_len
== 4);
213 return *reinterpret_cast<unsigned int*>(val
.integer
);
217 Snmp::Var::asCounter() const
219 Must(type
== SMI_COUNTER32
);
220 Must(val
.integer
!= NULL
&& val_len
== 4);
221 return *reinterpret_cast<int*>(val
.integer
);
225 Snmp::Var::asCounter64() const
227 Must(type
== SMI_COUNTER64
);
228 Must(val
.integer
!= NULL
&& val_len
== 8);
229 return *reinterpret_cast<long long int*>(val
.integer
);
233 Snmp::Var::asTimeTicks() const
235 Must(type
== SMI_TIMETICKS
);
236 Must(val
.integer
!= NULL
&& val_len
== sizeof(unsigned int));
237 return *reinterpret_cast<unsigned int*>(val
.integer
);
241 Snmp::Var::asObject() const
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
);
251 Snmp::Var::asString() const
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
);
259 Snmp::Var::setInt(int value
)
261 setValue(&value
, sizeof(value
), SMI_INTEGER
);
265 Snmp::Var::setCounter(int value
)
267 setValue(&value
, sizeof(value
), SMI_COUNTER32
);
271 Snmp::Var::setGauge(unsigned int value
)
273 setValue(&value
, sizeof(value
), SMI_GAUGE32
);
277 Snmp::Var::setString(const Range
<const u_char
*>& string
)
279 setValue(string
.start
, string
.size(), SMI_STRING
);
283 Snmp::Var::setObject(const Range
<const oid
*>& object
)
285 setValue(object
.start
, object
.size() * sizeof(oid
), SMI_OBJID
);
289 Snmp::Var::setCounter64(long long int counter
)
291 setValue(&counter
, sizeof(counter
), SMI_COUNTER64
);
295 Snmp::Var::setTimeTicks(unsigned int ticks
)
297 setValue(&ticks
, sizeof(ticks
), SMI_TIMETICKS
);
301 Snmp::Var::copyValue(const Var
& var
)
303 setValue(var
.val
.string
, var
.val_len
, var
.type
);
307 Snmp::Var::setValue(const void* value
, int length
, int aType
)
311 Must(length
> 0 && aType
> 0);
312 val
.string
= static_cast<u_char
*>(xmalloc(length
));
313 memcpy(val
.string
, value
, length
);
328 Snmp::Var::pack(Ipc::TypedMsgHdr
& msg
) const
330 msg
.putInt(name_length
);
331 if (name_length
> 0) {
333 msg
.putFixed(name
, name_length
* sizeof(oid
));
338 Must(val
.string
!= NULL
);
339 msg
.putFixed(val
.string
, val_len
);
344 Snmp::Var::unpack(const Ipc::TypedMsgHdr
& msg
)
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
));
355 val_len
= msg
.getInt();
358 val
.string
= static_cast<u_char
*>(xmalloc(val_len
));
359 msg
.getFixed(val
.string
, val_len
);