1 /*_############################################################################
6 _## -----------------------------------------------
7 _## Copyright (c) 2001-2010 Jochen Katz, Frank Fock
9 _## This software is based on SNMP++2.6 from Hewlett Packard:
11 _## Copyright (c) 1996
12 _## Hewlett-Packard Company
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.
26 _## Stuttgart, Germany, Thu Sep 2 00:07:47 CEST 2010
28 _##########################################################################*/
29 // $Id: asn1.h 1554 2009-06-28 19:50:17Z katz $
34 #include "snmp_pp/config_snmp_pp.h"
35 #include "snmp_pp/target.h"
37 #ifdef SNMP_PP_NAMESPACE
41 #ifndef EIGHTBIT_SUBIDS
42 typedef unsigned long oid;
43 #define MAX_SUBID 0xFFFFFFFF
45 typedef unsigned char oid;
46 #define MAX_SUBID 0xFF
49 #define MAX_OID_LEN 128 /* max subid's in an oid */
52 #define ASN_BOOLEAN (0x01)
54 #define ASN_INTEGER (0x02)
56 #define ASN_BIT_STR (0x03)
57 #define ASN_OCTET_STR (0x04)
59 #define ASN_NULL (0x05)
61 #define ASN_OBJECT_ID (0x06)
63 #define ASN_SEQUENCE (0x10)
65 #define ASN_SET (0x11)
67 #define ASN_UNIVERSAL (0x00)
69 #ifndef ASN_APPLICATION
70 #define ASN_APPLICATION (0x40)
73 #define ASN_CONTEXT (0x80)
76 #define ASN_PRIVATE (0xC0)
79 #define ASN_PRIMITIVE (0x00)
81 #ifndef ASN_CONSTRUCTOR
82 #define ASN_CONSTRUCTOR (0x20)
84 #define ASN_LONG_LEN (0x80)
85 #define ASN_EXTENSION_ID (0x1F)
86 #define ASN_BIT8 (0x80)
88 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
89 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
91 #define ASN_UNI_PRIM (ASN_UNIVERSAL | ASN_PRIMITIVE)
92 #define ASN_SEQ_CON (ASN_SEQUENCE | ASN_CONSTRUCTOR)
94 #define ASN_MAX_NAME_LEN 128
95 #define SNMP_VERSION_1 0
96 #define SNMP_VERSION_2C 1
97 #define SNMP_VERSION_2STERN 2
98 #define SNMP_VERSION_3 3
100 // defined types (from the SMI, RFC 1065)
101 #define SMI_IPADDRESS (ASN_APPLICATION | 0)
102 #define SMI_COUNTER (ASN_APPLICATION | 1)
103 #define SMI_GAUGE (ASN_APPLICATION | 2)
104 #define SMI_TIMETICKS (ASN_APPLICATION | 3)
105 #define SMI_OPAQUE (ASN_APPLICATION | 4)
106 #define SMI_NSAP (ASN_APPLICATION | 5)
107 #define SMI_COUNTER64 (ASN_APPLICATION | 6)
108 #define SMI_UINTEGER (ASN_APPLICATION | 7)
110 #define GET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
111 #define GETNEXT_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
112 #define GET_RSP_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
113 #define SET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
114 #define TRP_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
116 #define GETBULK_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
117 #define INFORM_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
118 #define TRP2_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
119 #define REPORT_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
121 #define SNMP_NOSUCHOBJECT (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
122 #define SNMP_NOSUCHINSTANCE (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
123 #define SNMP_ENDOFMIBVIEW (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
127 #define ASNERROR(string) debugprintf(3, "ASN parse error (%s)\n", string )
129 #define ASNERROR(string)
133 typedef struct sockaddr_in ipaddr;
137 int command; // pdu type
138 unsigned long reqid; // Request id
141 unsigned long maxsize_scopedpdu;
143 unsigned long errstat; // Error status
144 unsigned long errindex; // Error index
147 oid *enterprise; // System OID
148 int enterprise_length;
149 ipaddr agent_addr; // address of object generating trap
150 int trap_type; // trap type
151 int specific_type; // specific type
152 unsigned long time; // Uptime
155 struct variable_list *variables;
159 struct variable_list {
160 struct variable_list *next_variable; // NULL for last variable
161 oid *name; // Object identifier of variable
162 int name_length; // number of subid's in name
163 unsigned char type; // ASN type of variable
164 union { // value of variable
166 unsigned char *string;
168 unsigned char *bitstring;
169 struct counter64 *counter64;
180 // prototypes for encoding routines
181 DLLOPT unsigned char *asn_parse_int(unsigned char *data, int *datalength,
186 inline unsigned char *asn_parse_int(unsigned char *data, int *datalength,
189 { return asn_parse_int(data, datalength, type, (long*)intp); }
192 DLLOPT unsigned char *asn_parse_unsigned_int(unsigned char *data,
195 unsigned long *intp);
196 inline unsigned char *asn_parse_unsigned_int(unsigned char *data,
200 { return asn_parse_unsigned_int(data, datalength, type, (unsigned long*)intp); }
202 DLLOPT unsigned char *asn_build_int(unsigned char *data, int *datalength,
203 const unsigned char type,
206 inline unsigned char *asn_build_int(unsigned char *data, int *datalength,
207 const unsigned char type,
208 const unsigned long *intp)
209 { return asn_build_int(data, datalength, type, (const long*)intp); }
211 DLLOPT unsigned char *asn_build_unsigned_int(unsigned char *data,
214 unsigned long *intp);
216 DLLOPT unsigned char *asn_parse_string(unsigned char *data, int *datalength,
218 unsigned char *string,
221 DLLOPT unsigned char *asn_build_string(unsigned char *data, int *datalength,
222 const unsigned char type,
223 const unsigned char *string,
224 const int strlength);
226 DLLOPT unsigned char *asn_parse_header(unsigned char *data, int *datalength,
227 unsigned char *type);
229 DLLOPT unsigned char *asn_build_header(unsigned char *data, int *datalength,
230 unsigned char type, int length);
232 DLLOPT unsigned char *asn_build_sequence(unsigned char *data,
237 DLLOPT unsigned char *asn_parse_length(unsigned char *data,
238 unsigned long *length);
240 DLLOPT unsigned char *asn_build_length(unsigned char *data, int *datalength,
243 DLLOPT unsigned char *asn_parse_objid(unsigned char *data, int *datalength,
245 oid *objid, int *objidlength);
247 DLLOPT unsigned char *asn_build_objid(unsigned char *data, int *datalength,
249 oid *objid, int objidlength);
251 DLLOPT unsigned char *asn_parse_null(unsigned char *data, int *datalength,
252 unsigned char *type);
254 DLLOPT unsigned char *asn_build_null(unsigned char *data,int *datalength,
257 DLLOPT unsigned char *asn_parse_bitstring(unsigned char *data, int *datalength,
259 unsigned char *string,
262 DLLOPT unsigned char *asn_build_bitstring(unsigned char *data, int *datalength,
264 unsigned char *string,
267 DLLOPT unsigned char *asn_parse_unsigned_int64(unsigned char *data,
270 struct counter64 *cp);
272 DLLOPT unsigned char *asn_build_unsigned_int64(unsigned char *data,
275 struct counter64 *cp);
277 DLLOPT struct snmp_pdu *snmp_pdu_create(int command);
279 DLLOPT void snmp_free_pdu(struct snmp_pdu *pdu);
281 DLLOPT int snmp_build(struct snmp_pdu *pdu,
282 unsigned char *packet,
285 const unsigned char* community, const int community_len);
287 DLLOPT void snmp_add_var(struct snmp_pdu *pdu,
288 oid *name, int name_length,
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);
296 DLLOPT unsigned char *build_vb(struct snmp_pdu *pdu,
297 unsigned char *buf, int *buf_len);
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);
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,
307 unsigned char *var_val,
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,
314 unsigned char **var_val,
317 DLLOPT int snmp_parse_data_pdu(struct snmp_pdu *pdu,
318 unsigned char *&data, int &length);
320 DLLOPT int snmp_parse_vb(struct snmp_pdu *pdu,
321 unsigned char *&data, int &data_len);
323 DLLOPT void clear_pdu(struct snmp_pdu *pdu, bool clear_all = false);
326 * Encode the given values for the HeaderData into the buffer.
328 * HeaderData ::= SEQUENCE {
329 * msgID INTEGER (0..2147483647),
330 * msgMaxSize INTEGER (484..2147483647),
331 * msgFlags OCTET STRING (SIZE(1)),
332 * msgSecurityModel INTEGER (0..2147483647)
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
343 * @return - Pointer to the first free byte in the buffer or
344 * NULL if an error occured
346 DLLOPT unsigned char *asn1_build_header_data(unsigned char *outBuf,
350 unsigned char msgFlags,
354 * Parse the filled HeaderData of a SNMPv3 message and return
355 * the encoded values.
357 * HeaderData ::= SEQUENCE {
358 * msgID INTEGER (0..2147483647),
359 * msgMaxSize INTEGER (484..2147483647),
360 * msgFlags OCTET STRING (SIZE(1)),
361 * msgSecurityModel INTEGER (0..2147483647)
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
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
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.
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);
385 * Parse the ScopedPDU and return the encoded values.
387 * ScopedPDU ::= SEQUENCE {
388 * contextEngineID OCTET STRING,
389 * contextName OCTET STRING,
390 * data ANY -- e.g., PDUs as defined in RFC 1905
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
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
403 * @return - Pointer to the data object of the scopedPDU or
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 );
412 * Encode the given values for the scopedPDU into the buffer.
414 * ScopedPDU ::= SEQUENCE {
415 * contextEngineID OCTET STRING
416 * contextName OCTET STRING
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
430 * @return - Pointer to the first free byte in the buffer or
431 * NULL if an error occured
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);
440 #ifdef SNMP_PP_NAMESPACE
441 } // end of namespace Snmp_pp