Merged revisions 136062 via svnmerge from
[asterisk/asterisk.git] / channels / iax2-provision.c
index 8febd56..8bdfdbf 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -38,11 +34,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include <sys/socket.h>
 
 #include "asterisk/config.h"
-#include "asterisk/logger.h"
 #include "asterisk/cli.h"
 #include "asterisk/lock.h"
 #include "asterisk/frame.h"
-#include "asterisk/options.h"
 #include "asterisk/md5.h"
 #include "asterisk/astdb.h"
 #include "asterisk/utils.h"
@@ -51,10 +45,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "iax2-provision.h"
 #include "iax2-parser.h"
 
-#ifndef IPTOS_MINCOST
-#define IPTOS_MINCOST 0x02
-#endif
-
 static int provinit = 0;
 
 struct iax_template {
@@ -97,7 +87,7 @@ char *iax_provflags2str(char *buf, int buflen, unsigned int flags)
        
        buf[0] = '\0';
 
-       for (x = 0; x < sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
+       for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
                if (flags & iax_flags[x].value){
                        strncat(buf, iax_flags[x].name, buflen - strlen(buf) - 1);
                        strncat(buf, ",", buflen - strlen(buf) - 1);
@@ -126,7 +116,7 @@ static unsigned int iax_str2flags(const char *buf)
                else
                        len = 0;
                found = 0;
-               for (x=0;x<sizeof(iax_flags) / sizeof(iax_flags[0]); x++) {
+               for (x = 0; x < ARRAY_LEN(iax_flags); x++) {
                        if ((len && !strncasecmp(iax_flags[x].name, buf, len)) ||
                            (!len && !strcasecmp(iax_flags[x].name, buf))) {
                                flags |= iax_flags[x].value;
@@ -166,15 +156,16 @@ char *iax_prov_complete_template(const char *line, const char *word, int pos, in
        char *ret = NULL;
        int wordlen = strlen(word);
 
-       ast_mutex_lock(&provlock);
-       for (c = templates; c; c = c->next) {
-               if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
-                       ret = strdup(c->name);
-                       break;
+       if (pos == 3) {
+               ast_mutex_lock(&provlock);
+               for (c = templates; c; c = c->next) {
+                       if (!strncasecmp(word, c->name, wordlen) && ++which > state) {
+                               ret = ast_strdup(c->name);
+                               break;
+                       }
                }
+               ast_mutex_unlock(&provlock);
        }
-       ast_mutex_unlock(&provlock);
-       
        return ret;
 }
 
@@ -250,11 +241,11 @@ int iax_provision_version(unsigned int *version, const char *template, int force
                if (strcmp(tmp, "u")) {
                        ret = iax_provision_build(&ied, version, template, force);
                        if (ret)
-                               ast_log(LOG_DEBUG, "Unable to create provisioning packet for '%s'\n", template);
+                               ast_debug(1, "Unable to create provisioning packet for '%s'\n", template);
                } else
                        ret = -1;
-       } else if (option_debug)
-               ast_log(LOG_DEBUG, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
+       } else
+               ast_debug(1, "Retrieved cached version '%s' = '%08x'\n", tmp, *version);
        ast_mutex_unlock(&provlock);
        return ret;
 }
@@ -283,7 +274,7 @@ static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg,
                if (!src) {
                        src = iax_template_find(def, 0);
                        if (!src)
-                               ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.", def, s);
+                               ast_log(LOG_WARNING, "Unable to locate default base template '%s' for creating '%s', omitting.\n", def, s);
                }
                if (!src)
                        return -1;
@@ -332,7 +323,7 @@ static int iax_template_parse(struct iax_template *cur, struct ast_config *cfg,
                                ast_log(LOG_WARNING, "Ignoring invalid codec '%s' for '%s' at line %d\n", v->value, s, v->lineno);
                } else if (!strcasecmp(v->name, "tos")) {
                        if (ast_str2tos(v->value, &cur->tos))
-                               ast_log(LOG_WARNING, "Invalid tos value at line %d, see doc/ip-tos.txt for more information.\n", v->lineno);
+                               ast_log(LOG_WARNING, "Invalid tos value at line %d, refer to QoS documentation\n", v->lineno);
                } else if (!strcasecmp(v->name, "user")) {
                        strncpy(cur->user, v->value, sizeof(cur->user) - 1);
                        if (strcmp(cur->user, v->value))
@@ -376,13 +367,12 @@ static int iax_process_template(struct ast_config *cfg, char *s, char *def)
        }
        if (!cur) {
                mallocd = 1;
-               cur = malloc(sizeof(struct iax_template));
+               cur = ast_calloc(1, sizeof(*cur));
                if (!cur) {
                        ast_log(LOG_WARNING, "Out of memory!\n");
                        return -1;
                }
                /* Initialize entry */
-               memset(cur, 0, sizeof(*cur));
                strncpy(cur->name, s, sizeof(cur->name) - 1);
                cur->dead = 1;
        }
@@ -399,11 +389,6 @@ static int iax_process_template(struct ast_config *cfg, char *s, char *def)
        return 0;
 }
 
-static char show_provisioning_usage[] = 
-"Usage: iax list provisioning [template]\n"
-"       Lists all known IAX provisioning templates or a\n"
-"       specific one if specified.\n";
-
 static const char *ifthere(const char *s)
 {
        if (strlen(s))
@@ -425,56 +410,62 @@ static const char *iax_server(unsigned int addr)
 }
 
 
-static int iax_show_provisioning(int fd, int argc, char *argv[])
+static char *iax_show_provisioning(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct iax_template *cur;
        char server[INET_ADDRSTRLEN];
        char alternate[INET_ADDRSTRLEN];
        char flags[80]; /* Has to be big enough for 'flags' too */
        int found = 0;
-       if ((argc != 3) && (argc != 4))
-               return RESULT_SHOWUSAGE;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "iax2 show provisioning";
+               e->usage =
+                       "Usage: iax2 show provisioning [template]\n"
+                       "       Lists all known IAX provisioning templates or a\n"
+                       "       specific one if specified.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return iax_prov_complete_template(a->line, a->word, a->pos, a->n);
+       }
+
+       if ((a->argc != 3) && (a->argc != 4))
+               return CLI_SHOWUSAGE;
        ast_mutex_lock(&provlock);
        for (cur = templates;cur;cur = cur->next) {
-               if ((argc == 3) || (!strcasecmp(argv[3], cur->name)))  {
+               if ((a->argc == 3) || (!strcasecmp(a->argv[3], cur->name)))  {
                        if (found) 
-                               ast_cli(fd, "\n");
+                               ast_cli(a->fd, "\n");
                        ast_copy_string(server, iax_server(cur->server), sizeof(server));
                        ast_copy_string(alternate, iax_server(cur->altserver), sizeof(alternate));
-                       ast_cli(fd, "== %s ==\n", cur->name);
-                       ast_cli(fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
-                       ast_cli(fd, "Username:     %s\n", ifthere(cur->user));
-                       ast_cli(fd, "Secret:       %s\n", ifthere(cur->pass));
-                       ast_cli(fd, "Language:     %s\n", ifthere(cur->lang));
-                       ast_cli(fd, "Bind Port:    %d\n", cur->port);
-                       ast_cli(fd, "Server:       %s\n", server);
-                       ast_cli(fd, "Server Port:  %d\n", cur->serverport);
-                       ast_cli(fd, "Alternate:    %s\n", alternate);
-                       ast_cli(fd, "Flags:        %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
-                       ast_cli(fd, "Format:       %s\n", ast_getformatname(cur->format));
-                       ast_cli(fd, "TOS:          0x%x\n", cur->tos);
+                       ast_cli(a->fd, "== %s ==\n", cur->name);
+                       ast_cli(a->fd, "Base Templ:   %s\n", strlen(cur->src) ? cur->src : "<none>");
+                       ast_cli(a->fd, "Username:     %s\n", ifthere(cur->user));
+                       ast_cli(a->fd, "Secret:       %s\n", ifthere(cur->pass));
+                       ast_cli(a->fd, "Language:     %s\n", ifthere(cur->lang));
+                       ast_cli(a->fd, "Bind Port:    %d\n", cur->port);
+                       ast_cli(a->fd, "Server:       %s\n", server);
+                       ast_cli(a->fd, "Server Port:  %d\n", cur->serverport);
+                       ast_cli(a->fd, "Alternate:    %s\n", alternate);
+                       ast_cli(a->fd, "Flags:        %s\n", iax_provflags2str(flags, sizeof(flags), cur->flags));
+                       ast_cli(a->fd, "Format:       %s\n", ast_getformatname(cur->format));
+                       ast_cli(a->fd, "TOS:          0x%x\n", cur->tos);
                        found++;
                }
        }
        ast_mutex_unlock(&provlock);
        if (!found) {
-               if (argc == 3)
-                       ast_cli(fd, "No provisioning templates found\n");
+               if (a->argc == 3)
+                       ast_cli(a->fd, "No provisioning templates found\n");
                else
-                       ast_cli(fd, "No provisioning template matching '%s' found\n", argv[3]);
+                       ast_cli(a->fd, "No provisioning template matching '%s' found\n", a->argv[3]);
        }
-       return RESULT_SUCCESS;
+       return CLI_SUCCESS;
 }
 
-static struct ast_cli_entry cli_iax2_show_provisioning = {
-       { "iax2", "show", "provisioning", NULL },
-       iax_show_provisioning, NULL,
-       NULL, iax_prov_complete_template };
-
 static struct ast_cli_entry cli_iax2_provision[] = {
-       { { "iax2", "show", "provisioning", NULL },
-       iax_show_provisioning, "Display iax provisioning",
-       show_provisioning_usage, iax_prov_complete_template, &cli_iax2_show_provisioning },
+       AST_CLI_DEFINE(iax_show_provisioning, "Display iax provisioning"),
 };
 
 static int iax_provision_init(void)
@@ -491,35 +482,39 @@ int iax_provision_unload(void)
        return 0;
 }
 
-int iax_provision_reload(void)
+int iax_provision_reload(int reload)
 {
        struct ast_config *cfg;
        struct iax_template *cur, *prev, *next;
        char *cat;
        int found = 0;
+       struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
        if (!provinit)
                iax_provision_init();
-       /* Mark all as dead.  No need for locking */
-       cur = templates;
-       while(cur) {
-               cur->dead = 1;
-               cur = cur->next;
-       }
-       cfg = ast_config_load("iaxprov.conf");
-       if (cfg) {
+       
+       cfg = ast_config_load2("iaxprov.conf", "chan_iax2", config_flags);
+       if (cfg != NULL && cfg != CONFIG_STATUS_FILEUNCHANGED) {
+               /* Mark all as dead.  No need for locking */
+               cur = templates;
+               while(cur) {
+                       cur->dead = 1;
+                       cur = cur->next;
+               }
+
                /* Load as appropriate */
                cat = ast_category_browse(cfg, NULL);
                while(cat) {
                        if (strcasecmp(cat, "general")) {
                                iax_process_template(cfg, cat, found ? "default" : NULL);
                                found++;
-                               if (option_verbose > 2)
-                                       ast_verbose(VERBOSE_PREFIX_3 "Loaded provisioning template '%s'\n", cat);
+                               ast_verb(3, "Loaded provisioning template '%s'\n", cat);
                        }
                        cat = ast_category_browse(cfg, cat);
                }
                ast_config_destroy(cfg);
-       } else
+       } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
+               return 0;
+       else
                ast_log(LOG_NOTICE, "No IAX provisioning configuration found, IAX provisioning disabled.\n");
        ast_mutex_lock(&provlock);
        /* Drop dead entries while locked */
@@ -532,7 +527,7 @@ int iax_provision_reload(void)
                                prev->next = next;
                        else
                                templates = next;
-                       free(cur);
+                       ast_free(cur);
                } else 
                        prev = cur;
                cur = next;