]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/snmp/Var.cc
4140309a7059532a64eb2355d74f01fd0d78c16c
2 * Copyright (C) 1996-2020 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(static_cast<variable_list
*>(this), 0, sizeof(variable_list
));
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()
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()
185 val
.string
= nullptr;
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
);