SNMP++  3.3.11
asn1.h
Go to the documentation of this file.
1 /*_############################################################################
2  _##
3  _## asn1.h
4  _##
5  _## SNMP++ v3.3
6  _## -----------------------------------------------
7  _## Copyright (c) 2001-2013 Jochen Katz, Frank Fock
8  _##
9  _## This software is based on SNMP++2.6 from Hewlett Packard:
10  _##
11  _## Copyright (c) 1996
12  _## Hewlett-Packard Company
13  _##
14  _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
15  _## Permission to use, copy, modify, distribute and/or sell this software
16  _## and/or its documentation is hereby granted without fee. User agrees
17  _## to display the above copyright notice and this license notice in all
18  _## copies of the software and any documentation of the software. User
19  _## agrees to assume all liability for the use of the software;
20  _## Hewlett-Packard and Jochen Katz make no representations about the
21  _## suitability of this software for any purpose. It is provided
22  _## "AS-IS" without warranty of any kind, either express or implied. User
23  _## hereby grants a royalty-free license to any and all derivatives based
24  _## upon this software code base.
25  _##
26  _##########################################################################*/
27 // $Id$
28 
29 #ifndef _SNMP_ASN1_H_
30 #define _SNMP_ASN1_H_
31 
32 #include <libsnmp.h>
33 #include "snmp_pp/config_snmp_pp.h"
34 #include "snmp_pp/target.h"
35 
36 #ifdef SNMP_PP_NAMESPACE
37 namespace Snmp_pp {
38 #endif
39 
40 #ifndef EIGHTBIT_SUBIDS
41 typedef unsigned long oid;
42 #define MAX_SUBID 0xFFFFFFFF
43 #else
44 typedef unsigned char oid;
45 #define MAX_SUBID 0xFF
46 #endif
47 
48 #define MAX_OID_LEN 128 /* max subid's in an oid */
49 
50 // asn.1 values
51 #define ASN_BOOLEAN (0x01)
52 #ifndef ASN_INTEGER
53 #define ASN_INTEGER (0x02)
54 #endif
55 #define ASN_BIT_STR (0x03)
56 #define ASN_OCTET_STR (0x04)
57 #ifndef ASN_NULL
58 #define ASN_NULL (0x05)
59 #endif
60 #define ASN_OBJECT_ID (0x06)
61 #ifndef ASN_SEQUENCE
62 #define ASN_SEQUENCE (0x10)
63 #endif
64 #define ASN_SET (0x11)
65 #ifndef ASN_UNIVERSAL
66 #define ASN_UNIVERSAL (0x00)
67 #endif
68 #ifndef ASN_APPLICATION
69 #define ASN_APPLICATION (0x40)
70 #endif
71 #ifndef ASN_CONTEXT
72 #define ASN_CONTEXT (0x80)
73 #endif
74 #ifndef ASN_PRIVATE
75 #define ASN_PRIVATE (0xC0)
76 #endif
77 #ifndef ASN_PRIMITIVE
78 #define ASN_PRIMITIVE (0x00)
79 #endif
80 #ifndef ASN_CONSTRUCTOR
81 #define ASN_CONSTRUCTOR (0x20)
82 #endif
83 #define ASN_LONG_LEN (0x80)
84 #define ASN_EXTENSION_ID (0x1F)
85 #define ASN_BIT8 (0x80)
86 
87 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
88 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
89 
90 #define ASN_UNI_PRIM (ASN_UNIVERSAL | ASN_PRIMITIVE)
91 #define ASN_SEQ_CON (ASN_SEQUENCE | ASN_CONSTRUCTOR)
92 
93 #define ASN_MAX_NAME_LEN 128
94 #define SNMP_VERSION_1 0
95 #define SNMP_VERSION_2C 1
96 #define SNMP_VERSION_2STERN 2
97 #define SNMP_VERSION_3 3
98 
99 // defined types (from the SMI, RFC 1065)
100 #define SMI_IPADDRESS (ASN_APPLICATION | 0)
101 #define SMI_COUNTER (ASN_APPLICATION | 1)
102 #define SMI_GAUGE (ASN_APPLICATION | 2)
103 #define SMI_TIMETICKS (ASN_APPLICATION | 3)
104 #define SMI_OPAQUE (ASN_APPLICATION | 4)
105 #define SMI_NSAP (ASN_APPLICATION | 5)
106 #define SMI_COUNTER64 (ASN_APPLICATION | 6)
107 #define SMI_UINTEGER (ASN_APPLICATION | 7)
108 
109 #define GET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
110 #define GETNEXT_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
111 #define GET_RSP_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
112 #define SET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
113 #define TRP_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
114 
115 #define GETBULK_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
116 #define INFORM_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
117 #define TRP2_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
118 #define REPORT_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
119 
120 #define SNMP_NOSUCHOBJECT (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
121 #define SNMP_NOSUCHINSTANCE (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
122 #define SNMP_ENDOFMIBVIEW (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
123 
124 
125 #ifdef _DEBUG
126 #define ASNERROR(string) debugprintf(3, "ASN parse error (%s)\n", string )
127 #else
128 #define ASNERROR(string)
129 #endif
130 
131 
132 typedef struct sockaddr_in ipaddr;
133 
134 // pdu
135 struct snmp_pdu {
136  int command; // pdu type
137  unsigned long reqid; // Request id
138 #ifdef _SNMPv3
139  unsigned long msgid;
140  unsigned long maxsize_scopedpdu;
141 #endif
142  unsigned long errstat; // Error status
143  unsigned long errindex; // Error index
144 
145  // Trap information
146  oid *enterprise; // System OID
148  ipaddr agent_addr; // address of object generating trap
149  int trap_type; // trap type
150  int specific_type; // specific type
151  unsigned long time; // Uptime
152 
153  // vb list
155 };
156 
157 // vb list
159  struct variable_list *next_variable; // NULL for last variable
160  oid *name; // Object identifier of variable
161  int name_length; // number of subid's in name
162  unsigned char type; // ASN type of variable
163  union { // value of variable
164  long *integer;
165  unsigned char *string;
167  unsigned char *bitstring;
169  } val;
170  int val_len;
171 };
172 
173 struct counter64 {
174  unsigned long high;
175  unsigned long low;
176 };
177 
178 
179 // prototypes for encoding routines
180 DLLOPT unsigned char *asn_parse_int(unsigned char *data, int *datalength,
181  unsigned char *type,
182  long *intp);
183 
184 
185 inline unsigned char *asn_parse_int(unsigned char *data, int *datalength,
186  unsigned char *type,
187  unsigned long *intp)
188 { return asn_parse_int(data, datalength, type, (long*)intp); }
189 
190 
191 DLLOPT unsigned char *asn_parse_unsigned_int(unsigned char *data,
192  int *datalength,
193  unsigned char *type,
194  unsigned long *intp);
195 inline unsigned char *asn_parse_unsigned_int(unsigned char *data,
196  int *datalength,
197  unsigned char *type,
198  long *intp)
199 { return asn_parse_unsigned_int(data, datalength, type, (unsigned long*)intp); }
200 
201 DLLOPT unsigned char *asn_build_int(unsigned char *data, int *datalength,
202  const unsigned char type,
203  const long *intp);
204 
205 inline unsigned char *asn_build_int(unsigned char *data, int *datalength,
206  const unsigned char type,
207  const unsigned long *intp)
208 { return asn_build_int(data, datalength, type, (const long*)intp); }
209 
210 DLLOPT unsigned char *asn_build_unsigned_int(unsigned char *data,
211  int *datalength,
212  unsigned char type,
213  unsigned long *intp);
214 
215 DLLOPT unsigned char *asn_parse_string(unsigned char *data, int *datalength,
216  unsigned char *type,
217  unsigned char *string,
218  int *strlength);
219 
220 DLLOPT unsigned char *asn_build_string(unsigned char *data, int *datalength,
221  const unsigned char type,
222  const unsigned char *string,
223  const int strlength);
224 
225 DLLOPT unsigned char *asn_parse_header(unsigned char *data, int *datalength,
226  unsigned char *type);
227 
228 DLLOPT unsigned char *asn_build_header(unsigned char *data, int *datalength,
229  unsigned char type, int length);
230 
231 DLLOPT unsigned char *asn_build_sequence(unsigned char *data,
232  int *datalength,
233  unsigned char type,
234  int length);
235 
236 DLLOPT unsigned char *asn_parse_length(unsigned char *data,
237  unsigned long *length);
238 
239 DLLOPT unsigned char *asn_build_length(unsigned char *data, int *datalength,
240  int length);
241 
242 DLLOPT unsigned char *asn_parse_objid(unsigned char *data, int *datalength,
243  unsigned char *type,
244  oid *objid, int *objidlength);
245 
246 DLLOPT unsigned char *asn_build_objid(unsigned char *data, int *datalength,
247  unsigned char type,
248  oid *objid, int objidlength);
249 
250 DLLOPT void asn_build_subid(unsigned long subid, unsigned char*& bp);
251 
252 DLLOPT unsigned char *asn_parse_null(unsigned char *data, int *datalength,
253  unsigned char *type);
254 
255 DLLOPT unsigned char *asn_build_null(unsigned char *data,int *datalength,
256  unsigned char type);
257 
258 DLLOPT unsigned char *asn_parse_bitstring(unsigned char *data, int *datalength,
259  unsigned char *type,
260  unsigned char *string,
261  int *strlength);
262 
263 DLLOPT unsigned char *asn_build_bitstring(unsigned char *data, int *datalength,
264  unsigned char type,
265  unsigned char *string,
266  int strlength);
267 
268 DLLOPT unsigned char *asn_parse_unsigned_int64(unsigned char *data,
269  int *datalength,
270  unsigned char *type,
271  struct counter64 *cp);
272 
273 DLLOPT unsigned char *asn_build_unsigned_int64(unsigned char *data,
274  int *datalength,
275  unsigned char type,
276  struct counter64 *cp);
277 
279 
280 DLLOPT void snmp_free_pdu(struct snmp_pdu *pdu);
281 
282 DLLOPT int snmp_build(struct snmp_pdu *pdu,
283  unsigned char *packet,
284  int *out_length,
285  const long version,
286  const unsigned char* community, const int community_len);
287 
288 DLLOPT void snmp_add_var(struct snmp_pdu *pdu,
289  oid *name, int name_length,
290  SmiVALUE *smival);
291 
292 DLLOPT int snmp_parse(struct snmp_pdu *pdu,
293  unsigned char *data, int data_length,
294  unsigned char *community_name, int &community_len,
295  snmp_version &version);
296 
297 DLLOPT unsigned char *build_vb(struct snmp_pdu *pdu,
298  unsigned char *buf, int *buf_len);
299 
300 DLLOPT unsigned char *build_data_pdu(struct snmp_pdu *pdu,
301  unsigned char *buf, int *buf_len,
302  unsigned char *vb_buf, int vb_buf_len);
303 
304 DLLOPT unsigned char *snmp_build_var_op(unsigned char *data,
305  oid * var_name, int *var_name_len,
306  unsigned char var_val_type,
307  int var_val_len,
308  unsigned char *var_val,
309  int *listlength);
310 
311 DLLOPT unsigned char *snmp_parse_var_op(unsigned char *data,
312  oid *var_name, int *var_name_len,
313  unsigned char *var_val_type,
314  int *var_val_len,
315  unsigned char **var_val,
316  int *listlength);
317 
318 DLLOPT int snmp_parse_data_pdu(struct snmp_pdu *pdu,
319  unsigned char *&data, int &length);
320 
321 DLLOPT int snmp_parse_vb(struct snmp_pdu *pdu,
322  unsigned char *&data, int &data_len);
323 
324 DLLOPT void clear_pdu(struct snmp_pdu *pdu, bool clear_all = false);
325 
326 /**
327  * Encode the given values for the HeaderData into the buffer.
328  * <pre>
329  * HeaderData ::= SEQUENCE {
330  * msgID INTEGER (0..2147483647),
331  * msgMaxSize INTEGER (484..2147483647),
332  * msgFlags OCTET STRING (SIZE(1)),
333  * msgSecurityModel INTEGER (0..2147483647)
334  * }
335  * </pre>
336  * @param outBuf - The buffer
337  * @param maxLength - IN: length of the buffer
338  * OUT: free bytes left in the buffer
339  * @param msgID - The message ID
340  * @param maxMessageSize - The maximum size of a SNMPv3 message
341  * @param msgFlags - The message Flags
342  * @param securityModel - The security model
343  *
344  * @return - Pointer to the first free byte in the buffer or
345  * NULL if an error occured
346  */
347 DLLOPT unsigned char *asn1_build_header_data(unsigned char *outBuf,
348  int *maxLength,
349  long msgID,
350  long maxMessageSize,
351  unsigned char msgFlags,
352  long securityModel);
353 
354 /**
355  * Parse the filled HeaderData of a SNMPv3 message and return
356  * the encoded values.
357  * <pre>
358  * HeaderData ::= SEQUENCE {
359  * msgID INTEGER (0..2147483647),
360  * msgMaxSize INTEGER (484..2147483647),
361  * msgFlags OCTET STRING (SIZE(1)),
362  * msgSecurityModel INTEGER (0..2147483647)
363  * }
364  * </pre>
365  *
366  * @param buf - The buffer to parse
367  * @param buf_len - IN: The length of the buffer
368  * OUT: The number of bytes after this object
369  * int the buffer
370  * @param msg_id - OUT: The message id
371  * @param msg_max_size - OUT: THe maximum message size of the sender
372  * @param msg_flags - OUT: The message flags
373  * @param msg_security_model - OUT: The security model used to build this
374  * message
375  *
376  * @return - Returns a pointer to the first byte past the end of
377  * the object HeaderData (i.e. the start of the next object).
378  * Returns NULL on any error.
379  */
380 DLLOPT unsigned char *asn1_parse_header_data(unsigned char *buf, int *buf_len,
381  long *msg_id, long *msg_max_size,
382  unsigned char *msg_flags,
383  long *msg_security_model);
384 
385 /**
386  * Parse the ScopedPDU and return the encoded values.
387  * <pre>
388  * ScopedPDU ::= SEQUENCE {
389  * contextEngineID OCTET STRING,
390  * contextName OCTET STRING,
391  * data ANY -- e.g., PDUs as defined in RFC 1905
392  * }
393  * </pre>
394  *
395  * @param scoped_pdu - The buffer to parse
396  * @param scoped_pdu_len - IN: The length of the buffer
397  * OUT: The number of bytes after this object
398  * int the buffer
399  * @param context_engine_id - OUT: The parsed contextEngineID
400  * @param context_engine_id_len - OUT: The length of the contextEngineID
401  * @param context_name - OUT: The parsed contextName
402  * @param context_name_len - OUT: The length of the contextName
403  *
404  * @return - Pointer to the data object of the scopedPDU or
405  * NULL on any error.
406  */
407 DLLOPT unsigned char *asn1_parse_scoped_pdu(
408  unsigned char *scoped_pdu, int *scoped_pdu_len,
409  unsigned char *context_engine_id, int *context_engine_id_len,
410  unsigned char *context_name, int *context_name_len );
411 
412 /**
413  * Encode the given values for the scopedPDU into the buffer.
414  * <pre>
415  * ScopedPDU ::= SEQUENCE {
416  * contextEngineID OCTET STRING
417  * contextName OCTET STRING
418  * data ANY -- PDU
419  * }
420  * </pre>
421  * param outBuf - The buffer
422  * param max_len - IN: length of the buffer
423  * OUT: free bytes left in the buffer
424  * param contextEngineID - The contextEngineID
425  * param contextEngineIDLength - The length of the contextEngineID
426  * param contextName - The contextName
427  * param contextNameLength - The length of the contextName
428  * param data - The already encoded data
429  * param dataLength - The length of the data
430  *
431  * @return - Pointer to the first free byte in the buffer or
432  * NULL if an error occured
433  */
434 DLLOPT unsigned char *asn1_build_scoped_pdu(
435  unsigned char *outBuf, int *max_len,
436  unsigned char *contextEngineID, long contextEngineIDLength,
437  unsigned char *contextName, long contextNameLength,
438  unsigned char *data, long dataLength);
439 
440 
441 #ifdef SNMP_PP_NAMESPACE
442 } // end of namespace Snmp_pp
443 #endif
444 
445 #endif // _SNMP_ASN1_H_
DLLOPT unsigned char * asn_build_null(unsigned char *data, int *datalength, unsigned char type)
DLLOPT unsigned char * asn_build_unsigned_int64(unsigned char *data, int *datalength, unsigned char type, struct counter64 *cp)
unsigned long low
Definition: asn1.h:175
DLLOPT unsigned char * build_data_pdu(struct snmp_pdu *pdu, unsigned char *buf, int *buf_len, unsigned char *vb_buf, int vb_buf_len)
int trap_type
Definition: asn1.h:149
DLLOPT unsigned char * asn_build_unsigned_int(unsigned char *data, int *datalength, unsigned char type, unsigned long *intp)
oid * objid
Definition: asn1.h:166
DLLOPT struct snmp_pdu * snmp_pdu_create(int command)
oid * enterprise
Definition: asn1.h:146
DLLOPT unsigned char * asn1_parse_scoped_pdu(unsigned char *scoped_pdu, int *scoped_pdu_len, unsigned char *context_engine_id, int *context_engine_id_len, unsigned char *context_name, int *context_name_len)
Parse the ScopedPDU and return the encoded values.
DLLOPT unsigned char * asn_build_header(unsigned char *data, int *datalength, unsigned char type, int length)
int enterprise_length
Definition: asn1.h:147
#define DLLOPT
DLLOPT unsigned char * asn_parse_unsigned_int64(unsigned char *data, int *datalength, unsigned char *type, struct counter64 *cp)
DLLOPT unsigned char * asn_build_length(unsigned char *data, int *datalength, int length)
DLLOPT unsigned char * asn_parse_unsigned_int(unsigned char *data, int *datalength, unsigned char *type, unsigned long *intp)
DLLOPT unsigned char * asn_parse_null(unsigned char *data, int *datalength, unsigned char *type)
unsigned long high
Definition: asn1.h:174
ipaddr agent_addr
Definition: asn1.h:148
DLLOPT void asn_build_subid(unsigned long subid, unsigned char *&bp)
snmp_version
The SNMP version to use is passed with this enum.
Definition: target.h:76
DLLOPT unsigned char * snmp_build_var_op(unsigned char *data, oid *var_name, int *var_name_len, unsigned char var_val_type, int var_val_len, unsigned char *var_val, int *listlength)
DLLOPT int snmp_parse_vb(struct snmp_pdu *pdu, unsigned char *&data, int &data_len)
DLLOPT unsigned char * build_vb(struct snmp_pdu *pdu, unsigned char *buf, int *buf_len)
DLLOPT unsigned char * asn_parse_bitstring(unsigned char *data, int *datalength, unsigned char *type, unsigned char *string, int *strlength)
DLLOPT unsigned char * asn_build_int(unsigned char *data, int *datalength, const unsigned char type, const long *intp)
DLLOPT unsigned char * asn_build_bitstring(unsigned char *data, int *datalength, unsigned char type, unsigned char *string, int strlength)
DLLOPT void clear_pdu(struct snmp_pdu *pdu, bool clear_all=false)
DLLOPT unsigned char * asn_build_sequence(unsigned char *data, int *datalength, unsigned char type, int length)
DLLOPT unsigned char * asn_parse_length(unsigned char *data, unsigned long *length)
unsigned char type
Definition: asn1.h:162
DLLOPT unsigned char * asn_build_string(unsigned char *data, int *datalength, const unsigned char type, const unsigned char *string, const int strlength)
unsigned long time
Definition: asn1.h:151
struct variable_list * variables
Definition: asn1.h:154
int val_len
Definition: asn1.h:170
unsigned char * string
Definition: asn1.h:165
struct counter64 * counter64
Definition: asn1.h:168
DLLOPT unsigned char * asn_parse_int(unsigned char *data, int *datalength, unsigned char *type, long *intp)
DLLOPT unsigned char * asn_parse_string(unsigned char *data, int *datalength, unsigned char *type, unsigned char *string, int *strlength)
struct variable_list * next_variable
Definition: asn1.h:159
unsigned long maxsize_scopedpdu
Definition: asn1.h:140
int name_length
Definition: asn1.h:161
int specific_type
Definition: asn1.h:150
DLLOPT void snmp_free_pdu(struct snmp_pdu *pdu)
DLLOPT unsigned char * asn1_build_header_data(unsigned char *outBuf, int *maxLength, long msgID, long maxMessageSize, unsigned char msgFlags, long securityModel)
Encode the given values for the HeaderData into the buffer.
unsigned long reqid
Definition: asn1.h:137
DLLOPT unsigned char * asn_parse_header(unsigned char *data, int *datalength, unsigned char *type)
unsigned char * bitstring
Definition: asn1.h:167
DLLOPT void snmp_add_var(struct snmp_pdu *pdu, oid *name, int name_length, SmiVALUE *smival)
Definition: asn1.h:135
unsigned long errindex
Definition: asn1.h:143
DLLOPT int snmp_parse(struct snmp_pdu *pdu, unsigned char *data, int data_length, unsigned char *community_name, int &community_len, snmp_version &version)
oid * name
Definition: asn1.h:160
struct sockaddr_in ipaddr
Definition: asn1.h:132
unsigned long msgid
Definition: asn1.h:139
DLLOPT unsigned char * asn1_parse_header_data(unsigned char *buf, int *buf_len, long *msg_id, long *msg_max_size, unsigned char *msg_flags, long *msg_security_model)
Parse the filled HeaderData of a SNMPv3 message and return the encoded values.
DLLOPT int snmp_build(struct snmp_pdu *pdu, unsigned char *packet, int *out_length, const long version, const unsigned char *community, const int community_len)
DLLOPT unsigned char * asn1_build_scoped_pdu(unsigned char *outBuf, int *max_len, unsigned char *contextEngineID, long contextEngineIDLength, unsigned char *contextName, long contextNameLength, unsigned char *data, long dataLength)
Encode the given values for the scopedPDU into the buffer.
unsigned long errstat
Definition: asn1.h:142
int command
Definition: asn1.h:136
DLLOPT unsigned char * snmp_parse_var_op(unsigned char *data, oid *var_name, int *var_name_len, unsigned char *var_val_type, int *var_val_len, unsigned char **var_val, int *listlength)
DLLOPT unsigned char * asn_parse_objid(unsigned char *data, int *datalength, unsigned char *type, oid *objid, int *objidlength)
DLLOPT int snmp_parse_data_pdu(struct snmp_pdu *pdu, unsigned char *&data, int &length)
unsigned long oid
Definition: asn1.h:41
DLLOPT unsigned char * asn_build_objid(unsigned char *data, int *datalength, unsigned char type, oid *objid, int objidlength)
long * integer
Definition: asn1.h:164