SNMP 2.0.0
Loading...
Searching...
No Matches
SNMP::Message Class Reference

SNMP message object. More...

#include <src/SNMPMessage.h>

Inheritance diagram for SNMP::Message:
[legend]
Collaboration diagram for SNMP::Message:
[legend]

Classes

class  OID
 Helper class to defines some useful SNMP OIDs. More...
 

Public Member Functions

 Message (const uint8_t version=Version::V1, const char *community=nullptr, const uint8_t type=Type::GetRequest)
 Creates a Message.
 
virtual ~Message ()
 Message destructor.
 
virtual const unsigned int getSize (const bool refresh=false)
 Gets the size of the message.
 
VarBindadd (const char *oid, BER *value=nullptr)
 Adds a VarBind.
 
const uint8_t getVersion () const
 Gets the version.
 
const char * getCommunity () const
 Gets the community.
 
const uint8_t getType () const
 Gets the type.
 
const int32_t getRequestID () const
 Gets the request identifier.
 
void setRequestID (const int32_t requestId)
 Sets the request identifier.
 
const uint8_t getErrorStatus () const
 Gets the error status.
 
const uint8_t getErrorIndex () const
 Gets the error index.
 
void setError (const uint8_t status, const uint8_t index)
 Sets error status and error index.
 
const uint8_t getNonRepeaters () const
 Gets the non repeaters.
 
void setNonRepeaters (const uint8_t nonRepeaters)
 Sets the non repeaters.
 
const uint8_t getMaxRepetition () const
 Gets the maximum repetition.
 
void setMaxRepetitions (const uint8_t maxRepetitions)
 Sets the maximum repetition.
 
void setEnterprise (const char *enterprise)
 Sets the enterprise.
 
void setAgentAddress (IPAddress agentAddr)
 Sets the agent address.
 
void setTrap (const uint8_t generic, const uint8_t specific=0)
 Sets the generic and specific traps.
 
void setSNMPTrapOID (const char *name)
 Sets the SNMPTRAPOID variable binding.
 
VarBindListgetVarBindList () const
 Gets the variable bindings list.
 
- Public Member Functions inherited from SNMP::SequenceBER
 SequenceBER (const uint8_t type=Type::Sequence)
 Creates a SequenceBER.
 
- Public Member Functions inherited from SNMP::ArrayBER< SNMP_CAPACITY >
 ArrayBER (const uint8_t type)
 Creates an ArrayBER.
 
 ~ArrayBER ()
 ArrayBER destructor.
 
virtual void encode (Stream &stream)
 Encodes ArrayBER to stream.
 
virtual uint8_t * encode (uint8_t *buffer)
 Encodes ArrayBER to memory buffer.
 
virtual void decode (Stream &stream, const uint8_t flag=Flag::None)
 Decodes ArrayBER from stream.
 
virtual uint8_t * decode (uint8_t *buffer)
 Decodes ArrayBER from memory buffer.
 
BERoperator[] (const unsigned int index)
 Array subscript operator.
 
const uint8_t count () const
 Gets the count of BERs in the array.
 
- Public Member Functions inherited from SNMP::BER
 BER (const unsigned int type)
 Creates a BER object.
 
virtual ~BER ()
 BER destructor.
 
template<typename T >
void encodeNumeric (T value, Stream &stream)
 Encodes BER numeric value to stream.
 
template<typename T >
void decodeNumeric (T *value, Stream &stream, const uint8_t flag=Flag::None)
 Decodes BER numeric value from stream.
 
template<typename T >
uint8_t * encodeNumeric (T value, uint8_t *buffer)
 Encodes BER numeric value to memory buffer.
 
template<typename T >
uint8_t * decodeNumeric (T *value, uint8_t *buffer, const uint8_t flag=Flag::None)
 Decodes BER numeric value from memory buffer.
 
template<typename T >
void setNegative (T value)
 Computes BER length of a negative integer.
 
template<typename T >
void setPositive (T value)
 Computes BER length of a positive integer.
 
const unsigned int getType () const
 Gets the BER type.
 
const unsigned int getLength () const
 Gets the BER length.
 
- Public Member Functions inherited from SNMP::Base
void encode7bits (uint32_t value, uint8_t *buffer, const uint8_t size)
 Encodes integer value to memory buffer.
 

Private Member Functions

void build ()
 Builds the message.
 
