-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSNMPPacket.h
More file actions
102 lines (75 loc) · 2.35 KB
/
SNMPPacket.h
File metadata and controls
102 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef SNMPPacket_h
#define SNMPPacket_h
#include "VarBinds.h"
#include "defs.h"
#include <vector>
#include <math.h>
#include <string>
enum SNMPParsingState {
SNMPVERSION,
COMMUNITY,
PDU,
REQUESTID,
ERRORSTATUS,
ERRORID,
VARBINDS,
VARBIND,
DONE
};
typedef int SNMP_PACKET_PARSE_ERROR;
#define SNMP_PARSE_ERROR_MAGIC_BYTE -2 + SNMP_PACKET_PARSE_ERROR_OFFSET
#define SNMP_PARSE_ERROR_GENERIC -1 + SNMP_PACKET_PARSE_ERROR_OFFSET
union ErrorStatus {
SNMP_ERROR_STATUS errorStatus;
int nonRepeaters;
};
union ErrorIndex {
int errorIndex;
int maxRepititions;
};
class SNMPPacket {
public:
SNMPPacket(){};
explicit SNMPPacket(const SNMPPacket& packet){
this->setRequestID(packet.requestID);
this->setVersion(packet.snmpVersion);
this->setCommunityString(packet.communityString);
// Provide reusable ASN containers if required
if(packet.requestIDPtr){
this->requestIDPtr = packet.requestIDPtr;
}
if(packet.snmpVersionPtr){
this->snmpVersionPtr = packet.snmpVersionPtr;
}
if(packet.communityStringPtr){
this->communityStringPtr = packet.communityStringPtr;
}
};
virtual ~SNMPPacket();
static snmp_request_id_t generate_request_id();
SNMP_PACKET_PARSE_ERROR parseFrom(uint8_t* buf, size_t max_len);
int serialiseInto(uint8_t* buf, size_t max_len);
//TODO: put checks in all these setters
void setCommunityString(const std::string &CommunityString);
void setRequestID(snmp_request_id_t);
bool setPDUType(ASN_TYPE);
void setVersion(SNMP_VERSION);
bool reuse = false;
std::shared_ptr<IntegerType> requestIDPtr = nullptr;
std::shared_ptr<ByteType> snmpVersionPtr = nullptr;
std::shared_ptr<OctetType> communityStringPtr = nullptr;
snmp_request_id_t requestID = 0;
SNMP_VERSION snmpVersion = (SNMP_VERSION)0;
std::string communityString;
ASN_TYPE packetPDUType;
std::deque<VarBind> varbindList;
union ErrorStatus errorStatus = { NO_ERROR };
union ErrorIndex errorIndex = {0};
ComplexType* packet = nullptr;
protected:
virtual bool build();
virtual std::shared_ptr<ComplexType> generateVarBindList();
private:
SNMP_PACKET_PARSE_ERROR parsePacket(ComplexType* structure, enum SNMPParsingState state);
};
#endif