45 #define LOG_MODULE "SNMP [ber]" 46 #define LOG_LEVEL LOG_LEVEL_SNMP 50 snmp_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;
163 for(i = 0; i < length; ++i) {
164 if(snmp_packet->
used == snmp_packet->
max) {
168 *snmp_packet->
out-- = (uint8_t)*str--;
187 uint16_t original_out_len;
190 original_out_len = snmp_packet->
used;
194 val = oid->
data[pos];
196 if(snmp_packet->
used == snmp_packet->
max) {
200 *snmp_packet->
out-- = (uint8_t)(val & 0x7F);
205 if(snmp_packet->
used == snmp_packet->
max) {
209 *snmp_packet->
out-- = (uint8_t)((val & 0x7F) | 0x80);
217 if(snmp_packet->
used == snmp_packet->
max) {
221 val = *(snmp_packet->
out + 1) + 40 * oid->
data[pos];
225 if(snmp_packet->
used == snmp_packet->
max) {
229 *snmp_packet->
out-- = (uint8_t)(val & 0x7F);
235 if(snmp_packet->
used == snmp_packet->
max) {
239 *snmp_packet->
out-- = (uint8_t)((val & 0x7F) | 0x80);
260 if(snmp_packet->
used == snmp_packet->
max) {
264 *snmp_packet->
out-- = 0x00;
271 snmp_ber_decode_unsigned_integer(
snmp_packet_t *snmp_packet, uint8_t expected_type, uint32_t *num)
273 uint8_t i, len, type;
279 if(type != expected_type) {
299 if(snmp_packet->
used == 0) {
303 *num = (uint32_t)(*snmp_packet->
in++ & 0xFF);
306 for(i = 1; i < len; ++i) {
308 if(snmp_packet->
used == 0) {
311 *num |= (uint8_t)(*snmp_packet->
in++ & 0xFF);
321 if(snmp_packet->
used == 0) {
325 *type = *snmp_packet->
in++;
334 if(snmp_packet->
used == 0) {
338 *length = *snmp_packet->
in++;
359 uint8_t type, i, length_bytes;
373 if((*snmp_packet->
in & 0x80) == 0) {
375 if(snmp_packet->
used == 0) {
379 *length = (uint32_t)*snmp_packet->
in++;
383 if(snmp_packet->
used == 0) {
387 length_bytes = (uint8_t)(*snmp_packet->
in++ & 0x7F);
390 if(length_bytes > 4) {
398 if(snmp_packet->
used == 0) {
402 *length = (uint32_t)*snmp_packet->
in++;
405 for(i = 1; i < length_bytes; ++i) {
408 if(snmp_packet->
used == 0) {
412 *length |= *snmp_packet->
in++;
417 *str = (
const char *)snmp_packet->
in;
419 if(snmp_packet->
used == 0 || snmp_packet->
used - *length <= 0) {
423 snmp_packet->
used -= *length;
424 snmp_packet->
in += *length;
432 uint8_t *buf_end, type;
448 buf_end = snmp_packet->
in + len;
450 if(snmp_packet->
used == 0) {
455 first = div(*snmp_packet->
in++, 40);
459 oid->
data[oid->
length++] = (uint32_t)first.quot;
460 oid->
data[oid->
length++] = (uint32_t)first.rem;
462 while(snmp_packet->
in != buf_end) {
467 if(snmp_packet->
used == 0) {
470 oid->
data[oid->
length] = (uint32_t)(*snmp_packet->
in & 0x7F);
471 for(j = 0; j < 4; j++) {
473 if((*snmp_packet->
in++ & 0x80) == 0) {
477 if(snmp_packet->
used == 0) {
494 if(snmp_packet->
used == 0) {
501 if(snmp_packet->
used == 0) {
SNMP Implementation of the process
#define BER_DATA_TYPE_TIMETICKS
TimeTicks.
int snmp_ber_decode_null(snmp_packet_t *snmp_packet)
Decodes a null.
int snmp_ber_decode_integer(snmp_packet_t *snmp_packet, uint32_t *num)
Decodes an integer.
int snmp_ber_decode_type(snmp_packet_t *snmp_packet, uint8_t *type)
Decodes a type.
int snmp_ber_encode_integer(snmp_packet_t *snmp_packet, uint32_t number)
Encodes an integer.
#define BER_DATA_TYPE_OCTET_STRING
Octet String.
int snmp_ber_encode_timeticks(snmp_packet_t *snmp_packet, uint32_t timeticks)
Encodes a timeticks.
SNMP Implementation of the BER encoding
int snmp_ber_encode_type(snmp_packet_t *snmp_packet, uint8_t type)
Encodes a type.
int snmp_ber_decode_length(snmp_packet_t *snmp_packet, uint8_t *length)
Decodes a length.
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_oid(snmp_packet_t *snmp_packet, snmp_oid_t *oid)
Decodes an OID.
int snmp_ber_encode_string_len(snmp_packet_t *snmp_packet, const char *str, uint32_t length)
Encodes a string.
uint8_t * in
The pointer used for the incoming packet.
int snmp_ber_encode_oid(snmp_packet_t *snmp_packet, snmp_oid_t *oid)
Encodes a Oid.
uint8_t * out
The pointer used for the outgoing packet.
int snmp_ber_encode_length(snmp_packet_t *snmp_packet, uint16_t length)
Encodes the length.
#define SNMP_MSG_OID_MAX_LEN
Default maximum number of IDs in one OID.
uint32_t data[SNMP_MSG_OID_MAX_LEN]
The OID.
uint16_t used
The number os bytes used.
#define BER_DATA_TYPE_INTEGER
Integer.
uint16_t max
The maximum number of bytes.
snmp_oid_t oid
A OID struct.
#define BER_DATA_TYPE_OBJECT_IDENTIFIER
Object Identifier.
int snmp_ber_encode_null(snmp_packet_t *snmp_packet, uint8_t type)
Encodes a null.
int snmp_ber_decode_timeticks(snmp_packet_t *snmp_packet, uint32_t *timeticks)
Decodes a timeticks.
uint8_t length
The OID length.