void parse ()
 Parses the message.
 
void build (Stream &stream)
 Builds the message to stream.
 
void parse (Stream &stream)
 Parses the message from stream.
 
void build (uint8_t *buffer)
 Builds the message to buffer.
 
void parse (uint8_t *buffer)
 Parses the message from buffer.
 
const uint8_t mapErrorStatus (const uint8_t status) const
 Maps error status.
 
- Private Member Functions inherited from SNMP::PDU
 PDU ()
 Creates a PDU object.
 
 ~PDU ()
 PDU destructor.
 

Private Attributes

uint8_t _version
 
const char * _community
 
uint8_t _type
 
VarBindList_varBindList
 
- Private Attributes inherited from SNMP::PDU
union { 
 
   Generic   _generic 
 
   Trap   _trap 
 
};  
 

Friends

class SNMP
 

Additional Inherited Members

- Static Public Member Functions inherited from SNMP::Base
static void encode7bits (uint32_t value, Stream &stream, const uint8_t size)
 Encodes integer value to stream.
 
- Protected Member Functions inherited from SNMP::ArrayBER< SNMP_CAPACITY >
BERadd (BER *ber)
 Adds a BER to the array.
 
void remove ()
 Removes the last BER in the array.
 
- Protected Member Functions inherited from SNMP::BER
BERcreate (const Type &type)
 Creates a BER of given type.
 
- Protected Attributes inherited from SNMP::BER
Length _length
 
Type _type
 
- Protected Attributes inherited from SNMP::Base
unsigned int _size = 0
 

Detailed Description

SNMP message object.

Warning
Inherited PDU makes extensive use of unions to save memory. Therefore, some members of the class are valid and relevant only if message type is compatible.

Example

// Creates a Trap message
Messsage message(Version::V1, "public", Type::Trap);
message.setTrap(Trap::ColdStart); // Good
message.setNonRepeaters(2); // Bad, undefined behavior!
@ Trap
Definition BER.h:313
@ ColdStart
Definition BER.h:128

Definition at line 110 of file SNMPMessage.h.

Constructor & Destructor Documentation

◆ Message()

SNMP::Message::Message ( const uint8_t version = Version::V1,
const char * community = nullptr,
const uint8_t type = Type::GetRequest )
inline

Creates a Message.

Parameters
versionSNMP version.
communitySNMP community.
typePDU BER type.

Definition at line 139 of file SNMPMessage.h.

References _community, _type, _varBindList, _version, and SNMP::ArrayBER< SNMP_CAPACITY >::VarBindList.

◆ ~Message()

virtual SNMP::Message::~Message ( )
inlinevirtual

Message destructor.

Releases variable bindings.

Definition at line 153 of file SNMPMessage.h.

References _varBindList.

Member Function Documentation

◆ add()

VarBind * SNMP::Message::add ( const char * oid,
BER * value = nullptr )
inline

Adds a VarBind.

Creates and adds a VarBind to the message variable bindings list.

Parameters
oidOID BER value.
valueBER of any type.
Returns
VarBind added.

Definition at line 180 of file SNMPMessage.h.

References _varBindList, SNMP::ArrayBER< U >::add(), and SNMP::ArrayBER< SNMP_CAPACITY >::VarBind.

Referenced by setSNMPTrapOID().

◆ build() [1/3]

◆ build() [2/3]

void SNMP::Message::build ( Stream & stream)
inlineprivate

Builds the message to stream.

  • Builds the message.
  • Encodes the message to stream.
Parameters
streamStream to write to.

Definition at line 455 of file SNMPMessage.h.

References build(), and SNMP::ArrayBER< SNMP_CAPACITY >::encode().

◆ build() [3/3]

void SNMP::Message::build ( uint8_t * buffer)
inlineprivate

Builds the message to buffer.

  • Encodes the message to buffer.
Note
The message is already built to compute size needed to allocate the build buffer.
Parameters
bufferPointer to the buffer.

Definition at line 483 of file SNMPMessage.h.

References SNMP::ArrayBER< SNMP_CAPACITY >::encode().

◆ getCommunity()

const char * SNMP::Message::getCommunity ( ) const
inline

Gets the community.

Returns
SNMP community.

Definition at line 198 of file SNMPMessage.h.

References _community.

◆ getErrorIndex()

const uint8_t SNMP::Message::getErrorIndex ( ) const
inline

