SNMPConnection.cpp 4.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*
 * SNMPConnection.cpp
 *
 *  Created on: 05.07.2018
 *      Author: Axel Auweter (original), Micha Mueller
 */

#include "SNMPConnection.h"

Micha Mueller's avatar
Micha Mueller committed
10
11
#include <string>

12
SNMPConnection::SNMPConnection() {
Micha Mueller's avatar
Micha Mueller committed
13
	_isInitialized	= false;
Micha Mueller's avatar
Micha Mueller committed
14
15
	_snmpCommunity	= "";
	_oidPrefix		= "";
Micha Mueller's avatar
Micha Mueller committed
16
17
18
	_username		= "";
	_authKey		= "";
	_privKey		= "";
Micha Mueller's avatar
Micha Mueller committed
19
	_host			= "";
Micha Mueller's avatar
Micha Mueller committed
20
21
22
23
	_authProto		= NULL;
	_privProto		= NULL;
	_authProtoLen	= 0;
	_privProtoLen	= 0;
Micha Mueller's avatar
Micha Mueller committed
24
	_port			= 0;
Micha Mueller's avatar
Micha Mueller committed
25
26
	_securityLevel	= -1;
	_version		= -1;
27
	_snmpSession.peername = NULL;
Micha Mueller's avatar
Micha Mueller committed
28
	_snmpSession.community_len = 0;
Micha Mueller's avatar
Micha Mueller committed
29
	_snmpSession.securityNameLen = 0;
Micha Mueller's avatar
Micha Mueller committed
30
	_strand = NULL;
31
32
33
}

SNMPConnection::~SNMPConnection() {
34
35
36
	if (_snmpSession.peername) {
		free(_snmpSession.peername);
	}
Micha Mueller's avatar
Micha Mueller committed
37
38
39
40
41
42
43
	if (_version == SNMP_VERSION_2c) {
		if (_snmpSession.community_len) {
			free(_snmpSession.community);
			_snmpSession.community_len = 0;
		}
	} else if (_version == SNMP_VERSION_3) {
		free(_snmpSession.securityName);
Micha Mueller's avatar
Micha Mueller committed
44
	}
45
46
47
48

	if (_strand) {
		delete _strand;
	}
49
50
51
52
53
54
55
}

void SNMPConnection::initializeStrand(boost::asio::io_service& io) {
	if (!_strand) {
		_strand = new boost::asio::io_service::strand(io);
	}
}
Micha Mueller's avatar
Micha Mueller committed
56
57

void SNMPConnection::init() {
Micha Mueller's avatar
Micha Mueller committed
58
59
60
61
	if(_isInitialized) {
		return;
	}

Micha Mueller's avatar
Micha Mueller committed
62
	snmp_sess_init(&_snmpSession);
Micha Mueller's avatar
Micha Mueller committed
63
	_snmpSession.version = _version;
Micha Mueller's avatar
Micha Mueller committed
64
	_snmpSession.peername = strdup(_host.c_str());
Micha Mueller's avatar
Micha Mueller committed
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

	if (_version == SNMP_VERSION_2c) {
		_snmpSession.community = (u_char*) strdup(_snmpCommunity.c_str());
		_snmpSession.community_len = _snmpCommunity.length();
	} else if (_version == SNMP_VERSION_3) {
		_snmpSession.community = NULL;
		_snmpSession.community_len = 0;


		/* set the SNMPv3 user name */
		_snmpSession.securityName = strdup(_username.c_str());
		_snmpSession.securityNameLen = _username.length();

		/* set the security level */
		_snmpSession.securityLevel = _securityLevel;

		if(_snmpSession.securityLevel == SNMP_SEC_LEVEL_NOAUTH) {
			// no authentication, no privacy
			// nothing to do
		} else if (_snmpSession.securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || _snmpSession.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
			// authentication...
			_snmpSession.securityAuthKeyLen = USM_AUTH_KU_LEN;

			//set the authentication method
			_snmpSession.securityAuthProto = _authProto;
			_snmpSession.securityAuthProtoLen = _authProtoLen;//sizeof(_authProto)/sizeof(oid);

			/* set the authentication key to a hashed version of our
			_authKey (which must be at least 8 characters long) */
			if (generate_Ku(_snmpSession.securityAuthProto,	_snmpSession.securityAuthProtoLen,
					(u_char *) _authKey.c_str(), _authKey.length(), _snmpSession.securityAuthKey,
					&_snmpSession.securityAuthKeyLen) != SNMPERR_SUCCESS) {
				LOG(error) << "SNMP: Error generating Ku from authentication key";
			}

			if (_snmpSession.securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
				//...with privacy
				_snmpSession.securityPrivKeyLen = USM_PRIV_KU_LEN;

				//set the privacy method (aka encryption)
				_snmpSession.securityPrivProto = _privProto;
				_snmpSession.securityPrivProtoLen = _privProtoLen;

				/* set the privacy key to a hashed version of our
				_privKey (which must be at least 8 characters long) */
				if (generate_Ku(_snmpSession.securityAuthProto, _snmpSession.securityAuthProtoLen,
						(u_char *) _privKey.c_str(), _privKey.length(), _snmpSession.securityPrivKey,
						&_snmpSession.securityPrivKeyLen) != SNMPERR_SUCCESS) {
					LOG(error) << "SNMP: Error generating Ku from privacy key";
				}
			}
		} else {
			LOG(warning) << "SNMP security level unknown!";
		}
	} else {
		LOG(warning) << "SNMP Version " << _version << " not supported!";
	}
Micha Mueller's avatar
Micha Mueller committed
122
	_isInitialized = true;
Micha Mueller's avatar
Micha Mueller committed
123
124
}

125
int64_t SNMPConnection::issueGet(const oid* const OID, size_t OIDLen) {
Micha Mueller's avatar
Micha Mueller committed
126
127
128
129
	struct snmp_session* ss;
	struct snmp_pdu *pdu, *response;
	struct variable_list *vp;
	int status;
130
	int64_t ret = 0;
Micha Mueller's avatar
Micha Mueller committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

	ss = snmp_open(&_snmpSession);

	if (!ss) {
		throw std::runtime_error("Could not open SNMP Session!");
	}

	pdu = snmp_pdu_create(SNMP_MSG_GET);

	snmp_add_null_var(pdu, OID, OIDLen);

	status = snmp_synch_response(ss, pdu, &response);

	if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
		vp = response->variables;
		if (vp) {
			if (vp->type == ASN_INTEGER) {
				ret = *vp->val.integer;
			}
			else if (vp->type == 0x43) { /* Timeticks: No idea why there is no definition for this in the libray */
				ret = *vp->val.integer;
			}
			else {
				LOG(warning) << "Non-Integer and non-Timetick SNMP data received (type=" << std::to_string((int)vp->type) << "):";
				char buf[1024];
				while (vp) {
					snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
					LOG(warning) << ss->peername << ": " << buf;
					vp = vp->next_variable;
				}
			}
		}
	} else {
Micha Mueller's avatar
Micha Mueller committed
164
165
166
167
168
169
170
171
		if (status == STAT_SUCCESS) {
			LOG(error) << "Error in packet: " << snmp_errstring(response->errstat);
		} else {
			char*	err;
			snmp_error(ss, NULL, NULL, &err);
			LOG(error) << "SNMP-get: " << err;
		}

Micha Mueller's avatar
Micha Mueller committed
172
173
174
175
176
177
178
179
180
		throw std::runtime_error("Request failed!");
	}

	if (response) {
		snmp_free_pdu(response);
	}
	snmp_close(ss);
	return ret;
}