]>
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"
20 Snmp::Var::Var(const Var
& var
)
32 Snmp::Var::operator = (const Var
& var
)
42 memset(this, 0, sizeof(*this));
46 Snmp::Var::operator += (const Var
& var
)
50 setInt(asInt() + var
.asInt());
53 setGauge(asGauge() + var
.asGauge());
56 setCounter(asCounter() + var
.asCounter());
59 setCounter64(asCounter64() + var
.asCounter64());
62 setTimeTicks(asTimeTicks() + var
.asTimeTicks());
65 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
66 throw TexcHere("Unsupported type");
73 Snmp::Var::operator /= (int num
)
78 setInt(asInt() / num
);
81 setGauge(asGauge() / num
);
84 setCounter(asCounter() / num
);
87 setCounter64(asCounter64() / num
);
90 setTimeTicks(asTimeTicks() / num
);
93 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
94 throw TexcHere("Unsupported type");
101 Snmp::Var::operator < (const Var
& var
) const
105 return asInt() < var
.asInt();
107 return asGauge() < var
.asGauge();
109 return asCounter() < var
.asCounter();
111 return asCounter64() < var
.asCounter64();
113 return asTimeTicks() < var
.asTimeTicks();
115 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
116 throw TexcHere("Unsupported type");
119 return false; // unreachable
123 Snmp::Var::operator > (const Var
& var
) const
127 return asInt() > var
.asInt();
129 return asGauge() > var
.asGauge();
131 return asCounter() > var
.asCounter();
133 return asCounter64() > var
.asCounter64();
135 return asTimeTicks() > var
.asTimeTicks();
137 debugs(49, DBG_CRITICAL
, HERE
<< "Unsupported type: " << type
);
138 throw TexcHere("Unsupported type");
141 return false; // unreachable
145 Snmp::Var::assign(const Var
& var
)
147 setName(var
.getName());
152 Snmp::Var::clearName()
162 Snmp::Var::getName() const
164 return Range
<const oid
*>(name
, name
+ name_length
);
168 Snmp::Var::setName(const Range
<const oid
*>& aName
)
171 if (aName
.start
!= NULL
&& aName
.size() != 0) {
172 name_length
= aName
.size();
173 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
174 std::copy(aName
.start
, aName
.end
, name
);
179 Snmp::Var::clearValue()
181 if (val
.string
!= NULL
) {
190 Snmp::Var::isNull() const
192 return type
== SMI_NULLOBJ
;
196 Snmp::Var::asInt() const
198 Must(type
== SMI_INTEGER
);
199 Must(val
.integer
!= NULL
&& val_len
== sizeof(int));
204 Snmp::Var::asGauge() const
206 Must(type
== SMI_GAUGE32
);
207 Must(val
.integer
!= NULL
&& val_len
== 4);
208 return *reinterpret_cast<unsigned int*>(val
.integer
);
212 Snmp::Var::asCounter() const
214 Must(type
== SMI_COUNTER32
);
215 Must(val
.integer
!= NULL
&& val_len
== 4);
216 return *reinterpret_cast<int*>(val
.integer
);
220 Snmp::Var::asCounter64() const
222 Must(type
== SMI_COUNTER64
);
223 Must(val
.integer
!= NULL
&& val_len
== 8);
224 return *reinterpret_cast<long long int*>(val
.integer
);
228 Snmp::Var::asTimeTicks() const
230 Must(type
== SMI_TIMETICKS
);
231 Must(val
.integer
!= NULL
&& val_len
== sizeof(unsigned int));
232 return *reinterpret_cast<unsigned int*>(val
.integer
);
236 Snmp::Var::asObject() const
238 Must(type
== SMI_OBJID
);
239 Must(val_len
% sizeof(oid
) == 0);
240 int length
= val_len
/ sizeof(oid
);
241 Must(val
.objid
!= NULL
&& length
> 0);
242 return Range
<const oid
*>(val
.objid
, val
.objid
+ length
);
246 Snmp::Var::asString() const
248 Must(type
== SMI_STRING
);
249 Must(val
.string
!= NULL
&& val_len
> 0);
250 return Range
<const u_char
*>(val
.string
, val
.string
+ val_len
);
254 Snmp::Var::setInt(int value
)
256 setValue(&value
, sizeof(value
), SMI_INTEGER
);
260 Snmp::Var::setCounter(int value
)
262 setValue(&value
, sizeof(value
), SMI_COUNTER32
);
266 Snmp::Var::setGauge(unsigned int value
)
268 setValue(&value
, sizeof(value
), SMI_GAUGE32
);
272 Snmp::Var::setString(const Range
<const u_char
*>& string
)
274 setValue(string
.start
, string
.size(), SMI_STRING
);
278 Snmp::Var::setObject(const Range
<const oid
*>& object
)
280 setValue(object
.start
, object
.size() * sizeof(oid
), SMI_OBJID
);
284 Snmp::Var::setCounter64(long long int counter
)
286 setValue(&counter
, sizeof(counter
), SMI_COUNTER64
);
290 Snmp::Var::setTimeTicks(unsigned int ticks
)
292 setValue(&ticks
, sizeof(ticks
), SMI_TIMETICKS
);
296 Snmp::Var::copyValue(const Var
& var
)
298 setValue(var
.val
.string
, var
.val_len
, var
.type
);
302 Snmp::Var::setValue(const void* value
, int length
, int aType
)
306 Must(length
> 0 && aType
> 0);
307 val
.string
= static_cast<u_char
*>(xmalloc(length
));
308 memcpy(val
.string
, value
, length
);
323 Snmp::Var::pack(Ipc::TypedMsgHdr
& msg
) const
325 msg
.putInt(name_length
);
326 if (name_length
> 0) {
328 msg
.putFixed(name
, name_length
* sizeof(oid
));
333 Must(val
.string
!= NULL
);
334 msg
.putFixed(val
.string
, val_len
);
339 Snmp::Var::unpack(const Ipc::TypedMsgHdr
& msg
)
343 name_length
= msg
.getInt();
344 Must(name_length
>= 0);
345 if (name_length
> 0) {
346 name
= static_cast<oid
*>(xmalloc(name_length
* sizeof(oid
)));
347 msg
.getFixed(name
, name_length
* sizeof(oid
));
350 val_len
= msg
.getInt();
353 val
.string
= static_cast<u_char
*>(xmalloc(val_len
));
354 msg
.getFixed(val
.string
, val_len
);