CI: Various updates to buildAsterisk.sh
[asterisk/asterisk.git] / main / manager.c
index 029da70..3e41198 100644 (file)
@@ -54,8 +54,6 @@
 
 #include "asterisk.h"
 
-ASTERISK_REGISTER_FILE()
-
 #include "asterisk/paths.h"    /* use various ast_config_AST_* */
 #include <ctype.h>
 #include <sys/time.h>
@@ -149,6 +147,9 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Logoff the current manager session.</para>
                </description>
+               <see-also>
+                       <ref type="manager">Login</ref>
+               </see-also>
        </manager>
        <manager name="Login" language="en_US">
                <synopsis>
@@ -168,6 +169,9 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Login Manager.</para>
                </description>
+               <see-also>
+                       <ref type="manager">Logoff</ref>
+               </see-also>
        </manager>
        <manager name="Challenge" language="en_US">
                <synopsis>
@@ -249,14 +253,16 @@ ASTERISK_REGISTER_FILE()
                                <parameter name="DNID">
                                        <para>Dialed number identifier</para>
                                </parameter>
+                               <parameter name="EffectiveConnectedLineNum">
+                               </parameter>
+                               <parameter name="EffectiveConnectedLineName">
+                               </parameter>
                                <parameter name="TimeToHangup">
                                        <para>Absolute lifetime of the channel</para>
                                </parameter>
                                <parameter name="BridgeID">
                                        <para>Identifier of the bridge the channel is in, may be empty if not in one</para>
                                </parameter>
-                               <parameter name="Linkedid">
-                               </parameter>
                                <parameter name="Application">
                                        <para>Application currently executing on the channel</para>
                                </parameter>
@@ -329,6 +335,9 @@ ASTERISK_REGISTER_FILE()
                                <para>If a channel name is not provided then the variable is considered global.</para>
                        </note>
                </description>
+               <see-also>
+                       <ref type="manager">Getvar</ref>
+               </see-also>
        </manager>
        <manager name="Getvar" language="en_US">
                <synopsis>
@@ -349,6 +358,9 @@ ASTERISK_REGISTER_FILE()
                                <para>If a channel name is not provided then the variable is considered global.</para>
                        </note>
                </description>
+               <see-also>
+                       <ref type="manager">Setvar</ref>
+               </see-also>
        </manager>
        <manager name="GetConfig" language="en_US">
                <synopsis>
@@ -381,6 +393,12 @@ ASTERISK_REGISTER_FILE()
                        In the case where a category name is non-unique, a filter may be specified
                        to match only categories with matching variable values.</para>
                </description>
+               <see-also>
+                       <ref type="manager">GetConfigJSON</ref>
+                       <ref type="manager">UpdateConfig</ref>
+                       <ref type="manager">CreateConfig</ref>
+                       <ref type="manager">ListCategories</ref>
+               </see-also>
        </manager>
        <manager name="GetConfigJSON" language="en_US">
                <synopsis>
@@ -405,6 +423,12 @@ ASTERISK_REGISTER_FILE()
                        In the case where a category name is non-unique, a filter may be specified
                        to match only categories with matching variable values.</para>
                </description>
+               <see-also>
+                       <ref type="manager">GetConfig</ref>
+                       <ref type="manager">UpdateConfig</ref>
+                       <ref type="manager">CreateConfig</ref>
+                       <ref type="manager">ListCategories</ref>
+               </see-also>
        </manager>
        <manager name="UpdateConfig" language="en_US">
                <synopsis>
@@ -496,6 +520,12 @@ ASTERISK_REGISTER_FILE()
                        <para>This action will modify, create, or delete configuration elements
                        in Asterisk configuration files.</para>
                </description>
+               <see-also>
+                       <ref type="manager">GetConfig</ref>
+                       <ref type="manager">GetConfigJSON</ref>
+                       <ref type="manager">CreateConfig</ref>
+                       <ref type="manager">ListCategories</ref>
+               </see-also>
        </manager>
        <manager name="CreateConfig" language="en_US">
                <synopsis>
@@ -512,6 +542,12 @@ ASTERISK_REGISTER_FILE()
                        directory. This action is intended to be used before an UpdateConfig
                        action.</para>
                </description>
+               <see-also>
+                       <ref type="manager">GetConfig</ref>
+                       <ref type="manager">GetConfigJSON</ref>
+                       <ref type="manager">UpdateConfig</ref>
+                       <ref type="manager">ListCategories</ref>
+               </see-also>
        </manager>
        <manager name="ListCategories" language="en_US">
                <synopsis>
@@ -526,6 +562,12 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>This action will dump the categories in a given file.</para>
                </description>
+               <see-also>
+                       <ref type="manager">GetConfig</ref>
+                       <ref type="manager">GetConfigJSON</ref>
+                       <ref type="manager">UpdateConfig</ref>
+                       <ref type="manager">CreateConfig</ref>
+               </see-also>
        </manager>
        <manager name="Redirect" language="en_US">
                <synopsis>
@@ -561,6 +603,9 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Redirect (transfer) a call.</para>
                </description>
+               <see-also>
+                       <ref type="manager">BlindTransfer</ref>
+               </see-also>
        </manager>
        <manager name="Atxfer" language="en_US">
                <synopsis>
@@ -581,6 +626,28 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Attended transfer.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">AttendedTransfer</ref>
+               </see-also>
+       </manager>
+       <manager name="CancelAtxfer" language="en_US">
+               <synopsis>
+                       Cancel an attended transfer.
+               </synopsis>
+               <syntax>
+                       <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
+                       <parameter name="Channel" required="true">
+                               <para>The transferer channel.</para>
+                       </parameter>
+               </syntax>
+               <description>
+                       <para>Cancel an attended transfer. Note, this uses the configured cancel attended transfer
+                       feature option (atxferabort) to cancel the transfer. If not available this action will fail.
+                       </para>
+               </description>
+               <see-also>
+                       <ref type="managerEvent">AttendedTransfer</ref>
+               </see-also>
        </manager>
        <manager name="Originate" language="en_US">
                <synopsis>
@@ -705,6 +772,9 @@ ASTERISK_REGISTER_FILE()
                        <para>Will return an <literal>Extension Status</literal> message. The response will include
                        the hint for the extension and the status.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">ExtensionStatus</ref>
+               </see-also>
        </manager>
        <manager name="PresenceState" language="en_US">
                <synopsis>
@@ -721,6 +791,9 @@ ASTERISK_REGISTER_FILE()
                        <para>Will return a <literal>Presence State</literal> message. The response will include the
                        presence state and, if set, a presence subtype and custom message.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">PresenceStatus</ref>
+               </see-also>
        </manager>
        <manager name="AbsoluteTimeout" language="en_US">
                <synopsis>
@@ -758,6 +831,9 @@ ASTERISK_REGISTER_FILE()
                        <para>Waiting: <literal>0</literal> if messages waiting, <literal>1</literal>
                        if no messages waiting.</para>
                </description>
+               <see-also>
+                       <ref type="manager">MailboxCount</ref>
+               </see-also>
        </manager>
        <manager name="MailboxCount" language="en_US">
                <synopsis>
@@ -778,6 +854,9 @@ ASTERISK_REGISTER_FILE()
                        <para>NewMessages: <replaceable>count</replaceable></para>
                        <para>OldMessages: <replaceable>count</replaceable></para>
                </description>
+               <see-also>
+                       <ref type="manager">MailboxStatus</ref>
+               </see-also>
        </manager>
        <manager name="ListCommands" language="en_US">
                <synopsis>
@@ -827,6 +906,10 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Send an event to manager sessions.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">UserEvent</ref>
+                       <ref type="application">UserEvent</ref>
+               </see-also>
        </manager>
        <manager name="WaitEvent" language="en_US">
                <synopsis>
