allow peers and users to go into a hash table
[asterisk/asterisk.git] / res / res_snmp.c
index 5fb48d5..1fac2c3 100644 (file)
  * \brief SNMP Agent / SubAgent support for Asterisk
  *
  * \author Thorsten Lockert <tholo@voop.as>
+ *
+ * \extref Uses the Net-SNMP libraries available at
+ *      http://net-snmp.sourceforge.net/
  */
 
 /*** MODULEINFO
-       <defaultenabled>no</defaultenabled>
+       <depend>netsnmp</depend>
  ***/
 
 #include "asterisk.h"
@@ -37,107 +40,84 @@ int res_snmp_enabled;
 
 static pthread_t thread = AST_PTHREADT_NULL;
 
+/*!
+ * \brief Load res_snmp.conf config file
+ * \return 1 on load, 0 file does not exist
+*/
 static int load_config(void)
 {
-    struct ast_variable *var;
-    struct ast_config *cfg;
-    char *cat;
-       
+       struct ast_variable *var;
+       struct ast_config *cfg;
+       struct ast_flags config_flags = { 0 };
+       char *cat;
+
        res_snmp_enabled = 0;
        res_snmp_agentx_subagent = 1;
-    cfg = ast_config_load("res_snmp.conf");
-    if (cfg) {
-               cat = ast_category_browse(cfg, NULL);
-               while (cat) {
-                       var = ast_variable_browse(cfg, cat);
-
-                       if (strcasecmp(cat, "general") == 0) {
-                               while (var) {
-                                       if (strcasecmp(var->name, "subagent") == 0) {
-                                               if (ast_true(var->value))
-                                                       res_snmp_agentx_subagent = 1;
-                                               else if (ast_false(var->value))
-                                                       res_snmp_agentx_subagent = 0;
-                                               else {
-                                                       ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n",
-                                                                       var->value);
-                                                       ast_config_destroy(cfg);
-                                                       return 1;
-                                               }
-                                       } else if (strcasecmp(var->name, "enabled") == 0) {
-                                               res_snmp_enabled = ast_true(var->value);
-                                       } else {
-                                               ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n",
-                                                               var->name, cat);
+       cfg = ast_config_load("res_snmp.conf", config_flags);
+       if (!cfg) {
+               ast_log(LOG_WARNING, "Could not load res_snmp.conf\n");
+               return 0;
+       }
+       cat = ast_category_browse(cfg, NULL);
+       while (cat) {
+               var = ast_variable_browse(cfg, cat);
+
+               if (strcasecmp(cat, "general") == 0) {
+                       while (var) {
+                               if (strcasecmp(var->name, "subagent") == 0) {
+                                       if (ast_true(var->value))
+                                               res_snmp_agentx_subagent = 1;
+                                       else if (ast_false(var->value))
+                                               res_snmp_agentx_subagent = 0;
+                                       else {
+                                               ast_log(LOG_ERROR, "Value '%s' does not evaluate to true or false.\n", var->value);
                                                ast_config_destroy(cfg);
                                                return 1;
                                        }
-                                       var = var->next;
+                               } else if (strcasecmp(var->name, "enabled") == 0) {
+                                       res_snmp_enabled = ast_true(var->value);
+                               } else {
+                                       ast_log(LOG_ERROR, "Unrecognized variable '%s' in category '%s'\n", var->name, cat);
+                                       ast_config_destroy(cfg);
+                                       return 1;
                                }
+                               var = var->next;
                        }
-                       else {
-                               ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
-                               ast_config_destroy(cfg);
-                               return 1;
-                       }
-
-                       cat = ast_category_browse(cfg, cat);
+               } else {
+                       ast_log(LOG_ERROR, "Unrecognized category '%s'\n", cat);
+                       ast_config_destroy(cfg);
+                       return 1;
                }
-               ast_config_destroy(cfg);
-    }
 
-    return 0;
+               cat = ast_category_browse(cfg, cat);
+       }
+       ast_config_destroy(cfg);
+       return 1;
 }
 
-int load_module(void)
+static int load_module(void)
 {
-    load_config();
+       if(!load_config())
+               return AST_MODULE_LOAD_DECLINE;
 
-    ast_verbose(VERBOSE_PREFIX_1 "Loading [Sub]Agent Module\n");
+       ast_verb(1, "Loading [Sub]Agent Module\n");
 
-    res_snmp_dont_stop = 1;
+       res_snmp_dont_stop = 1;
        if (res_snmp_enabled)
-           return ast_pthread_create(&thread, NULL, agent_thread, NULL);
+               return ast_pthread_create_background(&thread, NULL, agent_thread, NULL);
        else
                return 0;
 }
 
-int unload_module(void)
+static int unload_module(void)
 {
-    ast_verbose(VERBOSE_PREFIX_1 "Unloading [Sub]Agent Module\n");
+       ast_verb(1, "Unloading [Sub]Agent Module\n");
 
-    res_snmp_dont_stop = 0;
-    return pthread_join(thread, NULL);
+       res_snmp_dont_stop = 0;
+       return ((thread != AST_PTHREADT_NULL) ? pthread_join(thread, NULL) : 0);
 }
 
-int reload(void)
-{
-    ast_verbose(VERBOSE_PREFIX_1 "Reloading [Sub]Agent Module\n");
-
-    res_snmp_dont_stop = 0;
-       if (thread != AST_PTHREADT_NULL)
-           pthread_join(thread, NULL);
-       thread = AST_PTHREADT_NULL;
-    load_config();
-
-    res_snmp_dont_stop = 1;
-       if (res_snmp_enabled)
-           return ast_pthread_create(&thread, NULL, agent_thread, NULL);
-       else
-               return 0;
-}
-
-int usecount(void)
-{
-    return 0;
-}
-
-const char *key(void)
-{
-    return ASTERISK_GPL_KEY;
-}
-
-const char *description(void)
-{
-    return MODULE_DESCRIPTION;
-}
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "SNMP [Sub]Agent for Asterisk",
+               .load = load_module,
+               .unload = unload_module,
+               );