OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [snmp/] [lib/] [v2_0/] [src/] [snmp.c] - Blame information for rev 377

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      ./lib/current/src/snmp.c
4
//
5
//
6
//==========================================================================
7
//####ECOSGPLCOPYRIGHTBEGIN####
8
// -------------------------------------------
9
// This file is part of eCos, the Embedded Configurable Operating System.
10
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
11
//
12
// eCos is free software; you can redistribute it and/or modify it under
13
// the terms of the GNU General Public License as published by the Free
14
// Software Foundation; either version 2 or (at your option) any later version.
15
//
16
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19
// for more details.
20
//
21
// You should have received a copy of the GNU General Public License along
22
// with eCos; if not, write to the Free Software Foundation, Inc.,
23
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24
//
25
// As a special exception, if other files instantiate templates or use macros
26
// or inline functions from this file, or you compile this file and link it
27
// with other works to produce a work based on this file, this file does not
28
// by itself cause the resulting work to be covered by the GNU General Public
29
// License. However the source code for this file must still be made available
30
// in accordance with section (3) of the GNU General Public License.
31
//
32
// This exception does not invalidate any other reasons why a work based on
33
// this file might be covered by the GNU General Public License.
34
//
35
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
36
// at http://sources.redhat.com/ecos/ecos-license/
37
// -------------------------------------------
38
//####ECOSGPLCOPYRIGHTEND####
39
//####UCDSNMPCOPYRIGHTBEGIN####
40
//
41
// -------------------------------------------
42
//
43
// Portions of this software may have been derived from the UCD-SNMP
44
// project,  <http://ucd-snmp.ucdavis.edu/>  from the University of
45
// California at Davis, which was originally based on the Carnegie Mellon
46
// University SNMP implementation.  Portions of this software are therefore
47
// covered by the appropriate copyright disclaimers included herein.
48
//
49
// The release used was version 4.1.2 of May 2000.  "ucd-snmp-4.1.2"
50
// -------------------------------------------
51
//
52
//####UCDSNMPCOPYRIGHTEND####
53
//==========================================================================
54
//#####DESCRIPTIONBEGIN####
55
//
56
// Author(s):    hmt
57
// Contributors: hmt
58
// Date:         2000-05-30
59
// Purpose:      Port of UCD-SNMP distribution to eCos.
60
// Description:  
61
//              
62
//
63
//####DESCRIPTIONEND####
64
//
65
//==========================================================================
66
/********************************************************************
67
       Copyright 1989, 1991, 1992 by Carnegie Mellon University
68
 
69
                          Derivative Work -
70
Copyright 1996, 1998, 1999, 2000 The Regents of the University of California
71
 
72
                         All Rights Reserved
73
 
74
Permission to use, copy, modify and distribute this software and its
75
documentation for any purpose and without fee is hereby granted,
76
provided that the above copyright notice appears in all copies and
77
that both that copyright notice and this permission notice appear in
78
supporting documentation, and that the name of CMU and The Regents of
79
the University of California not be used in advertising or publicity
80
pertaining to distribution of the software without specific written
81
permission.
82
 
83
CMU AND THE REGENTS OF THE UNIVERSITY OF CALIFORNIA DISCLAIM ALL
84
WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
85
WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL CMU OR
86
THE REGENTS OF THE UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY SPECIAL,
87
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
88
FROM THE LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
89
CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
90
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
91
*********************************************************************/
92
/*
93
 * Simple Network Management Protocol (RFC 1067).
94
 *
95
 */
96
/**********************************************************************
97
        Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
98
 
99
                      All Rights Reserved
100
 
101
Permission to use, copy, modify, and distribute this software and its
102
documentation for any purpose and without fee is hereby granted,
103
provided that the above copyright notice appear in all copies and that
104
both that copyright notice and this permission notice appear in
105
supporting documentation, and that the name of CMU not be
106
used in advertising or publicity pertaining to distribution of the
107
software without specific, written prior permission.
108
 
109
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
110
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
111
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
112
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
113
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
114
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
115
SOFTWARE.
116
******************************************************************/
117
 
118
#include <config.h>
119
#include <ctype.h>
120
 
121
#ifdef KINETICS
122
#include "gw.h"
123
#include "ab.h"
124
#include "inet.h"
125
#include "fp4/cmdmacro.h"
126
#include "fp4/pbuf.h"
127
#include "glob.h"
128
#endif
129
 
130
#include <stdio.h>
131
#include <stdlib.h>
132
 
