Make --with-pjproject-bundled the default for Asterisk 15
[asterisk/asterisk.git] / channels / chan_mgcp.c
index 0f6b916..6cac4bc 100644 (file)
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <net/if.h>
 #include <fcntl.h>
 #include <netdb.h>
-#include <sys/signal.h>
 #include <signal.h>
 #include <netinet/in.h>
 #include <netinet/in_systm.h>
@@ -489,14 +486,6 @@ static struct ast_channel_tech mgcp_tech = {
        .func_channel_read = acf_channel_read,
 };
 
-static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
-{
-       /* This module does not handle MWI in an event-based manner.  However, it
-        * subscribes to MWI for each mailbox that is configured so that the core
-        * knows that we care about it.  Then, chan_mgcp will get the MWI from the
-        * event cache instead of checking the mailbox directly. */
-}
-
 static int has_voicemail(struct mgcp_endpoint *p)
 {
        int new_msgs;
@@ -1261,7 +1250,7 @@ static int mgcp_write(struct ast_channel *ast, struct ast_frame *frame)
                }
        } else {
                if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
-                       struct ast_str *cap_buf = ast_str_alloca(64);
+                       struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
 
                        ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
                                ast_format_get_name(frame->subclass.format),
@@ -1835,17 +1824,12 @@ static struct mgcp_subchannel *find_subchannel_and_lock(char *name, int msgid, s
                        /* not dynamic, check if the name matches */
                        } else if (name) {
                                if (strcasecmp(g->name, at)) {
-                                       g = g->next;
                                        continue;
                                }
                        /* not dynamic, no name, check if the addr matches */
                        } else if (!name && sin) {
                                if ((g->addr.sin_addr.s_addr != sin->sin_addr.s_addr) ||
                                    (g->addr.sin_port != sin->sin_port)) {
-                                       if(!g->next)
-                                               g = find_realtime_gw(name, at, sin);
-                                       else
-                                               g = g->next;
                                        continue;
                                }
                        } else {
@@ -1999,9 +1983,9 @@ static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
        int codec, codec_count=0;
        int iterator;
        struct mgcp_endpoint *p = sub->parent;
-       struct ast_str *global_buf = ast_str_alloca(64);
-       struct ast_str *peer_buf = ast_str_alloca(64);
-       struct ast_str *pvt_buf = ast_str_alloca(64);
+       struct ast_str *global_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+       struct ast_str *peer_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+       struct ast_str *pvt_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
 
        /* Get codec and RTP info from SDP */
        m = get_sdp(req, "m");
@@ -2031,7 +2015,6 @@ static int process_sdp(struct mgcp_subchannel *sub, struct mgcp_request *req)
        ast_rtp_instance_set_remote_address(sub->rtp, &sin_tmp);
        ast_debug(3, "Peer RTP is at port %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
        /* Scan through the RTP payload types specified in a "m=" line: */
-       ast_rtp_codecs_payloads_clear(ast_rtp_instance_get_codecs(sub->rtp), sub->rtp);
        codecs = ast_strdupa(m + len);
        while (!ast_strlen_zero(codecs)) {
                if (sscanf(codecs, "%30d%n", &codec, &len) != 1) {
@@ -3985,7 +3968,7 @@ static struct ast_channel *mgcp_request(const char *type, struct ast_format_cap
        char tmp[256];
 
        if (!(ast_format_cap_iscompatible(cap, global_capability))) {
-               struct ast_str *cap_buf = ast_str_alloca(64);
+               struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
                ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n",
                        ast_format_cap_get_names(cap, &cap_buf));
                /*return NULL;*/
@@ -4109,7 +4092,19 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
                        ast_sockaddr_to_sin(&tmp, &gw->defaddr);
                } else if (!strcasecmp(v->name, "permit") ||
                        !strcasecmp(v->name, "deny")) {
-                       gw->ha = ast_append_ha(v->name, v->value, gw->ha, NULL);
+                       int acl_error = 0;
+                       gw->ha = ast_append_ha(v->name, v->value, gw->ha, &acl_error);
+                       if (acl_error) {
+                               ast_log(LOG_ERROR, "Invalid ACL '%s' specified for MGCP gateway '%s' on line %d. Not creating.\n",
+                                               v->value, cat, v->lineno);
+                               if (!gw_reload) {
+                                       ast_mutex_destroy(&gw->msgs_lock);
+                                       ast_free(gw);
+                               } else {
+                                       gw->delme = 1;
+                               }
+                               return NULL;
+                       }
                } else if (!strcasecmp(v->name, "port")) {
                        gw->addr.sin_port = htons(atoi(v->value));
                } else if (!strcasecmp(v->name, "context")) {
@@ -4242,7 +4237,11 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
 
                                        mailbox_specific_topic = ast_mwi_topic(e->mailbox);
                                        if (mailbox_specific_topic) {
-                                               e->mwi_event_sub = stasis_subscribe(mailbox_specific_topic, mwi_event_cb, NULL);
+                                               /* This module does not handle MWI in an event-based manner.  However, it
+                                                * subscribes to MWI for each mailbox that is configured so that the core
+                                                * knows that we care about it.  Then, chan_mgcp will get the MWI from the
+                                                * event cache instead of checking the mailbox directly. */
+                                               e->mwi_event_sub = stasis_subscribe_pool(mailbox_specific_topic, stasis_subscription_cb_noop, NULL);
                                        }
                                }
                                snprintf(e->rqnt_ident, sizeof(e->rqnt_ident), "%08lx", (unsigned long)ast_random());
@@ -4484,7 +4483,8 @@ static enum ast_rtp_glue_result mgcp_get_rtp_peer(struct ast_channel *chan, stru
        if (!(sub = ast_channel_tech_pvt(chan)) || !(sub->rtp))
                return AST_RTP_GLUE_RESULT_FORBID;
 
-       *instance = sub->rtp ? ao2_ref(sub->rtp, +1), sub->rtp : NULL;
+       ao2_ref(sub->rtp, +1);
+       *instance = sub->rtp;
 
        if (sub->parent->directmedia)
                return AST_RTP_GLUE_RESULT_REMOTE;
@@ -4860,11 +4860,11 @@ static int reload_config(int reload)
 static int load_module(void)
 {
        if (!(global_capability = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
        if (!(mgcp_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
                ao2_ref(global_capability, -1);
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
        ast_format_cap_append(global_capability, ast_format_ulaw, 0);
        ast_format_cap_append(mgcp_tech.capabilities, ast_format_ulaw, 0);
@@ -4873,7 +4873,7 @@ static int load_module(void)
                ast_log(LOG_WARNING, "Unable to create schedule context\n");
                ao2_ref(global_capability, -1);
                ao2_ref(mgcp_tech.capabilities, -1);
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        if (!(io = io_context_create())) {
@@ -4881,7 +4881,7 @@ static int load_module(void)
                ast_sched_context_destroy(sched);
                ao2_ref(global_capability, -1);
                ao2_ref(mgcp_tech.capabilities, -1);
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        if (reload_config(0)) {
@@ -4897,7 +4897,7 @@ static int load_module(void)
                ast_sched_context_destroy(sched);
                ao2_ref(global_capability, -1);
                ao2_ref(mgcp_tech.capabilities, -1);
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        ast_rtp_glue_register(&mgcp_rtp_glue);
@@ -5004,7 +5004,9 @@ static int unload_module(void)
                return -1;
        }
 
-       close(mgcpsock);
+       if (mgcpsock > -1) {
+               close(mgcpsock);
+       }
        ast_rtp_glue_unregister(&mgcp_rtp_glue);
        ast_cli_unregister_multiple(cli_mgcp, sizeof(cli_mgcp) / sizeof(struct ast_cli_entry));
        ast_sched_context_destroy(sched);
@@ -5018,10 +5020,10 @@ static int unload_module(void)
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Media Gateway Control Protocol (MGCP)",
-               .support_level = AST_MODULE_SUPPORT_EXTENDED,
-               .load = load_module,
-               .unload = unload_module,
-               .reload = reload,
-               .load_pri = AST_MODPRI_CHANNEL_DRIVER,
-               .nonoptreq = "res_pktccops",
-              );
+       .support_level = AST_MODULE_SUPPORT_EXTENDED,
+       .load = load_module,
+       .unload = unload_module,
+       .reload = reload,
+       .load_pri = AST_MODPRI_CHANNEL_DRIVER,
+       .nonoptreq = "res_pktccops",
+);