45#define LOG_MODULE "SNMP [ber]"
46#define LOG_LEVEL LOG_LEVEL_SNMP
50snmp_ber_encode_unsigned_integer(
snmp_packet_t *snmp_packet, uint8_t type, uint32_t number)
52 uint16_t original_out_len;
54 original_out_len = snmp_packet->
used;
56 if(snmp_packet->
used == snmp_packet->
max) {
60 *snmp_packet->
out-- = (uint8_t)number & 0xFF;
85 if(snmp_packet->
used == snmp_packet->
max) {
89 *snmp_packet->
out-- = type;
99 if(snmp_packet->
used == snmp_packet->
max) {
103 *snmp_packet->
out-- = (uint8_t)length & 0xFF;
106 if(snmp_packet->
used == snmp_packet->
max) {
110 *snmp_packet->
out-- = (uint8_t)(length >> 8) & 0xFF;
113 if(snmp_packet->
used == snmp_packet->
max) {
117 *snmp_packet->
out-- = 0x82;
119 }
else if(length > 0x7F) {
120 if(snmp_packet->
used == snmp_packet->
max) {
124 *snmp_packet->
out-- = (uint8_t)length & 0xFF;
127 if(snmp_packet->
used == snmp_packet->
max) {
131 *snmp_packet->
out-- = 0x81;
134 if(snmp_packet->
used == snmp_packet->
max) {
138 *snmp_packet->
out-- = (uint8_t)length & 0x7F;
164 for(i = 0; i < length; ++i) {
165 if(snmp_packet->
used == snmp_packet->
max) {
169 *snmp_packet->
out-- = (uint8_t)*str--;
189 uint16_t original_out_len;
192 original_out_len = snmp_packet->
used;
196 val = oid->
data[pos];
198 if(snmp_packet->
used == snmp_packet->
max) {
202 *snmp_packet->
out-- = (uint8_t)(val & 0x7F);
207 if(snmp_packet->
used == snmp_packet->
max) {
211 *snmp_packet->
out-- = (uint8_t)((val & 0x7F) | 0x80);
219 if(snmp_packet->
used == snmp_packet->
max) {
223 val = *(snmp_packet->
out + 1) + 40 * oid->
data[pos];
227 if(snmp_packet->
used == snmp_packet->
max) {
231 *snmp_packet->
out-- = (uint8_t)(val & 0x7F);
237 if(snmp_packet->
used == snmp_packet->
max) {
241 *snmp_packet->
out-- = (uint8_t)((val & 0x7F) | 0x80);
262 if(snmp_packet->
used == snmp_packet->
max) {
266 *snmp_packet->
out-- = 0x00;
273snmp_ber_decode_unsigned_integer(
snmp_packet_t *snmp_packet, uint8_t expected_type, uint32_t *num)
275 uint8_t i, len, type;
281 if(type != expected_type) {
301 if(snmp_packet->
used == 0) {
305 *num = (uint32_t)(*snmp_packet->
in++ & 0xFF);
308 for(i = 1; i < len; ++i) {
310 if(snmp_packet->
used == 0) {
313 *num |= (uint8_t)(*snmp_packet->
in++ & 0xFF);
323 if(snmp_packet->
used == 0) {
327 *type = *snmp_packet->
in++;
336 if(snmp_packet->
used == 0) {
340 *length = *snmp_packet->
in++;
361 uint8_t type, i, length_bytes;
375 if((*snmp_packet->
in & 0x80) == 0) {
377 if(snmp_packet->
used == 0) {
381 *length = (uint32_t)*snmp_packet->
in++;
385 if(snmp_packet->
used == 0) {
389 length_bytes = (uint8_t)(*snmp_packet->
in++ & 0x7F);
392 if(length_bytes > 4) {
400 if(snmp_packet->
used == 0) {
404 *length = (uint32_t)*snmp_packet->
in++;
407 for(i = 1; i < length_bytes; ++i) {
410 if(snmp_packet->
used == 0) {
414 *length |= *snmp_packet->
in++;
419 *str = (
const char *)snmp_packet->
in;
421 if(snmp_packet->
used == 0 || snmp_packet->
used < *length) {
425 snmp_packet->
used -= *length;
426 snmp_packet->
in += *length;
434 uint8_t *buf_end, type;
450 buf_end = snmp_packet->
in + len;
452 if(snmp_packet->
used == 0) {
457 first = div(*snmp_packet->
in++, 40);
461 oid->
data[oid->
length++] = (uint32_t)first.quot;
462 oid->
data[oid->
length++] = (uint32_t)first.rem;
464 while(snmp_packet->
in != buf_end) {
469 if(snmp_packet->
used == 0) {
472 oid->
data[oid->
length] = (uint32_t)(*snmp_packet->
in & 0x7F);
473 for(j = 0; j < 4; j++) {
475 if((*snmp_packet->
in++ & 0x80) == 0) {
479 if(snmp_packet->
used == 0) {
496 if(snmp_packet->
used == 0) {
503 if(snmp_packet->
used == 0) {
#define BER_DATA_TYPE_INTEGER
Integer.
int snmp_ber_encode_string_len(snmp_packet_t *snmp_packet, const char *str, uint32_t length)
Encodes a string.
int snmp_ber_encode_timeticks(snmp_packet_t *snmp_packet, uint32_t timeticks)
Encodes a timeticks.
#define BER_DATA_TYPE_OBJECT_IDENTIFIER
Object Identifier.
int snmp_ber_encode_integer(snmp_packet_t *snmp_packet, uint32_t number)
Encodes an integer.
int snmp_ber_decode_null(snmp_packet_t *snmp_packet)
Decodes a null.
#define BER_DATA_TYPE_TIMETICKS
TimeTicks.
int snmp_ber_decode_type(snmp_packet_t *snmp_packet, uint8_t *type)
Decodes a type.
int snmp_ber_encode_null(snmp_packet_t *snmp_packet, uint8_t type)
Encodes a null.
int snmp_ber_encode_oid(snmp_packet_t *snmp_packet, snmp_oid_t *oid)
Encodes a Oid.
int snmp_ber_decode_oid(snmp_packet_t *snmp_packet, snmp_oid_t *oid)
Decodes an OID.
int snmp_ber_decode_length(snmp_packet_t *snmp_packet, uint8_t *length)
Decodes a length.
int snmp_ber_decode_integer(snmp_packet_t *snmp_packet, uint32_t *num)
Decodes an integer.
int snmp_ber_encode_type(snmp_packet_t *snmp_packet, uint8_t type)
Encodes a type.
int snmp_ber_encode_length(snmp_packet_t *snmp_packet, uint16_t length)
Encodes the length.
#define BER_DATA_TYPE_OCTET_STRING
Octet String.
int snmp_ber_decode_string_len_buffer(snmp_packet_t *snmp_packet, const char **str, uint32_t *length)
Decodes a string.
int snmp_ber_decode_timeticks(snmp_packet_t *snmp_packet, uint32_t *timeticks)
Decodes a timeticks.
#define SNMP_MSG_OID_MAX_LEN
Default maximum number of IDs in one OID.
SNMP Implementation of the BER encoding.
SNMP Implementation of the process.
uint8_t length
The OID length.
uint32_t data[SNMP_MSG_OID_MAX_LEN]
The OID.
uint8_t * in
The pointer used for the incoming packet.
uint8_t * out
The pointer used for the outgoing packet.
uint16_t max
The maximum number of bytes.
uint16_t used
The number of bytes used.