asn1.h
上传用户:czjinwang
上传日期:2007-01-12
资源大小:2484k
文件大小:9k
源码类别:

SNMP编程

开发平台:

Visual C++

  1. #ifndef _ASN1
  2. #define _ASN1
  3. #ifdef WIN32
  4. #ifndef __unix
  5. #include <winsock.h>
  6. #endif
  7. #endif
  8. #include "snmp_pp.h"
  9. #ifndef EIGHTBIT_SUBIDS
  10. typedef unsigned long oid;
  11. #define MAX_SUBID   0xFFFFFFFF
  12. #else
  13. typedef unsigned char oid;
  14. #define MAX_SUBID   0xFF
  15. #endif
  16. #define MAX_OID_LEN     64 /* max subid's in an oid */
  17. // asn.1 values
  18. #define ASN_BOOLEAN      (0x01)
  19. #define ASN_INTEGER      (0x02)
  20. #define ASN_BIT_STR      (0x03)
  21. #define ASN_OCTET_STR  (0x04)
  22. #define ASN_NULL      (0x05)
  23. #define ASN_OBJECT_ID  (0x06)
  24. #define ASN_SEQUENCE  (0x10)
  25. #define ASN_SET      (0x11)
  26. #define ASN_UNIVERSAL  (0x00)
  27. #define ASN_APPLICATION  (0x40)
  28. #define ASN_CONTEXT      (0x80)
  29. #define ASN_PRIVATE      (0xC0)
  30. #define ASN_PRIMITIVE  (0x00)
  31. #define ASN_CONSTRUCTOR  (0x20)
  32. #define ASN_LONG_LEN  (0x80)
  33. #define ASN_EXTENSION_ID (0x1F)
  34. #define ASN_BIT8      (0x80)
  35. #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
  36. #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
  37. #define ASNERROR( string)
  38. #define MAX_NAME_LEN   64 
  39. #define SNMP_VERSION_1 0
  40. #define SNMP_VERSION_2C 1
  41. // defined types (from the SMI, RFC 1065) 
  42. #define SMI_IPADDRESS   (ASN_APPLICATION | 0)
  43. #define SMI_COUNTER     (ASN_APPLICATION | 1)
  44. #define SMI_GAUGE     (ASN_APPLICATION | 2)
  45. #define SMI_TIMETICKS   (ASN_APPLICATION | 3)
  46. #define SMI_OPAQUE     (ASN_APPLICATION | 4)
  47. #define SMI_NSAP        (ASN_APPLICATION | 5)
  48. #define SMI_COUNTER64   (ASN_APPLICATION | 6)
  49. #define SMI_UINTEGER    (ASN_APPLICATION | 7)
  50. #define GET_REQ_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
  51. #define GETNEXT_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
  52. #define GET_RSP_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
  53. #define SET_REQ_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
  54. #define TRP_REQ_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
  55. #define GETBULK_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
  56. #define INFORM_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
  57. #define TRP2_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
  58. #define REPORT_MSG     (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
  59. #define SNMP_NOSUCHOBJECT    (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
  60. #define SNMP_NOSUCHINSTANCE  (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
  61. #define SNMP_ENDOFMIBVIEW    (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
  62. #define SNMP_MSG_LENGTH 1500
  63. typedef struct sockaddr_in  ipaddr;
  64. // pdu
  65. struct snmp_pdu {
  66.     int     command;      // pdu type
  67.     unsigned long  reqid;    // Request id 
  68.     unsigned long  errstat;  // Error status 
  69.     unsigned long  errindex; // Error index 
  70.     // Trap information 
  71.     oid     *enterprise;     // System OID 
  72.     int     enterprise_length;
  73.     ipaddr  agent_addr;     // address of object generating trap 
  74.     int     trap_type;     // trap type 
  75.     int     specific_type;  // specific type 
  76.     unsigned long  time; // Uptime 
  77. // vb list
  78.     struct variable_list *variables;
  79. };
  80. // vb list
  81. struct variable_list {
  82.     struct variable_list *next_variable;    // NULL for last variable 
  83.     oid     *name;                          // Object identifier of variable 
  84.     int     name_length;                    // number of subid's in name 
  85.     unsigned char   type;                   // ASN type of variable 
  86.     union {                                 // value of variable 
  87. long *integer;
  88. unsigned char  *string;
  89. oid *objid;
  90. unsigned char   *bitstring;
  91. struct counter64 *counter64;
  92.     } val;
  93.     int     val_len;
  94. };
  95. // prototypes for encoding routines
  96. unsigned char * asn_parse_int( unsigned char *data, 
  97.                    int *datalength, 
  98.                    unsigned char *type, 
  99.                    long int *intp, 
  100.                    int intsize);
  101. unsigned char * asn_parse_unsigned_int( unsigned char *data,
  102.                                         int *datalength,
  103.                                         unsigned char *type,
  104.                                         unsigned long *intp,
  105.                                         int intsize);
  106. unsigned char * asn_build_int( unsigned char *data,
  107.                                int *datalength,
  108.                                unsigned char type,
  109.                                long *intp,
  110.                                int intsize);
  111. unsigned char * asn_build_unsigned_int( unsigned char *data,
  112.                                         int *datalength,
  113.                                         unsigned char type,
  114.                                         unsigned long *intp,
  115.                                         int intsize);
  116. unsigned char * asn_parse_string( unsigned char *data,
  117.                                   int *datalength,
  118.                                   unsigned char *type,
  119.                                   unsigned char *string,
  120.                                   int *strlength);
  121. unsigned char * asn_build_string( unsigned char *data,
  122.                                   int *datalength,
  123.                                   unsigned char type,
  124.                                   unsigned char *string,
  125.                                   int strlength);
  126. unsigned char *asn_parse_header( unsigned char *data,
  127.  int *datalength,
  128.                                  unsigned char *type);
  129. unsigned char * asn_build_header( unsigned char *data,
  130.                                   int *datalength,
  131.                                   unsigned char type,
  132.                                   int length);
  133. unsigned char * asn_build_sequence( unsigned char *data,
  134.                                     int *datalength,
  135.                                     unsigned char type,
  136.                                     int length);
  137. unsigned char * asn_parse_length( unsigned char *data,
  138.                                   unsigned long  *length);
  139. unsigned char *asn_build_length( unsigned char *data,
  140.                                  int *datalength,
  141.                                  int length);
  142. unsigned char *asn_parse_objid( unsigned char *data,
  143.                                 int *datalength,
  144.                                 unsigned char *type,
  145.                                 oid *objid,
  146.                                 int *objidlength);
  147. unsigned char *asn_build_objid( unsigned char *data,
  148.                                 int *datalength,
  149.                                 unsigned char type,
  150.                                 oid *objid,
  151.                                 int objidlength);
  152. unsigned char *asn_parse_null(unsigned char *data,
  153.                               int *datalength,
  154.                               unsigned char *type);
  155. unsigned char *asn_build_null( unsigned char *data,
  156.                                int *datalength,
  157.                                unsigned char type);
  158. unsigned char *asn_parse_bitstring( unsigned char *data,
  159.                                     int *datalength,
  160.                                     unsigned char *type,
  161.                                     unsigned char *string,
  162.                                     int *strlength);
  163. unsigned char *asn_build_bitstring( unsigned char *data,
  164.                                     int *datalength,
  165.                                     unsigned char type,
  166.                                     unsigned char *string,
  167.                                     int strlength);
  168. unsigned char * asn_parse_unsigned_int64(  unsigned char *data,
  169.                                            int *datalength,
  170.                                            unsigned char *type,
  171.                                            struct counter64 *cp,
  172.                                            int countersize);
  173. unsigned char * asn_build_unsigned_int64( unsigned char *data,
  174.                                           int *datalength,
  175.                                           unsigned char type,
  176.                                           struct counter64 *cp,
  177.                                           int countersize);
  178. struct counter64 {
  179.     unsigned long high;
  180.     unsigned long low;
  181. };
  182. struct snmp_pdu * snmp_pdu_create( int command);
  183. void snmp_free_pdu( struct snmp_pdu *pdu);
  184. int snmp_build( struct snmp_pdu *pdu, 
  185.     unsigned char *packet, 
  186. int *out_length, 
  187. long version,
  188. unsigned char* community,
  189. int community_len);
  190. void snmp_add_var(struct snmp_pdu *pdu, 
  191.       oid *name, 
  192.       int name_length,
  193.       SmiVALUE *smival);
  194. int snmp_parse( struct snmp_pdu *pdu,
  195.                 unsigned char  *data,
  196. unsigned char *community_name,
  197. unsigned long &community_len,
  198. snmp_version &version,
  199.                 int length);
  200. #endif  // _ASN1