Move IAX firmware related functionality into separate files.
authorSean Bright <sean@malleable.com>
Thu, 31 Jan 2013 19:52:48 +0000 (19:52 +0000)
committerSean Bright <sean@malleable.com>
Thu, 31 Jan 2013 19:52:48 +0000 (19:52 +0000)
This patch is mostly a reorganization of existing code with a few exceptions:

* Added doxygen comments to all of the extracted functions.

* Split reload_firmware(int unload) into iax_firmware_reload() and
  iax_firmware_unload() for readability.

* Create iax_firmware_traverse() to support the 'iax2 show firmware' CLI
  command.

* Renamed iax_check_version() to iax_firmware_get_version() and change its
  arguments and return value so that it returns a success/failure value and sets
  the selected version into an out parameter to avoid confusion with failure and
  version 0.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@380695 65c4cc65-6c06-0410-ace0-fbb531ad65f3

channels/chan_iax2.c
channels/iax2/firmware.c [new file with mode: 0644]
channels/iax2/include/firmware.h [new file with mode: 0644]
channels/iax2/include/parser.h

index c45647f..d0eb591 100644 (file)
@@ -64,7 +64,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include <sys/stat.h>
 #include <regex.h>
 
-#include "asterisk/paths.h"    /* need ast_config_AST_DATA_DIR for firmware */
+#include "asterisk/paths.h"
 
 #include "asterisk/lock.h"
 #include "asterisk/frame.h" 
@@ -103,6 +103,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/netsock2.h"
 
 #include "iax2/include/iax2.h"
+#include "iax2/include/firmware.h"
 #include "iax2/include/parser.h"
 #include "iax2/include/provision.h"
 #include "jitterbuf.h"
