summaryrefslogtreecommitdiff
path: root/ASNMP/asnmp/asn1.h
blob: ce0256c43df40ec8c671d772f11726dec476e2af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*-C++-*- */
// $Id$
#ifndef ASN1
#define ASN1
// ============================================================================
//
// = LIBRARY
//    asnmp
//
// = FILENAME
//    asn1.h
//
// = DESCRIPTION
// Abstract Syntax Notation One, ASN.1
// As defined in ISO/IS 8824 and ISO/IS 8825
// This implements a subset of the above International Standards that
// is sufficient to implement SNMP.
// 
// = AUTHOR
//   Steve Waldbusser/CMU ?
//
// ============================================================================

/**********************************************************************
	Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University

                      All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of CMU not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
******************************************************************/

#include "ace/OS.h"
#include "asnmp/snmp.h"

// CMU code to define "oid" typedef (old C style)
#ifndef EIGHTBIT_SUBIDS
typedef unsigned long	oid;
#define MAX_SUBID   0xFFFFFFFF
#else
typedef unsigned char	oid;
#define MAX_SUBID   0xFF
#endif

#define MAX_OID_LEN	    64	/* max subid's in an oid */


// asn.1 values
#define ASN_BOOLEAN	     (0x01)
#define ASN_INTEGER	     (0x02)
#define ASN_BIT_STR	     (0x03)
#define ASN_OCTET_STR	 (0x04)
#define ASN_NULL	     (0x05)
#define ASN_OBJECT_ID	 (0x06)
#define ASN_SEQUENCE	 (0x10)
#define ASN_SET		     (0x11)
#define ASN_UNIVERSAL	 (0x00)
#define ASN_APPLICATION  (0x40)
#define ASN_CONTEXT	     (0x80)
#define ASN_PRIVATE	     (0xC0)
#define ASN_PRIMITIVE	 (0x00)
#define ASN_CONSTRUCTOR	 (0x20)
#define ASN_LONG_LEN	 (0x80)
#define ASN_EXTENSION_ID (0x1F)
#define ASN_BIT8	     (0x80)

#define IS_CONSTRUCTOR(byte)	((byte) & ASN_CONSTRUCTOR)
#define IS_EXTENSION_ID(byte)	(((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)

#define ASNERROR( string)
#define MAX_NAME_LEN   64 
#define SNMP_VERSION_1 0
#define SNMP_VERSION_2C 1


// defined types (from the SMI, RFC 1065) 
#define SMI_IPADDRESS   (ASN_APPLICATION | 0)
#define SMI_COUNTER	    (ASN_APPLICATION | 1)
#define SMI_GAUGE	    (ASN_APPLICATION | 2)
#define SMI_TIMETICKS   (ASN_APPLICATION | 3)
#define SMI_OPAQUE	    (ASN_APPLICATION | 4)
#define SMI_NSAP        (ASN_APPLICATION | 5)
#define SMI_COUNTER64   (ASN_APPLICATION | 6)
#define SMI_UINTEGER    (ASN_APPLICATION | 7)

#define GET_REQ_MSG	    (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x0)
#define GETNEXT_REQ_MSG	(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x1)
#define GET_RSP_MSG	    (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x2)
#define SET_REQ_MSG	    (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x3)
#define TRP_REQ_MSG	    (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x4)

#define GETBULK_REQ_MSG	(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x5)
#define INFORM_REQ_MSG	(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x6)
#define TRP2_REQ_MSG	(ASN_CONTEXT | ASN_CONSTRUCTOR | 0x7)
#define REPORT_MSG	    (ASN_CONTEXT | ASN_CONSTRUCTOR | 0x8)

#define SNMP_NOSUCHOBJECT    (ASN_CONTEXT | ASN_PRIMITIVE | 0x0)
#define SNMP_NOSUCHINSTANCE  (ASN_CONTEXT | ASN_PRIMITIVE | 0x1)
#define SNMP_ENDOFMIBVIEW    (ASN_CONTEXT | ASN_PRIMITIVE | 0x2)

#define SNMP_MSG_LENGTH 1500

typedef struct sockaddr_in  ipaddr;

// pdu
struct snmp_pdu {
    int	    command;	     // pdu type
    unsigned long  reqid;    // Request id 
    unsigned long  errstat;	 // Error status 
    unsigned long  errindex; // Error index 

    // Trap information 
    oid	    *enterprise;     // System OID 
    int	    enterprise_length;
    ipaddr  agent_addr;	    // address of object generating trap 
    int	    trap_type;	    // trap type 
    int	    specific_type;  // specific type 
    unsigned long  time;	// Uptime 

	// vb list
    struct variable_list *variables;
};

// vb list
struct variable_list {
    struct variable_list *next_variable;    // NULL for last variable 
    oid	    *name;                          // Object identifier of variable 
    int	    name_length;                    // number of subid's in name 
    unsigned char   type;                   // ASN type of variable 
    union {                                 // value of variable 
	long	*integer;
	unsigned char 	*string;
	oid	*objid;
	unsigned char   *bitstring;
	struct counter64 *counter64;
    } val;
    int	    val_len;
};

struct counter64 {
    unsigned long high;
    unsigned long low;
};

// CMU Interface class for ASN.1 routines
class ACE_Export asn1 
  // = TITLE
  //     Defines the member functions for the asn1 concrete interface class 
  //     CMU routines for performing ASN.1 data marshalling / representation

{
public:
static unsigned char *parse_int( unsigned char *data, 
			      int *datalength, 
			      unsigned char *type, 
			      long int *intp, 
			      int intsize);
static unsigned char *parse_unsigned_int( unsigned char *data,	
				       int *datalength,
				       unsigned char *type,
				       unsigned long *intp,
				       int	intsize);
static unsigned char *build_int( unsigned char *data,
			      int *datalength,
			      unsigned char type,
			      long *intp,
			      int intsize);

static unsigned char *build_unsigned_int( unsigned char *data,
				       int *datalength,
				       unsigned char type,
				       unsigned long *intp,
				       int intsize);

static unsigned char *parse_string( unsigned char	*data,
				 int *datalength,
				 unsigned char *type,
				 unsigned char *string,
				 int *strlength);

static unsigned char *build_string( unsigned char *data,
				 int *datalength,
				 unsigned char type,
				 unsigned char *string,
				 int strlength);

static unsigned char *parse_header( unsigned char *data,
				int *datalength,
				unsigned char *type);

static unsigned char * build_header( unsigned char *data,
				 int *datalength,
				 unsigned char type,
				 int length);

static unsigned char * build_sequence( unsigned char *data,
				   int *datalength,
				   unsigned char type,
				   int length);

static unsigned char * parse_length( unsigned char *data,
				 unsigned long  *length);

static unsigned char *build_length( unsigned char *data,
				int *datalength,
				int length);

static unsigned char *build_objid( unsigned char *data,
			       int *datalength,
			       unsigned char type,
			       oid *objid,
			       int objidlength);

static unsigned char *parse_objid( unsigned char *data,
			       int *datalength,
			       unsigned char *type,
			       oid *objid,
			       int *objidlength);

static unsigned char *parse_null(unsigned char *data, 
				int *datalength, unsigned 
				char *type);

static unsigned char *build_null( unsigned char *data,
			      int *datalength,
			      unsigned char type);

static unsigned char *parse_bitstring( unsigned char *data,
				   int *datalength,
				   unsigned char *type,
				   unsigned char *string,
				   int *strlength);

static unsigned char *build_bitstring( unsigned char *data,
				   int *datalength,
				   unsigned char type,	
				   unsigned char *string,
				   int strlength);

static unsigned char * parse_unsigned_int64(unsigned char *data,
					 int *datalength,
					 unsigned char *type,
					 struct counter64 *cp,
					 int countersize);

static unsigned char * build_unsigned_int64( unsigned char *data,
					 int *datalength,
					 unsigned char	type,
					 struct counter64 *cp,
					 int countersize);

};


// CMU Interface class for SNMPv1, SNMPv2c routines
class ACE_Export cmu_snmp 
  // = TITLE
  //    Defines the static functions for the cmu_snmp concrete class. 
  //    These routine create cmu pdu's that can be sent/received on/from agents 
{
 public:
  static struct snmp_pdu *pdu_create( int command);
  static void free_pdu( struct snmp_pdu *pdu);
  static int build( struct snmp_pdu	*pdu, 
		   unsigned char *packet, 
		   int *out_length, 
		   long version,
		   unsigned char* community,
		   int community_len);

  static void add_var(struct snmp_pdu *pdu, 
		      oid *name, 
		      int name_length,
		      SmiVALUE *smival);

  static int parse( struct snmp_pdu *pdu,
		   unsigned char  *data,
		   unsigned char *community_name,
		   unsigned long &community_len,
		   snmp_version &version,
		   int length);
  static unsigned char *auth_build( unsigned char *data,
			       int *length,
			       long int version,
			       unsigned char *community,
			       int community_len,
			       int messagelen);

   static unsigned char *build_var_op(unsigned char *data, 
				  oid * var_name, 
				  int *var_name_len, 
				  unsigned char var_val_type, 
				  int var_val_len,
				  unsigned char *var_val, 
				  int *listlength);


static unsigned char *auth_parse(unsigned char *data,
                               int *length,
                               unsigned char *sid,
			       int *slen,
                               long	*version);

/*
 @params  data, IN - pointer to the start of object 
	var_name OUT - object id of variable 
     var_name_len IN/OUT - length of variable name 
     var_val_type OUT - type of variable (int or octet string) (one byte) 
     var_val_len  OUT - length of variable
     var_val,	 OUT - pointer to ASN1 encoded value of variable
     listlength IN/OUT - number of valid bytes left in var_op_list
*/
static unsigned char *parse_var_op( unsigned char *data, 
			    oid	    *var_name,	  
			    int	    *var_name_len, 
			    unsigned char  *var_val_type,
			    int	    *var_val_len,	 
			    unsigned char  **var_val,	 
			    int	    *listlength);

};

#endif  // ASN1