Contiki-NG
snmp-mib.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019-2020 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  * SNMP Implementation of the MIB
36  * \author
37  * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br
38  */
39 
40 #include "contiki.h"
41 
42 #include "snmp-mib.h"
43 #include "lib/list.h"
44 
45 #define LOG_MODULE "SNMP [mib]"
46 #define LOG_LEVEL LOG_LEVEL_SNMP
47 
48 LIST(snmp_mib);
49 
50 /*---------------------------------------------------------------------------*/
51 /**
52  * @brief Compares to oids
53  *
54  * @param oid1 First Oid
55  * @param oid2 Second Oid
56  *
57  * @return < 0 if oid1 < oid2, > 0 if oid1 > oid2 and 0 if they are equal
58  */
59 static inline int
61 {
62  uint8_t i;
63 
64  i = 0;
65  while(i < oid1->length && i < oid2->length) {
66  if(oid1->data[i] != oid2->data[i]) {
67  if(oid1->data[i] < oid2->data[i]) {
68  return -1;
69  }
70  return 1;
71  }
72  i++;
73  }
74 
75  if(i == oid1->length &&
76  i < oid2->length) {
77  return -1;
78  }
79 
80  if(i < oid1->length &&
81  i == oid2->length) {
82  return 1;
83  }
84 
85  return 0;
86 }
87 /*---------------------------------------------------------------------------*/
90 {
91  snmp_mib_resource_t *resource;
92 
93  resource = NULL;
94  for(resource = list_head(snmp_mib);
95  resource; resource = resource->next) {
96 
97  if(!snmp_mib_cmp_oid(oid, &resource->oid)) {
98  return resource;
99  }
100  }
101 
102  return NULL;
103 }
104 /*---------------------------------------------------------------------------*/
107 {
108  snmp_mib_resource_t *resource;
109 
110  resource = NULL;
111  for(resource = list_head(snmp_mib);
112  resource; resource = resource->next) {
113 
114  if(snmp_mib_cmp_oid(&resource->oid, oid) > 0) {
115  return resource;
116  }
117  }
118 
119  return NULL;
120 }
121 /*---------------------------------------------------------------------------*/
122 void
124 {
125  snmp_mib_resource_t *resource;
126  uint8_t i;
127 
128  for(resource = list_head(snmp_mib);
129  resource; resource = resource->next) {
130 
131  if(snmp_mib_cmp_oid(&resource->oid, &new_resource->oid) > 0) {
132  break;
133  }
134  }
135  if(resource == NULL) {
136  list_add(snmp_mib, new_resource);
137  } else {
138  list_insert(snmp_mib, new_resource, resource);
139  }
140 
141  if(LOG_DBG_ENABLED) {
142  /*
143  * We print the entire resource table
144  */
145  LOG_DBG("Table after insert.\n");
146  for(resource = list_head(snmp_mib);
147  resource; resource = resource->next) {
148 
149  i = 0;
150  LOG_DBG("{");
151  while(i < resource->oid.length) {
152  LOG_DBG_("%lu", (unsigned long)resource->oid.data[i]);
153  i++;
154  if(i < resource->oid.length) {
155  LOG_DBG_(".");
156  }
157  }
158  LOG_DBG_("}\n");
159  }
160  }
161 }
162 /*---------------------------------------------------------------------------*/
163 void
165 {
166  list_init(snmp_mib);
167 }
void snmp_mib_add(snmp_mib_resource_t *new_resource)
Adds a resource into the linked list.
Definition: snmp-mib.c:123
void list_insert(list_t list, void *previtem, void *newitem)
Insert an item after a specified item on the list.
Definition: list.c:300
The MIB Resource struct.
Definition: snmp-mib.h:75
struct snmp_mib_resource_s * next
A pointer to the next element in the linked list.
Definition: snmp-mib.h:81
The OID struct.
Definition: snmp.h:144
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
uint32_t data[SNMP_MSG_OID_MAX_LEN]
The OID.
Definition: snmp.h:148
snmp_mib_resource_t * snmp_mib_find(snmp_oid_t *oid)
Finds the MIB Resource for this OID.
Definition: snmp-mib.c:89
void snmp_mib_init(void)
Initialize the MIB resources list.
Definition: snmp-mib.c:164
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
void list_init(list_t list)
Initialize a list.
Definition: list.c:65
#define LIST(name)
Declare a linked list.
Definition: list.h:89
snmp_oid_t oid
A OID struct.
Definition: snmp-mib.h:85
SNMP Implementation of the MIB
static int snmp_mib_cmp_oid(snmp_oid_t *oid1, snmp_oid_t *oid2)
Compares to oids.
Definition: snmp-mib.c:60
snmp_mib_resource_t * snmp_mib_find_next(snmp_oid_t *oid)
Finds the next MIB Resource after this OID.
Definition: snmp-mib.c:106
uint8_t length
The OID length.
Definition: snmp.h:153