Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / ASNMP / asnmp / asn1.h
blobf2d7d3099059029e4754b262d0bd507d6cb66a3b
1 /* -*-C++-*- */
2 #ifndef ASN1
3 #define ASN1
4 //=============================================================================
5 /**
6 * @file asn1.h
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
21 All Rights Reserved
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
37 SOFTWARE.
38 ******************************************************************/
40 #include "ace/config-all.h"
42 #if !defined (ACE_LACKS_PRAGMA_ONCE)
43 # 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
52 #else
53 typedef unsigned char oid;
54 #define MAX_SUBID 0xFF
55 #endif
57 #define MAX_OID_LEN 64 /* max subid's in an oid */
60 // asn.1 values
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;
117 // pdu
118 struct snmp_pdu {
119 int command; // pdu type
120 unsigned long reqid; // Request id
121 unsigned long errstat; // Error status
122 unsigned long errindex; // Error index
124 // Trap information
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
132 // vb list
133 struct variable_list *variables;
136 // vb list
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
143 long *integer;
144 unsigned char *string;
145 oid *objid;
146 unsigned char *bitstring;
147 struct counter64 *counter64;
148 } val;
149 int val_len;
152 struct counter64 {
153 unsigned long high;
154 unsigned long low;
157 // CMU Interface class for ASN.1 routines
159 * @class asn1
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
166 public:
167 static unsigned char *parse_int( unsigned char *data,
168 int *datalength,
169 unsigned char *type,
170 long int *intp,
171 int intsize);
172 static unsigned char *parse_unsigned_int( unsigned char *data,
173 int *datalength,
174 unsigned char *type,
175 unsigned long *intp,
176 int intsize);
177 static unsigned char *build_int(unsigned char *data,
178 int *datalength,
179 unsigned char type,
180 long *intp,
181 int intsize);
183 static unsigned char *build_unsigned_int( unsigned char *data,
184 int *datalength,
185 unsigned char type,
186 unsigned long *intp,
187 int intsize);
189 static unsigned char *parse_string( unsigned char *data,
190 int *datalength,
191 unsigned char *type,
192 unsigned char *string,
193 int *strlength);
195 static unsigned char *build_string( unsigned char *data,
196 int *datalength,
197 unsigned char type,
198 unsigned char *string,
199 int strlength);
201 static unsigned char *parse_header( unsigned char *data,
202 int *datalength,
203 unsigned char *type);
205 static unsigned char * build_header( unsigned char *data,
206 int *datalength,
207 unsigned char type,
208 int length);
210 static unsigned char * build_sequence( unsigned char *data,
211 int *datalength,
212 unsigned char type,
213 int length);
215 static unsigned char * parse_length( unsigned char *data,
216 unsigned long *length);
218 static unsigned char *build_length( unsigned char *data,
219 int *datalength,
220 int length);
222 static unsigned char *build_objid( unsigned char *data,
223 int *datalength,
224 unsigned char type,
225 oid *objid,
226 int objidlength);
228 static unsigned char *parse_objid( unsigned char *data,
229 int *datalength,
230 unsigned char *type,
231 oid *objid,
232 int *objidlength);
234 static unsigned char *parse_null(unsigned char *data,
235 int *datalength, unsigned
236 char *type);
238 static unsigned char *build_null( unsigned char *data,
239 int *datalength,
240 unsigned char type);
242 static unsigned char *parse_bitstring( unsigned char *data,
243 int *datalength,
244 unsigned char *type,
245 unsigned char *string,
246 int *strlength);
248 static unsigned char *build_bitstring( unsigned char *data,
249 int *datalength,
250 unsigned char type,
251 unsigned char *string,
252 int strlength);
254 static unsigned char * parse_unsigned_int64(unsigned char *data,
255 int *datalength,
256 unsigned char *type,
257 struct counter64 *cp,
258 int countersize);
260 static unsigned char * build_unsigned_int64( unsigned char *data,
261 int *datalength,
262 unsigned char type,
263 struct counter64 *cp,
264 int countersize);
268 // CMU Interface class for SNMPv1, SNMPv2c routines
270 * @class cmu_snmp
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
277 public:
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,
282 int *out_length,
283 long version,
284 unsigned char* community,
285 int community_len);
287 static void add_var(struct snmp_pdu *pdu,
288 oid *name,
289 int name_length,
290 SmiVALUE *smival);
292 static int parse( struct snmp_pdu *pdu,
293 unsigned char *data,
294 unsigned char *community_name,
295 unsigned long &community_len,
296 snmp_version &version,
297 int length);
298 static unsigned char *auth_build( unsigned char *data,
299 int *length,
300 long int version,
301 unsigned char *community,
302 int community_len,
303 int messagelen);
305 static unsigned char *build_var_op(unsigned char *data,
306 oid * var_name,
307 int *var_name_len,
308 unsigned char var_val_type,
309 int var_val_len,
310 unsigned char *var_val,
311 int *listlength);
314 static unsigned char *auth_parse(unsigned char *data,
315 int *length,
316 unsigned char *sid,
317 int *slen,
318 long *version);
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,
330 oid *var_name,
331 int *var_name_len,
332 unsigned char *var_val_type,
333 int *var_val_len,
334 unsigned char **var_val,
335 int *listlength);
338 #endif // ASN1