133
#include <sys/types.h>
134
#ifdef HAVE_STRING_H
135
#include <string.h>
136
#else
137
#include <strings.h>
138
#endif
139
#if HAVE_NETINET_IN_H
140
#include <netinet/in.h>
141
#endif
142
#ifdef HAVE_SYS_SELECT_H
143
#include <sys/select.h>
144
#endif
145
#if HAVE_WINSOCK_H
146
#include <winsock.h>
147
#endif
148
#ifndef NULL
149
#define NULL 0
150
#endif
151
 
152
#if HAVE_DMALLOC_H
153
#include <dmalloc.h>
154
#endif
155
 
156
#ifdef vms
157
#include <in.h>
158
#endif
159
 
160
#include "asn1.h"
161
#include "snmp.h"
162
#include "snmp_api.h"
163
#include "snmp_impl.h"
164
#include "snmp_logging.h"
165
#include "mib.h"
166
 
167
void
168
xdump(const u_char *cp,
169
      size_t length,
170
      const char *prefix)
171
{
172
    int col, count;
173
    char *buffer;
174
 
175
    buffer=(char *)malloc(strlen(prefix)+80);
176
    if (!buffer) {
177
      snmp_log(LOG_NOTICE, "xdump: malloc failed. packet-dump skipped\n");
178
      return;
179
    }
180
 
181
    count = 0;
182
    while(count < (int)length){
183
        strcpy(buffer, prefix);
184
        sprintf (buffer+strlen(buffer), "%.4d: ", count);
185
 
186
        for(col = 0; ((count + col) < (int)length) && col < 16; col++){
187
            sprintf(buffer+strlen(buffer), "%02X ", cp[count + col]);
188
            if (col % 4 == 3) strcat(buffer, " ");
189
        }
190
        for(;col < 16;col++){   /* pad end of buffer with zeros */
191
            strcat(buffer, "   ");
192
            if (col % 4 == 3) strcat(buffer, " ");
193
        }
194
        strcat(buffer, "  ");
195
        for(col = 0; ((count + col) < (int)length) && col < 16; col++){
196
            buffer[col+60]=isprint(cp[count+col])?cp[count+col]:'.';
197
        }
198
        buffer[col+60]='\n';
199
        buffer[col+60+1]=0;
200
        snmp_log(LOG_DEBUG, "%s", buffer);
201
        count += col;
202
    }
203
    snmp_log(LOG_DEBUG, "\n");
204
    free(buffer);
205
 
206
}  /* end xdump() */
207
 
208
/*
209
   u_char * snmp_parse_var_op(
210
   u_char *data              IN - pointer to the start of object
211
   oid *var_name             OUT - object id of variable
212
   int *var_name_len         IN/OUT - length of variable name
213
   u_char *var_val_type      OUT - type of variable (int or octet string) (one byte)
214
   int *var_val_len          OUT - length of variable
215
   u_char **var_val          OUT - pointer to ASN1 encoded value of variable
216
   int *listlength          IN/OUT - number of valid bytes left in var_op_list
217
*/
218
 
219
u_char *
220
snmp_parse_var_op(u_char *data,
221
                  oid *var_name,
222
                  size_t *var_name_len,
223
                  u_char *var_val_type,
224
                  size_t *var_val_len,
225
                  u_char **var_val,
226
                  size_t *listlength)
227
{
228
    u_char          var_op_type;
229
    size_t                  var_op_len = *listlength;
230
    u_char          *var_op_start = data;
231
 
232
    data = asn_parse_sequence(data, &var_op_len, &var_op_type,
233
                        (ASN_SEQUENCE | ASN_CONSTRUCTOR), "var_op");
234
    if (data == NULL){
235
        /* msg detail is set */
236
        return NULL;
237
    }
238
    data = asn_parse_objid(data, &var_op_len, &var_op_type, var_name, var_name_len);
239
    if (data == NULL){
240
        ERROR_MSG("No OID for variable");
241
        return NULL;
242
    }
243
    if (var_op_type != (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID))
244
        return NULL;
245
    *var_val = data;    /* save pointer to this object */
246
    /* find out what type of object this is */
247
    data = asn_parse_header(data, &var_op_len, var_val_type);
248
    if (data == NULL){
249
        ERROR_MSG("No header for value");
250
        return NULL;
251
    }
252
    /* XXX no check for type! */
253
    *var_val_len = var_op_len;
254
    data += var_op_len;
255
    *listlength -= (int)(data - var_op_start);
256
    return data;
257
}
258
 