Gets the error index.

Warning
Valid only if the PDU is generic, except for the GetBulkRequest PDU.
Returns
Error index.

Definition at line 251 of file SNMPMessage.h.

References SNMP::PDU::Generic::_error, SNMP::PDU::_generic, and SNMP::Error::_index.

◆ getErrorStatus()

const uint8_t SNMP::Message::getErrorStatus ( ) const
inline

Gets the error status.

Warning
Valid only if the PDU is generic, except for the GetBulkRequest PDU.
Returns
Error status.

Definition at line 240 of file SNMPMessage.h.

References SNMP::PDU::Generic::_error, SNMP::PDU::_generic, and SNMP::Error::_status.

◆ getMaxRepetition()

const uint8_t SNMP::Message::getMaxRepetition ( ) const
inline

Gets the maximum repetition.

Warning
Valid only for GetBulkRequest PDU.
Returns
Number of get next operations for each additional OIDs.

Definition at line 297 of file SNMPMessage.h.

References SNMP::PDU::Generic::_bulk, SNMP::PDU::_generic, and SNMP::PDU::Generic::Bulk::_maxRepetitions.

◆ getNonRepeaters()

const uint8_t SNMP::Message::getNonRepeaters ( ) const
inline

Gets the non repeaters.

Warning
Valid only for GetBulkRequest PDU.
Returns
Number of OIDs treated as getRequest.

Definition at line 275 of file SNMPMessage.h.

References SNMP::PDU::Generic::_bulk, SNMP::PDU::_generic, and SNMP::PDU::Generic::Bulk::_nonRepeaters.

◆ getRequestID()

const int32_t SNMP::Message::getRequestID ( ) const
inline

Gets the request identifier.

Warning
Valid only if the PDU is generic.
Returns
Request identifier.

Definition at line 218 of file SNMPMessage.h.

References SNMP::PDU::_generic, and SNMP::PDU::Generic::_requestID.

◆ getSize()

virtual const unsigned int SNMP::Message::getSize ( const bool refresh = false)
inlinevirtual

Gets the size of the message.

Builds the message and call inherited ArrayBER::getSize() to ensure the size is valid.

Parameters
refreshRefresh parameter for inherited ArrayBER::getSize().
Returns
Message size.

Reimplemented from SNMP::ArrayBER< SNMP_CAPACITY >.

Definition at line 166 of file SNMPMessage.h.

References build(), and SNMP::ArrayBER< U >::getSize().

Referenced by SNMP::SNMP::send().

◆ getType()

const uint8_t SNMP::Message::getType ( ) const
inline

Gets the type.

Returns
PDU type.

Definition at line 207 of file SNMPMessage.h.

References _type.

◆ getVarBindList()

VarBindList * SNMP::Message::getVarBindList ( ) const
inline

Gets the variable bindings list.

Returns
Variable bindings list.

Definition at line 369 of file SNMPMessage.h.

References _varBindList.

◆ getVersion()

const uint8_t SNMP::Message::getVersion ( ) const
inline

Gets the version.

Returns
SNMP version.

Definition at line 189 of file SNMPMessage.h.

References _version.

◆ mapErrorStatus()

const uint8_t SNMP::Message::mapErrorStatus ( const uint8_t status) const
inlineprivate

◆ parse() [1/3]

◆ parse() [2/3]

void SNMP::Message::parse ( Stream & stream)
inlineprivate

Parses the message from stream.

  • Decodes the message from stream.
  • Parses the message.
Parameters
streamStream to read from.

Definition at line 468 of file SNMPMessage.h.

References SNMP::ArrayBER< SNMP_CAPACITY >::decode(), and parse().

◆ parse() [3/3]

void SNMP::Message::parse ( uint8_t * buffer)
inlineprivate

Parses the message from buffer.

  • Decodes the message from buffer.
  • Parses the message.
Parameters
bufferPointer to the buffer.

Definition at line 495 of file SNMPMessage.h.

References SNMP::ArrayBER< SNMP_CAPACITY >::decode(), and parse().

◆ setAgentAddress()

void SNMP::Message::setAgentAddress ( IPAddress agentAddr)
inline

Sets the agent address.

Warning
Valid only for Trap PDU.
Parameters
agentAddrNetwork address of the agent.

Definition at line 330 of file SNMPMessage.h.

