Dangerous SNMP
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
scopedpdu.hpp
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
6 namespace dangerous { namespace snmp { namespace encoding {
7 
8 class ScopedPDU {
9 public:
10  std::string contextEngineID;
11  std::string contextName;
12  std::vector<char> pduBytes;
13 };
14 
15 class SCOPEDPDU {
16 public:
17  static constexpr const char* NAME = "ScopedPDU";
18  static const int TYPE = asn1::helper::SEQUENCE::TYPE;
19  typedef ScopedPDU value_type;
20 
21  static unsigned int length( const value_type& value ) {
22  unsigned int size = 0;
23 
24  size += asn1::encodedSize<encoding::OCTET_STRING>( value.contextEngineID );
25  size += asn1::encodedSize<encoding::OCTET_STRING>( value.contextName );
26  // Then, we're just going to dump our PDU data in.
27  size += value.pduBytes.size();
28 
29  return size;
30  }
31 
32  static bool write( const value_type& value, char* buffer, unsigned int bufferSize ) {
33  ByteStream byteStream;
34  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_WRITE );
35 
36  BerStream berStream( &byteStream );
37 
38  bool success = true;
39 
40  success = berStream.write<encoding::OCTET_STRING>( value.contextEngineID );
41  if( ! success ) {
42  return false;
43  }
44 
45  success = berStream.write<encoding::OCTET_STRING>( value.contextName );
46  if( ! success ) {
47  return false;
48  }
49 
50  unsigned int bytesWritten = 0;
51  success = berStream.writeBytes( value.pduBytes.data(), value.pduBytes.size(), bytesWritten );
52  if( ! success ) {
53  return false;
54  }
55 
56  return true;
57  }
58 
59  static bool read( value_type& value, char* buffer, unsigned int bufferSize ) {
60  ByteStream byteStream;
61  byteStream.linkFrom( buffer, bufferSize, ByteStream::READ_ONLY );
62 
63  BerStream berStream( &byteStream );
64 
65  bool success = true;
66 
67  success = berStream.read<encoding::OCTET_STRING>( value.contextEngineID );
68  if( ! success ) {
69  return false;
70  }
71 
72  success = berStream.read<encoding::OCTET_STRING>( value.contextName );
73  if( ! success ) {
74  return false;
75  }
76 
77  berStream.copyTo( value.pduBytes );
78 
79  return true;
80  }
81 
82 };
83 
84 } } }
85 
bool write(const typename EncodingClass::value_type &value)
This performs a full, logical write of a kind of encoded data.
Definition: berstream.hpp:207
A ByteStream is an object that is basically a big wrapper around a character buffer.
Definition: bytestream.hpp:30
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
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.
Definition: scopedpdu.hpp:15
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
Definition: scopedpdu.hpp:8