Fix stuck channel in ARI through the introduction of synchronous bridge actions.
[asterisk/asterisk.git] / res / res_pjsip_endpoint_identifier_user.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28
29 #include "asterisk/res_pjsip.h"
30 #include "asterisk/module.h"
31
32 static int get_endpoint_details(pjsip_rx_data *rdata, char *endpoint, size_t endpoint_size, char *domain, size_t domain_size)
33 {
34         pjsip_uri *from = rdata->msg_info.from->uri;
35         pjsip_sip_uri *sip_from;
36         if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
37                 return -1;
38         }
39         sip_from = (pjsip_sip_uri *) pjsip_uri_get_uri(from);
40         ast_copy_pj_str(endpoint, &sip_from->user, endpoint_size);
41         ast_copy_pj_str(domain, &sip_from->host, domain_size);
42         return 0;
43 }
44
45 static int find_transport_in_use(void *obj, void *arg, int flags)
46 {
47         struct ast_sip_transport *transport = obj;
48         pjsip_rx_data *rdata = arg;
49
50         if ((transport->state->transport == rdata->tp_info.transport) ||
51                 (transport->state->factory && !pj_strcmp(&transport->state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) &&
52                         transport->state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
53                 return CMP_MATCH | CMP_STOP;
54         }
55
56         return 0;
57 }
58
59 static struct ast_sip_endpoint *username_identify(pjsip_rx_data *rdata)
60 {
61         char endpoint_name[64], domain_name[64], id[AST_UUID_STR_LEN];
62         struct ast_sip_endpoint *endpoint;
63         RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
64         RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup);
65         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
66
67         if (get_endpoint_details(rdata, endpoint_name, sizeof(endpoint_name), domain_name, sizeof(domain_name))) {
68                 return NULL;
69         }
70
71         /* Attempt to find the endpoint given the name and domain provided */
72         snprintf(id, sizeof(id), "%s@%s", endpoint_name, domain_name);
73         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
74                 goto done;
75         }
76
77         /* See if an alias exists for the domain provided */
78         if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
79                 snprintf(id, sizeof(id), "%s@%s", endpoint_name, alias->domain);
80                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
81                         goto done;
82                 }
83         }
84
85         /* See if the transport this came in on has a provided domain */
86         if ((transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) &&
87                 (transport = ao2_callback(transports, 0, find_transport_in_use, rdata)) &&
88                 !ast_strlen_zero(transport->domain)) {
89                 snprintf(id, sizeof(id), "%s@%s", endpoint_name, transport->domain);
90                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
91                         goto done;
92                 }
93         }
94
95         /* Fall back to no domain */
96         endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
97
98 done:
99         if (endpoint) {
100                 if (!(endpoint->ident_method & AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME)) {
101                         ao2_ref(endpoint, -1);
102                         return NULL;
103                 }
104                 ast_debug(3, "Retrieved endpoint %s\n", ast_sorcery_object_get_id(endpoint));
105         } else {
106                 ast_debug(3, "Could not identify endpoint by username '%s'\n", endpoint_name);
107         }
108         return endpoint;
109 }
110
111 static struct ast_sip_endpoint_identifier username_identifier = {
112         .identify_endpoint = username_identify,
113 };
114
115 static int load_module(void)
116 {
117         ast_sip_register_endpoint_identifier(&username_identifier);
118         return AST_MODULE_LOAD_SUCCESS;
119 }
120
121 static int unload_module(void)
122 {
123         ast_sip_unregister_endpoint_identifier(&username_identifier);
124         return 0;
125 }
126
127 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP username endpoint identifier",
128                 .load = load_module,
129                 .unload = unload_module,
130                 .load_pri = AST_MODPRI_APP_DEPEND,
131                );