@@ -879,6 +962,9 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Send a reload event.</para>
                </description>
+               <see-also>
+                       <ref type="manager">ModuleLoad</ref>
+               </see-also>
        </manager>
        <managerEvent language="en_US" name="CoreShowChannel">
                <managerEventInstance class="EVENT_FLAG_CALL">
@@ -991,6 +1077,10 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Loads, unloads or reloads an Asterisk module in a running system.</para>
                </description>
+               <see-also>
+                       <ref type="manager">Reload</ref>
+                       <ref type="manager">ModuleCheck</ref>
+               </see-also>
        </manager>
        <manager name="ModuleCheck" language="en_US">
                <synopsis>
@@ -1006,6 +1096,9 @@ ASTERISK_REGISTER_FILE()
                        <para>Checks if Asterisk module is loaded. Will return Success/Failure.
                        For success returns, the module revision number is included.</para>
                </description>
+               <see-also>
+                       <ref type="manager">ModuleLoad</ref>
+               </see-also>
        </manager>
        <manager name="AOCMessage" language="en_US">
                <synopsis>
@@ -1112,6 +1205,10 @@ ASTERISK_REGISTER_FILE()
                <description>
                        <para>Generates an AOC-D or AOC-E message on a channel.</para>
                </description>
+               <see-also>
+                       <ref type="managerEvent">AOC-D</ref>
+                       <ref type="managerEvent">AOC-E</ref>
+               </see-also>
        </manager>
        <function name="AMI_CLIENT" language="en_US">
                <synopsis>
@@ -1171,6 +1268,9 @@ ASTERISK_REGISTER_FILE()
                        this command can be used to create filters that may bypass
                        filters defined in manager.conf</para>
                </description>
+               <see-also>
+                       <ref type="manager">FilterList</ref>
+               </see-also>
        </manager>
        <manager name="FilterList" language="en_US">
                <synopsis>
@@ -1180,6 +1280,9 @@ ASTERISK_REGISTER_FILE()
                        <para>The filters displayed are for the current session.  Only those filters defined in
                         manager.conf will be present upon starting a new session.</para>
                </description>
+               <see-also>
+                       <ref type="manager">Filter</ref>
+               </see-also>
        </manager>
        <manager name="BlindTransfer" language="en_US">
                <synopsis>
@@ -1198,6 +1301,7 @@ ASTERISK_REGISTER_FILE()
                </description>
                <see-also>
                        <ref type="manager">Redirect</ref>
+                       <ref type="managerEvent">BlindTransfer</ref>
                </see-also>
        </manager>
        <managerEvent name="ExtensionStatus" language="en_US">
@@ -1275,6 +1379,9 @@ ASTERISK_REGISTER_FILE()
                                        </enumlist>
                                </parameter>
                        </syntax>
+                       <see-also>
+                               <ref type="manager">ExtensionState</ref>
+                       </see-also>
                </managerEventInstance>
        </managerEvent>
        <managerEvent name="PresenceStatus" language="en_US">
@@ -1288,6 +1395,9 @@ ASTERISK_REGISTER_FILE()
                                <parameter name="Subtype" />
                                <parameter name="Message" />
                        </syntax>
+                       <see-also>
+                               <ref type="manager">PresenceState</ref>
+                       </see-also>
                </managerEventInstance>
        </managerEvent>
  ***/
@@ -1417,6 +1527,8 @@ static void acl_change_stasis_subscribe(void)
        if (!acl_change_sub) {
                acl_change_sub = stasis_subscribe(ast_security_topic(),
                        acl_change_stasis_cb, NULL);
+               stasis_subscription_accept_message_type(acl_change_sub, ast_named_acl_change_type());
+               stasis_subscription_set_filter(acl_change_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
        }
 }
 