@@ -575,15 +576,6 @@ struct iax2_trunk_peer {
 
 static AST_LIST_HEAD_STATIC(tpeers, iax2_trunk_peer);
 
-struct iax_firmware {
-       AST_LIST_ENTRY(iax_firmware) list;
-       int fd;
-       int mmaplen;
-       int dead;
-       struct ast_iax2_firmware_header *fwh;
-       unsigned char *buf;
-};
-
 enum iax_reg_state {
        REG_STATE_UNREGISTERED = 0,
        REG_STATE_REGSENT,
@@ -950,8 +942,6 @@ struct callno_entry {
        unsigned char validated;
 };
 
-static AST_LIST_HEAD_STATIC(firmwares, iax_firmware);
-
 enum {
        /*! Extension exists */
        CACHE_FLAG_EXISTS      = (1 << 0),
@@ -3084,253 +3074,6 @@ static int iax2_queue_control_data(int callno,
        }
        return 0;
 }
-static void destroy_firmware(struct iax_firmware *cur)
-{
-       /* Close firmware */
-       if (cur->fwh) {
-               munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
-       }
-       close(cur->fd);
-       ast_free(cur);
-}
-
-static int try_firmware(char *s)
-{
-       struct stat stbuf;
-       struct iax_firmware *cur = NULL;
-       int ifd, fd, res, len, chunk;
-       struct ast_iax2_firmware_header *fwh, fwh2;
-       struct MD5Context md5;
-       unsigned char sum[16], buf[1024];
-       char *s2, *last;
-
-       s2 = ast_alloca(strlen(s) + 100);
-
-       last = strrchr(s, '/');
-       if (last)
-               last++;
-       else
-               last = s;
-
-       snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)ast_random());
-
-       if (stat(s, &stbuf) < 0) {
-               ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
-               return -1;
-       }
-
-       /* Make sure it's not a directory */
-       if (S_ISDIR(stbuf.st_mode))
-               return -1;
-       ifd = open(s, O_RDONLY);
-       if (ifd < 0) {
-               ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
-               return -1;
-       }
-       fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
-       if (fd < 0) {
-               ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
-               close(ifd);
-               return -1;
-       }
-       /* Unlink our newly created file */
-       unlink(s2);
-       
-       /* Now copy the firmware into it */
-       len = stbuf.st_size;
-       while(len) {
-               chunk = len;
-               if (chunk > sizeof(buf))
-                       chunk = sizeof(buf);
-               res = read(ifd, buf, chunk);
-               if (res != chunk) {
-                       ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
-                       close(ifd);
-                       close(fd);
-                       return -1;
-               }
-               res = write(fd, buf, chunk);
-               if (res != chunk) {
-                       ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
-                       close(ifd);
-                       close(fd);
-                       return -1;
-               }
-               len -= chunk;
-       }
-       close(ifd);
-       /* Return to the beginning */
-       lseek(fd, 0, SEEK_SET);
-       if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
-               ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
-               close(fd);
-               return -1;
-       }
-       if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
-               ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
-               close(fd);
-               return -1;
-       }
-       if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
-               ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
-               close(fd);
-               return -1;
-       }
-       if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
-               ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
-               close(fd);
-               return -1;
-       }
-       fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 
-       if (fwh == MAP_FAILED) {
-               ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
-               close(fd);
-               return -1;
-       }
-       MD5Init(&md5);
-       MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
-       MD5Final(sum, &md5);
-       if (memcmp(sum, fwh->chksum, sizeof(sum))) {
-               ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
-               munmap((void*)fwh, stbuf.st_size);
-               close(fd);
-               return -1;
-       }
-
-       AST_LIST_TRAVERSE(&firmwares, cur, list) {
-               if (!strcmp((char *)cur->fwh->devname, (char *)fwh->devname)) {
-                       /* Found a candidate */
-                       if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
-                               /* The version we have on loaded is older, load this one instead */
-                               break;
-                       /* This version is no newer than what we have.  Don't worry about it.
-                          We'll consider it a proper load anyhow though */
-                       munmap((void*)fwh, stbuf.st_size);
-                       close(fd);
-                       return 0;
-               }
-       }
-       
-       if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
-               cur->fd = -1;
-               AST_LIST_INSERT_TAIL(&firmwares, cur, list);
-       }
-       
-       if (cur) {
-               if (cur->fwh)
-                       munmap((void*)cur->fwh, cur->mmaplen);
-               if (cur->fd > -1)
-                       close(cur->fd);
-               cur->fwh = fwh;
-               cur->fd = fd;
-               cur->mmaplen = stbuf.st_size;
-               cur->dead = 0;
-       }
-       
-       return 0;
-}
-
-static int iax_check_version(char *dev)
-{
-       int res = 0;
-       struct iax_firmware *cur = NULL;
-
-       if (ast_strlen_zero(dev))
-               return 0;
-
-       AST_LIST_LOCK(&firmwares);
-       AST_LIST_TRAVERSE(&firmwares, cur, list) {
-               if (!strcmp(dev, (char *)cur->fwh->devname)) {
-                       res = ntohs(cur->fwh->version);
-                       break;
-               }
-       }
-       AST_LIST_UNLOCK(&firmwares);
-
-       return res;
-}
-
-static int iax_firmware_append(struct iax_ie_data *ied, const unsigned char *dev, unsigned int desc)
-{
-       int res = -1;
-       unsigned int bs = desc & 0xff;
-       unsigned int start = (desc >> 8) & 0xffffff;
-       unsigned int bytes;
-       struct iax_firmware *cur;
-
-       if (ast_strlen_zero((char *)dev) || !bs)
-               return -1;
-
-       start *= bs;
-       
-       AST_LIST_LOCK(&firmwares);
-       AST_LIST_TRAVERSE(&firmwares, cur, list) {
-               if (strcmp((char *)dev, (char *)cur->fwh->devname))
-                       continue;
-               iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
-               if (start < ntohl(cur->fwh->datalen)) {
-                       bytes = ntohl(cur->fwh->datalen) - start;
-                       if (bytes > bs)
-                               bytes = bs;
-                       iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
-               } else {
-                       bytes = 0;
-                       iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
-               }
-               if (bytes == bs)
-                       res = 0;
-               else
-                       res = 1;
-               break;
-       }
-       AST_LIST_UNLOCK(&firmwares);
-
-       return res;
-}
-
-
-static void reload_firmware(int unload)
-{
-       struct iax_firmware *cur = NULL;
-       DIR *fwd;
-       struct dirent *de;
-       char dir[256], fn[256];
-
-       AST_LIST_LOCK(&firmwares);
-
-       /* Mark all as dead */
-       AST_LIST_TRAVERSE(&firmwares, cur, list)
-               cur->dead = 1;
-
-       /* Now that we have marked them dead... load new ones */
-       if (!unload) {
-               snprintf(dir, sizeof(dir), "%s/firmware/iax", ast_config_AST_DATA_DIR);
-               fwd = opendir(dir);
-               if (fwd) {
-                       while((de = readdir(fwd))) {
-                               if (de->d_name[0] != '.') {
-                                       snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
-                                       if (!try_firmware(fn)) {
-                                               ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
-                                       }
-                               }
-                       }
-                       closedir(fwd);
-               } else 
-                       ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
-       }
-
-       /* Clean up leftovers */
-       AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
-               if (!cur->dead)
-                       continue;
-               AST_LIST_REMOVE_CURRENT(list);
-               destroy_firmware(cur);
-       }
-       AST_LIST_TRAVERSE_SAFE_END;
-
-       AST_LIST_UNLOCK(&firmwares);
-}
 
 /*!
  * \note This function assumes that iaxsl[callno] is locked when called.
@@ -7062,10 +6805,21 @@ static int manager_iax2_show_netstats(struct mansession *s, const struct message
        return RESULT_SUCCESS;
 }
 
-static char *handle_cli_iax2_show_firmware(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+static int firmware_show_callback(struct ast_iax2_firmware_header *header,
+       void *user_data)
 {
-       struct iax_firmware *cur = NULL;
+       int *fd = user_data;
+
+       ast_cli(*fd, "%-15.15s  %-15d %-15d\n",
+               header->devname,
+               ntohs(header->version),
+               (int) ntohl(header->datalen));
+
+       return 0;
+}
 
+static char *handle_cli_iax2_show_firmware(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
        switch (cmd) {
        case CLI_INIT:
                e->command = "iax2 show firmware";
@@ -7081,14 +6835,11 @@ static char *handle_cli_iax2_show_firmware(struct ast_cli_entry *e, int cmd, str
                return CLI_SHOWUSAGE;
 
        ast_cli(a->fd, "%-15.15s  %-15.15s %-15.15s\n", "Device", "Version", "Size");
-       AST_LIST_LOCK(&firmwares);
-       AST_LIST_TRAVERSE(&firmwares, cur, list) {
-               if ((a->argc == 3) || (!strcasecmp(a->argv[3], (char *) cur->fwh->devname)))  {
-                       ast_cli(a->fd, "%-15.15s  %-15d %-15d\n", cur->fwh->devname, 
-                               ntohs(cur->fwh->version), (int)ntohl(cur->fwh->datalen));
-               }
-       }
-       AST_LIST_UNLOCK(&firmwares);
+
+       iax_firmware_traverse(
+               a->argc == 3 ? NULL : a->argv[3],
+               firmware_show_callback,
+               (void *) &a->fd);
 
        return CLI_SUCCESS;
 }
@@ -8824,7 +8575,7 @@ static int update_registry(struct sockaddr_in *sin, int callno, char *devtype, i
        struct iax2_peer *p;
        int msgcount;
        char data[80];
-       int version;
+       uint16_t version;
        const char *peer_name;
        int res = -1;
        struct ast_sockaddr sockaddr;
@@ -8971,9 +8722,9 @@ static int update_registry(struct sockaddr_in *sin, int callno, char *devtype, i
                        iax_ie_append_str(&ied, IAX_IE_CALLING_NAME, p->cid_name);
                }
        }
-       version = iax_check_version(devtype);
-       if (version) 
+       if (iax_firmware_get_version(devtype, &version)) {
                iax_ie_append_short(&ied, IAX_IE_FIRMWAREVER, version);
+       }
 
        res = 0;
 
@@ -11638,7 +11389,7 @@ immediatedial:
                                        break;
                                }
                                memset(&ied0, 0, sizeof(ied0));
-                               res = iax_firmware_append(&ied0, (unsigned char *)ies.devicetype, ies.fwdesc);
+                               res = iax_firmware_append(&ied0, ies.devicetype, ies.fwdesc);
                                if (res < 0)
                                        send_command_final(iaxs[fr->callno], AST_FRAME_IAX, IAX_COMMAND_REJECT, 0, ied0.buf, ied0.pos, -1);
                                else if (res > 0)
@@ -13761,7 +13512,7 @@ static int reload_config(int forced_reload)
                poke_all_peers();
        }
        
-       reload_firmware(0);
+       iax_firmware_reload();
        iax_provision_reload(1);
        ast_unload_realtime("iaxpeers");
 
@@ -14502,7 +14253,7 @@ static int __unload_module(void)
        ast_channel_unregister(&iax2_tech);
        delete_users();
        iax_provision_unload();
-       reload_firmware(1);
+       iax_firmware_unload();
 
        for (x = 0; x < ARRAY_LEN(iaxsl); x++) {
                ast_mutex_destroy(&iaxsl[x]);
@@ -14956,7 +14707,7 @@ static int load_module(void)
        ao2_callback(peers, 0, iax2_poke_peer_cb, NULL);
 
 
-       reload_firmware(0);
+       iax_firmware_reload();
        iax_provision_reload(0);
 
        ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
diff --git a/channels/iax2/firmware.c b/channels/iax2/firmware.c
new file mode 100644 (file)
index 0000000..3cc0b0a
--- /dev/null
@@ -0,0 +1,341 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief IAX Firmware Support
+ *
+ * \author Mark Spencer <markster@digium.com>
+ */
+
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <sys/mman.h>
+#include <arpa/inet.h>
+
+#include "asterisk/astmm.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/md5.h"
+#include "asterisk/paths.h"
+#include "asterisk/utils.h"
+
+#include "include/firmware.h"
+
+struct iax_firmware {
+       AST_LIST_ENTRY(iax_firmware) list;
+       int fd;
+       int mmaplen;
+       int dead;
+       struct ast_iax2_firmware_header *fwh;
+       unsigned char *buf;
+};
+
+static AST_LIST_HEAD_STATIC(firmwares, iax_firmware);
+
+static int try_firmware(char *s)
+{
+       struct stat stbuf;
+       struct iax_firmware *cur = NULL;
+       int ifd, fd, res, len, chunk;
+       struct ast_iax2_firmware_header *fwh, fwh2;
+       struct MD5Context md5;
+       unsigned char sum[16], buf[1024];
+       char *s2, *last;
+
+       s2 = ast_alloca(strlen(s) + 100);
+
+       last = strrchr(s, '/');
+       if (last)
+               last++;
+       else
+               last = s;
+
+       snprintf(s2, strlen(s) + 100, "/var/tmp/%s-%ld", last, (unsigned long)ast_random());
+
+       if (stat(s, &stbuf) < 0) {
+               ast_log(LOG_WARNING, "Failed to stat '%s': %s\n", s, strerror(errno));
+               return -1;
+       }
+
+       /* Make sure it's not a directory */
+       if (S_ISDIR(stbuf.st_mode))
+               return -1;
+       ifd = open(s, O_RDONLY);
+       if (ifd < 0) {
+               ast_log(LOG_WARNING, "Cannot open '%s': %s\n", s, strerror(errno));
+               return -1;
+       }
+       fd = open(s2, O_RDWR | O_CREAT | O_EXCL, AST_FILE_MODE);
+       if (fd < 0) {
+               ast_log(LOG_WARNING, "Cannot open '%s' for writing: %s\n", s2, strerror(errno));
+               close(ifd);
+               return -1;
+       }
+       /* Unlink our newly created file */
+       unlink(s2);
+
+       /* Now copy the firmware into it */
+       len = stbuf.st_size;
+       while(len) {
+               chunk = len;
+               if (chunk > sizeof(buf))
+                       chunk = sizeof(buf);
+               res = read(ifd, buf, chunk);
+               if (res != chunk) {
+                       ast_log(LOG_WARNING, "Only read %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
+                       close(ifd);
+                       close(fd);
+                       return -1;
+               }
+               res = write(fd, buf, chunk);
+               if (res != chunk) {
+                       ast_log(LOG_WARNING, "Only write %d of %d bytes of data :(: %s\n", res, chunk, strerror(errno));
+                       close(ifd);
+                       close(fd);
+                       return -1;
+               }
+               len -= chunk;
+       }
+       close(ifd);
+       /* Return to the beginning */
+       lseek(fd, 0, SEEK_SET);
+       if ((res = read(fd, &fwh2, sizeof(fwh2))) != sizeof(fwh2)) {
+               ast_log(LOG_WARNING, "Unable to read firmware header in '%s'\n", s);
+               close(fd);
+               return -1;
+       }
+       if (ntohl(fwh2.magic) != IAX_FIRMWARE_MAGIC) {
+               ast_log(LOG_WARNING, "'%s' is not a valid firmware file\n", s);
+               close(fd);
+               return -1;
+       }
+       if (ntohl(fwh2.datalen) != (stbuf.st_size - sizeof(fwh2))) {
+               ast_log(LOG_WARNING, "Invalid data length in firmware '%s'\n", s);
+               close(fd);
+               return -1;
+       }
+       if (fwh2.devname[sizeof(fwh2.devname) - 1] || ast_strlen_zero((char *)fwh2.devname)) {
+               ast_log(LOG_WARNING, "No or invalid device type specified for '%s'\n", s);
+               close(fd);
+               return -1;
+       }
+       fwh = (struct ast_iax2_firmware_header*)mmap(NULL, stbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       if (fwh == MAP_FAILED) {
+               ast_log(LOG_WARNING, "mmap failed: %s\n", strerror(errno));
+               close(fd);
+               return -1;
+       }
+       MD5Init(&md5);
+       MD5Update(&md5, fwh->data, ntohl(fwh->datalen));
+       MD5Final(sum, &md5);
+       if (memcmp(sum, fwh->chksum, sizeof(sum))) {
+               ast_log(LOG_WARNING, "Firmware file '%s' fails checksum\n", s);
+               munmap((void*)fwh, stbuf.st_size);
+               close(fd);
+               return -1;
+       }
+
+       AST_LIST_TRAVERSE(&firmwares, cur, list) {
+               if (!strcmp((const char *) cur->fwh->devname, (const char *) fwh->devname)) {
+                       /* Found a candidate */
+                       if (cur->dead || (ntohs(cur->fwh->version) < ntohs(fwh->version)))
+                               /* The version we have on loaded is older, load this one instead */
+                               break;
+                       /* This version is no newer than what we have.  Don't worry about it.
+                          We'll consider it a proper load anyhow though */
+                       munmap((void*)fwh, stbuf.st_size);
+                       close(fd);
+                       return 0;
+               }
+       }
+
+       if (!cur && ((cur = ast_calloc(1, sizeof(*cur))))) {
+               cur->fd = -1;
+               AST_LIST_INSERT_TAIL(&firmwares, cur, list);
+       }
+
+       if (cur) {
+               if (cur->fwh)
+                       munmap((void*)cur->fwh, cur->mmaplen);
+               if (cur->fd > -1)
+                       close(cur->fd);
+               cur->fwh = fwh;
+               cur->fd = fd;
+               cur->mmaplen = stbuf.st_size;
+               cur->dead = 0;
+       }
+
+       return 0;
+}
+
+static void destroy_firmware(struct iax_firmware *cur)
+{
+       /* Close firmware */
+       if (cur->fwh) {
+               munmap((void*)cur->fwh, ntohl(cur->fwh->datalen) + sizeof(*(cur->fwh)));
+       }
+       close(cur->fd);
+       ast_free(cur);
+}
+
+void iax_firmware_reload(void)
+{
+       struct iax_firmware *cur = NULL;
+       DIR *fwd;
+       struct dirent *de;
+       char dir[256], fn[256];
+
+       AST_LIST_LOCK(&firmwares);
+
+       /* Mark all as dead */
+       AST_LIST_TRAVERSE(&firmwares, cur, list) {
+               cur->dead = 1;
+       }
+
+       /* Now that we have marked them dead... load new ones */
+       snprintf(dir, sizeof(dir), "%s/firmware/iax", ast_config_AST_DATA_DIR);
+       fwd = opendir(dir);
+       if (fwd) {
+               while((de = readdir(fwd))) {
+                       if (de->d_name[0] != '.') {
+                               snprintf(fn, sizeof(fn), "%s/%s", dir, de->d_name);
+                               if (!try_firmware(fn)) {
+                                       ast_verb(2, "Loaded firmware '%s'\n", de->d_name);
+                               }
+                       }
+               }
+               closedir(fwd);
+       } else {
+               ast_log(LOG_WARNING, "Error opening firmware directory '%s': %s\n", dir, strerror(errno));
+       }
+
+       /* Clean up leftovers */
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
+               if (!cur->dead)
+                       continue;
+               AST_LIST_REMOVE_CURRENT(list);
+               destroy_firmware(cur);
+       }
+       AST_LIST_TRAVERSE_SAFE_END;
+
+       AST_LIST_UNLOCK(&firmwares);
+}
+
+void iax_firmware_unload(void)
+{
+       struct iax_firmware *cur = NULL;
+
+       AST_LIST_LOCK(&firmwares);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&firmwares, cur, list) {
+               AST_LIST_REMOVE_CURRENT(list);
+               destroy_firmware(cur);
+       }
+       AST_LIST_TRAVERSE_SAFE_END;
+       AST_LIST_UNLOCK(&firmwares);
+}
+
+int iax_firmware_get_version(const char *dev, uint16_t *version)
+{
+       struct iax_firmware *cur = NULL;
+
+       if (ast_strlen_zero(dev))
+               return 0;
+
+       AST_LIST_LOCK(&firmwares);
+       AST_LIST_TRAVERSE(&firmwares, cur, list) {
+               if (!strcmp(dev, (const char *) cur->fwh->devname)) {
+                       *version = ntohs(cur->fwh->version);
+                       AST_LIST_UNLOCK(&firmwares);
+                       return 1;
+               }
+       }
+       AST_LIST_UNLOCK(&firmwares);
+
+       return 0;
+}
+
+int iax_firmware_append(struct iax_ie_data *ied, const char *dev, unsigned int desc)
+{
+       int res = -1;
+       unsigned int bs = desc & 0xff;
+       unsigned int start = (desc >> 8) & 0xffffff;
+       unsigned int bytes;
+       struct iax_firmware *cur;
+
+       if (ast_strlen_zero((char *)dev) || !bs)
+               return -1;
+
+       start *= bs;
+
+       AST_LIST_LOCK(&firmwares);
+       AST_LIST_TRAVERSE(&firmwares, cur, list) {
+               if (strcmp(dev, (const char *) cur->fwh->devname))
+                       continue;
+               iax_ie_append_int(ied, IAX_IE_FWBLOCKDESC, desc);
+               if (start < ntohl(cur->fwh->datalen)) {
+                       bytes = ntohl(cur->fwh->datalen) - start;
+                       if (bytes > bs)
+                               bytes = bs;
+                       iax_ie_append_raw(ied, IAX_IE_FWBLOCKDATA, cur->fwh->data + start, bytes);
+               } else {
+                       bytes = 0;
+                       iax_ie_append(ied, IAX_IE_FWBLOCKDATA);
+               }
+               if (bytes == bs)
+                       res = 0;
+               else
+                       res = 1;
+               break;
+       }
+       AST_LIST_UNLOCK(&firmwares);
+
+       return res;
+}
+
+void iax_firmware_traverse(
+       const char *filter,
+       int (*callback)(struct ast_iax2_firmware_header *header, void *data),
+       void *data)
+{
+       struct iax_firmware *cur = NULL;
+
+       if (!callback) {
+               return;
+       }
+
+       AST_LIST_LOCK(&firmwares);
+       AST_LIST_TRAVERSE(&firmwares, cur, list) {
+               if (!filter || !strcasecmp(filter, (const char *) cur->fwh->devname)) {
+                       if (callback(cur->fwh, data)) {
+                               break;
+                       }
+               }
+       }
+       AST_LIST_UNLOCK(&firmwares);
+}
diff --git a/channels/iax2/include/firmware.h b/channels/iax2/include/firmware.h
new file mode 100644 (file)
index 0000000..f8063b7
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief IAX Firmware Support header file
+ */
+
+#ifndef _IAX2_FIRMWARE_H
+#define _IAX2_FIRMWARE_H
+
+#include "parser.h"
+
+/*!
+ * \internal
+ * \brief Reload the list of available firmware.
+ *
+ * Searches the IAX firmware directory, adding new firmware that is available
+ * and removing firmware that is no longer available.
+ *
+ * \return Nothing
+ */
+void iax_firmware_reload(void);
+
+/*!
+ * \internal
+ * \brief Unload all of the currently loaded firmware.
+ *
+ * return Nothing
+ */
+void iax_firmware_unload(void);
+
+/*!
+ * \internal
+ * \brief Determine the version number of the specified firmware.
+ *
+ * \param      device_name The device name of the firmware for which we want the
+ *                         version.
+ * \param[out] version     Pointer to a variable where the version number is
+ *                         stored upon return.
+ *
+ * \retval 1 on success
+ * \retval 0 on failure
+ */
+int iax_firmware_get_version(const char *device_name,
+       uint16_t *version);
+
+/*!
+ * \internal
+ * \brief Add firwmare related IEs to an IAX2 IE buffer.
+ *
+ * \param ie_data     The IE buffer being appended to.
+ * \param device_name The name of the requested firmware.
+ * \param block_desc  The requested firmware block identification.
+ *
+ * Search the list of loaded firmware for \c device_name and, if found, add the
+ * appropriate FWBLOCKDESC and FWBLOCKDATA IEs to the specified \c ie_data
+ * list.
+ *
+ * \retval 0 on success
+ * \retval non-zero on failure
+ */
+int iax_firmware_append(struct iax_ie_data *ie_data,
+       const char *device_name, unsigned int block_desc);
+
+/*!
+ * \internal
+ * \brief Iterate over the list of currently loaded IAX firmware.
+ *
+ * \param prefix    The prefix of the device to filter for, or \c NULL to visit
+ *                  all firmware records.
+ * \param callback  A pointer to a function to call for each firmware record
+ *                  that is visited.
+ * \param user_data A pointer to user supplied data that will be passed to the
+ *                  \c callback function each time it is invoked.
+ *
+ * This function visits each of the elements in the IAX firmware list, calling
+ * the specfied \c callback for each element. Iteration continues until the end
+ * of the list is reached, or the \c callback returns non-zero.
+ *
+ * The \c callback function receives a pointer to the firmware header and the
+ * value of the \c user_data argument that was passed in, which may be \c NULL.
+ *
+ * \return Nothing
+ */
+void iax_firmware_traverse(const char *prefix,
+       int (*callback)(struct ast_iax2_firmware_header *header, void *user_data),
+       void *user_data);
+
+#endif
index caa1210..caaa098 100644 (file)
@@ -18,6 +18,7 @@
 #ifndef _IAX2_PARSER_H
 #define _IAX2_PARSER_H
 
+#include "asterisk/frame.h"
 #include "asterisk/linkedlists.h"
 #include "asterisk/crypto.h"
 #include "iax2.h"