default_store.c
上传用户:cxs890
上传日期:2021-05-22
资源大小:347k
文件大小:7k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* default_store.h: storage space for defaults */
  2. #include <config.h>
  3. #include <sys/types.h>
  4. #if HAVE_STDLIB_H
  5. #include <stdlib.h>
  6. #endif
  7. #if HAVE_NETINET_IN_H
  8. #include <netinet/in.h>
  9. #endif
  10. #if HAVE_STDLIB_H
  11. #include <stdlib.h>
  12. #endif
  13. #if HAVE_STRING_H
  14. #include <string.h>
  15. #else
  16. #include <strings.h>
  17. #endif
  18. #if HAVE_WINSOCK_H
  19. #include <ip/socket.h>
  20. #endif
  21. #if HAVE_DMALLOC_H
  22. #include <dmalloc.h>
  23. #endif
  24. #include "asn1.h"
  25. #include "snmp_api.h"
  26. #include "snmp_debug.h"
  27. #include "snmp_logging.h"
  28. #include "tools.h"
  29. #include "read_config.h"
  30. #include "default_store.h"
  31. #include "system.h"
  32. #include <libsys/misc.h>
  33. struct ds_read_config *ds_configs = NULL;
  34. int ds_integers[DS_MAX_IDS][DS_MAX_SUBIDS];
  35. char ds_booleans[DS_MAX_IDS][DS_MAX_SUBIDS/8];  /* bit vector storage. */
  36. char *ds_strings[DS_MAX_IDS][DS_MAX_SUBIDS];  /* bit vector storage. */
  37. /*extern char *strdup (const char*);
  38. extern int strcasecmp(const char* s1, const char* s2);*/
  39. extern char *staticstrdup(const char *);
  40. int
  41. ds_set_boolean(int storeid, int which, int value) {
  42.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  43.       storeid < 0 || which < 0)
  44.     return SNMPERR_GENERR;
  45.     
  46.   DEBUGMSGTL(("ds_set_boolean","Setting %d:%d = %d/%sn", storeid, which,
  47.               value, ((value)?"True":"False")));
  48.   if (value > 0)
  49.     ds_booleans[storeid][which/8] |= (1 << (which%8));
  50.   else
  51.     ds_booleans[storeid][which/8] &= (0xff7f >> (7-(which%8)));
  52.   return SNMPERR_SUCCESS;
  53. }
  54. int
  55. ds_toggle_boolean(int storeid, int which) {
  56.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  57.       storeid < 0 || which < 0)
  58.     return SNMPERR_GENERR;
  59.     
  60.   if ((ds_booleans[storeid][which/8] & (1 << (which % 8))) == 0)
  61.     ds_booleans[storeid][which/8] |= (1 << (which%8));
  62.   else
  63.     ds_booleans[storeid][which/8] &= (0xff7f >> (7-(which%8)));
  64.   DEBUGMSGTL(("ds_toggle_boolean","Setting %d:%d = %d/%sn", storeid, which,
  65.               ds_booleans[storeid][which/8],
  66.               ((ds_booleans[storeid][which/8])?"True":"False")));
  67.   return SNMPERR_SUCCESS;
  68. }
  69. int
  70. ds_get_boolean(int storeid, int which) {
  71.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  72.       storeid < 0 || which < 0)
  73.     return SNMPERR_GENERR;
  74.   return ((ds_booleans[storeid][which/8] & (1 << (which%8))) ? 1 : 0);
  75. }
  76. int
  77. ds_set_int(int storeid, int which, int value) {
  78.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  79.       storeid < 0 || which < 0)
  80.     return SNMPERR_GENERR;
  81.   DEBUGMSGTL(("ds_set_int","Setting %d:%d = %dn", storeid, which, value));
  82.   ds_integers[storeid][which] = value;
  83.   return SNMPERR_SUCCESS;
  84. }
  85. int
  86. ds_get_int(int storeid, int which) {
  87.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  88.       storeid < 0 || which < 0)
  89.     return SNMPERR_GENERR;
  90.   return (ds_integers[storeid][which]);
  91. }
  92. int
  93. ds_set_string(int storeid, int which, const char *value) {
  94.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  95.       storeid < 0 || which < 0)
  96.     return SNMPERR_GENERR;
  97.     
  98.   DEBUGMSGTL(("ds_set_string","Setting %d:%d = %sn", storeid, which,
  99.               value));
  100.   if (ds_strings[storeid][which] != NULL)
  101.     free(ds_strings[storeid][which]);
  102.   if (value)
  103.     ds_strings[storeid][which] = strdup(value);
  104.   else 
  105.     ds_strings[storeid][which] = NULL;
  106.   
  107.   return SNMPERR_SUCCESS;
  108. }
  109. char *
  110. ds_get_string(int storeid, int which) {
  111.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  112.       storeid < 0 || which < 0)
  113.     return NULL;
  114.   return (ds_strings[storeid][which]);
  115. }
  116. void
  117. ds_handle_config(const char *token, char *line) {
  118.   struct ds_read_config *drsp;
  119.   char buf[SNMP_MAXBUF];
  120.   int itmp;
  121.   DEBUGMSGTL(("ds_handle_config", "handling %sn", token));
  122.   for(drsp = ds_configs; drsp != NULL && strcasecmp(token, drsp->token) != 0;
  123.       drsp = drsp->next);
  124.   if (drsp != NULL) {
  125.     DEBUGMSGTL(("ds_handle_config",
  126.                 "setting: token=%s, type=%d, id=%d, which=%dn",
  127.                 drsp->token, drsp->type, drsp->storeid, drsp->which));
  128.     switch (drsp->type) {
  129.       case ASN_BOOLEAN:
  130.         if (strncasecmp(line,"yes",3) == 0 || strncasecmp(line,"true",4) == 0) {
  131.           itmp = 1;
  132.         } else if (strncasecmp(line,"no",3) == 0 ||
  133.                 strncasecmp(line,"false",5) == 0) {
  134.           itmp = 0;
  135.         } else if (atoi(line) > 0) {
  136.           itmp = 1;
  137.         } else {
  138.           itmp = 0;
  139.         }
  140.         ds_set_boolean(drsp->storeid, drsp->which, itmp);
  141.         DEBUGMSGTL(("ds_handle_config", "bool: %dn", itmp));
  142.         break;
  143.       case ASN_INTEGER:
  144.         ds_set_int(drsp->storeid, drsp->which, atoi(line));
  145.         DEBUGMSGTL(("ds_handle_config", "int: %dn", atoi(line)));
  146.         break;
  147.       case ASN_OCTET_STR:
  148.         if (*line == '"') {
  149.             copy_word(line, buf);
  150.             ds_set_string(drsp->storeid, drsp->which, buf);
  151.         } else {
  152.             ds_set_string(drsp->storeid, drsp->which, line);
  153.         }
  154.         DEBUGMSGTL(("ds_handle_config", "string: %sn", line));
  155.         break;
  156.       default:
  157.         snmp_log(LOG_CRIT,"ds_handle_config *** unknown type %dn", drsp->type);
  158.         break;
  159.     }
  160.   } else {
  161.     snmp_log(LOG_CRIT, "ds_handle_config *** no registration for %sn", token);
  162.   }
  163. }
  164. int
  165. ds_register_config(u_char type, const char *ftype, const char *token,
  166.                    int storeid, int which) {
  167.   struct ds_read_config *drsp;
  168.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  169.       storeid < 0 || which < 0 || token == NULL)
  170.     return SNMPERR_GENERR;
  171.   if (ds_configs == NULL) {
  172.     ds_configs = SNMP_MALLOC_STRUCT(ds_read_config);
  173.     drsp = ds_configs;
  174.   } else {
  175.     for(drsp = ds_configs; drsp->next != NULL; drsp = drsp->next);
  176.     drsp->next = SNMP_MALLOC_STRUCT(ds_read_config);
  177.     drsp = drsp->next;
  178.   }
  179.   drsp->type = type;
  180.   drsp->token = staticstrdup(token);
  181.   drsp->storeid = storeid;
  182.   drsp->which = which;
  183.   switch (type) {
  184.     case ASN_BOOLEAN:
  185.       register_config_handler(ftype, token, ds_handle_config, NULL,"(1|yes|true|0|no|false)");
  186.       break;
  187.     case ASN_INTEGER:
  188.       register_config_handler(ftype, token, ds_handle_config, NULL,"integerValue");
  189.       break;
  190.     case ASN_OCTET_STR:
  191.       register_config_handler(ftype, token, ds_handle_config, NULL,"string");
  192.       break;
  193.     
  194.   }
  195.   return SNMPERR_SUCCESS;
  196. }
  197. int
  198. ds_register_premib(u_char type, const char *ftype, const char *token,
  199.                    int storeid, int which) {
  200.   struct ds_read_config *drsp;
  201.   if (storeid >= DS_MAX_IDS || which >= DS_MAX_SUBIDS ||
  202.       storeid < 0 || which < 0 || token == NULL)
  203.     return SNMPERR_GENERR;
  204.   if (ds_configs == NULL) {
  205.     ds_configs = SNMP_MALLOC_STRUCT(ds_read_config);
  206.     drsp = ds_configs;
  207.   } else {
  208.     for(drsp = ds_configs; drsp->next != NULL; drsp = drsp->next);
  209.     drsp->next = SNMP_MALLOC_STRUCT(ds_read_config);
  210.     drsp = drsp->next;
  211.   }
  212.   drsp->type = type;
  213.   drsp->token = staticstrdup(token);
  214.   drsp->storeid = storeid;
  215.   drsp->which = which;
  216.   switch (type) {
  217.     case ASN_BOOLEAN:
  218.       register_premib_handler(ftype, token, ds_handle_config, NULL,"(1|yes|true|0|no|false)");
  219.       break;
  220.     case ASN_INTEGER:
  221.       register_premib_handler(ftype, token, ds_handle_config, NULL,"integerValue");
  222.       break;
  223.     case ASN_OCTET_STR:
  224.       register_premib_handler(ftype, token, ds_handle_config, NULL,"string");
  225.       break;
  226.     
  227.   }
  228.   return SNMPERR_SUCCESS;
  229. }