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