References SNMP::Trap::_agentAddr, and SNMP::PDU::_trap.

◆ setEnterprise()

void SNMP::Message::setEnterprise ( const char * enterprise)
inline

Sets the enterprise.

Warning
Valid only for Trap PDU.
Parameters
enterpriseEnterprise OID.

Definition at line 319 of file SNMPMessage.h.

References SNMP::Trap::_enterprise, and SNMP::PDU::_trap.

◆ setError()

void SNMP::Message::setError ( const uint8_t status,
const uint8_t index )
inline

Sets error status and error index.

Warning
Valid only if the PDU is generic, except for the GetBulkRequest PDU.
Parameters
statusError status.
indexError index.

Definition at line 263 of file SNMPMessage.h.

References SNMP::PDU::Generic::_error, SNMP::PDU::_generic, SNMP::Error::_index, SNMP::Error::_status, and mapErrorStatus().

◆ setMaxRepetitions()

void SNMP::Message::setMaxRepetitions ( const uint8_t maxRepetitions)
inline

Sets the maximum repetition.

Warning
Valid only for GetBulkRequest PDU.
Parameters
maxRepetitionsNumber of get next operations for each additional OIDs.

Definition at line 308 of file SNMPMessage.h.

References SNMP::PDU::Generic::_bulk, SNMP::PDU::_generic, and SNMP::PDU::Generic::Bulk::_maxRepetitions.

◆ setNonRepeaters()

void SNMP::Message::setNonRepeaters ( const uint8_t nonRepeaters)
inline

Sets the non repeaters.

Warning
Valid only for GetBulkRequest PDU.
Parameters
nonRepeatersNumber of OIDs treated as getRequest.

Definition at line 286 of file SNMPMessage.h.

References SNMP::PDU::Generic::_bulk, SNMP::PDU::_generic, and SNMP::PDU::Generic::Bulk::_nonRepeaters.

◆ setRequestID()

void SNMP::Message::setRequestID ( const int32_t requestId)
inline

Sets the request identifier.

Warning
Valid only if the PDU is generic.
Parameters
requestIdRequest identifier.

Definition at line 229 of file SNMPMessage.h.

References SNMP::PDU::_generic, and SNMP::PDU::Generic::_requestID.

◆ setSNMPTrapOID()

void SNMP::Message::setSNMPTrapOID ( const char * name)
inline

Sets the SNMPTRAPOID variable binding.

Warning
Valid only for InformRequest or SNMPv2Trap PDU.
  • Adds mandatory variable binding sysUpTime.0. Value will be set at build time.

Adds variable binding snmpTrapOID.0 with value name.

Parameters
namesnmpTrapOID.0 value.

Definition at line 358 of file SNMPMessage.h.

References add(), SNMP::Message::OID::SNMPTRAPOID, and SNMP::Message::OID::SYSUPTIME.

◆ setTrap()

void SNMP::Message::setTrap ( const uint8_t generic,
const uint8_t specific = 0 )
inline

Sets the generic and specific traps.

Warning
Valid only for Trap PDU.
Parameters
genericGeneric trap code.
specificSpecific trap code.

Definition at line 342 of file SNMPMessage.h.

References SNMP::Trap::_genericTrap, SNMP::Trap::_specificTrap, and SNMP::PDU::_trap.

Friends And Related Symbol Documentation

◆ SNMP

friend class SNMP
friend

Definition at line 545 of file SNMPMessage.h.

Member Data Documentation

◆ _community

const char* SNMP::Message::_community
private

SNMP community.

Definition at line 539 of file SNMPMessage.h.

Referenced by build(), getCommunity(), Message(), and parse().

◆ _type

uint8_t SNMP::Message::_type
private

PDU BER type.

See also
Type.

Definition at line 541 of file SNMPMessage.h.

Referenced by build(), getType(), Message(), and parse().

◆ _varBindList

VarBindList* SNMP::Message::_varBindList
private

Variable bindings list.

Definition at line 543 of file SNMPMessage.h.

Referenced by add(), build(), getVarBindList(), Message(), parse(), and ~Message().

◆ _version

uint8_t SNMP::Message::_version
private

SNMP version.

See also
Version.

Definition at line 537 of file SNMPMessage.h.

Referenced by build(), getVersion(), mapErrorStatus(), Message(), and parse().


The documentation for this class was generated from the following file: