Contiki-NG
snmp-oid.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*---------------------------------------------------------------------------*/
32 
33 /**
34  * \file
35  * An implementation of the Simple Network Management Protocol (RFC 3411-3418)
36  * \author
37  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
38  */
39 
40 #include "contiki.h"
41 
42 #include "snmp-oid.h"
43 #include "snmp-ber.h"
44 
45 #define LOG_MODULE "SNMP [oid]"
46 #define LOG_LEVEL LOG_LEVEL_SNMP
47 
48 /*---------------------------------------------------------------------------*/
49 int
50 snmp_oid_cmp_oid(uint32_t *oid1, uint32_t *oid2)
51 {
52  uint8_t i;
53 
54  i = 0;
55  while(oid1[i] != ((uint32_t)-1) &&
56  oid2[i] != ((uint32_t)-1)) {
57  if(oid1[i] != oid2[i]) {
58  if(oid1[i] < oid2[i]) {
59  return -1;
60  }
61  return 1;
62  }
63  i++;
64  }
65 
66  if(oid1[i] == ((uint32_t)-1) &&
67  oid2[i] != ((uint32_t)-1)) {
68  return -1;
69  }
70 
71  if(oid1[i] != ((uint32_t)-1) &&
72  oid2[i] == ((uint32_t)-1)) {
73  return 1;
74  }
75 
76  return 0;
77 }
78 /*---------------------------------------------------------------------------*/
79 unsigned char *
80 snmp_oid_encode_oid(unsigned char *out, uint32_t *out_len, uint32_t *oid)
81 {
82  uint32_t original_out_len;
83  uint32_t *oid_start = oid;
84  uint32_t num;
85 
86  original_out_len = *out_len;
87  while(*oid != ((uint32_t)-1)) {
88  ++oid;
89  }
90  --oid;
91 
92  while(oid != oid_start) {
93  num = *oid;
94  (*out_len)++;
95  *out-- = (uint8_t)(num & 0x7F);
96  num >>= 7;
97 
98  while(num) {
99  (*out_len)++;
100  *out-- = (uint8_t)((num & 0x7F) | 0x80);
101  num >>= 7;
102  }
103  --oid;
104  }
105 
106  num = *(out + 1) + 40 * *oid;
107  (*out_len)--;
108  out++;
109  (*out_len)++;
110  *out-- = (uint8_t)(num & 0x7F);
111  num >>= 7;
112 
113  while(num) {
114  (*out_len)++;
115  *out-- = (uint8_t)((num & 0x7F) | 0x80);
116  num >>= 7;
117  }
118 
119  out = snmp_ber_encode_length(out, out_len, ((*out_len - original_out_len) & 0xFF));
120  out = snmp_ber_encode_type(out, out_len, SNMP_DATA_TYPE_OBJECT);
121 
122  return out;
123 }
124 /*---------------------------------------------------------------------------*/
125 uint8_t *
126 snmp_oid_decode_oid(uint8_t *buf, uint32_t *buff_len, uint32_t *oid, uint32_t *oid_len)
127 {
128  uint32_t *start;
129  uint8_t *buf_end, type;
130  uint8_t len;
131  div_t first;
132 
133  start = oid;
134 
135  buf = snmp_ber_decode_type(buf, buff_len, &type);
136  if(buf == NULL) {
137  return NULL;
138  }
139 
140  if(type != SNMP_DATA_TYPE_OBJECT) {
141  return NULL;
142  }
143 
144  buf = snmp_ber_decode_length(buf, buff_len, &len);
145  if(buf == NULL) {
146  return NULL;
147  }
148 
149  buf_end = buf + len;
150 
151  (*buff_len)--;
152  first = div(*buf++, 40);
153  *oid++ = (uint32_t)first.quot;
154  *oid++ = (uint32_t)first.rem;
155 
156  while(buf != buf_end) {
157  --(*oid_len);
158  if(*oid_len == 0) {
159  return NULL;
160  }
161 
162  int i;
163 
164  *oid = (uint32_t)(*buf & 0x7F);
165  for(i = 0; i < 4; i++) {
166  (*buff_len)--;
167  if((*buf++ & 0x80) == 0) {
168  break;
169  }
170 
171  *oid <<= 7;
172  *oid |= (*buf & 0x7F);
173  }
174 
175  ++oid;
176  }
177 
178  *oid++ = ((uint32_t)-1);
179  *oid_len = (uint32_t)(oid - start);
180 
181  return buf;
182 }
183 /*---------------------------------------------------------------------------*/
184 void
185 snmp_oid_copy(uint32_t *dst, uint32_t *src)
186 {
187  uint8_t i;
188 
189  i = 0;
190  while(src[i] != ((uint32_t)-1)) {
191  dst[i] = src[i];
192  i++;
193  }
194  /*
195  * Copy the "null" terminator
196  */
197  dst[i] = src[i];
198 }
199 /*---------------------------------------------------------------------------*/
200 #if LOG_LEVEL == LOG_LEVEL_DBG
201 void
202 snmp_oid_print(uint32_t *oid)
203 {
204  uint8_t i;
205 
206  i = 0;
207  LOG_DBG("{");
208  while(oid[i] != ((uint32_t)-1)) {
209  LOG_DBG_("%lu", (unsigned long)oid[i]);
210  i++;
211  if(oid[i] != ((uint32_t)-1)) {
212  LOG_DBG_(".");
213  }
214  }
215  LOG_DBG_("}\n");
216 }
217 #endif /* LOG_LEVEL == LOG_LEVEL_DBG */
void snmp_oid_copy(uint32_t *dst, uint32_t *src)
Copies a Oid.
Definition: snmp-oid.c:185
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
uint8_t * snmp_oid_decode_oid(uint8_t *buf, uint32_t *buff_len, uint32_t *oid, uint32_t *oid_len)
Decodes a Oid.
Definition: snmp-oid.c:126
unsigned char * snmp_ber_decode_type(unsigned char *buff, uint32_t *buff_len, uint8_t *type)
Decodes a type.
Definition: snmp-ber.c:129
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
int snmp_oid_cmp_oid(uint32_t *oid1, uint32_t *oid2)
Compares to oids.
Definition: snmp-oid.c:50
static void start(void)
Start measurement.
unsigned char * snmp_ber_encode_length(unsigned char *out, uint32_t *out_len, uint8_t length)
Encodes the length.
Definition: snmp-ber.c:58
unsigned char * snmp_ber_decode_length(unsigned char *buff, uint32_t *buff_len, uint8_t *length)
Decodes a length.
Definition: snmp-ber.c:138
uint32_t * oid
A array that represents the OID.
Definition: snmp-mib.h:73
unsigned char * snmp_oid_encode_oid(unsigned char *out, uint32_t *out_len, uint32_t *oid)
Encodes a Oid.
Definition: snmp-oid.c:80
unsigned char * snmp_ber_encode_type(unsigned char *out, uint32_t *out_len, uint8_t type)
Encodes a type.
Definition: snmp-ber.c:50
void snmp_oid_print(uint32_t *oid)
Prints a oid.
Definition: snmp-oid.c:202