@@ -1460,8 +1572,7 @@ static void acl_change_stasis_unsubscribe(void)
 struct mansession_session {
                                /*! \todo XXX need to document which fields it is protecting */
        struct ast_sockaddr addr;       /*!< address we are connecting from */
-       FILE *f;                /*!< fdopen() on the underlying fd */
-       int fd;                 /*!< descriptor used for output. Either the socket (AMI) or a temporary file (HTTP) */
+       struct ast_iostream *stream;    /*!< AMI stream */
        int inuse;              /*!< number of HTTP sessions using this entry */
        int needdestroy;        /*!< Whether an HTTP session should be destroyed */
        pthread_t waiting_thread;       /*!< Sleeping thread using this descriptor */
@@ -1503,9 +1614,8 @@ enum mansession_message_parsing {
  */
 struct mansession {
        struct mansession_session *session;
+       struct ast_iostream *stream;
        struct ast_tcptls_session_instance *tcptls_session;
-       FILE *f;
-       int fd;
        enum mansession_message_parsing parsing;
        int write_error:1;
        struct manager_custom_hook *hook;
@@ -1547,8 +1657,10 @@ static AST_RWLIST_HEAD_STATIC(actions, manager_action);
 /*! \brief list of hooks registered */
 static AST_RWLIST_HEAD_STATIC(manager_hooks, manager_custom_hook);
 
+#ifdef AST_XML_DOCS
 /*! \brief A container of event documentation nodes */
 static AO2_GLOBAL_OBJ_STATIC(event_docs);
+#endif
 
 static int __attribute__((format(printf, 9, 0))) __manager_event_sessions(
        struct ao2_container *sessions,
@@ -1660,7 +1772,12 @@ void manager_json_to_ast_str(struct ast_json *obj, const char *key,
 {
        struct ast_json_iter *i;
 
-       if (!obj || (!res && !(*res) && (!(*res = ast_str_create(1024))))) {
+       /* If obj or res is not given, just return */
+       if (!obj || !res) {
+               return;
+       }
+
+       if (!*res && !(*res = ast_str_create(1024))) {
                return;
        }
 
@@ -1691,11 +1808,14 @@ void manager_json_to_ast_str(struct ast_json *obj, const char *key,
        }
 }
 
-
 struct ast_str *ast_manager_str_from_json_object(struct ast_json *blob, key_exclusion_cb exclusion_cb)
 {
        struct ast_str *res = ast_str_create(1024);
-       manager_json_to_ast_str(blob, NULL, &res, exclusion_cb);
+
+       if (!ast_json_is_null(blob)) {
+          manager_json_to_ast_str(blob, NULL, &res, exclusion_cb);
+       }
+
        return res;
 }
 
@@ -2077,10 +2197,6 @@ static void session_destructor(void *obj)
                ast_datastore_free(datastore);
        }
 
-       if (session->f != NULL) {
-               fflush(session->f);
-               fclose(session->f);
-       }
        if (eqe) {
                ast_atomic_fetchadd_int(&eqe->usecount, -1);
        }
@@ -2108,14 +2224,13 @@ static struct mansession_session *build_mansession(const struct ast_sockaddr *ad
                return NULL;
        }
 
-       newsession->whitefilters = ao2_container_alloc(1, NULL, NULL);
-       newsession->blackfilters = ao2_container_alloc(1, NULL, NULL);
+       newsession->whitefilters = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
+       newsession->blackfilters = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
        if (!newsession->whitefilters || !newsession->blackfilters) {
                ao2_ref(newsession, -1);
                return NULL;
        }
 
-       newsession->fd = -1;
        newsession->waiting_thread = AST_PTHREADT_NULL;
        newsession->writetimeout = 100;
        newsession->send_events = -1;
@@ -2204,15 +2319,17 @@ static int manager_displayconnects(struct mansession_session *session)
        return ret;
 }
 
+#ifdef AST_XML_DOCS
 static void print_event_instance(struct ast_cli_args *a, struct ast_xml_doc_item *instance);
+#endif
 
 static char *handle_showmancmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct manager_action *cur;
        struct ast_str *authority;
-       int num, l, which;
+       int num;
+       int l;
        const char *auth_str;
-       char *ret = NULL;
 #ifdef AST_XML_DOCS
        char syntax_title[64], description_title[64], synopsis_title[64], seealso_title[64];
        char arguments_title[64], privilege_title[64], final_response_title[64], list_responses_title[64];
@@ -2227,22 +2344,23 @@ static char *handle_showmancmd(struct ast_cli_entry *e, int cmd, struct ast_cli_
                return NULL;
        case CLI_GENERATE:
                l = strlen(a->word);
-               which = 0;
                AST_RWLIST_RDLOCK(&actions);
                AST_RWLIST_TRAVERSE(&actions, cur, list) {
-                       if (!strncasecmp(a->word, cur->action, l) && ++which > a->n) {
-                               ret = ast_strdup(cur->action);
-                               break;  /* make sure we exit even if ast_strdup() returns NULL */
+                       if (!strncasecmp(a->word, cur->action, l)) {
+                               if (ast_cli_completion_add(ast_strdup(cur->action))) {
+                                       break;
+                               }
                        }
                }
                AST_RWLIST_UNLOCK(&actions);
-               return ret;
+               return NULL;
        }
-       authority = ast_str_alloca(MAX_AUTH_PERM_STRING);
        if (a->argc < 4) {
                return CLI_SHOWUSAGE;
        }
 
+       authority = ast_str_alloca(MAX_AUTH_PERM_STRING);
+
 #ifdef AST_XML_DOCS
        /* setup the titles */
        term_color(synopsis_title, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
@@ -2270,6 +2388,22 @@ static char *handle_showmancmd(struct ast_cli_entry *e, int cmd, struct ast_cli_
                                        char *seealso = ast_xmldoc_printable(S_OR(cur->seealso, "Not available"), 1);
                                        char *privilege = ast_xmldoc_printable(S_OR(auth_str, "Not available"), 1);
                                        char *responses = ast_xmldoc_printable("None", 1);
+
+                                       if (!syntax || !synopsis || !description || !arguments
+                                                       || !seealso || !privilege || !responses) {
+                                               ast_free(syntax);
+                                               ast_free(synopsis);
+                                               ast_free(description);
+                                               ast_free(arguments);
+                                               ast_free(seealso);
+                                               ast_free(privilege);
+                                               ast_free(responses);
+                                               ast_cli(a->fd, "Allocation failure.\n");
+                                               AST_RWLIST_UNLOCK(&actions);
+
+                                               return CLI_FAILURE;
+                                       }
+
                                        ast_cli(a->fd, "%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s%s\n\n%s",
                                                syntax_title, syntax,
                                                synopsis_title, synopsis,
@@ -2297,6 +2431,14 @@ static char *handle_showmancmd(struct ast_cli_entry *e, int cmd, struct ast_cli_
                                                ast_cli(a->fd, "Event: %s\n", cur->final_response->name);
                                                print_event_instance(a, cur->final_response);
                                        }
+
+                                       ast_free(syntax);
+                                       ast_free(synopsis);
+                                       ast_free(description);
+                                       ast_free(arguments);
+                                       ast_free(seealso);
+                                       ast_free(privilege);
+                                       ast_free(responses);
                                } else
 #endif
                                {
@@ -2341,8 +2483,7 @@ static char *handle_mandebug(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
 static char *handle_showmanager(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ast_manager_user *user = NULL;
-       int l, which;
-       char *ret = NULL;
+       int l;
        struct ast_str *rauthority = ast_str_alloca(MAX_AUTH_PERM_STRING);
        struct ast_str *wauthority = ast_str_alloca(MAX_AUTH_PERM_STRING);
        struct ast_variable *v;
@@ -2356,19 +2497,19 @@ static char *handle_showmanager(struct ast_cli_entry *e, int cmd, struct ast_cli
                return NULL;
        case CLI_GENERATE:
                l = strlen(a->word);
-               which = 0;
                if (a->pos != 3) {
                        return NULL;
                }
                AST_RWLIST_RDLOCK(&users);
                AST_RWLIST_TRAVERSE(&users, user, list) {
-                       if ( !strncasecmp(a->word, user->username, l) && ++which > a->n ) {
-                               ret = ast_strdup(user->username);
-                               break;
+                       if (!strncasecmp(a->word, user->username, l)) {
+                               if (ast_cli_completion_add(ast_strdup(user->username))) {
+                                       break;
+                               }
                        }
                }
                AST_RWLIST_UNLOCK(&users);
-               return ret;
+               return NULL;
        }
 
        if (a->argc != 4) {
@@ -2528,7 +2669,7 @@ static char *handle_showmanconn(struct ast_cli_entry *e, int cmd, struct ast_cli
                                ast_sockaddr_stringify_addr(&session->addr),
                                (int) (session->sessionstart),
                                (int) (now - session->sessionstart),
-                               session->fd,
+                               session->stream ? ast_iostream_get_fd(session->stream) : -1,
                                session->inuse,
                                session->readperm,
                                session->writeperm);
@@ -2570,6 +2711,8 @@ static char *handle_showmaneventq(struct ast_cli_entry *e, int cmd, struct ast_c
        return CLI_SUCCESS;
 }
 
+static int reload_module(void);
+
 /*! \brief CLI command manager reload */
 static char *handle_manager_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
@@ -2586,7 +2729,7 @@ static char *handle_manager_reload(struct ast_cli_entry *e, int cmd, struct ast_
        if (a->argc > 2) {
                return CLI_SHOWUSAGE;
        }
-       reload_manager();
+       reload_module();
        return CLI_SUCCESS;
 }
 
@@ -2663,6 +2806,34 @@ const char *astman_get_header(const struct message *m, char *var)
 }
 
 /*!
+ * \brief Append additional headers into the message structure from params.
+ *
+ * \note You likely want to initialize m->hdrcount to 0 before calling this.
+ */
+static void astman_append_headers(struct message *m, const struct ast_variable *params)
+{
+       const struct ast_variable *v;
+
+       for (v = params; v && m->hdrcount < ARRAY_LEN(m->headers); v = v->next) {
+               if (ast_asprintf((char**)&m->headers[m->hdrcount], "%s: %s", v->name, v->value) > -1) {
+                       ++m->hdrcount;
+               }
+       }
+}
+
+/*!
+ * \brief Free headers inside message structure, but not the message structure itself.
+ */
+static void astman_free_headers(struct message *m)
+{
+       while (m->hdrcount) {
+               --m->hdrcount;
+               ast_free((void *) m->headers[m->hdrcount]);
+               m->headers[m->hdrcount] = NULL;
+       }
+}
+
+/*!
  * \internal
  * \brief Process one "Variable:" header value string.
  *
@@ -2791,49 +2962,53 @@ int ast_hook_send_action(struct manager_custom_hook *hook, const char *msg)
        }
 
        action = astman_get_header(&m, "Action");
-       if (strcasecmp(action, "login")) {
+
+       do {
+               if (!strcasecmp(action, "login")) {
+                       break;
+               }
+
                act_found = action_find(action);
-               if (act_found) {
-                       /*
-                        * we have to simulate a session for this action request
-                        * to be able to pass it down for processing
-                        * This is necessary to meet the previous design of manager.c
-                        */
-                       s.hook = hook;
-                       s.f = (void*)1; /* set this to something so our request will make it through all functions that test it*/
+               if (!act_found) {
+                       break;
+               }
 
-                       ao2_lock(act_found);
-                       if (act_found->registered && act_found->func) {
-                               if (act_found->module) {
-                                       ast_module_ref(act_found->module);
-                               }
-                               ao2_unlock(act_found);
+               /*
+                * we have to simulate a session for this action request
+                * to be able to pass it down for processing
+                * This is necessary to meet the previous design of manager.c
+                */
+               s.hook = hook;
+
+               ret = -1;
+               ao2_lock(act_found);
+               if (act_found->registered && act_found->func) {
+                       struct ast_module *mod_ref = ast_module_running_ref(act_found->module);
+
+                       ao2_unlock(act_found);
+                       /* If the action is in a module it must be running. */
+                       if (!act_found->module || mod_ref) {
                                ret = act_found->func(&s, &m);
-                               ao2_lock(act_found);
-                               if (act_found->module) {
-                                       ast_module_unref(act_found->module);
-                               }
-                       } else {
-                               ret = -1;
+                               ast_module_unref(mod_ref);
                        }
+               } else {
                        ao2_unlock(act_found);
-                       ao2_t_ref(act_found, -1, "done with found action object");
                }
-       }
+               ao2_t_ref(act_found, -1, "done with found action object");
+       } while (0);
+
        ast_free(dup_str);
        return ret;
 }
 
-
 /*!
  * helper function to send a string to the socket.
  * Return -1 on error (e.g. buffer full).
  */
 static int send_string(struct mansession *s, char *string)
 {
-       int res;
-       FILE *f = s->f ? s->f : s->session->f;
-       int fd = s->f ? s->fd : s->session->fd;
+       struct ast_iostream *stream;
+       int len, res;
 
        /* It's a result from one of the hook's action invocation */
        if (s->hook) {
@@ -2845,7 +3020,14 @@ static int send_string(struct mansession *s, char *string)
                return 0;
        }
 
-       if ((res = ast_careful_fwrite(f, fd, string, strlen(string), s->session->writetimeout))) {
+       stream = s->stream ? s->stream : s->session->stream;
+
+       len = strlen(string);
+       ast_iostream_set_timeout_inactivity(stream, s->session->writetimeout);
+       res = ast_iostream_write(stream, string, len);
+       ast_iostream_set_timeout_disable(stream);
+
+       if (res < len) {
                s->write_error = 1;
        }
 
@@ -2886,10 +3068,10 @@ void astman_append(struct mansession *s, const char *fmt, ...)
                return;
        }
 
-       if (s->f != NULL || s->session->f != NULL) {
+       if (s->hook || (s->tcptls_session != NULL && s->tcptls_session->stream != NULL)) {
                send_string(s, ast_str_buffer(buf));
        } else {
-               ast_verbose("fd == -1 in astman_append, should not happen\n");
+               ast_verbose("No connection stream in astman_append, should not happen\n");
        }
 }
 
@@ -4030,7 +4212,7 @@ static int action_waitevent(struct mansession *s, const struct message *m)
                        break;
                }
                if (s->session->managerid == 0) {       /* AMI session */
-                       if (ast_wait_for_input(s->session->fd, 1000)) {
+                       if (ast_wait_for_input(ast_iostream_get_fd(s->session->stream), 1000)) {
                                break;
                        }
                } else {        /* HTTP session */
@@ -4480,7 +4662,6 @@ static void generate_status(struct mansession *s, struct ast_channel *chan, char
                "EffectiveConnectedLineName: %s\r\n"
                "TimeToHangup: %ld\r\n"
                "BridgeID: %s\r\n"
-               "Linkedid: %s\r\n"
                "Application: %s\r\n"
                "Data: %s\r\n"
                "Nativeformats: %s\r\n"
@@ -4501,7 +4682,6 @@ static void generate_status(struct mansession *s, struct ast_channel *chan, char
                S_COR(effective_id.name.valid, effective_id.name.str, "<unknown>"),
                (long)ast_channel_whentohangup(chan)->tv_sec,
                bridge ? bridge->uniqueid : "",
-               ast_channel_linkedid(chan),
                ast_channel_appl(chan),
                ast_channel_data(chan),
                ast_format_cap_get_names(ast_channel_nativeformats(chan), &codec_buf),
@@ -4595,10 +4775,13 @@ static int action_status(struct mansession *s, const struct message *m)
 
 static int action_sendtext(struct mansession *s, const struct message *m)
 {
-       struct ast_channel *c = NULL;
+       struct ast_channel *c;
        const char *name = astman_get_header(m, "Channel");
        const char *textmsg = astman_get_header(m, "Message");
-       int res = 0;
+       struct ast_control_read_action_payload *frame_payload;
+       int payload_size;
+       int frame_size;
+       int res;
 
        if (ast_strlen_zero(name)) {
                astman_send_error(s, m, "No channel specified");
@@ -4610,13 +4793,29 @@ static int action_sendtext(struct mansession *s, const struct message *m)
                return 0;
        }
 
-       if (!(c = ast_channel_get_by_name(name))) {
+       c = ast_channel_get_by_name(name);
+       if (!c) {
                astman_send_error(s, m, "No such channel");
                return 0;
        }
 
-       res = ast_sendtext(c, textmsg);
-       c = ast_channel_unref(c);
+       payload_size = strlen(textmsg) + 1;
+       frame_size = payload_size + sizeof(*frame_payload);
+
+       frame_payload = ast_malloc(frame_size);
+       if (!frame_payload) {
+               ast_channel_unref(c);
+               astman_send_error(s, m, "Failure");
+               return 0;
+       }
+
+       frame_payload->action = AST_FRAME_READ_ACTION_SEND_TEXT;
+       frame_payload->payload_size = payload_size;
+       memcpy(frame_payload->payload, textmsg, payload_size);
+       res = ast_queue_control_data(c, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
+
+       ast_free(frame_payload);
+       ast_channel_unref(c);
 
        if (res >= 0) {
                astman_send_ack(s, m, "Success");
@@ -4762,14 +4961,10 @@ static int action_redirect(struct mansession *s, const struct message *m)
 
        /* Release the bridge wait. */
        if (chan1_wait) {
-               ast_channel_lock(chan);
-               ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT);
-               ast_channel_unlock(chan);
+               ast_channel_clear_flag(chan, AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT);
        }
        if (chan2_wait) {
-               ast_channel_lock(chan2);
-               ast_clear_flag(ast_channel_flags(chan2), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT);
-               ast_channel_unlock(chan2);
+               ast_channel_clear_flag(chan2, AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT);
        }
 
        chan2 = ast_channel_unref(chan2);
@@ -4877,6 +5072,47 @@ static int action_atxfer(struct mansession *s, const struct message *m)
        return 0;
 }
 
+static int action_cancel_atxfer(struct mansession *s, const struct message *m)
+{
+       const char *name = astman_get_header(m, "Channel");
+       struct ast_channel *chan = NULL;
+       char *feature_code;
+       const char *digit;
+
+       if (ast_strlen_zero(name)) {
+               astman_send_error(s, m, "No channel specified");
+               return 0;
+       }
+
+       if (!(chan = ast_channel_get_by_name(name))) {
+               astman_send_error(s, m, "Channel specified does not exist");
+               return 0;
+       }
+
+       ast_channel_lock(chan);
+       feature_code = ast_get_chan_features_atxferabort(chan);
+       ast_channel_unlock(chan);
+
+       if (!feature_code) {
+               astman_send_error(s, m, "No disconnect feature code found");
+               ast_channel_unref(chan);
+               return 0;
+       }
+
+       for (digit = feature_code; *digit; ++digit) {
+               struct ast_frame f = { AST_FRAME_DTMF, .subclass.integer = *digit };
+               ast_queue_frame(chan, &f);
+       }
+       ast_free(feature_code);
+
+       chan = ast_channel_unref(chan);
+
+       astman_send_ack(s, m, "CancelAtxfer successfully queued");
+
+       return 0;
+}
+
+
 static int check_blacklist(const char *cmd)
 {
        char *cmd_copy, *cur_cmd;
@@ -5045,13 +5281,15 @@ static void *fast_originate(void *data)
 
        if (!ast_strlen_zero(in->app)) {
                res = ast_pbx_outgoing_app(in->tech, in->cap, in->data,
-                       in->timeout, in->app, in->appdata, &reason, 1,
+                       in->timeout, in->app, in->appdata, &reason,
+                       AST_OUTGOING_WAIT,
                        S_OR(in->cid_num, NULL),
                        S_OR(in->cid_name, NULL),
                        in->vars, in->account, &chan, &assignedids);
        } else {
                res = ast_pbx_outgoing_exten(in->tech, in->cap, in->data,
-                       in->timeout, in->context, in->exten, in->priority, &reason, 1,
+                       in->timeout, in->context, in->exten, in->priority, &reason,
+                       AST_OUTGOING_WAIT,
                        S_OR(in->cid_num, NULL),
                        S_OR(in->cid_name, NULL),
                        in->vars, in->account, &chan, in->early_media, &assignedids);
@@ -5522,11 +5760,16 @@ static int action_originate(struct mansession *s, const struct message *m)
                        }
                }
        } else if (!ast_strlen_zero(app)) {
-               res = ast_pbx_outgoing_app(tech, cap, data, to, app, appdata, &reason, 1, l, n, vars, account, NULL, assignedids.uniqueid ? &assignedids : NULL);
+               res = ast_pbx_outgoing_app(tech, cap, data, to, app, appdata, &reason,
+                               AST_OUTGOING_WAIT, l, n, vars, account, NULL,
+                               assignedids.uniqueid ? &assignedids : NULL);
                ast_variables_destroy(vars);
        } else {
                if (exten && context && pi) {
-                       res = ast_pbx_outgoing_exten(tech, cap, data, to, context, exten, pi, &reason, 1, l, n, vars, account, NULL, bridge_early, assignedids.uniqueid ? &assignedids : NULL);
+                       res = ast_pbx_outgoing_exten(tech, cap, data, to,
+                                       context, exten, pi, &reason, AST_OUTGOING_WAIT,
+                                       l, n, vars, account, NULL, bridge_early,
+                                       assignedids.uniqueid ? &assignedids : NULL);
                        ast_variables_destroy(vars);
                } else {
                        astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
@@ -5588,8 +5831,9 @@ static int action_extensionstate(struct mansession *s, const struct message *m)
 {
        const char *exten = astman_get_header(m, "Exten");
        const char *context = astman_get_header(m, "Context");
-       char hint[256] = "";
+       char hint[256];
        int status;
+
        if (ast_strlen_zero(exten)) {
                astman_send_error(s, m, "Extension not specified");
                return 0;
@@ -5598,16 +5842,18 @@ static int action_extensionstate(struct mansession *s, const struct message *m)
                context = "default";
        }
        status = ast_extension_state(NULL, context, exten);
-       ast_get_hint(hint, sizeof(hint) - 1, NULL, 0, NULL, context, exten);
+       hint[0] = '\0';
+       ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten);
        astman_start_ack(s, m);
-       astman_append(s,   "Message: Extension Status\r\n"
-                          "Exten: %s\r\n"
-                          "Context: %s\r\n"
-                          "Hint: %s\r\n"
-                          "Status: %d\r\n"
-                          "StatusText: %s\r\n\r\n",
-                     exten, context, hint, status,
-                     ast_extension_state2str(status));
+       astman_append(s, "Message: Extension Status\r\n"
+               "Exten: %s\r\n"
+               "Context: %s\r\n"
+               "Hint: %s\r\n"
+               "Status: %d\r\n"
+               "StatusText: %s\r\n"
+               "\r\n",
+               exten, context, hint, status,
+               ast_extension_state2str(status));
        return 0;
 }
 
@@ -5804,7 +6050,11 @@ static int match_filter(struct mansession *s, char *eventdata)
 {
        int result = 0;
 
-       ast_debug(3, "Examining AMI event:\n%s\n", eventdata);
+       if (manager_debug) {
+               ast_verbose("<-- Examining AMI event: -->\n%s\n", eventdata);
+       } else {
+               ast_debug(3, "Examining AMI event:\n%s\n", eventdata);
+       }
        if (!ao2_container_count(s->session->whitefilters) && !ao2_container_count(s->session->blackfilters)) {
                return 1; /* no filtering means match all */
        } else if (ao2_container_count(s->session->whitefilters) && !ao2_container_count(s->session->blackfilters)) {
@@ -5835,7 +6085,7 @@ static int process_events(struct mansession *s)
        int ret = 0;
 
        ao2_lock(s->session);
-       if (s->session->f != NULL) {
+       if (s->session->stream != NULL) {
                struct eventqent *eqe = s->session->last_ev;
 
                while ((eqe = advance_event(eqe))) {
@@ -6000,7 +6250,7 @@ static int action_coreshowchannels(struct mansession *s, const struct message *m
        int numchans = 0;
        struct ao2_container *channels;
        struct ao2_iterator it_chans;
-       struct stasis_message *msg;
+       struct ast_channel_snapshot *cs;
 
        if (!ast_strlen_zero(actionid)) {
                snprintf(idText, sizeof(idText), "ActionID: %s\r\n", actionid);
@@ -6008,28 +6258,23 @@ static int action_coreshowchannels(struct mansession *s, const struct message *m
                idText[0] = '\0';
        }
 
-       channels = stasis_cache_dump(ast_channel_cache_by_name(), ast_channel_snapshot_type());
-       if (!channels) {
-               astman_send_error(s, m, "Could not get cached channels");
-               return 0;
-       }
+       channels = ast_channel_cache_by_name();
 
        astman_send_listack(s, m, "Channels will follow", "start");
 
        it_chans = ao2_iterator_init(channels, 0);
-       for (; (msg = ao2_iterator_next(&it_chans)); ao2_ref(msg, -1)) {
-               struct ast_channel_snapshot *cs = stasis_message_data(msg);
+       for (; (cs = ao2_iterator_next(&it_chans)); ao2_ref(cs, -1)) {
                struct ast_str *built = ast_manager_build_channel_state_string_prefix(cs, "");
-               char durbuf[10] = "";
+               char durbuf[16] = "";
 
                if (!built) {
                        continue;
                }
 
-               if (!ast_tvzero(cs->creationtime)) {
+               if (!ast_tvzero(cs->base->creationtime)) {
                        int duration, durh, durm, durs;
 
-                       duration = (int)(ast_tvdiff_ms(ast_tvnow(), cs->creationtime) / 1000);
+                       duration = (int)(ast_tvdiff_ms(ast_tvnow(), cs->base->creationtime) / 1000);
                        durh = duration / 3600;
                        durm = (duration % 3600) / 60;
                        durs = duration % 60;
@@ -6047,10 +6292,10 @@ static int action_coreshowchannels(struct mansession *s, const struct message *m
                        "\r\n",
                        idText,
                        ast_str_buffer(built),
-                       cs->appl,
-                       cs->data,
+                       cs->dialplan->appl,
+                       cs->dialplan->data,
                        durbuf,
-                       cs->bridgeid);
+                       cs->bridge->id);
 
                numchans++;
 
@@ -6257,21 +6502,21 @@ static int process_message(struct mansession *s, const struct message *m)
                if ((s->session->writeperm & act_found->authority)
                        || act_found->authority == 0) {
                        /* We have the authority to execute the action. */
+                       ret = -1;
                        ao2_lock(act_found);
                        if (act_found->registered && act_found->func) {
-                               ast_debug(1, "Running action '%s'\n", act_found->action);
-                               if (act_found->module) {
-                                       ast_module_ref(act_found->module);
-                               }
+                               struct ast_module *mod_ref = ast_module_running_ref(act_found->module);
+
                                ao2_unlock(act_found);
-                               ret = act_found->func(s, m);
-                               acted = 1;
-                               ao2_lock(act_found);
-                               if (act_found->module) {
-                                       ast_module_unref(act_found->module);
+                               if (mod_ref || !act_found->module) {
+                                       ast_debug(1, "Running action '%s'\n", act_found->action);
+                                       ret = act_found->func(s, m);
+                                       acted = 1;
+                                       ast_module_unref(mod_ref);
                                }
+                       } else {
+                               ao2_unlock(act_found);
                        }
-                       ao2_unlock(act_found);
                }
                if (!acted) {
                        /*
@@ -6377,7 +6622,7 @@ static int get_input(struct mansession *s, char *output)
                s->session->waiting_thread = pthread_self();
                ao2_unlock(s->session);
 
-               res = ast_wait_for_input(s->session->fd, timeout);
+               res = ast_wait_for_input(ast_iostream_get_fd(s->session->stream), timeout);
 
                ao2_lock(s->session);
                s->session->waiting_thread = AST_PTHREADT_NULL;
@@ -6395,7 +6640,7 @@ static int get_input(struct mansession *s, char *output)
        }
 
        ao2_lock(s->session);
-       res = fread(src + s->session->inlen, 1, maxlen - s->session->inlen, s->session->f);
+       res = ast_iostream_read(s->session->stream, src + s->session->inlen, maxlen - s->session->inlen);
        if (res < 1) {
                res = -1;       /* error return */
        } else {
@@ -6438,7 +6683,6 @@ static int do_message(struct mansession *s)
        struct message m = { 0 };
        char header_buf[sizeof(s->session->inbuf)] = { '\0' };
        int res;
-       int idx;
        int hdr_loss;
        time_t now;
 
@@ -6506,10 +6750,8 @@ static int do_message(struct mansession *s)
                }
        }
 
-       /* Free AMI request headers. */
-       for (idx = 0; idx < m.hdrcount; ++idx) {
-               ast_free((void *) m.headers[idx]);
-       }
+       astman_free_headers(&m);
+
        return res;
 }
 
@@ -6528,13 +6770,11 @@ static void *session_do(void *data)
        struct mansession s = {
                .tcptls_session = data,
        };
-       int flags;
        int res;
+       int arg = 1;
        struct ast_sockaddr ser_remote_address_tmp;
-       struct protoent *p;
 
        if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= authlimit) {
-               fclose(ser->f);
                ast_atomic_fetchadd_int(&unauth_sessions, -1);
                goto done;
        }
@@ -6543,7 +6783,6 @@ static void *session_do(void *data)
        session = build_mansession(&ser_remote_address_tmp);
 
        if (session == NULL) {
-               fclose(ser->f);
                ast_atomic_fetchadd_int(&unauth_sessions, -1);
                goto done;
        }
@@ -6551,20 +6790,10 @@ static void *session_do(void *data)
        /* here we set TCP_NODELAY on the socket to disable Nagle's algorithm.
         * This is necessary to prevent delays (caused by buffering) as we
         * write to the socket in bits and pieces. */
-       p = getprotobyname("tcp");
-       if (p) {
-               int arg = 1;
-               if( setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
-                       ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\nSome manager actions may be slow to respond.\n", strerror(errno));
-               }
-       } else {
-               ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY, getprotobyname(\"tcp\") failed\nSome manager actions may be slow to respond.\n");
+       if (setsockopt(ast_iostream_get_fd(ser->stream), IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) {
+               ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on manager connection: %s\n", strerror(errno));
        }
-
-       /* make sure socket is non-blocking */
-       flags = fcntl(ser->fd, F_GETFL);
-       flags |= O_NONBLOCK;
-       fcntl(ser->fd, F_SETFL, flags);
+       ast_iostream_nonblock(ser->stream);
 
        ao2_lock(session);
        /* Hook to the tail of the event queue */
@@ -6573,8 +6802,7 @@ static void *session_do(void *data)
        ast_mutex_init(&s.lock);
 
        /* these fields duplicate those in the 'ser' structure */
-       session->fd = s.fd = ser->fd;
-       session->f = s.f = ser->f;
+       session->stream = s.stream = ser->stream;
        ast_sockaddr_copy(&session->addr, &ser_remote_address_tmp);
        s.session = session;
 
@@ -6593,9 +6821,9 @@ static void *session_do(void *data)
         * We cannot let the stream exclusively wait for data to arrive.
         * We have to wake up the task to send async events.
         */
-       ast_tcptls_stream_set_exclusive_input(ser->stream_cookie, 0);
+       ast_iostream_set_exclusive_input(ser->stream, 0);
 
-       ast_tcptls_stream_set_timeout_sequence(ser->stream_cookie,
+       ast_iostream_set_timeout_sequence(ser->stream,
                ast_tvnow(), authtimeout * 1000);
 
        astman_append(&s, "Asterisk Call Manager/%s\r\n", AMI_VERSION); /* welcome prompt */
@@ -6604,7 +6832,7 @@ static void *session_do(void *data)
                        break;
                }
                if (session->authenticated) {
-                       ast_tcptls_stream_set_timeout_disable(ser->stream_cookie);
+                       ast_iostream_set_timeout_disable(ser->stream);
                }
        }
        /* session is over, explain why and terminate */
@@ -6891,6 +7119,7 @@ static int manager_state_cb(const char *context, const char *exten, struct ast_s
        /* Notify managers of change */
        char hint[512];
 
+       hint[0] = '\0';
        ast_get_hint(hint, sizeof(hint), NULL, 0, NULL, context, exten);
 
        switch(info->reason) {
@@ -7405,7 +7634,8 @@ static void xml_translate(struct ast_str **out, char *in, struct ast_variable *g
                        if (xml) {
                                ast_str_append(out, 0, "<response type='object' id='%s'><%s", dest, objtype);
                        }
-                       vco = ao2_container_alloc(37, variable_count_hash_fn, variable_count_cmp_fn);
+                       vco = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 37,
+                               variable_count_hash_fn, NULL, variable_count_cmp_fn);
                        inobj = 1;
                }
 
@@ -7463,23 +7693,9 @@ static void xml_translate(struct ast_str **out, char *in, struct ast_variable *g
 
 static void close_mansession_file(struct mansession *s)
 {
-       if (s->f) {
-               if (fclose(s->f)) {
-                       ast_log(LOG_ERROR, "fclose() failed: %s\n", strerror(errno));
-               }
-               s->f = NULL;
-               s->fd = -1;
-       } else if (s->fd != -1) {
-               /*
-                * Issuing shutdown() is necessary here to avoid a race
-                * condition where the last data written may not appear
-                * in the TCP stream.  See ASTERISK-23548
-                */
-               shutdown(s->fd, SHUT_RDWR);
-               if (close(s->fd)) {
-                       ast_log(LOG_ERROR, "close() failed: %s\n", strerror(errno));
-               }
-               s->fd = -1;
+       if (s->stream) {
+               ast_iostream_close(s->stream);
+               s->stream = NULL;
        } else {
                ast_log(LOG_ERROR, "Attempted to close file/file descriptor on mansession without a valid file or file descriptor.\n");
        }
@@ -7488,17 +7704,20 @@ static void close_mansession_file(struct mansession *s)
 static void process_output(struct mansession *s, struct ast_str **out, struct ast_variable *params, enum output_format format)
 {
        char *buf;
-       size_t l;
+       off_t l;
+       int fd;
 
-       if (!s->f)
+       if (!s->stream)
                return;
 
        /* Ensure buffer is NULL-terminated */
-       fprintf(s->f, "%c", 0);
-       fflush(s->f);
+       ast_iostream_write(s->stream, "", 1);
+
+       fd = ast_iostream_get_fd(s->stream);
 
-       if ((l = ftell(s->f)) > 0) {
-               if (MAP_FAILED == (buf = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, s->fd, 0))) {
+       l = lseek(fd, 0, SEEK_CUR);
+       if (l > 0) {
+               if (MAP_FAILED == (buf = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0))) {
                        ast_log(LOG_WARNING, "mmap failed.  Manager output was not processed\n");
                } else {
                        if (format == FORMAT_XML || format == FORMAT_HTML) {
@@ -7525,14 +7744,12 @@ static int generic_http_callback(struct ast_tcptls_session_instance *ser,
        struct mansession s = { .session = NULL, .tcptls_session = ser };
        struct mansession_session *session = NULL;
        uint32_t ident;
+       int fd;
        int blastaway = 0;
-       struct ast_variable *v;
        struct ast_variable *params = get_params;
        char template[] = "/tmp/ast-http-XXXXXX";       /* template for temporary file */
        struct ast_str *http_header = NULL, *out = NULL;
        struct message m = { 0 };
-       unsigned int idx;
-       size_t hdrlen;
 
        if (method != AST_HTTP_GET && method != AST_HTTP_HEAD && method != AST_HTTP_POST) {
                ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");
@@ -7580,17 +7797,17 @@ static int generic_http_callback(struct ast_tcptls_session_instance *ser,
        }
 
        s.session = session;
-       s.fd = mkstemp(template);       /* create a temporary file for command output */
+       fd = mkstemp(template); /* create a temporary file for command output */
        unlink(template);
-       if (s.fd <= -1) {
+       if (fd <= -1) {
                ast_http_error(ser, 500, "Server Error", "Internal Server Error (mkstemp failed)");
                goto generic_callback_out;
        }
-       s.f = fdopen(s.fd, "w+");
-       if (!s.f) {
+       s.stream = ast_iostream_from_fd(&fd);
+       if (!s.stream) {
                ast_log(LOG_WARNING, "HTTP Manager, fdopen failed: %s!\n", strerror(errno));
                ast_http_error(ser, 500, "Server Error", "Internal Server Error (fdopen failed)");
-               close(s.fd);
+               close(fd);
                goto generic_callback_out;
        }
 
@@ -7615,17 +7832,7 @@ static int generic_http_callback(struct ast_tcptls_session_instance *ser,
                }
        }
 
-       for (v = params; v && m.hdrcount < ARRAY_LEN(m.headers); v = v->next) {
-               hdrlen = strlen(v->name) + strlen(v->value) + 3;
-               m.headers[m.hdrcount] = ast_malloc(hdrlen);
-               if (!m.headers[m.hdrcount]) {
-                       /* Allocation failure */
-                       continue;
-               }
-               snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);
-               ast_debug(1, "HTTP Manager add header %s\n", m.headers[m.hdrcount]);
-               ++m.hdrcount;
-       }
+       astman_append_headers(&m, params);
 
        if (process_message(&s, &m)) {
                if (session->authenticated) {
@@ -7640,11 +7847,7 @@ static int generic_http_callback(struct ast_tcptls_session_instance *ser,
                session->needdestroy = 1;
        }
 
-       /* Free request headers. */
-       for (idx = 0; idx < m.hdrcount; ++idx) {
-               ast_free((void *) m.headers[idx]);
-               m.headers[idx] = NULL;
-       }
+       astman_free_headers(&m);
 
        ast_str_append(&http_header, 0,
                "Content-type: text/%s\r\n"
@@ -7730,9 +7933,9 @@ generic_callback_out:
                if (blastaway) {
                        session_destroy(session);
                } else {
-                       if (session->f) {
-                               fclose(session->f);
-                               session->f = NULL;
+                       if (session->stream) {
+                               ast_iostream_close(session->stream);
+                               session->stream = NULL;
                        }
                        unref_mansession(session);
                }
@@ -7755,8 +7958,7 @@ static int auth_http_callback(struct ast_tcptls_session_instance *ser,
        struct ast_str *http_header = NULL, *out = NULL;
        size_t result_size;
        struct message m = { 0 };
-       unsigned int idx;
-       size_t hdrlen;
+       int fd;
 
        time_t time_now = time(NULL);
        unsigned long nonce = 0, nc;
@@ -7827,13 +8029,21 @@ static int auth_http_callback(struct ast_tcptls_session_instance *ser,
 
        /* compute the expected response to compare with what we received */
        {
-               char a2[256];
-               char a2_hash[256];
+               char *a2;
+               /* ast_md5_hash outputs 32 characters plus NULL terminator. */
+               char a2_hash[33];
                char resp[256];
 
                /* XXX Now request method are hardcoded in A2 */
-               snprintf(a2, sizeof(a2), "%s:%s", ast_get_http_method(method), d.uri);
+               if (ast_asprintf(&a2, "%s:%s", ast_get_http_method(method), d.uri) < 0) {
+                       AST_RWLIST_UNLOCK(&users);
+                       ast_http_request_close_on_completion(ser);
+                       ast_http_error(ser, 500, "Server Error", "Internal Server Error (out of memory)");
+                       return 0;
+               }
+
                ast_md5_hash(a2_hash, a2);
+               ast_free(a2);
 
                if (d.qop) {
                        /* RFC 2617 */
@@ -7935,17 +8145,17 @@ static int auth_http_callback(struct ast_tcptls_session_instance *ser,
 
        ast_mutex_init(&s.lock);
        s.session = session;
-       s.fd = mkstemp(template);       /* create a temporary file for command output */
+       fd = mkstemp(template); /* create a temporary file for command output */
        unlink(template);
-       if (s.fd <= -1) {
+       if (fd <= -1) {
                ast_http_error(ser, 500, "Server Error", "Internal Server Error (mkstemp failed)");
                goto auth_callback_out;
        }
-       s.f = fdopen(s.fd, "w+");
-       if (!s.f) {
+       s.stream = ast_iostream_from_fd(&fd);
+       if (!s.stream) {
                ast_log(LOG_WARNING, "HTTP Manager, fdopen failed: %s!\n", strerror(errno));
                ast_http_error(ser, 500, "Server Error", "Internal Server Error (fdopen failed)");
-               close(s.fd);
+               close(fd);
                goto auth_callback_out;
        }
 
@@ -7970,17 +8180,7 @@ static int auth_http_callback(struct ast_tcptls_session_instance *ser,
                }
        }
 
-       for (v = params; v && m.hdrcount < ARRAY_LEN(m.headers); v = v->next) {
-               hdrlen = strlen(v->name) + strlen(v->value) + 3;
-               m.headers[m.hdrcount] = ast_malloc(hdrlen);
-               if (!m.headers[m.hdrcount]) {
-                       /* Allocation failure */
-                       continue;
-               }
-               snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);
-               ast_verb(4, "HTTP Manager add header %s\n", m.headers[m.hdrcount]);
-               ++m.hdrcount;
-       }
+       astman_append_headers(&m, params);
 
        if (process_message(&s, &m)) {
                if (u_displayconnects) {
@@ -7990,13 +8190,9 @@ static int auth_http_callback(struct ast_tcptls_session_instance *ser,
                session->needdestroy = 1;
        }
 
-       /* Free request headers. */
-       for (idx = 0; idx < m.hdrcount; ++idx) {
-               ast_free((void *) m.headers[idx]);
-               m.headers[idx] = NULL;
-       }
+       astman_free_headers(&m);
 
-       result_size = ftell(s.f); /* Calculate approx. size of result */
+       result_size = lseek(ast_iostream_get_fd(s.stream), 0, SEEK_CUR); /* Calculate approx. size of result */
 
        http_header = ast_str_create(80);
        out = ast_str_create(result_size * 2 + 512);
@@ -8048,11 +8244,10 @@ auth_callback_out:
        ast_free(out);
 
        ao2_lock(session);
-       if (session->f) {
-               fclose(session->f);
+       if (session->stream) {
+               ast_iostream_close(session->stream);
+               session->stream = NULL;
        }
-       session->f = NULL;
-       session->fd = -1;
        ao2_unlock(session);
 
        if (session->needdestroy) {
@@ -8316,7 +8511,7 @@ static char *handle_manager_show_settings(struct ast_cli_entry *e, int cmd, stru
        ast_cli(a->fd, FORMAT, "Manager (AMI):", AST_CLI_YESNO(manager_enabled));
        ast_cli(a->fd, FORMAT, "Web Manager (AMI/HTTP):", AST_CLI_YESNO(webmanager_enabled));
        ast_cli(a->fd, FORMAT, "TCP Bindaddress:", manager_enabled != 0 ? ast_sockaddr_stringify(&ami_desc.local_address) : "Disabled");
-       ast_cli(a->fd, FORMAT2, "HTTP Timeout (minutes):", httptimeout);
+       ast_cli(a->fd, FORMAT2, "HTTP Timeout (seconds):", httptimeout);
        ast_cli(a->fd, FORMAT, "TLS Enable:", AST_CLI_YESNO(ami_tls_cfg.enabled));
        ast_cli(a->fd, FORMAT, "TLS Bindaddress:", ami_tls_cfg.enabled != 0 ? ast_sockaddr_stringify(&amis_desc.local_address) : "Disabled");
        ast_cli(a->fd, FORMAT, "TLS Certfile:", ami_tls_cfg.certfile);
@@ -8466,8 +8661,6 @@ static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct
        struct ao2_iterator it_events;
        struct ast_xml_doc_item *item, *temp;
        int length;
-       int which;
-       char *match = NULL;
 
        if (cmd == CLI_INIT) {
                e->command = "manager show event";
@@ -8484,19 +8677,24 @@ static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct
        }
 
        if (cmd == CLI_GENERATE) {
+               if (a->pos != 3) {
+                       return NULL;
+               }
+
                length = strlen(a->word);
-               which = 0;
                it_events = ao2_iterator_init(events, 0);
                while ((item = ao2_iterator_next(&it_events))) {
-                       if (!strncasecmp(a->word, item->name, length) && ++which > a->n) {
-                               match = ast_strdup(item->name);
-                               ao2_ref(item, -1);
-                               break;
+                       if (!strncasecmp(a->word, item->name, length)) {
+                               if (ast_cli_completion_add(ast_strdup(item->name))) {
+                                       ao2_ref(item, -1);
+                                       break;
+                               }
                        }
                        ao2_ref(item, -1);
                }
                ao2_iterator_destroy(&it_events);
-               return match;
+
+               return NULL;
        }
 
        if (a->argc != 4) {
@@ -8606,6 +8804,7 @@ static void manager_shutdown(void)
        ast_manager_unregister("ListCategories");
        ast_manager_unregister("Redirect");
        ast_manager_unregister("Atxfer");
+       ast_manager_unregister("CancelAtxfer");
        ast_manager_unregister("Originate");
        ast_manager_unregister("Command");
        ast_manager_unregister("ExtensionState");
@@ -8660,6 +8859,10 @@ static void manager_shutdown(void)
        ami_tls_cfg.pvtfile = NULL;
        ast_free(ami_tls_cfg.cipher);
        ami_tls_cfg.cipher = NULL;
+       ast_free(ami_tls_cfg.cafile);
+       ami_tls_cfg.cafile = NULL;
+       ast_free(ami_tls_cfg.capath);
+       ami_tls_cfg.capath = NULL;
 
        ao2_global_obj_release(mgr_sessions);
 
@@ -8760,6 +8963,10 @@ static void manager_set_defaults(void)
        ami_tls_cfg.pvtfile = ast_strdup("");
        ast_free(ami_tls_cfg.cipher);
        ami_tls_cfg.cipher = ast_strdup("");
+       ast_free(ami_tls_cfg.cafile);
+       ami_tls_cfg.cafile = ast_strdup("");
+       ast_free(ami_tls_cfg.capath);
+       ami_tls_cfg.capath = ast_strdup("");
 }
 
 static int __init_manager(int reload, int by_external_config)
@@ -8785,8 +8992,6 @@ static int __init_manager(int reload, int by_external_config)
 #endif
                int res;
 
-               ast_register_cleanup(manager_shutdown);
-
                res = STASIS_MESSAGE_TYPE_INIT(ast_manager_get_generic_type);
                if (res != 0) {
                        return -1;
@@ -8813,6 +9018,7 @@ static int __init_manager(int reload, int by_external_config)
                ast_manager_register_xml_core("ListCategories", EVENT_FLAG_CONFIG, action_listcategories);
                ast_manager_register_xml_core("Redirect", EVENT_FLAG_CALL, action_redirect);
                ast_manager_register_xml_core("Atxfer", EVENT_FLAG_CALL, action_atxfer);
+               ast_manager_register_xml_core("CancelAtxfer", EVENT_FLAG_CALL, action_cancel_atxfer);
                ast_manager_register_xml_core("Originate", EVENT_FLAG_ORIGINATE, action_originate);
                ast_manager_register_xml_core("Command", EVENT_FLAG_COMMAND, action_command);
                ast_manager_register_xml_core("ExtensionState", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING, action_extensionstate);
@@ -8857,7 +9063,7 @@ static int __init_manager(int reload, int by_external_config)
 #endif
 
                /* If you have a NULL hash fn, you only need a single bucket */
-               sessions = ao2_container_alloc(1, NULL, mansession_cmp_fn);
+               sessions = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, mansession_cmp_fn);
                if (!sessions) {
                        return -1;
                }
@@ -9113,8 +9319,8 @@ static int __init_manager(int reload, int by_external_config)
                        /* Default allowmultiplelogin from [general] */
                        user->allowmultiplelogin = allowmultiplelogin;
                        user->writetimeout = 100;
-                       user->whitefilters = ao2_container_alloc(1, NULL, NULL);
-                       user->blackfilters = ao2_container_alloc(1, NULL, NULL);
+                       user->whitefilters = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
+                       user->blackfilters = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
                        if (!user->whitefilters || !user->blackfilters) {
                                manager_free_user(user);
                                break;
@@ -9268,12 +9474,19 @@ static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
        __init_manager(1, 1);
 }
 
-int init_manager(void)
+static int unload_module(void)
 {
-       return __init_manager(0, 0);
+       return 0;
 }
 
-int reload_manager(void)
+static int load_module(void)
+{
+       ast_register_cleanup(manager_shutdown);
+
+       return __init_manager(0, 0) ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
+}
+
+static int reload_module(void)
 {
        return __init_manager(1, 0);
 }
@@ -9318,23 +9531,16 @@ struct ast_datastore *astman_datastore_find(struct mansession *s, const struct a
 }
 
 int ast_str_append_event_header(struct ast_str **fields_string,
-                                       const char *header, const char *value)
+       const char *header, const char *value)
 {
-       struct ast_str *working_str = *fields_string;
-
-       if (!working_str) {
-               working_str = ast_str_create(128);
-               if (!working_str) {
+       if (!*fields_string) {
+               *fields_string = ast_str_create(128);
+               if (!*fields_string) {
                        return -1;
                }
-               *fields_string = working_str;
        }
 
-       ast_str_append(&working_str, 0,
-               "%s: %s\r\n",
-               header, value);
-
-       return 0;
+       return (ast_str_append(fields_string, 0, "%s: %s\r\n", header, value) < 0) ? -1 : 0;
 }
 
 static void manager_event_blob_dtor(void *obj)
@@ -9377,3 +9583,12 @@ ast_manager_event_blob_create(
 
        return ev;
 }
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk Manager Interface",
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload_module,
+       .load_pri = AST_MODPRI_CORE,
+       .requires = "extconfig,acl,http",
+);