]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Var.cc
4 * DEBUG: section 49 SNMP Interface
9 #include "base/TextException.h"
10 #include "ipc/TypedMsgHdr.h"
23 Snmp::Var::Var(const Var
& var
)
35 Snmp::Var::operator = (const Var
& var
)
45 xmemset(this, 0, sizeof(*this));
49 Snmp::Var::operator += (const Var
& var
)
53 setInt(asInt() + var
.asInt());
56 setGauge(asGauge() + var
.asGauge());
59 setCounter(asCounter() + var
.asCounter());
62 setCounter64(asCounter64() + var
.asCounter64());
65 setTimeTicks(asTimeTicks() + var
.asTimeTicks());
68 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
69 throw TexcHere("Unsupported type");
76 Snmp::Var::operator /= (int num
)
81 setInt(asInt() / num
);
84 setGauge(asGauge() / num
);
87 setCounter(asCounter() / num
);
90 setCounter64(asCounter64() / num
);
93 setTimeTicks(asTimeTicks() / num
);
96 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
97 throw TexcHere("Unsupported type");
104 Snmp::Var::operator < (const Var
& var
) const
108 return asInt() < var
.asInt();
110 return asGauge() < var
.asGauge();
112 return asCounter() < var
.asCounter();
114 return asCounter64() < var
.asCounter64();
116 return asTimeTicks() < var
.asTimeTicks();
118 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
119 throw TexcHere("Unsupported type");
122 return false; // unreachable
126 Snmp::Var::operator > (const Var
& var
) const
130 return asInt() > var
.asInt();
132 return asGauge() > var
.asGauge();
134 return asCounter() > var
.asCounter();
136 return asCounter64() > var
.asCounter64();
138 return asTimeTicks() > var
.asTimeTicks();
140 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
141 throw TexcHere("Unsupported type");
144 return false; // unreachable
148 Snmp::Var::assign(const Var
& var
)
150 setName(var
.getName());
155 Snmp::Var::clearName()
165 Snmp::Var::getName() const
167 return Range
<const oid
*>(name
, name
+ name_length
);
171 Snmp::Var::setName(const Range
<const oid
*>& aName
)
174 if (aName
.start
!= NULL
&& aName
.size() != 0) {
175 name_length
= aName
.size();
176 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
177 std::copy(aName
.start
, aName
.end
, name
);
182 Snmp::Var::clearValue()
184 if (val
.string
!= NULL
) {
193 Snmp::Var::isNull() const
195 return type
== SMI_NULLOBJ
;
199 Snmp::Var::asInt() const
201 Must(type
== SMI_INTEGER
);
202 Must(val
.integer
!= NULL
&& val_len
== sizeof(int));
207 Snmp::Var::asGauge() const
209 Must(type
== SMI_GAUGE32
);
210 Must(val
.integer
!= NULL
&& val_len
== 4);
211 return *reinterpret_cast<unsigned int*>(val
.integer
);
215 Snmp::Var::asCounter() const
217 Must(type
== SMI_COUNTER32
);
218 Must(val
.integer
!= NULL
&& val_len
== 4);
219 return *reinterpret_cast<int*>(val
.integer
);
223 Snmp::Var::asCounter64() const
225 Must(type
== SMI_COUNTER64
);
226 Must(val
.integer
!= NULL
&& val_len
== 8);
227 return *reinterpret_cast<long long int*>(val
.integer
);
231 Snmp::Var::asTimeTicks() const
233 Must(type
== SMI_TIMETICKS
);
234 Must(val
.integer
!= NULL
&& val_len
== sizeof(unsigned int));
235 return *reinterpret_cast<unsigned int*>(val
.integer
);
239 Snmp::Var::asObject() const
241 Must(type
== SMI_OBJID
);
242 Must(val_len
% sizeof(oid
) == 0);
243 int length
= val_len
/ sizeof(oid
);
244 Must(val
.objid
!= NULL
&& length
> 0);
245 return Range
<const oid
*>(val
.objid
, val
.objid
+ length
);
249 Snmp::Var::asString() const
251 Must(type
== SMI_STRING
);
252 Must(val
.string
!= NULL
&& val_len
> 0);
253 return Range
<const u_char
*>(val
.string
, val
.string
+ val_len
);
257 Snmp::Var::setInt(int value
)
259 setValue(&value
, sizeof(value
), SMI_INTEGER
);
263 Snmp::Var::setCounter(int value
)
265 setValue(&value
, sizeof(value
), SMI_COUNTER32
);
269 Snmp::Var::setGauge(unsigned int value
)
271 setValue(&value
, sizeof(value
), SMI_GAUGE32
);
275 Snmp::Var::setString(const Range
<const u_char
*>& string
)
277 setValue(string
.start
, string
.size(), SMI_STRING
);
281 Snmp::Var::setObject(const Range
<const oid
*>& object
)
283 setValue(object
.start
, object
.size() * sizeof(oid
), SMI_OBJID
);
287 Snmp::Var::setCounter64(long long int counter
)
289 setValue(&counter
, sizeof(counter
), SMI_COUNTER64
);
293 Snmp::Var::setTimeTicks(unsigned int ticks
)
295 setValue(&ticks
, sizeof(ticks
), SMI_TIMETICKS
);
299 Snmp::Var::copyValue(const Var
& var
)
301 setValue(var
.val
.string
, var
.val_len
, var
.type
);
305 Snmp::Var::setValue(const void* value
, int length
, int aType
)
309 Must(length
> 0 && aType
> 0);
310 val
.string
= static_cast<u_char
*>(xmalloc(length
));
311 memcpy(val
.string
, value
, length
);
326 Snmp::Var::pack(Ipc::TypedMsgHdr
& msg
) const
328 msg
.putInt(name_length
);
329 if (name_length
> 0) {
331 msg
.putFixed(name
, name_length
* sizeof(oid
));
336 Must(val
.string
!= NULL
);
337 msg
.putFixed(val
.string
, val_len
);
342 Snmp::Var::unpack(const Ipc::TypedMsgHdr
& msg
)
346 name_length
= msg
.getInt();
347 Must(name_length
>= 0);
348 if (name_length
> 0) {
349 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
350 msg
.getFixed(name
, name_length
* sizeof(oid
));
353 val_len
= msg
.getInt();
356 val
.string
= static_cast<u_char
*>(xmalloc(val_len
));
357 msg
.getFixed(val
.string
, val_len
);