snmp_config.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <sys/types.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #ifndef WIN32
  37. #include <memdebug.h>
  38. #endif
  39. #include <pro/snmp_config.h>
  40. /*!
  41. * \addtogroup xgSNMP
  42. */
  43. /*@{*/
  44. static VIEW_LIST *views;
  45. static COMMUNITY_LIST *communities;
  46. /*!
  47. * \param name Symbolic name of this view.
  48. * \param type Either SNMP_VIEW_INCLUDED or SNMP_VIEW_EXCLUDED.
  49. *
  50. * \return View index on success. Otherwise -1 is returned.
  51. */
  52. int SnmpViewCreate(const char *name, const OID * subtree, size_t subtreelen, int type)
  53. {
  54. static int nextview = 1;
  55. VIEW_LIST *vp;
  56. VIEW_LIST *nvp;
  57. VIEW_LIST *prev = NULL;
  58. size_t i;
  59. /* Check name length. */
  60. if (strlen(name) > (sizeof(vp->view_name) - 1)) {
  61. return -1;
  62. }
  63. /* Check if this name exists already. */
  64. for (vp = views; vp; prev = vp, vp = vp->next) {
  65. if (strcmp(name, vp->view_name) == 0) {
  66. break;
  67. }
  68. }
  69. /* Allocate a new view entry. */
  70. nvp = malloc(sizeof(VIEW_LIST));
  71. memset(nvp, 0, sizeof(VIEW_LIST));
  72. strcpy(nvp->view_name, name);
  73. nvp->view_type = type;
  74. nvp->view_subtree_len = subtreelen;
  75. for (i = 0; i < subtreelen; i++) {
  76. nvp->view_subtree[i] = subtree[i];
  77. }
  78. /* Set index, either of the existing entry or a new one. */
  79. nvp->view_index = vp ? vp->view_index : nextview++;
  80. /* Add the new entry to the linked list. */
  81. if (views) {
  82. for (; vp; prev = vp, vp = vp->next);
  83. prev->next = nvp;
  84. } else {
  85. views = nvp;
  86. }
  87. return nvp->view_index;
  88. }
  89. int SnmpViewFind(char *name)
  90. {
  91. VIEW_LIST *vp;
  92. if (strcmp(name, "-") == 0) {
  93. return 0;
  94. }
  95. for (vp = views; vp; vp = vp->next) {
  96. if (strcmp(vp->view_name, name) == 0) {
  97. return vp->view_index;
  98. }
  99. }
  100. return -1;
  101. }
  102. /*!
  103. * \brief Find community entry by name.
  104. *
  105. * \param name Community name.
  106. * \param readView Pointer to a variable that receives the view index
  107. * for read access.
  108. * \param writeView Pointer to a variable that receives the view index
  109. * for write access.
  110. *
  111. * \return 0 on success, -1 otherwise.
  112. */
  113. int SnmpCommunityFind(const char *name, int *readView, int *writeView)
  114. {
  115. COMMUNITY_LIST *cp;
  116. for (cp = communities; cp; cp = cp->next) {
  117. if (strcmp(cp->comm_name, name) == 0) {
  118. if (readView) {
  119. *readView = cp->comm_read_view;
  120. }
  121. if (writeView) {
  122. *writeView = cp->comm_write_view;
  123. }
  124. return 0;
  125. }
  126. }
  127. return -1;
  128. }
  129. /*!
  130. * \brief Create a community entry.
  131. *
  132. * \param name Community name.
  133. * \param readView View index for read access, obtained from a previous
  134. * call to SnmpViewCreate().
  135. * \param writeView View index for write access, obtained from a previous
  136. * call to SnmpViewCreate().
  137. *
  138. * \return 0 on success, -1 otherwise.
  139. */
  140. int SnmpCommunityCreate(const char *name, int readView, int writeView)
  141. {
  142. COMMUNITY_LIST *cp;
  143. COMMUNITY_LIST *prev = 0;
  144. if (strlen(name) > (sizeof(cp->comm_name) - 1)) {
  145. return -1;
  146. }
  147. for (cp = communities; cp; cp = cp->next) {
  148. if (strcmp(name, cp->comm_name) == 0) {
  149. return 0;
  150. }
  151. prev = cp;
  152. }
  153. cp = malloc(sizeof(COMMUNITY_LIST));
  154. memset(cp, 0, sizeof(COMMUNITY_LIST));
  155. strcpy(cp->comm_name, name);
  156. cp->comm_read_view = readView;
  157. cp->comm_write_view = writeView;
  158. if (prev) {
  159. prev->next = cp;
  160. } else {
  161. communities = cp;
  162. }
  163. return 0;
  164. }
  165. /*@}*/