Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
message.hpp
1 #pragma once
2 
3 #include "berstream.hpp"
4 
5 #include <string>
6 #include <vector>
7 
8 namespace dangerous { namespace snmp { namespace encoding {
9 
15 class Message {
16 public:
20  int version;
21 
23  std::string communityString;
24 
26  std::vector<char> pduBytes;
27 };
28 
32 class MESSAGE {
33 public:
34  static constexpr const char* NAME = "Message";
35  static const int TYPE = asn1::helper::SEQUENCE::TYPE;
36  typedef Message value_type;
37 
38  static unsigned int length( const value_type& message ) {
39  unsigned int size = 0;
40  // The header will require any space necessary to store the version.
41  size += asn1::encodedSize<encoding::INTEGER>( message.version );
42  // The header will require any space necessary to store the community string.
43  size += asn1::encodedSize<encoding::OCTET_STRING>( message.communityString );
44  // Then, we're just going to dump our PDU data in.
45  size += message.pduBytes.size();
46 
47  return size;
48  }
49 
50  static bool write( const value_type& message, char* buffer, unsigned int bufferSize ) {
51  ByteStream byteStream;
52  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_WRITE );
53 
54  BerStream berStream( &byteStream );
55 
56  bool success = true;
57 
58  success = berStream.write<encoding::INTEGER>( message.version );
59  if( ! success ) {
60  return false;
61  }
62  success = berStream.write<encoding::OCTET_STRING>( message.communityString );
63  if( ! success ) {
64  return false;
65  }
66  unsigned int bytesWritten = 0;
67  success = berStream.writeBytes( message.pduBytes.data(), message.pduBytes.size(), bytesWritten );
68  if( ! success ) {
69  return false;
70  }
71 
72  return true;
73  }
74 
75  static bool read( value_type& message, char* buffer, unsigned int bufferSize ) {
76  ByteStream byteStream;
77  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_ONLY );
78 
79  BerStream berStream( &byteStream );
80 
81  bool success = true;
82 
83  success = berStream.read<encoding::INTEGER>( message.version );
84  if( ! success ) {
85  return false;
86  }
87  success = berStream.read<encoding::OCTET_STRING>( message.communityString );
88  if( ! success ) {
89  return false;
90  }
91 
92  berStream.copyTo( message.pduBytes );
93 
94  return true;
95  }
96 };
97 
98 } } }
99 
int version
This is the version number.
Definition: message.hpp:20
bool write(const typename EncodingClass::value_type &value)
This performs a full, logical write of a kind of encoded data.
Definition: berstream.hpp:207
std::string communityString
This is the "community string" for the message.
Definition: message.hpp:23
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
Definition: encoding.hpp:20
Only read operations may be performed.
Definition: bytestream.hpp:38
A BerStream represents a BER-encoded stream of data.
Definition: berstream.hpp:16
Both read and write operations may be performed.
Definition: bytestream.hpp:40
This is the encoder/decoder for the "Message" class.
Definition: message.hpp:32
std::vector< char > pduBytes
This is the actual payload of the message.
Definition: message.hpp:26
bool copyTo(std::vector< char > &buffer)
TODO.
bool writeBytes(const char *buffer, unsigned int bufferSize, unsigned int &bytesWritten)
This writes the bytes given to the BerStream.
void linkFrom(char *bytes, unsigned int bytesSize, Access access)
This links the bytes given to the ByteStream's character buffer.
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
This class is the version-1 and version-2c encapsulation for a message.
Definition: message.hpp:15