Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

asn1.h

Go to the documentation of this file.
00001 /*_############################################################################
00002   _## 
00003   _##  asn1.h  
00004   _##
00005   _##  SNMP++v3.2.15
00006   _##  -----------------------------------------------
00007   _##  Copyright (c) 2001-2004 Jochen Katz, Frank Fock
00008   _##
00009   _##  This software is based on SNMP++2.6 from Hewlett Packard:
00010   _##  
00011   _##    Copyright (c) 1996
00012   _##    Hewlett-Packard Company
00013   _##  
00014   _##  ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
00015   _##  Permission to use, copy, modify, distribute and/or sell this software 
00016   _##  and/or its documentation is hereby granted without fee. User agrees 
00017   _##  to display the above copyright notice and this license notice in all 
00018   _##  copies of the software and any documentation of the software. User 
00019   _##  agrees to assume all liability for the use of the software; 
00020   _##  Hewlett-Packard and Jochen Katz make no representations about the 
00021   _##  suitability of this software for any purpose. It is provided 
00022   _##  "AS-IS" without warranty of any kind, either express or implied. User 
00023   _##  hereby grants a royalty-free license to any and all derivatives based
00024   _##  upon this software code base. 
00025   _##  
00026   _##  Stuttgart, Germany, Tue Jan  4 21:42:42 CET 2005 
00027   _##  
00028   _##########################################################################*/
00029 // $Id: asn1.h,v 1.7 2004/03/28 21:47:15 katz Exp $
00030 
00031 #ifndef _ASN1
00032 #define _ASN1
00033 
00034 #ifdef WIN32
00035 #ifndef __unix
00036 #include <winsock.h>
00037 #endif
00038 #endif
00039 
00040 #include "snmp_pp/target.h"
00041 
00042 #ifdef SNMP_PP_NAMESPACE
00043 namespace Snmp_pp {
00044 #endif
00045 
00046 #define MAXLENGTH_BUFFER         SNMP_MSG_LENGTH
00047 #ifndef EIGHTBIT_SUBIDS
00048 typedef unsigned long oid;
00049 #define MAX_SUBID   0xFFFFFFFF
00050 #else
00051 typedef unsigned char oid;
00052 #define MAX_SUBID   0xFF
00053 #endif
00054 
00055 #define MAX_OID_LEN      128 /* max subid's in an oid */
00056 
00057 // asn.1 values
00058 #define ASN_BOOLEAN      (0x01)
00059 #ifndef ASN_INTEGER
00060 #define ASN_INTEGER      (0x02)
00061 #endif
00062 #define ASN_BIT_STR      (0x03)
00063 #define ASN_OCTET_STR    (0x04)
00064 #ifndef ASN_NULL
00065 #define ASN_NULL         (0x05)
00066 #endif
00067 #define ASN_OBJECT_ID    (0x06)
00068 #ifndef ASN_SEQUENCE
00069 #define ASN_SEQUENCE     (0x10)
00070 #endif
00071 #define ASN_SET          (0x11)
00072 #ifndef ASN_UNIVERSAL
00073 #define ASN_UNIVERSAL    (0x00)
00074 #endif
00075 #ifndef ASN_APPLICATION
00076 #define ASN_APPLICATION  (0x40)
00077 #endif
00078 #ifndef ASN_CONTEXT
00079 #define ASN_CONTEXT      (0x80)
00080 #endif
00081 #ifndef ASN_PRIVATE
00082 #define ASN_PRIVATE      (0xC0)
00083 #endif
00084 #ifndef ASN_PRIMITIVE
00085 #define ASN_PRIMITIVE    (0x00)
00086 #endif
00087 #ifndef ASN_CONSTRUCTOR
00088 #define ASN_CONSTRUCTOR  (0x20)
00089 #endif
00090 #define ASN_LONG_LEN     (0x80)
00091 #define ASN_EXTENSION_ID (0x1F)
00092 #define ASN_BIT8         (0x80)
00093 
00094 #define IS_CONSTRUCTOR(byte)  ((byte) & ASN_CONSTRUCTOR)
00095 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
00096 
00097 #define ASN_MAX_NAME_LEN   128
00098 #define SNMP_VERSION_1      0
00099 #define SNMP_VERSION_2C     1
00100 #define SNMP_VERSION_2STERN 2
00101 #define SNMP_VERSION_3      3
00102 
00103 // defined types (from the SMI, RFC 1065)
00104 #define SMI_IPADDRESS       (ASN_APPLICATION | 0)
00105 #define SMI_COUNTER         (ASN_APPLICATION | 1)
00106 #define SMI_GAUGE           (ASN_APPLICATION | 2)
00107 #define SMI_TIMETICKS       (ASN_APPLICATION | 3)
00108 #define SMI_OPAQUE          (ASN_APPLICATION | 4)
00109 #define SMI_NSAP            (ASN_APPLICATION | 5)
00110 #define SMI_COUNTER64       (ASN_APPLICATION | 6)
00111 #define SMI_UINTEGER        (ASN_APPLICATION | 7)
00112 
00113 #define GET_REQ_MSG         (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
00114 #define GETNEXT_REQ_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
00115 #define GET_RSP_MSG         (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
00116 #define SET_REQ_MSG         (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
00117 #define TRP_REQ_MSG         (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
00118 
00119 #define GETBULK_REQ_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
00120 #define INFORM_REQ_MSG      (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
00121 #define TRP2_REQ_MSG        (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
00122 #define REPORT_MSG          (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
00123 
00124 #define SNMP_NOSUCHOBJECT   (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
00125 #define SNMP_NOSUCHINSTANCE (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
00126 #define SNMP_ENDOFMIBVIEW   (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
00127 
00128 #define SNMP_MSG_LENGTH MAX_SNMP_PACKET
00129 
00130 #ifdef _DEBUG
00131 #define ASNERROR( string)    debugprintf(3, "ASN parse error (%s)\n", string )
00132 #else
00133 #define ASNERROR( string)
00134 #endif
00135 
00136 
00137 typedef struct sockaddr_in  ipaddr;
00138 
00139 // pdu
00140 struct snmp_pdu {
00141     int        command;      // pdu type
00142     unsigned long  reqid;    // Request id
00143 #ifdef _SNMPv3
00144     unsigned long  msgid;
00145     unsigned long  maxsize_scopedpdu;
00146 #endif
00147     unsigned long  errstat;  // Error status
00148     unsigned long  errindex; // Error index
00149 
00150     // Trap information
00151     oid        *enterprise;   // System OID
00152     int        enterprise_length;
00153     ipaddr  agent_addr;       // address of object generating trap
00154     int        trap_type;     // trap type
00155     int        specific_type; // specific type
00156     unsigned long  time;      // Uptime
00157 
00158     // vb list
00159     struct variable_list *variables;
00160 };
00161 
00162 // vb list
00163 struct variable_list {
00164     struct variable_list *next_variable;    // NULL for last variable
00165     oid        *name;                       // Object identifier of variable
00166     int        name_length;                 // number of subid's in name
00167     unsigned char   type;                   // ASN type of variable
00168     union {                                 // value of variable
00169     long    *integer;
00170     unsigned char     *string;
00171     oid    *objid;
00172     unsigned char   *bitstring;
00173     struct counter64 *counter64;
00174     } val;
00175     int        val_len;
00176 };
00177 
00178 struct counter64 {
00179     unsigned long high;
00180     unsigned long low;
00181 };
00182 
00183 
00184 // prototypes for encoding routines
00185 DLLOPT unsigned char *asn_parse_int( unsigned char *data, int *datalength,
00186                                      unsigned char *type,
00187                                      long int *intp, int intsize);
00188 
00189 
00190 DLLOPT unsigned char *asn_parse_unsigned_int( unsigned char *data,        
00191                                               int *datalength,
00192                                               unsigned char *type,
00193                                               unsigned long *intp,
00194                                               int intsize);
00195 
00196 DLLOPT unsigned char *asn_build_int(unsigned char *data, int *datalength,
00197                                     const unsigned char type,
00198                                     const long *intp, int intsize);
00199 
00200 DLLOPT unsigned char *asn_build_unsigned_int( unsigned char *data,
00201                                               int *datalength,
00202                                               unsigned char type,
00203                                               unsigned long *intp,
00204                                               int intsize);
00205 
00206 DLLOPT unsigned char *asn_parse_string( unsigned char *data, int *datalength,
00207                                         unsigned char *type,
00208                                         unsigned char *string,
00209                                         int *strlength);
00210 
00211 DLLOPT unsigned char *asn_build_string( unsigned char *data, int *datalength,
00212                                         const unsigned char type,
00213                                         const unsigned char *string,
00214                                         const int strlength);
00215 
00216 DLLOPT unsigned char *asn_parse_header( unsigned char *data, int *datalength,
00217                                         unsigned char *type);
00218 
00219 DLLOPT unsigned char *asn_build_header( unsigned char *data, int *datalength,
00220                                         unsigned char type,
00221                                         int length);
00222 
00223 DLLOPT unsigned char *asn_build_sequence( unsigned char *data,
00224                                           int *datalength,
00225                                           unsigned char type,
00226                                           int length);
00227 
00228 DLLOPT unsigned char *asn_parse_length( unsigned char *data,
00229                                         unsigned long *length);
00230 
00231 DLLOPT unsigned char *asn_build_length( unsigned char *data, int *datalength,
00232                                         int length);
00233 
00234 DLLOPT unsigned char *asn_parse_objid( unsigned char *data, int *datalength,
00235                                        unsigned char *type,
00236                                        oid *objid,
00237                                        int *objidlength);
00238 
00239 DLLOPT unsigned char *asn_build_objid( unsigned char *data,
00240                                        int *datalength,
00241                                        unsigned char type,
00242                                        oid *objid,
00243                                        int objidlength);
00244 
00245 DLLOPT unsigned char *asn_parse_null(unsigned char *data, int *datalength,
00246                                      unsigned char *type);
00247 
00248 DLLOPT unsigned char *asn_build_null( unsigned char *data,int *datalength,
00249                                       unsigned char type);
00250 
00251 DLLOPT unsigned char *asn_parse_bitstring( unsigned char *data,
00252                                            int *datalength,
00253                                            unsigned char *type,
00254                                            unsigned char *string,
00255                                            int *strlength);
00256 
00257 DLLOPT unsigned char *asn_build_bitstring( unsigned char *data,
00258                                            int *datalength,
00259                                            unsigned char type,
00260                                            unsigned char *string,
00261                                            int strlength);
00262 
00263 DLLOPT unsigned char *asn_parse_unsigned_int64( unsigned char *data,
00264                                                 int *datalength,
00265                                                 unsigned char *type,
00266                                                 struct counter64 *cp,
00267                                                 int countersize);
00268 
00269 DLLOPT unsigned char *asn_build_unsigned_int64( unsigned char *data,
00270                                                 int *datalength,
00271                                                 unsigned char        type,
00272                                                 struct counter64 *cp,
00273                                                 int countersize);
00274 
00275 DLLOPT struct snmp_pdu *snmp_pdu_create( int command);
00276 
00277 DLLOPT void snmp_free_pdu( struct snmp_pdu *pdu);
00278 
00279 DLLOPT int snmp_build(struct snmp_pdu *pdu,
00280                       unsigned char *packet,
00281                       int *out_length,
00282                       const long version,
00283                       const unsigned char* community,
00284                       const int community_len);
00285 
00286 DLLOPT void snmp_add_var(struct snmp_pdu *pdu,
00287                          oid *name, int name_length,
00288                          SmiVALUE *smival);
00289 
00290 DLLOPT int snmp_parse(struct snmp_pdu *pdu,
00291                       unsigned char *data,
00292                       int data_length,
00293                       unsigned char *community_name,
00294                       int &community_len,
00295                       snmp_version &version);
00296 
00297 DLLOPT unsigned char *build_vb(struct snmp_pdu *pdu,
00298                                unsigned char *buf, int *buf_len);
00299 
00300 DLLOPT unsigned char *build_data_pdu(struct snmp_pdu *pdu,
00301                                      unsigned char *buf, int *buf_len,
00302                                      unsigned char *vb_buf, int vb_buf_len);
00303 
00304 DLLOPT unsigned char *snmp_build_var_op(unsigned char *data,
00305                                         oid * var_name,
00306                                         int *var_name_len,
00307                                         unsigned char var_val_type,
00308                                         int var_val_len,
00309                                         unsigned char *var_val,
00310                                         int *listlength);
00311 
00312 DLLOPT unsigned char *snmp_parse_var_op( unsigned char *data,
00313                                          oid *var_name,        
00314                                          int *var_name_len,
00315                                          unsigned char  *var_val_type,
00316                                          int *var_val_len,
00317                                          unsigned char  **var_val,        
00318                                          int *listlength);
00319 
00320 DLLOPT int snmp_parse_data_pdu(struct snmp_pdu *pdu,
00321                                unsigned char *&data, int &length);
00322                                
00323 DLLOPT int snmp_parse_vb(struct snmp_pdu *pdu,
00324                          unsigned char *&data, int &data_len);
00325 
00326 DLLOPT void clear_pdu(struct snmp_pdu *pdu, bool clear_all = false);
00327 
00328 /**
00329  * Encode the given values for the HeaderData into the buffer.
00330  *                                                                  <pre>
00331  *  HeaderData ::= SEQUENCE {
00332  *    msgID      INTEGER (0..2147483647),
00333  *    msgMaxSize INTEGER (484..2147483647),
00334  *    msgFlags   OCTET STRING (SIZE(1)),
00335  *    msgSecurityModel INTEGER (0..2147483647)
00336  *  }
00337  *                                                                 </pre>
00338  * @param outBuf         - The buffer
00339  * @param maxLength      - IN: length of the buffer
00340  *                         OUT: free bytes left in the buffer
00341  * @param msgID          - The message ID
00342  * @param maxMessageSize - The maximum size of a SNMPv3 message
00343  * @param msgFlags       - The message Flags
00344  * @param securityModel  - The security model
00345  *
00346  * @return - Pointer to the first free byte in the buffer or
00347  *           NULL if an error occured
00348  */
00349 DLLOPT unsigned char *asn1_build_header_data(unsigned char *outBuf,
00350                                              int *maxLength,
00351                                              long msgID,
00352                                              long maxMessageSize,
00353                                              unsigned char msgFlags,
00354                                              long securityModel);
00355 
00356 /**
00357  * Parse the filled HeaderData of a SNMPv3 message and return
00358  * the encoded values.
00359  *                                                                  <pre>
00360  *      HeaderData ::= SEQUENCE {
00361  *          msgID      INTEGER (0..2147483647),
00362  *          msgMaxSize INTEGER (484..2147483647),
00363  *          msgFlags   OCTET STRING (SIZE(1)),
00364  *          msgSecurityModel INTEGER (0..2147483647)
00365  *      }
00366  *                                                                 </pre>
00367  *
00368  * @param buf                - The buffer to parse
00369  * @param buf_len            - IN: The length of the buffer
00370  *                             OUT: The number of bytes after this object
00371  *                                  int the buffer
00372  * @param msg_id             - OUT: The message id
00373  * @param msg_max_size       - OUT: THe maximum message size of the sender
00374  * @param msg_flags          - OUT: The message flags
00375  * @param msg_security_model - OUT: The security model used to build this
00376  *                                message
00377  *
00378  * @return -  Returns a pointer to the first byte past the end of
00379  *            the object HeaderData (i.e. the start of the next object).
00380  *            Returns NULL on any error.
00381  */
00382 DLLOPT unsigned char *asn1_parse_header_data(unsigned char *buf, int *buf_len,
00383                                              long *msg_id, long *msg_max_size,
00384                                              unsigned char *msg_flags,
00385                                              long *msg_security_model);
00386 
00387 /**
00388  * Parse the ScopedPDU and return the encoded values.
00389  *                                                                  <pre>
00390  *      ScopedPDU ::= SEQUENCE {
00391  *          contextEngineID  OCTET STRING,
00392  *          contextName      OCTET STRING,
00393  *          data             ANY -- e.g., PDUs as defined in RFC 1905
00394  *      }
00395  *                                                                 </pre>
00396  *
00397  * @param scoped_pdu            - The buffer to parse
00398  * @param scoped_pdu_len        - IN: The length of the buffer
00399  *                                OUT: The number of bytes after this object
00400  *                                     int the buffer
00401  * @param context_engine_id     - OUT: The parsed contextEngineID
00402  * @param context_engine_id_len - OUT: The length of the contextEngineID
00403  * @param context_name          - OUT: The parsed contextName
00404  * @param context_name_len      - OUT: The length of the contextName
00405  *
00406  * @return - Pointer to the data object of the scopedPDU or
00407  *           NULL on any error.
00408  */
00409 DLLOPT unsigned char *asn1_parse_scoped_pdu(
00410          unsigned char *scoped_pdu, int *scoped_pdu_len,
00411          unsigned char *context_engine_id, int *context_engine_id_len,
00412          unsigned char *context_name, int *context_name_len );
00413 
00414 /**
00415  * Encode the given values for the scopedPDU into the buffer.
00416  *                                                                  <pre>
00417  *    ScopedPDU ::= SEQUENCE {
00418  *           contextEngineID OCTET STRING
00419  *           contextName     OCTET STRING
00420  *           data            ANY  -- PDU
00421  *       }
00422  *                                                                 </pre>
00423  * param outBuf            - The buffer
00424  * param max_len           - IN: length of the buffer
00425  *                           OUT: free bytes left in the buffer
00426  * param contextEngineID   - The contextEngineID
00427  * param contextEngineIDLength - The length of the contextEngineID
00428  * param contextName       - The contextName
00429  * param contextNameLength - The length of the contextName
00430  * param data              - The already encoded data
00431  * param dataLength        - The length of the data
00432  *
00433  * @return - Pointer to the first free byte in the buffer or
00434  *           NULL if an error occured
00435  */
00436 DLLOPT unsigned char *asn1_build_scoped_pdu(
00437                unsigned char *outBuf, int *max_len,
00438                unsigned char *contextEngineID, long contextEngineIDLength,
00439                unsigned char *contextName, long contextNameLength,
00440                unsigned char *data, long dataLength);
00441 
00442 
00443 #ifdef SNMP_PP_NAMESPACE
00444 }; // end of namespace Snmp_pp
00445 #endif 
00446 
00447 #endif  // _ASN1
00448 

Generated on Tue Jan 4 22:42:13 2005 for SNMP++ by doxygen 1.3.2