3 #include "dangerous/snmp/logger.hpp"
4 #include "dangerous/snmp/types.hpp"
5 #include "dangerous/snmp/pdu.hpp"
7 #include "berstream.hpp"
9 #include "../asn1/asn1.hpp"
14 namespace dangerous {
namespace snmp {
namespace encoding {
22 static constexpr
const char* NAME =
"INTEGER";
27 static constexpr
const char* NAME =
"OCTET STRING";
32 static constexpr
const char* NAME =
"NULL";
37 static constexpr
const char* NAME =
"OBJECT IDENTIFIER";
46 static constexpr
const char* NAME =
"IpAddress";
51 static constexpr
const char* NAME =
"Counter32";
56 static constexpr
const char* NAME =
"Gauge32";
61 static constexpr
const char* NAME =
"TimeTicks";
66 static constexpr
const char* NAME =
"Opaque";
73 static constexpr
const char* NAME =
"Counter64";
82 static constexpr
const char* NAME =
"noSuchObject";
87 static constexpr
const char* NAME =
"noSuchInstance";
92 static constexpr
const char* NAME =
"endOfMibView";
109 switch( variant.
type() ) {
116 return OBJECT_IDENTIFIER::length( variant.
get_oid() );
118 return OCTET_STRING::length( variant.
get_string() );
120 return NULL_TYPE::length( NULL_TYPE::throwaway );
126 return IPADDRESS::length( variant.
get_string() );
134 return OPAQUE::length( variant.
get_string() );
142 return NOSUCHOBJECT::length( NOSUCHOBJECT::throwaway );
144 return NOSUCHINSTANCE::length( NOSUCHINSTANCE::throwaway );
146 return ENDOFMIBVIEW::length( ENDOFMIBVIEW::throwaway );
150 logger.
out() <<
"Warning: Unknown variant type: " << (int)variant.
type() <<
"; using null." << std::endl;
152 return NULL_TYPE::length( NULL_TYPE::throwaway );
163 static constexpr
const char*
NAME =
"VarBind";
168 static const int TYPE = asn1::helper::SEQUENCE::TYPE;
173 static unsigned int length(
const value_type& varbind ) {
174 unsigned int size = 0;
177 logger.
out() <<
"VARBIND: length: OID part: " << asn1::encodedSize<OBJECT_IDENTIFIER>( varbind->oid ) << std::endl;
178 logger.
out() <<
"VARBIND: length: VAR part: " << asn1::encodedSize<VARIANT>( varbind->value ) << std::endl;
181 size += asn1::encodedSize<OBJECT_IDENTIFIER>( varbind->oid );
182 size += asn1::encodedSize<VARIANT>( varbind->value );
187 static bool write(
const value_type& varbind,
char* buffer,
unsigned int bufferSize ) {
199 switch( varbind->value.type() ) {
204 success = berStream.
write<
INTEGER>( varbind->value.get_int32_t() );
220 success = berStream.
write<
IPADDRESS>( varbind->value.get_string() );
223 success = berStream.
write<
COUNTER32>( varbind->value.get_uint32_t() );
226 success = berStream.
write<
GAUGE32>( varbind->value.get_uint32_t() );
229 success = berStream.
write<
TIMETICKS>( varbind->value.get_uint32_t() );
232 success = berStream.
write<
OPAQUE>( varbind->value.get_string() );
235 success = berStream.
write<
COUNTER64>( varbind->value.get_uint64_t() );
253 logger.
out() <<
"Warning: Unknown varbind->value type: " << (int)varbind->value.type() <<
"; using null." << std::endl;
265 logger.
out() <<
"VARBIND: write: Original length: " << bufferSize << std::endl;
293 success = berStream.
peekType( currentType );
298 switch( currentType ) {
302 case INTEGER::TYPE: {
308 case OCTET_STRING::TYPE: {
314 case OBJECT_IDENTIFIER::TYPE: {
320 case NULL_TYPE::TYPE:
327 case IPADDRESS::TYPE: {
333 case COUNTER32::TYPE: {
339 case GAUGE32::TYPE: {
345 case TIMETICKS::TYPE: {
357 case COUNTER64::TYPE: {
367 case NOSUCHOBJECT::TYPE:
371 case NOSUCHINSTANCE::TYPE:
375 case ENDOFMIBVIEW::TYPE:
382 logger.
out() <<
"Unknown type: " << (int)currentType << std::endl;
402 static constexpr
const char*
NAME =
"SEQUENCE OF VarBind";
405 static const int TYPE = asn1::helper::SEQUENCE::TYPE;
410 static unsigned int length(
const VarBindList& varbinds ) {
411 unsigned int size = 0;
412 for(
const auto& varbind : varbinds ) {
413 size += asn1::encodedSize<VARBIND>( varbind );
426 for(
const auto& varbind : varbinds ) {
436 static bool read(
value_type& varbinds,
const char* buffer,
unsigned int bufferSize ) {
451 std::unique_ptr<VarBind> varbind(
new VarBind );
457 varbinds.push_back( std::move(varbind) );
464 template<
int tag,ConfirmedClass::Type confirmedClass>
481 static constexpr
const int TYPE = 0xa0 | tag;
489 static unsigned int length(
const value_type& pdu ) {
490 unsigned int size = 0;
491 size += asn1::encodedSize<INTEGER>( pdu.
requestId );
492 size += asn1::encodedSize<INTEGER>( pdu.
errorStatus );
493 size += asn1::encodedSize<INTEGER>( pdu.
errorIndex );
494 size += asn1::encodedSize<VARBINDLIST>( pdu.
varbinds );
532 static bool read(
value_type& pdu,
const char* buffer,
unsigned int bufferSize ) {
575 static constexpr
const char* NAME =
"GetRequest-PDU";
583 static constexpr
const char* NAME =
"GetNextRequest-PDU";
591 static constexpr
const char* NAME =
"Response-PDU";
599 static constexpr
const char* NAME =
"SetRequest-PDU";
610 static constexpr
const char* NAME =
"GetBulkRequest-PDU";
618 static constexpr
const char* NAME =
"InformRequest-PDU";
626 static constexpr
const char* NAME =
"SNMPv2-Trap-PDU";
634 static constexpr
const char* NAME =
"Report-PDU";
Stored as v.oid.
Definition: variant.hpp:91
static constexpr const char * NAME
This is the name of this entity.
Definition: encoding.hpp:402
A SetRequest-PDU is a PDU with tag "3".
Definition: encoding.hpp:597
bool write(const typename EncodingClass::value_type &value)
This performs a full, logical write of a kind of encoded data.
Definition: berstream.hpp:207
Definition: encoding.hpp:80
static bool read(value_type &pdu, const char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:532
A SNMPv2-Trap-PDU is a PDU with tag "7".
Definition: encoding.hpp:624
bool system(System system)
This returns the logging status for the given system.
Definition: logger.hpp:47
Functionally SIMPLE_NULL.
Definition: variant.hpp:101
Definition: encoding.hpp:44
This class allows for the encoding and decoding of null values.
Definition: asn1.hpp:356
A PDU represents an SNMP Protocol Data Unit (PDU).
Definition: pdu.hpp:56
std::ostream & out()
This returns the output stream for the Logger.
Definition: logger.hpp:77
Functionally SIMPLE_NULL.
Definition: variant.hpp:99
static bool write(const value_type &pdu, char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:498
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
Definition: encoding.hpp:49
VarBindList value_type
The value type that this class operates on is "VarBindList".
Definition: encoding.hpp:408
static const int TYPE
A VarBind list is a sequence:
Definition: encoding.hpp:405
A Variant defines an arbitrary value (within the scope of SNMP).
Definition: variant.hpp:78
PDU value_type
The value type that this class operates on is "PDU".
Definition: encoding.hpp:484
Definition: encoding.hpp:20
Stored as v.u64.
Definition: variant.hpp:98
Definition: encoding.hpp:71
Functionally SIMPLE_NULL.
Definition: variant.hpp:100
Definition: encoding.hpp:85
This defines a variable binding, or "varbind".
Definition: types.hpp:77
Stored as v.s.
Definition: variant.hpp:97
unsigned int remainingReadLength() const
This returns the number of bytes remaining in the stream.
Definition: berstream.hpp:32
Not stored.
Definition: variant.hpp:92
static bool write(const VarBindList &varbinds, char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:419
A GetRequest-PDU is a PDU with tag "0".
Definition: encoding.hpp:573
Definition: encoding.hpp:465
Definition: encoding.hpp:64
Stored as v.u32.
Definition: variant.hpp:96
Stored as v.u32.
Definition: variant.hpp:95
Only read operations may be performed.
Definition: bytestream.hpp:38
A BerStream represents a BER-encoded stream of data.
Definition: berstream.hpp:16
A Report-PDU is a PDU with tag "8".
Definition: encoding.hpp:632
Both read and write operations may be performed.
Definition: bytestream.hpp:40
Definition: encoding.hpp:59
int errorStatus
This is the ErrorStatus for the PDU.
Definition: pdu.hpp:116
Definition: encoding.hpp:35
uint32_t get_uint32_t() const
This returns the 32-bit unsigned integer value of the Variant.
Definition: variant.hpp:450
static constexpr const char * NAME
This is the name of this entity.
Definition: encoding.hpp:163
A NumericOid represents an "true" OID; that is, one identified by a series of integers (as opposed to...
Definition: numericoid.hpp:22
Stored as v.i32.
Definition: variant.hpp:89
Definition: encoding.hpp:54
int errorIndex
This is the ErrorIndex for the PDU.
Definition: pdu.hpp:124
static bool write(const value_type &varbind, char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:187
static unsigned int length(const value_type &value)
Definition: asn1.hpp:153
std::unique_ptr< VarBind > value_type
This class operates on unique VarBind pointers.
Definition: encoding.hpp:171
static constexpr const int TYPE
This is the type of the PDU.
Definition: encoding.hpp:481
This class allows for the encoding and decoding of VarBind pointers.
Definition: encoding.hpp:160
bool peekType(uint8_t &type)
This reads an ASN.1 type from the BerStream.
Logger logger
We will expose a global Logger instance.
static unsigned int length(const Variant &variant)
The byte size of a null value is always 2:
Definition: encoding.hpp:108
static constexpr const ConfirmedClass::Type confirmed
This is the "confirmed class" of the PDU.
Definition: encoding.hpp:487
This class allows for the encoding and decoding of VarBind lists.
Definition: encoding.hpp:399
Logging related to decoding messages.
Definition: logger.hpp:21
int32_t get_int32_t() const
This returns the 32-bit signed integer value of the Variant.
Definition: variant.hpp:417
A Response-PDU is a PDU with tag "2".
Definition: encoding.hpp:589
Definition: encoding.hpp:30
Type
A Type represents one of the two defined values for the "confirmed class" of a PDU.
Definition: types.hpp:65
Type type() const
This returns the type of the Variant.
Definition: variant.hpp:167
This class allows for the encoding and decoding of an arbitrary value.
Definition: encoding.hpp:98
unsigned int remainingWriteLength() const
This returns the number of bytes remaining in the stream.
Definition: berstream.hpp:38
void linkFrom(char *bytes, unsigned int bytesSize, Access access)
This links the bytes given to the ByteStream's character buffer.
Definition: encoding.hpp:90
A GetBulkRequest-PDU is a PDU with tag "5".
Definition: encoding.hpp:608
Definition: encoding.hpp:25
bool read(typename EncodingClass::value_type &value)
This performs a full, logical read of a kind of encoded data.
Definition: berstream.hpp:145
static bool read(value_type &varbind, char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:273
const NumericOid & get_oid() const
This returns the OID value of the Variant.
Definition: variant.hpp:549
Stored as v.s.
Definition: variant.hpp:90
static bool read(value_type &varbinds, const char *buffer, unsigned int bufferSize)
Definition: encoding.hpp:436
Stored as v.s.
Definition: variant.hpp:93
int requestId
This is the RequestID for the PDU.
Definition: pdu.hpp:109
std::vector< std::unique_ptr< VarBind > > VarBindList
This defines a list of varbinds.
Definition: types.hpp:116
const std::string & get_string() const
This returns the string value of the Variant.
Definition: variant.hpp:516
VarBindList varbinds
This is the list of VarBinds in the PDU.
Definition: pdu.hpp:127
Stored as v.u32.
Definition: variant.hpp:94
static const int TYPE
A VarBind is encoded as a sequence with two children:
Definition: encoding.hpp:168
A GetNextRequest-PDU is a PDU with tag "1".
Definition: encoding.hpp:581