259
/*
260
        u_char * snmp_build_var_op(
261
        u_char *data         IN - pointer to the beginning of the output buffer
262
        oid *var_name        IN - object id of variable
263
        int *var_name_len    IN - length of object id
264
        u_char var_val_type  IN - type of variable
265
        int    var_val_len   IN - length of variable
266
        u_char *var_val      IN - value of variable
267
        int *listlength      IN/OUT - number of valid bytes left in
268
                                   output buffer
269
*/
270
 
271
u_char *
272
snmp_build_var_op(u_char *data,
273
                  oid *var_name,
274
                  size_t *var_name_len,
275
                  u_char var_val_type,
276
                  size_t var_val_len,
277
                  u_char *var_val,
278
                  size_t *listlength)
279
{
280
    size_t     dummyLen, headerLen;
281
    u_char    *dataPtr;
282
 
283
    dummyLen = *listlength;
284
    dataPtr = data;
285
#if 0
286
    data = asn_build_sequence(data, &dummyLen,
287
                              (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), 0);
288
    if (data == NULL){
289
        return NULL;
290
    }
291
#endif
292
    if (dummyLen < 4)
293
        return NULL;
294
    data += 4;
295
    dummyLen -=4;
296
 
297
    headerLen = data - dataPtr;
298
    *listlength -= headerLen;
299
    data = asn_build_objid(data, listlength,
300
            (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID),
301
            var_name, *var_name_len);
302
    if (data == NULL){
303
        ERROR_MSG("Can't build OID for variable");
304
        return NULL;
305
    }
306
    switch(var_val_type){
307
        case ASN_INTEGER:
308
            data = asn_build_int(data, listlength, var_val_type,
309
                    (long *)var_val, var_val_len);
310
            break;
311
        case ASN_GAUGE:
312
        case ASN_COUNTER:
313
        case ASN_TIMETICKS:
314
        case ASN_UINTEGER:
315
            data = asn_build_unsigned_int(data, listlength, var_val_type,
316
                                          (u_long *)var_val, var_val_len);
317
            break;
318
#ifdef OPAQUE_SPECIAL_TYPES
319
        case ASN_OPAQUE_COUNTER64:
320
        case ASN_OPAQUE_U64:
321
#endif
322
        case ASN_COUNTER64:
323
            data = asn_build_unsigned_int64(data, listlength, var_val_type,
324
                                           (struct counter64 *)var_val,
325
                                            var_val_len);
326
            break;
327
        case ASN_OCTET_STR:
328
        case ASN_IPADDRESS:
329
        case ASN_OPAQUE:
330
        case ASN_NSAP:
331
            data = asn_build_string(data, listlength, var_val_type,
332
                    var_val, var_val_len);
333
            break;
334
        case ASN_OBJECT_ID:
335
            data = asn_build_objid(data, listlength, var_val_type,
336
                    (oid *)var_val, var_val_len / sizeof(oid));
337
            break;
338
        case ASN_NULL:
339
            data = asn_build_null(data, listlength, var_val_type);
340
            break;
341
        case ASN_BIT_STR:
342
            data = asn_build_bitstring(data, listlength, var_val_type,
343
                    var_val, var_val_len);
344
            break;
345
        case SNMP_NOSUCHOBJECT:
346
        case SNMP_NOSUCHINSTANCE:
347
        case SNMP_ENDOFMIBVIEW:
348
            data = asn_build_null(data, listlength, var_val_type);
349
            break;
350
#ifdef OPAQUE_SPECIAL_TYPES
351
      case ASN_OPAQUE_FLOAT:
352
        data = asn_build_float(data, listlength, var_val_type,
353
                               (float *) var_val, var_val_len);
354
        break;
355
      case ASN_OPAQUE_DOUBLE:
356
        data = asn_build_double(data, listlength, var_val_type,
357
                               (double *) var_val, var_val_len);
358
        break;
359
      case ASN_OPAQUE_I64:
360
        data = asn_build_signed_int64(data, listlength, var_val_type,
361
                                      (struct counter64 *) var_val,
362
                                      var_val_len);
363
        break;
364
#endif /* OPAQUE_SPECIAL_TYPES */
365
        default:
366
            ERROR_MSG("wrong type");
367
            return NULL;
368
    }
369
    if (data == NULL){
370
        ERROR_MSG("Can't build value");
371
        return NULL;
372
    }
373
    dummyLen = (data - dataPtr) - headerLen;
374
 
375
    asn_build_sequence(dataPtr, &dummyLen,
376
                       (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), dummyLen);
377
    return data;
378
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.