Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
message-v3.hpp
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 namespace dangerous { namespace snmp { namespace encoding {
7 
12 public:
14  static constexpr const char AUTHENTICATION = (char)0b00000001;
18  static constexpr const char ENCRYPTION = (char)0b00000010;
21  static constexpr const char REPORTABLE = (char)0b00000100;
22 };
23 
29 public:
33 
34  class HeaderData {
35  public:
36  int msgID;
37  int msgMaxSize;
38  std::string msgFlags;
42  } msgHeaderData;
43 
47  std::string msgSecurityParameters;
48 
50  std::vector<char> pduBytes;
51 };
52 
57 public:
58  static constexpr const char* NAME = "SNMPv3Message.HeaderData";
59  static const int TYPE = asn1::helper::SEQUENCE::TYPE;
60  typedef typename SNMPv3Message::HeaderData value_type;
61 
62  static unsigned int length( const value_type& headerData ) {
63  unsigned int size = 0;
64 
65  size += asn1::encodedSize<encoding::INTEGER>( headerData.msgID );
66 
67  size += asn1::encodedSize<encoding::INTEGER>( headerData.msgMaxSize );
68 
69  size += asn1::encodedSize<encoding::OCTET_STRING>( headerData.msgFlags );
70 
71  size += asn1::encodedSize<encoding::INTEGER>( headerData.msgSecurityModel );
72 
73  return size;
74  }
75 
76  static bool write( const value_type& headerData, char* buffer, unsigned int bufferSize ) {
77  ByteStream byteStream;
78  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_WRITE );
79 
80  BerStream berStream( &byteStream );
81 
82  bool success = true;
83 
84  success = berStream.write<encoding::INTEGER>( headerData.msgID );
85  if( ! success ) {
86  return false;
87  }
88 
89  success = berStream.write<encoding::INTEGER>( headerData.msgMaxSize );
90  if( ! success ) {
91  return false;
92  }
93 
94  success = berStream.write<encoding::OCTET_STRING>( headerData.msgFlags );
95  if( ! success ) {
96  return false;
97  }
98 
99  success = berStream.write<encoding::INTEGER>( headerData.msgSecurityModel );
100  if( ! success ) {
101  return false;
102  }
103 
104  return true;
105  }
106 
107  static bool read( value_type& headerData, char* buffer, unsigned int bufferSize ) {
108  ByteStream byteStream;
109  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_ONLY );
110 
111  BerStream berStream( &byteStream );
112 
113  bool success = true;
114 
115  success = berStream.read<encoding::INTEGER>( headerData.msgID );
116  if( ! success ) {
117  return false;
118  }
119 
120  success = berStream.read<encoding::INTEGER>( headerData.msgMaxSize );
121  if( ! success ) {
122  return false;
123  }
124 
125  success = berStream.read<encoding::OCTET_STRING>( headerData.msgFlags );
126  if( ! success ) {
127  return false;
128  }
129 
130  success = berStream.read<encoding::INTEGER>( headerData.msgSecurityModel );
131  if( ! success ) {
132  return false;
133  }
134 
135  return true;
136  }
137 };
138 
143 public:
144  static constexpr const char* NAME = "SNMPv3Message";
145  static const int TYPE = asn1::helper::SEQUENCE::TYPE;
146  typedef SNMPv3Message value_type;
147 
148  static unsigned int length( const value_type& message ) {
149  unsigned int size = 0;
150  // The header will require any space necessary to store the version.
151  size += asn1::encodedSize<encoding::INTEGER>( message.msgVersion );
152 
153  size += asn1::encodedSize<SNMPV3MESSAGE_HEADERDATA>( message.msgHeaderData );
154 
155  // The header will require any space necessary to store the community string.
156  size += asn1::encodedSize<encoding::OCTET_STRING>( message.msgSecurityParameters );
157 
158  // Then, we're just going to dump our PDU data in.
159  size += message.pduBytes.size();
160 
161  return size;
162  }
163 
164  static bool write( const value_type& message, char* buffer, unsigned int bufferSize ) {
165  ByteStream byteStream;
166  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_WRITE );
167 
168  BerStream berStream( &byteStream );
169 
170  bool success = true;
171 
172  success = berStream.write<encoding::INTEGER>( message.msgVersion );
173  if( ! success ) {
174  return false;
175  }
176 
177  success = berStream.write<SNMPV3MESSAGE_HEADERDATA>( message.msgHeaderData );
178  if( ! success ) {
179  return false;
180  }
181 
182  success = berStream.write<encoding::OCTET_STRING>( message.msgSecurityParameters );
183  if( ! success ) {
184  return false;
185  }
186 
187  unsigned int bytesWritten = 0;
188  success = berStream.writeBytes( message.pduBytes.data(), message.pduBytes.size(), bytesWritten );
189  if( ! success ) {
190  return false;
191  }
192 
193  return true;
194  }
195 
196  static bool read( value_type& message, char* buffer, unsigned int bufferSize ) {
197  ByteStream byteStream;
198  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_ONLY );
199 
200  BerStream berStream( &byteStream );
201 
202  bool success = true;
203 
204  success = berStream.read<encoding::INTEGER>( message.msgVersion );
205  if( ! success ) {
206  return false;
207  }
208 
209  success = berStream.read<SNMPV3MESSAGE_HEADERDATA>( message.msgHeaderData );
210  if( ! success ) {
211  return false;
212  }
213 
214  success = berStream.read<encoding::OCTET_STRING>( message.msgSecurityParameters );
215  if( ! success ) {
216  return false;
217  }
218 
219  berStream.copyTo( message.pduBytes );
220 
221  return true;
222  }
223 };
224 
225 } } }
226 
int msgSecurityModel
TODO This will determine the meaning of "msgSecurityParameters" below.
Definition: message-v3.hpp:41
bool write(const typename EncodingClass::value_type &value)
This performs a full, logical write of a kind of encoded data.
Definition: berstream.hpp:207
static constexpr const char ENCRYPTION
This is set when encryption is enabled.
Definition: message-v3.hpp:18
This class is a container for the message flags.
Definition: message-v3.hpp:11
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
int msgVersion
This is the version number.
Definition: message-v3.hpp:32
Definition: encoding.hpp:20
This is the encoder/decoder for the "SNMPv3Message.HeaderData" class.
Definition: message-v3.hpp:56
static constexpr const char AUTHENTICATION
This is set when authentication is enabled.
Definition: message-v3.hpp:14
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 "SNMPv3Message" class.
Definition: message-v3.hpp:142
static constexpr const char REPORTABLE
This is set when a response is requested.
Definition: message-v3.hpp:21
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.
std::vector< char > pduBytes
This is the actual payload of the message.
Definition: message-v3.hpp:50
This class is the version-3 encapsulation for a message.
Definition: message-v3.hpp:28
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
std::string msgSecurityParameters
This is the BER-encoded security information.
Definition: message-v3.hpp:47