snmp.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 1998-2007 by egnite Software GmbH
00003  * Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00025  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00026  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00028  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  *
00031  * For additional information see http://www.ethernut.de/
00032  */
00033 
00034 #include <pro/snmp.h>
00035 #include <pro/snmp_api.h>
00036 
00060 CONST uint8_t *SnmpVarParse(CONST uint8_t * data, size_t * dlen, OID * name, size_t * nlen, uint8_t * type,
00061                            uint8_t ** value, size_t * vlen)
00062 {
00063     CONST uint8_t *dp;
00064     uint8_t vtype = ASN_SEQUENCE | ASN_CONSTRUCTOR;
00065     size_t len = *dlen;
00066 
00067     /* Get the object's length and check its type. */
00068     if ((dp = AsnSequenceParse(data, &len, vtype)) == NULL) {
00069         return NULL;
00070     }
00071     /* Get type, value and length of the name. */
00072     if ((dp = AsnOidParse(dp, &len, &vtype, name, nlen)) == NULL) {
00073         return NULL;
00074     }
00075     /* Check the name's type. */
00076     if (vtype != (uint8_t) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID)) {
00077         return NULL;
00078     }
00079     /* Return a pointer to the value. */
00080     *value = (uint8_t *) dp;
00081     /* Find out what type of object this is. */
00082     if ((dp = AsnHeaderParse(dp, &len, type)) == NULL) {
00083         return NULL;
00084     }
00085     *vlen = len;
00086     dp += *vlen;
00087     *dlen -= dp - data;
00088 
00089     return dp;
00090 }
00091 
00109 uint8_t *SnmpVarBuild(uint8_t * data, size_t * dlen, CONST OID * name, size_t nlen, uint8_t type, CONST uint8_t * value, size_t vlen)
00110 {
00111     size_t headerLen = 4;
00112     uint8_t *dp;
00113 
00114     /* 
00115      * The final length is not known now, thus the header will have to 
00116      * be build later. 
00117      */
00118     if (*dlen < headerLen) {
00119         SnmpStatsInc(SNMP_STAT_OUTTOOBIGS);
00120         return NULL;
00121     }
00122     *dlen -= headerLen;
00123     dp = data + headerLen;
00124 
00125     /* Build the name. */
00126     if ((dp = AsnOidBuild(dp, dlen, ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID, name, nlen)) == NULL) {
00127         SnmpStatsInc(SNMP_STAT_OUTTOOBIGS);
00128         return NULL;
00129     }
00130     /* Build the value. */
00131     switch (type) {
00132     case ASN_INTEGER:
00133         dp = AsnIntegerBuild(dp, dlen, type, (long *) value);
00134         break;
00135     case ASN_GAUGE:
00136     case ASN_COUNTER:
00137     case ASN_TIMETICKS:
00138     case ASN_UINTEGER:
00139         dp = AsnUnsignedBuild(dp, dlen, type, (uint32_t *) value);
00140         break;
00141     case ASN_COUNTER64:
00142         dp = AsnUnsigned64Build(dp, dlen, type, (UNSIGNED64 *) value);
00143         break;
00144     case ASN_OCTET_STR:
00145     case ASN_IPADDRESS:
00146     case ASN_OPAQUE:
00147     case ASN_NSAP:
00148         dp = AsnOctetStringBuild(dp, dlen, type, value, vlen);
00149         break;
00150     case ASN_OBJECT_ID:
00151         dp = AsnOidBuild(dp, dlen, type, (OID *) value, vlen / sizeof(OID));
00152         break;
00153     case ASN_NULL:
00154         dp = AsnNullBuild(dp, dlen, type);
00155         break;
00156     case ASN_BIT_STR:
00157         dp = AsnBitStringBuild(dp, dlen, type, value, vlen);
00158         break;
00159     case SNMP_NOSUCHOBJECT:
00160     case SNMP_NOSUCHINSTANCE:
00161     case SNMP_ENDOFMIBVIEW:
00162         dp = AsnNullBuild(dp, dlen, type);
00163         break;
00164     default:
00165         SnmpStatsInc(SNMP_STAT_OUTBADVALUES);
00166         return NULL;
00167     }
00168     /* Now build the header. */
00169     if (dp) {
00170         size_t dummyLen = (dp - data) - headerLen;
00171         AsnSequenceBuild(data, &dummyLen, ASN_SEQUENCE | ASN_CONSTRUCTOR, dummyLen);
00172     } else {
00173         SnmpStatsInc(SNMP_STAT_OUTTOOBIGS);
00174     }
00175     return dp;
00176 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/