Nut/OS  4.10.3
API Reference
snmp_api.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 <string.h>
00035 
00036 #include <pro/snmp_api.h>
00037 
00042 
00043 /*
00044  * generic statistics counter functions 
00045  */
00046 static uint32_t statistics[SNMP_STAT_MAX];
00047 
00048 
00056 int SnmpOidLenCmp(CONST OID * name1, CONST OID * name2, size_t len)
00057 {
00058     /* Find first non-matching element. */
00059     while (len--) {
00060         if (*name1 < *name2) {
00061             /* First is lower than second. */
00062             return -1;
00063         }
00064         if (*name1++ > *name2++) {
00065             /* First is larger than second. */
00066             return 1;
00067         }
00068     }
00069     /* Elements match up to the given length. */
00070     return 0;
00071 }
00072 
00084 int SnmpOidCmp(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2)
00085 {
00086     /* Compare elements up to the length of shortest name. */
00087     int rc = SnmpOidLenCmp(name1, name2, (len1 < len2) ? len1 : len2);
00088     /* If equal, compare lengths. */
00089     if (rc == 0) {
00090         if (len1 < len2) {
00091             rc = -1;
00092         } else if (len1 > len2) {
00093             rc = 1;
00094         }
00095     }
00096     return rc;
00097 }
00098 
00111 int SnmpOidTreeCmp(CONST OID * objid, size_t objlen, CONST OID * treeid, size_t treelen)
00112 {
00113     /* Compare elements up to the length of shortest name. */
00114     int rc = SnmpOidLenCmp(objid, treeid, (objlen < treelen) ? objlen : treelen);
00115     /* If equal, compare lengths. */
00116     if (rc == 0 && objlen < treelen) {
00117         rc = -1;
00118     }
00119     return rc;
00120 }
00121 
00134 int SnmpOidCmpIdx(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2, OID index)
00135 {
00136     size_t len = (len1 < len2) ? len1 : len2;
00137     /* Compare elements up to the length of shortest name. */
00138     int rc = SnmpOidLenCmp(name1, name2, len);
00139     /* If equal, compare lengths. */
00140     if (rc == 0) {
00141         if (len1 < len2) {
00142             rc = -1;
00143         } else if (len1 > len2) {
00144             if (*(name1 + len) < index) {
00145                 /* First is lower than second. */
00146                 rc = -1;
00147             } else if (*(name1 + len) > index) {
00148                 /* First is larger than second. */
00149                 rc = 1;
00150             } else if (len1 > len2 + 1) {
00151                 rc = 1;
00152             }
00153         }
00154     }
00155     return rc;
00156 }
00157 
00158 /*
00159  * This should be faster than doing a SnmpOidCmp for different 
00160  * length OIDs, since the length is checked first and if != returns
00161  * immediately.  
00162  *
00163  * Might be very slighly faster if lengths are ==.
00164  *
00165  * \param name1 A pointer to the first OID.
00166  * \param len1  Length of the first OID.
00167  * \param name2 A pointer to the second OID.
00168  * \param len2  Length of the second OID.
00169  *
00170  * \return 0 if they are equal, -1 if they are not.
00171  */
00172 int SnmpOidEquals(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2)
00173 {
00174     if (len1 != len2 || memcmp(name1, name2, len1)) {
00175         return -1;
00176     }
00177     return 0;
00178 }
00179 
00180 
00181 void SnmpStatsInc(int which)
00182 {
00183     if (which >= 0 && which < SNMP_STAT_MAX) {
00184         statistics[which]++;
00185     }
00186 }
00187 
00188 uint32_t SnmpStatsGet(int which)
00189 {
00190     if (which >= 0 && which < SNMP_STAT_MAX) {
00191         return statistics[which];
00192     }
00193     return 0;
00194 }
00195 
00196 void SnmpStatsSet(int which, uint32_t value)
00197 {
00198     if (which >= 0 && which < SNMP_STAT_MAX) {
00199         statistics[which] = value;
00200     }
00201 }
00202