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 00038 /* 00039 * generic statistics counter functions 00040 */ 00041 static u_long statistics[SNMP_STAT_MAX]; 00042 00043 00051 int SnmpOidLenCmp(CONST OID * name1, CONST OID * name2, size_t len) 00052 { 00053 /* Find first non-matching element. */ 00054 while (len--) { 00055 if (*name1 < *name2) { 00056 /* First is lower than second. */ 00057 return -1; 00058 } 00059 if (*name1++ > *name2++) { 00060 /* First is larger than second. */ 00061 return 1; 00062 } 00063 } 00064 /* Elements match up to the given length. */ 00065 return 0; 00066 } 00067 00079 int SnmpOidCmp(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2) 00080 { 00081 /* Compare elements up to the length of shortest name. */ 00082 int rc = SnmpOidLenCmp(name1, name2, (len1 < len2) ? len1 : len2); 00083 /* If equal, compare lengths. */ 00084 if (rc == 0) { 00085 if (len1 < len2) { 00086 rc = -1; 00087 } else if (len1 > len2) { 00088 rc = 1; 00089 } 00090 } 00091 return rc; 00092 } 00093 00106 int SnmpOidTreeCmp(CONST OID * objid, size_t objlen, CONST OID * treeid, size_t treelen) 00107 { 00108 /* Compare elements up to the length of shortest name. */ 00109 int rc = SnmpOidLenCmp(objid, treeid, (objlen < treelen) ? objlen : treelen); 00110 /* If equal, compare lengths. */ 00111 if (rc == 0 && objlen < treelen) { 00112 rc = -1; 00113 } 00114 return rc; 00115 } 00116 00129 int SnmpOidCmpIdx(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2, OID index) 00130 { 00131 size_t len = (len1 < len2) ? len1 : len2; 00132 /* Compare elements up to the length of shortest name. */ 00133 int rc = SnmpOidLenCmp(name1, name2, len); 00134 /* If equal, compare lengths. */ 00135 if (rc == 0) { 00136 if (len1 < len2) { 00137 rc = -1; 00138 } else if (len1 > len2) { 00139 if (*(name1 + len) < index) { 00140 /* First is lower than second. */ 00141 rc = -1; 00142 } else if (*(name1 + len) > index) { 00143 /* First is larger than second. */ 00144 rc = 1; 00145 } else if (len1 > len2 + 1) { 00146 rc = 1; 00147 } 00148 } 00149 } 00150 return rc; 00151 } 00152 00153 /* 00154 * This should be faster than doing a SnmpOidCmp for different 00155 * length OIDs, since the length is checked first and if != returns 00156 * immediately. 00157 * 00158 * Might be very slighly faster if lengths are ==. 00159 * 00160 * \param name1 A pointer to the first OID. 00161 * \param len1 Length of the first OID. 00162 * \param name2 A pointer to the second OID. 00163 * \param len2 Length of the second OID. 00164 * 00165 * \return 0 if they are equal, -1 if they are not. 00166 */ 00167 int SnmpOidEquals(CONST OID * name1, size_t len1, CONST OID * name2, size_t len2) 00168 { 00169 if (len1 != len2 || memcmp(name1, name2, len1)) { 00170 return -1; 00171 } 00172 return 0; 00173 } 00174 00175 00176 void SnmpStatsInc(int which) 00177 { 00178 if (which >= 0 && which < SNMP_STAT_MAX) { 00179 statistics[which]++; 00180 } 00181 } 00182 00183 u_long SnmpStatsGet(int which) 00184 { 00185 if (which >= 0 && which < SNMP_STAT_MAX) { 00186 return statistics[which]; 00187 } 00188 return 0; 00189 }