snmp_api.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 1998-2007 by egnite Software GmbH
  3. * Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. */
  33. #include <string.h>
  34. #include <pro/snmp_api.h>
  35. /*!
  36. * \addtogroup xgSNMP
  37. */
  38. /*@{*/
  39. /*
  40. * generic statistics counter functions
  41. */
  42. static uint32_t statistics[SNMP_STAT_MAX];
  43. /*!
  44. * \brief Compare object identifiers up to a specified length.
  45. *
  46. * \param name1 First object identifier.
  47. * \param name2 Second object identifier.
  48. * \param len Number of sub identifiers to compare.
  49. */
  50. int SnmpOidLenCmp(const OID * name1, const OID * name2, size_t len)
  51. {
  52. /* Find first non-matching element. */
  53. while (len--) {
  54. if (*name1 < *name2) {
  55. /* First is lower than second. */
  56. return -1;
  57. }
  58. if (*name1++ > *name2++) {
  59. /* First is larger than second. */
  60. return 1;
  61. }
  62. }
  63. /* Elements match up to the given length. */
  64. return 0;
  65. }
  66. /*!
  67. * \brief Compare object identifiers.
  68. *
  69. * \param name1 First object identifier.
  70. * \param len1 Length of first object identifier.
  71. * \param name2 Second object identifier.
  72. * \param len2 Length of second object identifier.
  73. *
  74. * \return 0 if both are equal, 1 if first element is larger or -1
  75. * if first element is lower than the second one.
  76. */
  77. int SnmpOidCmp(const OID * name1, size_t len1, const OID * name2, size_t len2)
  78. {
  79. /* Compare elements up to the length of shortest name. */
  80. int rc = SnmpOidLenCmp(name1, name2, (len1 < len2) ? len1 : len2);
  81. /* If equal, compare lengths. */
  82. if (rc == 0) {
  83. if (len1 < len2) {
  84. rc = -1;
  85. } else if (len1 > len2) {
  86. rc = 1;
  87. }
  88. }
  89. return rc;
  90. }
  91. /*!
  92. * \brief Compare object identifier with tree element.
  93. *
  94. * \param name1 Object identifier.
  95. * \param len1 Length of object identifier.
  96. * \param name2 Tree identifier.
  97. * \param len2 Length of tree identifier.
  98. *
  99. * \return 0 if the object identifier is part of the subtree, -1 if it
  100. * is located before the tree element or 1 if it is located
  101. * after the tree element.
  102. */
  103. int SnmpOidTreeCmp(const OID * objid, size_t objlen, const OID * treeid, size_t treelen)
  104. {
  105. /* Compare elements up to the length of shortest name. */
  106. int rc = SnmpOidLenCmp(objid, treeid, (objlen < treelen) ? objlen : treelen);
  107. /* If equal, compare lengths. */
  108. if (rc == 0 && objlen < treelen) {
  109. rc = -1;
  110. }
  111. return rc;
  112. }
  113. /*!
  114. * \brief Compare object identifiers with index added.
  115. *
  116. * \param name1 First object identifier.
  117. * \param len1 Length of first object identifier.
  118. * \param name2 Second object identifier.
  119. * \param len2 Length of second object identifier.
  120. * \param index Index sub identifier.
  121. *
  122. * \return 0 if both are equal, 1 if first element is larger or -1
  123. * if first element is lower than the second one.
  124. */
  125. int SnmpOidCmpIdx(const OID * name1, size_t len1, const OID * name2, size_t len2, OID index)
  126. {
  127. size_t len = (len1 < len2) ? len1 : len2;
  128. /* Compare elements up to the length of shortest name. */
  129. int rc = SnmpOidLenCmp(name1, name2, len);
  130. /* If equal, compare lengths. */
  131. if (rc == 0) {
  132. if (len1 < len2) {
  133. rc = -1;
  134. } else if (len1 > len2) {
  135. if (*(name1 + len) < index) {
  136. /* First is lower than second. */
  137. rc = -1;
  138. } else if (*(name1 + len) > index) {
  139. /* First is larger than second. */
  140. rc = 1;
  141. } else if (len1 > len2 + 1) {
  142. rc = 1;
  143. }
  144. }
  145. }
  146. return rc;
  147. }
  148. /*
  149. * This should be faster than doing a SnmpOidCmp for different
  150. * length OIDs, since the length is checked first and if != returns
  151. * immediately.
  152. *
  153. * Might be very slighly faster if lengths are ==.
  154. *
  155. * \param name1 A pointer to the first OID.
  156. * \param len1 Length of the first OID.
  157. * \param name2 A pointer to the second OID.
  158. * \param len2 Length of the second OID.
  159. *
  160. * \return 0 if they are equal, -1 if they are not.
  161. */
  162. int SnmpOidEquals(const OID * name1, size_t len1, const OID * name2, size_t len2)
  163. {
  164. if (len1 != len2 || memcmp(name1, name2, len1)) {
  165. return -1;
  166. }
  167. return 0;
  168. }
  169. void SnmpStatsInc(int which)
  170. {
  171. if (which >= 0 && which < SNMP_STAT_MAX) {
  172. statistics[which]++;
  173. }
  174. }
  175. uint32_t SnmpStatsGet(int which)
  176. {
  177. if (which >= 0 && which < SNMP_STAT_MAX) {
  178. return statistics[which];
  179. }
  180. return 0;
  181. }
  182. void SnmpStatsSet(int which, uint32_t value)
  183. {
  184. if (which >= 0 && which < SNMP_STAT_MAX) {
  185. statistics[which] = value;
  186. }
  187. }
  188. /*@}*/