4 //=============================================================================
8 * Abstract Syntax Notation One, ASN.1
9 * As defined in ISO/IS 8824 and ISO/IS 8825
10 * This implements a subset of the above International Standards that
11 * is sufficient to implement SNMP.
13 * @author Steve Waldbusser/CMU ?
15 //=============================================================================
18 /**********************************************************************
19 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
23 Permission to use, copy, modify, and distribute this software and its
24 documentation for any purpose and without fee is hereby granted,
25 provided that the above copyright notice appear in all copies and that
26 both that copyright notice and this permission notice appear in
27 supporting documentation, and that the name of CMU not be
28 used in advertising or publicity pertaining to distribution of the
29 software without specific, written prior permission.
31 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
32 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
33 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
34 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
35 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
36 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
38 ******************************************************************/
40 #include "ace/config-all.h"
42 #if !defined (ACE_LACKS_PRAGMA_ONCE)
44 #endif /* ACE_LACKS_PRAGMA_ONCE */
46 #include "asnmp/snmp.h"
48 // CMU code to define "oid" typedef (old C style)
49 #ifndef EIGHTBIT_SUBIDS
50 typedef unsigned long oid
;
51 #define MAX_SUBID 0xFFFFFFFF
53 typedef unsigned char oid
;
54 #define MAX_SUBID 0xFF
57 #define MAX_OID_LEN 64 /* max subid's in an oid */
61 #define ASN_BOOLEAN (0x01)
62 #define ASN_INTEGER (0x02)
63 #define ASN_BIT_STR (0x03)
64 #define ASN_OCTET_STR (0x04)
65 #define ASN_NULL (0x05)
66 #define ASN_OBJECT_ID (0x06)
67 #define ASN_SEQUENCE (0x10)
68 #define ASN_SET (0x11)
69 #define ASN_UNIVERSAL (0x00)
70 #define ASN_APPLICATION (0x40)
71 #define ASN_CONTEXT (0x80)
72 #define ASN_PRIVATE (0xC0)
73 #define ASN_PRIMITIVE (0x00)
74 #define ASN_CONSTRUCTOR (0x20)
75 #define ASN_LONG_LEN (0x80)
76 #define ASN_EXTENSION_ID (0x1F)
77 #define ASN_BIT8 (0x80)
79 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
80 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
82 #define ASNERROR( string)
83 #define MAX_NAME_LEN 64
84 #define SNMP_VERSION_1 0
85 #define SNMP_VERSION_2C 1
88 // defined types (from the SMI, RFC 1065)
89 #define SMI_IPADDRESS (ASN_APPLICATION | 0)
90 #define SMI_COUNTER (ASN_APPLICATION | 1)
91 #define SMI_GAUGE (ASN_APPLICATION | 2)
92 #define SMI_TIMETICKS (ASN_APPLICATION | 3)
93 #define SMI_OPAQUE (ASN_APPLICATION | 4)
94 #define SMI_NSAP (ASN_APPLICATION | 5)
95 #define SMI_COUNTER64 (ASN_APPLICATION | 6)
96 #define SMI_UINTEGER (ASN_APPLICATION | 7)
98 #define GET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
99 #define GETNEXT_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
100 #define GET_RSP_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
101 #define SET_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
102 #define TRP_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)
104 #define GETBULK_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
105 #define INFORM_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
106 #define TRP2_REQ_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
107 #define REPORT_MSG (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)
109 #define SNMP_NOSUCHOBJECT (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
110 #define SNMP_NOSUCHINSTANCE (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
111 #define SNMP_ENDOFMIBVIEW (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)
113 #define SNMP_MSG_LENGTH 1500
115 typedef struct sockaddr_in ipaddr
;
119 int command
; // pdu type
120 unsigned long reqid
; // Request id
121 unsigned long errstat
; // Error status
122 unsigned long errindex
; // Error index
125 oid
*enterprise
; // System OID
126 int enterprise_length
;
127 ipaddr agent_addr
; // address of object generating trap
128 int trap_type
; // trap type
129 int specific_type
; // specific type
130 unsigned long time
; // Uptime
133 struct variable_list
*variables
;
137 struct variable_list
{
138 struct variable_list
*next_variable
; // 0 for last variable
139 oid
*name
; // Object identifier of variable
140 int name_length
; // number of subid's in name
141 unsigned char type
; // ASN type of variable
142 union { // value of variable
144 unsigned char *string
;
146 unsigned char *bitstring
;
147 struct counter64
*counter64
;
157 // CMU Interface class for ASN.1 routines
161 * @brief Defines the member functions for the asn1 concrete interface class
162 * CMU routines for performing ASN.1 data marshalling / representation
164 class ASNMP_Export asn1
167 static unsigned char *parse_int( unsigned char *data
,
172 static unsigned char *parse_unsigned_int( unsigned char *data
,
177 static unsigned char *build_int(unsigned char *data
,
183 static unsigned char *build_unsigned_int( unsigned char *data
,
189 static unsigned char *parse_string( unsigned char *data
,
192 unsigned char *string
,
195 static unsigned char *build_string( unsigned char *data
,
198 unsigned char *string
,
201 static unsigned char *parse_header( unsigned char *data
,
203 unsigned char *type
);
205 static unsigned char * build_header( unsigned char *data
,
210 static unsigned char * build_sequence( unsigned char *data
,
215 static unsigned char * parse_length( unsigned char *data
,
216 unsigned long *length
);
218 static unsigned char *build_length( unsigned char *data
,
222 static unsigned char *build_objid( unsigned char *data
,
228 static unsigned char *parse_objid( unsigned char *data
,
234 static unsigned char *parse_null(unsigned char *data
,
235 int *datalength
, unsigned
238 static unsigned char *build_null( unsigned char *data
,
242 static unsigned char *parse_bitstring( unsigned char *data
,
245 unsigned char *string
,
248 static unsigned char *build_bitstring( unsigned char *data
,
251 unsigned char *string
,
254 static unsigned char * parse_unsigned_int64(unsigned char *data
,
257 struct counter64
*cp
,
260 static unsigned char * build_unsigned_int64( unsigned char *data
,
263 struct counter64
*cp
,
268 // CMU Interface class for SNMPv1, SNMPv2c routines
272 * @brief Defines the static functions for the cmu_snmp concrete class.
273 * These routine create cmu pdu's that can be sent/received on/from agents
275 class ASNMP_Export cmu_snmp
278 static struct snmp_pdu
*pdu_create( int command
);
279 static void free_pdu( struct snmp_pdu
*pdu
);
280 static int build( struct snmp_pdu
*pdu
,
281 unsigned char *packet
,
284 unsigned char* community
,
287 static void add_var(struct snmp_pdu
*pdu
,
292 static int parse( struct snmp_pdu
*pdu
,
294 unsigned char *community_name
,
295 unsigned long &community_len
,
296 snmp_version
&version
,
298 static unsigned char *auth_build( unsigned char *data
,
301 unsigned char *community
,
305 static unsigned char *build_var_op(unsigned char *data
,
308 unsigned char var_val_type
,
310 unsigned char *var_val
,
314 static unsigned char *auth_parse(unsigned char *data
,
321 @params data, IN - pointer to the start of object
322 var_name OUT - object id of variable
323 var_name_len IN/OUT - length of variable name
324 var_val_type OUT - type of variable (int or octet string) (one byte)
325 var_val_len OUT - length of variable
326 var_val, OUT - pointer to ASN1 encoded value of variable
327 listlength IN/OUT - number of valid bytes left in var_op_list
329 static unsigned char *parse_var_op( unsigned char *data
,
332 unsigned char *var_val_type
,
334 unsigned char **var_val
,