Contiki-NG
snmp-mib.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-mib.h"
43 #include "snmp-oid.h"
44 #include "lib/list.h"
45 
46 #define LOG_MODULE "SNMP [mib]"
47 #define LOG_LEVEL LOG_LEVEL_SNMP
48 
49 LIST(snmp_mib);
50 
52 snmp_mib_find(uint32_t *oid)
53 {
54  snmp_mib_resource_t *resource;
55 
56  resource = NULL;
57  for(resource = list_head(snmp_mib);
58  resource; resource = resource->next) {
59 
60  if(!snmp_oid_cmp_oid(oid, resource->oid)) {
61  return resource;
62  }
63  }
64 
65  return NULL;
66 }
69 {
70  snmp_mib_resource_t *resource;
71 
72  resource = NULL;
73  for(resource = list_head(snmp_mib);
74  resource; resource = resource->next) {
75 
76  if(snmp_oid_cmp_oid(resource->oid, oid) > 0) {
77  return resource;
78  }
79  }
80 
81  return NULL;
82 }
83 void
85 {
86  snmp_mib_resource_t *resource;
87 
88  for(resource = list_head(snmp_mib);
89  resource; resource = resource->next) {
90 
91  if(snmp_oid_cmp_oid(resource->oid, new_resource->oid) > 0) {
92  break;
93  }
94  }
95  if(resource == NULL) {
96  list_add(snmp_mib, new_resource);
97  } else {
98  list_insert(snmp_mib, new_resource, resource);
99  }
100 
101 #if LOG_LEVEL == LOG_LEVEL_DBG
102  /*
103  * We print the entire resource table
104  */
105  LOG_DBG("Table after insert.\n");
106  for(resource = list_head(snmp_mib);
107  resource; resource = resource->next) {
108 
109  snmp_oid_print(resource->oid);
110  }
111 #endif /* LOG_LEVEL == LOG_LEVEL_DBG */
112 }
113 void
115 {
116  list_init(snmp_mib);
117 }
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
snmp_mib_resource_t * snmp_mib_find_next(uint32_t *oid)
Finds the next MIB Resource after this OID.
Definition: snmp-mib.c:68
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:61
int snmp_oid_cmp_oid(uint32_t *oid1, uint32_t *oid2)
Compares to oids.
Definition: snmp-oid.c:50
struct snmp_mib_resource_s * next
A pointer to the next element in the linked list.
Definition: snmp-mib.h:67
Linked list manipulation routines.
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
void snmp_mib_init(void)
Initialize the MIB resources list.
Definition: snmp-mib.c:114
snmp_mib_resource_t * snmp_mib_find(uint32_t *oid)
Finds the MIB Resource for this OID.
Definition: snmp-mib.c:52
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
uint32_t * oid
A array that represents the OID.
Definition: snmp-mib.h:73
An implementation of the Simple Network Management Protocol (RFC 3411-3418)
void snmp_mib_add(snmp_mib_resource_t *new_resource)
Adds a resource into the linked list.
Definition: snmp-mib.c:84
void snmp_oid_print(uint32_t *oid)
Prints a oid.
Definition: snmp-oid.c:202