]> git.stg.codes - ssmd.git/blob - 3rdparty/snmp++/include/snmp_pp/asn1.h
Initial adding
[ssmd.git] / 3rdparty / snmp++ / include / snmp_pp / asn1.h
1 /*_############################################################################
2   _## 
3   _##  asn1.h  
4   _##
5   _##  SNMP++v3.2.25
6   _##  -----------------------------------------------
7   _##  Copyright (c) 2001-2010 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   _##  Stuttgart, Germany, Thu Sep  2 00:07:47 CEST 2010 
27   _##  
28   _##########################################################################*/
29 // $Id: asn1.h 1554 2009-06-28 19:50:17Z katz $
30
31 #ifndef _ASN1
32 #define _ASN1
33
34 #include "snmp_pp/config_snmp_pp.h"
35 #include "snmp_pp/target.h"
36
37 #ifdef SNMP_PP_NAMESPACE
38 namespace Snmp_pp {
39 #endif
40
41 #ifndef EIGHTBIT_SUBIDS
42 typedef unsigned long oid;
43 #define MAX_SUBID   0xFFFFFFFF
44 #else
45 typedef unsigned char oid;
46 #define MAX_SUBID   0xFF
47 #endif
48
49 #define MAX_OID_LEN      128 /* max subid's in an oid */
50
51 // asn.1 values
52 #define ASN_BOOLEAN      (0x01)
53 #ifndef ASN_INTEGER
54 #define ASN_INTEGER      (0x02)
55 #endif
56 #define ASN_BIT_STR      (0x03)
57 #define ASN_OCTET_STR    (0x04)
58 #ifndef ASN_NULL
59 #define ASN_NULL         (0x05)
60 #endif
61 #define ASN_OBJECT_ID    (0x06)
62 #ifndef ASN_SEQUENCE
63 #define ASN_SEQUENCE     (0x10)
64 #endif
65 #define ASN_SET          (0x11)
66 #ifndef ASN_UNIVERSAL
67 #define ASN_UNIVERSAL    (0x00)
68 #endif
69 #ifndef ASN_APPLICATION
70 #define ASN_APPLICATION  (0x40)
71 #endif
72 #ifndef ASN_CONTEXT
73 #define ASN_CONTEXT      (0x80)
74 #endif
75 #ifndef ASN_PRIVATE
76 #define ASN_PRIVATE      (0xC0)
77 #endif
78 #ifndef ASN_PRIMITIVE
79 #define ASN_PRIMITIVE    (0x00)
80 #endif
81 #ifndef ASN_CONSTRUCTOR
82 #define ASN_CONSTRUCTOR  (0x20)
83 #endif
84 #define ASN_LONG_LEN     (0x80)
85 #define ASN_EXTENSION_ID (0x1F)
86 #define ASN_BIT8         (0x80)
87
88 #define IS_CONSTRUCTOR(byte)  ((byte) & ASN_CONSTRUCTOR)
89 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
90
91 #define ASN_UNI_PRIM (ASN_UNIVERSAL | ASN_PRIMITIVE)
92 #define ASN_SEQ_CON  (ASN_SEQUENCE | ASN_CONSTRUCTOR)
93
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
99
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)
109
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)
115
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)
120
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)
124
125
126 #ifdef _DEBUG
127 #define ASNERROR(string)    debugprintf(3, "ASN parse error (%s)\n", string )
128 #else
129 #define ASNERROR(string)
130 #endif
131
132
133 typedef struct sockaddr_in  ipaddr;
134
135 // pdu
136 struct snmp_pdu {
137     int        command;      // pdu type
138     unsigned long  reqid;    // Request id
139 #ifdef _SNMPv3
140     unsigned long  msgid;
141     unsigned long  maxsize_scopedpdu;
142 #endif
143     unsigned long  errstat;  // Error status
144     unsigned long  errindex; // Error index
145
146     // Trap information
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
153
154     // vb list
155     struct variable_list *variables;
156 };
157
158 // vb list
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
165     long    *integer;
166     unsigned char     *string;
167     oid    *objid;
168     unsigned char   *bitstring;
169     struct counter64 *counter64;
170     } val;
171     int        val_len;
172 };
173
174 struct counter64 {
175     unsigned long high;
176     unsigned long low;
177 };
178
179
180 // prototypes for encoding routines
181 DLLOPT unsigned char *asn_parse_int(unsigned char *data, int *datalength,
182                                     unsigned char *type,
183                                     long *intp);
184
185
186 inline unsigned char *asn_parse_int(unsigned char *data, int *datalength,
187                                     unsigned char *type,
188                                     unsigned long *intp)
189 { return asn_parse_int(data, datalength, type, (long*)intp); }
190
191
192 DLLOPT unsigned char *asn_parse_unsigned_int(unsigned char *data,        
193                                              int *datalength,
194                                              unsigned char *type,
195                                              unsigned long *intp);
196 inline unsigned char *asn_parse_unsigned_int(unsigned char *data,        
197                                              int *datalength,
198                                              unsigned char *type,
199                                              long *intp)
200 { return asn_parse_unsigned_int(data, datalength, type, (unsigned long*)intp); }
201
202 DLLOPT unsigned char *asn_build_int(unsigned char *data, int *datalength,
203                                     const unsigned char type,
204                                     const long *intp);
205
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); }
210
211 DLLOPT unsigned char *asn_build_unsigned_int(unsigned char *data,
212                                              int *datalength,
213                                              unsigned char type,
214                                              unsigned long *intp);
215
216 DLLOPT unsigned char *asn_parse_string(unsigned char *data, int *datalength,
217                                        unsigned char *type,
218                                        unsigned char *string,
219                                        int *strlength);
220
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);
225
226 DLLOPT unsigned char *asn_parse_header(unsigned char *data, int *datalength,
227                                        unsigned char *type);
228
229 DLLOPT unsigned char *asn_build_header(unsigned char *data, int *datalength,
230                                        unsigned char type, int length);
231
232 DLLOPT unsigned char *asn_build_sequence(unsigned char *data,
233                                          int *datalength,
234                                          unsigned char type,
235                                          int length);
236
237 DLLOPT unsigned char *asn_parse_length(unsigned char *data,
238                                        unsigned long *length);
239
240 DLLOPT unsigned char *asn_build_length(unsigned char *data, int *datalength,
241                                        int length);
242
243 DLLOPT unsigned char *asn_parse_objid(unsigned char *data, int *datalength,
244                                       unsigned char *type,
245                                       oid *objid, int *objidlength);
246
247 DLLOPT unsigned char *asn_build_objid(unsigned char *data, int *datalength,
248                                       unsigned char type,
249                                       oid *objid, int objidlength);
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
277 DLLOPT struct snmp_pdu *snmp_pdu_create(int command);
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