SNMP 2.0.0
Loading...
Searching...
No Matches
SNMPMessage.h
Go to the documentation of this file.
1#ifndef SNMPMESSAGE_H_
2#define SNMPMESSAGE_H_
3
4#include "BER.h"
5
10namespace SNMP {
11
17class PDU {
18private:
27 struct Generic {
32 struct Bulk {
37 };
38
46 _error() {
47 _requestID = rand();
48 }
49
54 uint32_t _requestID;
55
64 union {
69 };
70 };
71public:
75 PDU() :
76 _generic() {
77 }
78
82 ~PDU() {
83 }
84
85 union {
90 };
91};
92
110class Message: public SequenceBER, private PDU {
111public:
116 class OID {
117 public:
118 static constexpr char *COLDSTART = "1.3.6.1.6.3.1.1.5.1";
119 static constexpr char *WARMSTART = "1.3.6.1.6.3.1.1.5.2";
120 static constexpr char *LINKDOWN = "1.3.6.1.6.3.1.1.5.3";
121 static constexpr char *LINKUP = "1.3.6.1.6.3.1.1.5.4";
122 static constexpr char *AUTHENTICATIONFAILURE = "1.3.6.1.6.3.1.1.5.5";
123
124 private:
125 static constexpr char *SYSUPTIME = "1.3.6.1.2.1.1.3.0";
126 static constexpr char *SNMPTRAPOID = "1.3.6.1.6.3.1.1.4.1.0";
127 static constexpr char *SNMPTRAPENTERPRISE = "1.3.6.1.6.3.1.1.4.3.0";
128
129 friend class Message;
130 };
131
139 Message(const uint8_t version = Version::V1, const char *community = nullptr,
140 const uint8_t type = Type::GetRequest) :
141 SequenceBER(), PDU() {
142 _version = version;
143 _community = community;
144 _type = type;
146 }
147
153 virtual ~Message() {
154 delete _varBindList;
155 }
156
166 virtual const unsigned int getSize(const bool refresh = false) {
167 build();
168 return ArrayBER::getSize(refresh);
169 }
170
180 VarBind* add(const char *oid, BER *value = nullptr) {
181 return static_cast<VarBind*>(_varBindList->add(new VarBind(oid, value)));
182 }
183
189 const uint8_t getVersion() const {
190 return _version;
191 }
192
198 const char* getCommunity() const {
199 return _community;
200 }
201
207 const uint8_t getType() const {
208 return _type;
209 }
210
218 const int32_t getRequestID() const {
219 return _generic._requestID;
220 }
221
229 void setRequestID(const int32_t requestId) {
230 _generic._requestID = requestId;
231 }
232
240 const uint8_t getErrorStatus() const {
241 return _generic._error._status;
242 }
243
251 const uint8_t getErrorIndex() const {
252 return _generic._error._index;
253 }
254
263 void setError(const uint8_t status, const uint8_t index) {
265 _generic._error._index = index;
266 }
267
275 const uint8_t getNonRepeaters() const {
277 }
278
286 void setNonRepeaters(const uint8_t nonRepeaters) {
287 _generic._bulk._nonRepeaters = nonRepeaters;
288 }
289
297 const uint8_t getMaxRepetition() const {
299 }
300
308 void setMaxRepetitions(const uint8_t maxRepetitions) {
309 _generic._bulk._maxRepetitions = maxRepetitions;
310 }
311
319 void setEnterprise(const char *enterprise) {
320 _trap._enterprise = enterprise;
321 }
322
330 void setAgentAddress(IPAddress agentAddr) {
331 _trap._agentAddr = agentAddr;
332 }
333
342 void setTrap(const uint8_t generic, const uint8_t specific = 0) {
343 _trap._genericTrap = generic;
344 _trap._specificTrap = specific;
345 }
346
358 void setSNMPTrapOID(const char *name) {
359// add(OID::SYSUPTIME, new TimeTicksBER(0));
360 add(OID::SYSUPTIME, new TimeTicksBER(millis() / 10));
362 }
363
370 return _varBindList;
371 }
372
373private:
379 void build() {
380 SequenceBER *pdu = new SequenceBER(_type);
381 switch (_type) {
382 case Type::Trap:
384 pdu->add(new IPAddressBER(_trap._agentAddr));
385 pdu->add(new IntegerBER(_trap._genericTrap));
387 pdu->add(new TimeTicksBER(millis() / 10));
388 break;
393 break;
394 default:
398// if ((_type == Type::SNMPv2Trap) || (_type == Type::InformRequest)) {
399// // TODO Is it really useful?
400// static_cast<TimeTicksBER*>((*_varBindList)[0]->getValue())->setValue(
401// millis() / 10);
402// // Refresh size
403// _varBindList->getSize(true);
404// }
405 break;
406 }
407 pdu->add(_varBindList);
410 ArrayBER::add(pdu);
411 _varBindList = nullptr;
412 }
413
417 void parse() {
418 _version = static_cast<IntegerBER*>(operator [](0))->getValue();
419 _community = static_cast<OctetStringBER*>(operator [](1))->getValue();
420 SequenceBER *pdu = static_cast<SequenceBER*>(operator [](2));
421 _type = pdu->getType();
422 switch (_type) {
423 case Type::Trap:
424 _trap._enterprise = static_cast<ObjectIdentifierBER*>((*pdu)[0])->getValue();
425 _trap._agentAddr = static_cast<IPAddressBER*>((*pdu)[1])->getValue();
426 _trap._genericTrap = static_cast<IntegerBER*>((*pdu)[2])->getValue();
427 _trap._specificTrap = static_cast<IntegerBER*>((*pdu)[3])->getValue();
428 _trap._timeStamp = static_cast<TimeTicksBER*>((*pdu)[4])->getValue();
429 break;
431 _generic._requestID = static_cast<IntegerBER*>((*pdu)[0])->getValue();
432 _generic._bulk._nonRepeaters = static_cast<IntegerBER*>((*pdu)[1])->getValue();
433 _generic._bulk._maxRepetitions = static_cast<IntegerBER*>((*pdu)[2])->getValue();
434 break;
435 default:
436 _generic._requestID = static_cast<IntegerBER*>((*pdu)[0])->getValue();
437 _generic._error._status = static_cast<IntegerBER*>((*pdu)[1])->getValue();
438 _generic._error._index = static_cast<IntegerBER*>((*pdu)[2])->getValue();
439 break;
440 }
441 // Branch variable bind list
442 delete _varBindList;
443 _varBindList = static_cast<VarBindList*>((*pdu)[_type == Type::Trap ? 5 : 3]);
444 pdu->remove();
445 }
446#if SNMP_STREAM
455 void build(Stream &stream) {
456 build();
457 encode(stream);
458 }
459
468 void parse(Stream &stream) {
469 decode(stream);
470 parse();
471 }
472#else
483 void build(uint8_t *buffer) {
484 encode(buffer);
485 }
486
495 void parse(uint8_t *buffer) {
496 decode(buffer);
497 parse();
498 }
499#endif
500
511 const uint8_t mapErrorStatus(const uint8_t status) const {
512 if (_version == Version::V1) {
513 // RFC 2089 2.1 Mapping SNMPv2 error-status into SNMPv1 error-status
514 switch (status) {
517 case Error::WrongType:
520 return Error::BadValue;
521 case Error::NoAccess:
526 return Error::NoSuchName;
530 return Error::GenErr;
531 }
532 }
533 return status;
534 }
535
537 uint8_t _version;
539 const char *_community;
541 uint8_t _type;
544
545 friend class SNMP;
546};
547
548} // namespace SNMP
549
550#endif /* SNMPMESSAGE_H_ */
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Definition BER.h:1776
void remove()
Removes the last BER in the array.
Definition BER.h:1918
BER * operator[](const unsigned int index)
Definition BER.h:1877
BER * add(BER *ber)
Adds a BER to the array.
Definition BER.h:1899
virtual void encode(Stream &stream)
Definition BER.h:1753
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the ArrayBER.
Definition BER.h:1859
BER object.
Definition BER.h:792
const unsigned int getType() const
Gets the BER type.
Definition BER.h:996
BER object to handle IP address.
Definition BER.h:2053
BER object to handle integer.
Definition BER.h:1178
Helper class to defines some useful SNMP OIDs.
static constexpr char * LINKUP
static constexpr char * SNMPTRAPOID
static constexpr char * COLDSTART
static constexpr char * LINKDOWN
static constexpr char * SNMPTRAPENTERPRISE
static constexpr char * WARMSTART
static constexpr char * SYSUPTIME
static constexpr char * AUTHENTICATIONFAILURE
SNMP message object.
void setRequestID(const int32_t requestId)
Sets the request identifier.
const char * getCommunity() const
Gets the community.
const int32_t getRequestID() const
Gets the request identifier.
VarBindList * _varBindList
const char * _community
Message(const uint8_t version=Version::V1, const char *community=nullptr, const uint8_t type=Type::GetRequest)
Creates a Message.
void build(Stream &stream)
Builds the message to stream.
void setTrap(const uint8_t generic, const uint8_t specific=0)
Sets the generic and specific traps.
const uint8_t getType() const
Gets the type.
const uint8_t mapErrorStatus(const uint8_t status) const
Maps error status.
void setNonRepeaters(const uint8_t nonRepeaters)
Sets the non repeaters.
VarBindList * getVarBindList() const
Gets the variable bindings list.
const uint8_t getErrorStatus() const
Gets the error status.
void setMaxRepetitions(const uint8_t maxRepetitions)
Sets the maximum repetition.
void parse(Stream &stream)
Parses the message from stream.
friend class SNMP
void setSNMPTrapOID(const char *name)
Sets the SNMPTRAPOID variable binding.
void build()
Builds the message.
VarBind * add(const char *oid, BER *value=nullptr)
Adds a VarBind.
const uint8_t getVersion() const
Gets the version.
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the message.
void setEnterprise(const char *enterprise)
Sets the enterprise.
const uint8_t getNonRepeaters() const
Gets the non repeaters.
void setAgentAddress(IPAddress agentAddr)
Sets the agent address.
void build(uint8_t *buffer)
Builds the message to buffer.
void setError(const uint8_t status, const uint8_t index)
Sets error status and error index.
uint8_t _version
void parse()
Parses the message.
const uint8_t getErrorIndex() const
Gets the error index.
void parse(uint8_t *buffer)
Parses the message from buffer.
virtual ~Message()
Message destructor.
const uint8_t getMaxRepetition() const
Gets the maximum repetition.
BER object to handle OID.
Definition BER.h:1487
BER object to handle octet string.
Definition BER.h:1288
Helper class to handle PDU.
Definition SNMPMessage.h:17
Generic _generic
Definition SNMPMessage.h:87
PDU()
Creates a PDU object.
Definition SNMPMessage.h:75
~PDU()
PDU destructor.
Definition SNMPMessage.h:82
BER object to handle sequence of BER objects.
Definition BER.h:1952
SequenceBER(const uint8_t type=Type::Sequence)
Creates a SequenceBER.
Definition BER.h:1959
BER object to handle time.
Definition BER.h:2283
@ Trap
Definition BER.h:313
@ GetRequest
Definition BER.h:309
@ GetBulkRequest
Definition BER.h:315
BER object to handle variable binding.
Definition BER.h:1974
BER object to handle a list of variable bindings.
Definition BER.h:2022
SNMP library namespace.
Helper struct to handle SNMP error.
Definition BER.h:72
@ InconsistentName
Definition BER.h:97
@ InconsistentValue
Definition BER.h:91
@ WrongEncoding
Definition BER.h:88
@ AuthorizationError
Definition BER.h:95
@ WrongValue
Definition BER.h:89
@ CommitFailed
Definition BER.h:93
@ WrongType
Definition BER.h:86
@ WrongLength
Definition BER.h:87
@ NotWritable
Definition BER.h:96
@ NoAccess
Definition BER.h:85
@ BadValue
Definition BER.h:81
@ GenErr
Definition BER.h:83
@ UndoFailed
Definition BER.h:94
@ NoCreation
Definition BER.h:90
@ ResourceUnavailable
Definition BER.h:92
@ NoSuchName
Definition BER.h:80
uint8_t _index
Definition BER.h:114
uint8_t _status
Definition BER.h:112
Helper struct to handle GetBulkRequest PDU.
Definition SNMPMessage.h:32
Helper struct to handle generic PDU.
Definition SNMPMessage.h:27
Generic()
Initializes to default values.
Definition SNMPMessage.h:45
Helper struct to handle trap PDU.
Definition BER.h:123
IPAddress _agentAddr
Definition BER.h:155
uint32_t _timeStamp
Definition BER.h:161
uint8_t _genericTrap
Definition BER.h:157
const char * _enterprise
Definition BER.h:153
uint8_t _specificTrap
Definition BER.h:159