]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Var.cc
2 * DEBUG: section 49 SNMP Interface
7 #include "base/TextException.h"
9 #include "ipc/TypedMsgHdr.h"
21 Snmp::Var::Var(const Var
& var
)
33 Snmp::Var::operator = (const Var
& var
)
43 xmemset(this, 0, sizeof(*this));
47 Snmp::Var::operator += (const Var
& var
)
51 setInt(asInt() + var
.asInt());
54 setGauge(asGauge() + var
.asGauge());
57 setCounter(asCounter() + var
.asCounter());
60 setCounter64(asCounter64() + var
.asCounter64());
63 setTimeTicks(asTimeTicks() + var
.asTimeTicks());
66 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
67 throw TexcHere("Unsupported type");
74 Snmp::Var::operator /= (int num
)
79 setInt(asInt() / num
);
82 setGauge(asGauge() / num
);
85 setCounter(asCounter() / num
);
88 setCounter64(asCounter64() / num
);
91 setTimeTicks(asTimeTicks() / num
);
94 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
95 throw TexcHere("Unsupported type");
102 Snmp::Var::operator < (const Var
& var
) const
106 return asInt() < var
.asInt();
108 return asGauge() < var
.asGauge();
110 return asCounter() < var
.asCounter();
112 return asCounter64() < var
.asCounter64();
114 return asTimeTicks() < var
.asTimeTicks();
116 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
117 throw TexcHere("Unsupported type");
120 return false; // unreachable
124 Snmp::Var::operator > (const Var
& var
) const
128 return asInt() > var
.asInt();
130 return asGauge() > var
.asGauge();
132 return asCounter() > var
.asCounter();
134 return asCounter64() > var
.asCounter64();
136 return asTimeTicks() > var
.asTimeTicks();
138 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
139 throw TexcHere("Unsupported type");
142 return false; // unreachable
146 Snmp::Var::assign(const Var
& var
)
148 setName(var
.getName());
153 Snmp::Var::clearName()
163 Snmp::Var::getName() const
165 return Range
<const oid
*>(name
, name
+ name_length
);
169 Snmp::Var::setName(const Range
<const oid
*>& aName
)
172 if (aName
.start
!= NULL
&& aName
.size() != 0) {
173 name_length
= aName
.size();
174 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
175 std::copy(aName
.start
, aName
.end
, name
);
180 Snmp::Var::clearValue()
182 if (val
.string
!= NULL
) {
191 Snmp::Var::isNull() const
193 return type
== SMI_NULLOBJ
;
197 Snmp::Var::asInt() const
199 Must(type
== SMI_INTEGER
);
200 Must(val
.integer
!= NULL
&& val_len
== sizeof(int));
205 Snmp::Var::asGauge() const
207 Must(type
== SMI_GAUGE32
);
208 Must(val
.integer
!= NULL
&& val_len
== 4);
209 return *reinterpret_cast<unsigned int*>(val
.integer
);
213 Snmp::Var::asCounter() const
215 Must(type
== SMI_COUNTER32
);
216 Must(val
.integer
!= NULL
&& val_len
== 4);
217 return *reinterpret_cast<int*>(val
.integer
);
221 Snmp::Var::asCounter64() const
223 Must(type
== SMI_COUNTER64
);
224 Must(val
.integer
!= NULL
&& val_len
== 8);
225 return *reinterpret_cast<long long int*>(val
.integer
);
229 Snmp::Var::asTimeTicks() const
231 Must(type
== SMI_TIMETICKS
);
232 Must(val
.integer
!= NULL
&& val_len
== sizeof(unsigned int));
233 return *reinterpret_cast<unsigned int*>(val
.integer
);
237 Snmp::Var::asObject() const
239 Must(type
== SMI_OBJID
);
240 Must(val_len
% sizeof(oid
) == 0);
241 int length
= val_len
/ sizeof(oid
);
242 Must(val
.objid
!= NULL
&& length
> 0);
243 return Range
<const oid
*>(val
.objid
, val
.objid
+ length
);
247 Snmp::Var::asString() const
249 Must(type
== SMI_STRING
);
250 Must(val
.string
!= NULL
&& val_len
> 0);
251 return Range
<const u_char
*>(val
.string
, val
.string
+ val_len
);
255 Snmp::Var::setInt(int value
)
257 setValue(&value
, sizeof(value
), SMI_INTEGER
);
261 Snmp::Var::setCounter(int value
)
263 setValue(&value
, sizeof(value
), SMI_COUNTER32
);
267 Snmp::Var::setGauge(unsigned int value
)
269 setValue(&value
, sizeof(value
), SMI_GAUGE32
);
273 Snmp::Var::setString(const Range
<const u_char
*>& string
)
275 setValue(string
.start
, string
.size(), SMI_STRING
);
279 Snmp::Var::setObject(const Range
<const oid
*>& object
)
281 setValue(object
.start
, object
.size() * sizeof(oid
), SMI_OBJID
);
285 Snmp::Var::setCounter64(long long int counter
)
287 setValue(&counter
, sizeof(counter
), SMI_COUNTER64
);
291 Snmp::Var::setTimeTicks(unsigned int ticks
)
293 setValue(&ticks
, sizeof(ticks
), SMI_TIMETICKS
);
297 Snmp::Var::copyValue(const Var
& var
)
299 setValue(var
.val
.string
, var
.val_len
, var
.type
);
303 Snmp::Var::setValue(const void* value
, int length
, int aType
)
307 Must(length
> 0 && aType
> 0);
308 val
.string
= static_cast<u_char
*>(xmalloc(length
));
309 memcpy(val
.string
, value
, length
);
324 Snmp::Var::pack(Ipc::TypedMsgHdr
& msg
) const
326 msg
.putInt(name_length
);
327 if (name_length
> 0) {
329 msg
.putFixed(name
, name_length
* sizeof(oid
));
334 Must(val
.string
!= NULL
);
335 msg
.putFixed(val
.string
, val_len
);
340 Snmp::Var::unpack(const Ipc::TypedMsgHdr
& msg
)
344 name_length
= msg
.getInt();
345 Must(name_length
>= 0);
346 if (name_length
> 0) {
347 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
348 msg
.getFixed(name
, name_length
* sizeof(oid
));
351 val_len
= msg
.getInt();
354 val
.string
= static_cast<u_char
*>(xmalloc(val_len
));
355 msg
.getFixed(val
.string
, val_len
);