Contiki-NG
snmp-ber.h
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 /**
41  * \addtogroup snmp
42  * @{
43  */
44 
45 #ifndef SNMP_BER_H_
46 #define SNMP_BER_H_
47 
48 #define BER_DATA_TYPE_INTEGER 0x02
49 #define BER_DATA_TYPE_OCTET_STRING 0x04
50 #define BER_DATA_TYPE_NULL 0x05
51 #define BER_DATA_TYPE_OID 0x06
52 #define BER_DATA_TYPE_SEQUENCE 0x30
53 
54 /**
55  * @brief Encodes a type
56  *
57  * @param out A pointer to the end of the buffer
58  * @param out_len A pointer to the buffer length
59  * @param type A type
60  *
61  * @return NULL if error or the next entry in the buffer
62  */
63 unsigned char *
64 snmp_ber_encode_type(unsigned char *out, uint32_t *out_len, uint8_t type);
65 
66 /**
67  * @brief Encodes the length
68  *
69  * @param out A pointer to the end of the buffer
70  * @param out_len A pointer to the buffer length
71  * @param length A length
72  *
73  * @return NULL if error or the next entry in the buffer
74  */
75 unsigned char *
76 snmp_ber_encode_length(unsigned char *out, uint32_t *out_len, uint8_t length);
77 
78 /**
79  * @brief Encodes an integer
80  *
81  * @param out A pointer to the end of the buffer
82  * @param out_len A pointer to the buffer length
83  * @param integer A integer
84  *
85  * @return NULL if error or the next entry in the buffer
86  */
87 unsigned char *
88 snmp_ber_encode_integer(unsigned char *out, uint32_t *out_len, uint32_t integer);
89 
90 /**
91  * @brief Encodes an unsigned integer
92  *
93  * @param out A pointer to the end of the buffer
94  * @param out_len A pointer to the buffer length
95  * @param type A type that represents an unsigned integer
96  * @param number A number
97  *
98  * @return NULL if error or the next entry in the buffer
99  */
100 unsigned char *
101 snmp_ber_encode_unsigned_integer(unsigned char *out, uint32_t *out_len, uint8_t type, uint32_t number);
102 
103 /**
104  * @brief Encodes a string
105  *
106  * @param out A pointer to the end of the buffer
107  * @param out_len A pointer to the buffer length
108  * @param str A string
109  * @param length The string length
110  *
111  * @return NULL if error or the next entry in the buffer
112  */
113 unsigned char *
114 snmp_ber_encode_string_len(unsigned char *out, uint32_t *out_len, const char *str, uint32_t length);
115 
116 /**
117  * @brief Encodes a null
118  *
119  * @param out A pointer to the end of the buffer
120  * @param out_len A pointer to the buffer length
121  * @param type A type
122  *
123  * @return NULL if error or the next entry in the buffer
124  */
125 unsigned char *
126 snmp_ber_encode_null(unsigned char *out, uint32_t *out_len, uint8_t type);
127 
128 /**
129  * @brief Decodes a type
130  *
131  * @param buff A pointer to the beginning of the buffer
132  * @param buff_len A pointer to the buffer length
133  * @param type A pointer to the type
134  *
135  * @return NULL if error or the first entry after the oid in the buffer
136  */
137 unsigned char *
138 snmp_ber_decode_type(unsigned char *buff, uint32_t *buff_len, uint8_t *type);
139 
140 /**
141  * @brief Decodes a length
142  *
143  * @param buff A pointer to the beginning of the buffer
144  * @param buff_len A pointer to the buffer length
145  * @param length A pointer to the length
146  *
147  * @return NULL if error or the first entry after the oid in the buffer
148  */
149 unsigned char *
150 snmp_ber_decode_length(unsigned char *buff, uint32_t *buff_len, uint8_t *length);
151 
152 /**
153  * @brief Decodes an integer
154  *
155  * @param buff A pointer to the beginning of the buffer
156  * @param buff_len A pointer to the buffer length
157  * @param integer A pointer to the integer
158  *
159  * @return NULL if error or the first entry after the oid in the buffer
160  */
161 unsigned char *
162 snmp_ber_decode_integer(unsigned char *buff, uint32_t *buff_len, uint32_t *integer);
163 
164 /**
165  * @brief Decodes an unsigned number
166  *
167  * @param buff A pointer to the beginning of the buffer
168  * @param buff_len A pointer to the buffer length
169  * @param expected_type The expected type that represents an unsingned integer
170  * @param number A pointer to the number
171  *
172  * @return NULL if error or the first entry after the oid in the buffer
173  */
174 unsigned char *
175 snmp_ber_decode_unsigned_integer(unsigned char *buff, uint32_t *buff_len, uint8_t expected_type, uint32_t *number);
176 
177 /**
178  * @brief Decodes a string
179  *
180  * @param buff A pointer to the beginning of the buffer
181  * @param buff_len A pointer to the buffer length
182  * @param str A pointer to the string
183  * @param length A pointer to the string length
184  *
185  * @return NULL if error or the first entry after the oid in the buffer
186  */
187 unsigned char *
188 snmp_ber_decode_string_len_buffer(unsigned char *buff, uint32_t *buff_len, const char **str, uint32_t *length);
189 
190 /**
191  * @brief Decodes a null
192  *
193  * @param buff A pointer to the beginning of the buffer
194  * @param buff_len A pointer to the buffer length
195  *
196  * @return NULL if error or the first entry after the oid in the buffer
197  */
198 unsigned char *
199 snmp_ber_decode_null(unsigned char *buff, uint32_t *buff_len);
200 
201 #endif /* SNMP_BER_H_ */
202 /** @} */
unsigned char * snmp_ber_encode_string_len(unsigned char *out, uint32_t *out_len, const char *str, uint32_t length)
Encodes a string.
Definition: snmp-ber.c:102
unsigned char * snmp_ber_decode_type(unsigned char *buff, uint32_t *buff_len, uint8_t *type)
Decodes a type.
Definition: snmp-ber.c:129
unsigned char * snmp_ber_encode_null(unsigned char *out, uint32_t *out_len, uint8_t type)
Encodes a null.
Definition: snmp-ber.c:119
unsigned char * snmp_ber_decode_null(unsigned char *buff, uint32_t *buff_len)
Decodes a null.
Definition: snmp-ber.c:264
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
unsigned char * snmp_ber_encode_integer(unsigned char *out, uint32_t *out_len, uint32_t integer)
Encodes an integer.
Definition: snmp-ber.c:66
unsigned char * snmp_ber_decode_unsigned_integer(unsigned char *buff, uint32_t *buff_len, uint8_t expected_type, uint32_t *number)
Decodes an unsigned number.
Definition: snmp-ber.c:183
unsigned char * snmp_ber_encode_unsigned_integer(unsigned char *out, uint32_t *out_len, uint8_t type, uint32_t number)
Encodes an unsigned integer.
Definition: snmp-ber.c:84
unsigned char * snmp_ber_decode_integer(unsigned char *buff, uint32_t *buff_len, uint32_t *integer)
Decodes an integer.
Definition: snmp-ber.c:147
unsigned char * snmp_ber_decode_string_len_buffer(unsigned char *buff, uint32_t *buff_len, const char **str, uint32_t *length)
Decodes a string.
Definition: snmp-ber.c:219
unsigned char * snmp_ber_encode_type(unsigned char *out, uint32_t *out_len, uint8_t type)
Encodes a type.
Definition: snmp-ber.c:50