]>
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"
22 Snmp::Var::Var(const Var
& var
)
34 Snmp::Var::operator = (const Var
& var
)
44 xmemset(this, 0, sizeof(*this));
48 Snmp::Var::operator += (const Var
& var
)
52 setInt(asInt() + var
.asInt());
55 setGauge(asGauge() + var
.asGauge());
58 setCounter(asCounter() + var
.asCounter());
61 setCounter64(asCounter64() + var
.asCounter64());
64 setTimeTicks(asTimeTicks() + var
.asTimeTicks());
67 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
68 throw TexcHere("Unsupported type");
75 Snmp::Var::operator /= (int num
)
80 setInt(asInt() / num
);
83 setGauge(asGauge() / num
);
86 setCounter(asCounter() / num
);
89 setCounter64(asCounter64() / num
);
92 setTimeTicks(asTimeTicks() / num
);
95 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
96 throw TexcHere("Unsupported type");
103 Snmp::Var::operator < (const Var
& var
) const
107 return asInt() < var
.asInt();
109 return asGauge() < var
.asGauge();
111 return asCounter() < var
.asCounter();
113 return asCounter64() < var
.asCounter64();
115 return asTimeTicks() < var
.asTimeTicks();
117 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
118 throw TexcHere("Unsupported type");
121 return false; // unreachable
125 Snmp::Var::operator > (const Var
& var
) const
129 return asInt() > var
.asInt();
131 return asGauge() > var
.asGauge();
133 return asCounter() > var
.asCounter();
135 return asCounter64() > var
.asCounter64();
137 return asTimeTicks() > var
.asTimeTicks();
139 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
140 throw TexcHere("Unsupported type");
143 return false; // unreachable
147 Snmp::Var::assign(const Var
& var
)
149 setName(var
.getName());
154 Snmp::Var::clearName()
164 Snmp::Var::getName() const
166 return Range
<const oid
*>(name
, name
+ name_length
);
170 Snmp::Var::setName(const Range
<const oid
*>& aName
)
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
);
181 Snmp::Var::clearValue()
183 if (val
.string
!= NULL
) {
192 Snmp::Var::isNull() const
194 return type
== SMI_NULLOBJ
;
198 Snmp::Var::asInt() const
200 Must(type
== SMI_INTEGER
);
201 Must(val
.integer
!= NULL
&& val_len
== sizeof(int));
206 Snmp::Var::asGauge() const
208 Must(type
== SMI_GAUGE32
);
209 Must(val
.integer
!= NULL
&& val_len
== 4);
210 return *reinterpret_cast<unsigned int*>(val
.integer
);
214 Snmp::Var::asCounter() const
216 Must(type
== SMI_COUNTER32
);
217 Must(val
.integer
!= NULL
&& val_len
== 4);
218 return *reinterpret_cast<int*>(val
.integer
);
222 Snmp::Var::asCounter64() const
224 Must(type
== SMI_COUNTER64
);
225 Must(val
.integer
!= NULL
&& val_len
== 8);
226 return *reinterpret_cast<long long int*>(val
.integer
);
230 Snmp::Var::asTimeTicks() const
232 Must(type
== SMI_TIMETICKS
);
233 Must(val
.integer
!= NULL
&& val_len
== sizeof(unsigned int));
234 return *reinterpret_cast<unsigned int*>(val
.integer
);
238 Snmp::Var::asObject() const
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
);
248 Snmp::Var::asString() const
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
);
256 Snmp::Var::setInt(int value
)
258 setValue(&value
, sizeof(value
), SMI_INTEGER
);
262 Snmp::Var::setCounter(int value
)
264 setValue(&value
, sizeof(value
), SMI_COUNTER32
);
268 Snmp::Var::setGauge(unsigned int value
)
270 setValue(&value
, sizeof(value
), SMI_GAUGE32
);
274 Snmp::Var::setString(const Range
<const u_char
*>& string
)
276 setValue(string
.start
, string
.size(), SMI_STRING
);
280 Snmp::Var::setObject(const Range
<const oid
*>& object
)
282 setValue(object
.start
, object
.size() * sizeof(oid
), SMI_OBJID
);
286 Snmp::Var::setCounter64(long long int counter
)
288 setValue(&counter
, sizeof(counter
), SMI_COUNTER64
);
292 Snmp::Var::setTimeTicks(unsigned int ticks
)
294 setValue(&ticks
, sizeof(ticks
), SMI_TIMETICKS
);
298 Snmp::Var::copyValue(const Var
& var
)
300 setValue(var
.val
.string
, var
.val_len
, var
.type
);
304 Snmp::Var::setValue(const void* value
, int length
, int aType
)
308 Must(length
> 0 && aType
> 0);
309 val
.string
= static_cast<u_char
*>(xmalloc(length
));
310 memcpy(val
.string
, value
, length
);
325 Snmp::Var::pack(Ipc::TypedMsgHdr
& msg
) const
327 msg
.putInt(name_length
);
328 if (name_length
> 0) {
330 msg
.putFixed(name
, name_length
* sizeof(oid
));
335 Must(val
.string
!= NULL
);
336 msg
.putFixed(val
.string
, val_len
);
341 Snmp::Var::unpack(const Ipc::TypedMsgHdr
& msg
)
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
));
352 val_len
= msg
.getInt();
355 val
.string
= static_cast<u_char
*>(xmalloc(val_len
));
356 msg
.getFixed(val
.string
, val_len
);