Merge the pimp_my_sip branch into trunk.
[asterisk/asterisk.git] / res / res_sip_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         <support_level>core</support_level>
22  ***/
23
24 #include "asterisk.h"
25
26 #include <pjsip.h>
27
28 #include "asterisk/res_sip.h"
29 #include "asterisk/module.h"
30
31 static int get_endpoint_details(pjsip_rx_data *rdata, char *endpoint, size_t endpoint_size, char *domain, size_t domain_size)
32 {
33         pjsip_uri *from = rdata->msg_info.from->uri;
34         pjsip_sip_uri *sip_from;
35         if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
36                 return -1;
37         }
38         sip_from = (pjsip_sip_uri *) pjsip_uri_get_uri(from);
39         ast_copy_pj_str(endpoint, &sip_from->user, endpoint_size);
40         ast_copy_pj_str(domain, &sip_from->host, domain_size);
41         return 0;
42 }
43
44 static int find_transport_in_use(void *obj, void *arg, int flags)
45 {
46         struct ast_sip_transport *transport = obj;
47         pjsip_rx_data *rdata = arg;
48
49         if ((transport->state->transport == rdata->tp_info.transport) ||
50                 (transport->state->factory && !pj_strcmp(&transport->state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) &&
51                         transport->state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
52                 return CMP_MATCH | CMP_STOP;
53         }
54
55         return 0;
56 }
57
58 static struct ast_sip_endpoint *username_identify(pjsip_rx_data *rdata)
59 {
60         char endpoint_name[64], domain_name[64], id[AST_UUID_STR_LEN];
61         struct ast_sip_endpoint *endpoint;
62         RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
63         RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup);
64         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
65
66         if (get_endpoint_details(rdata, endpoint_name, sizeof(endpoint_name), domain_name, sizeof(domain_name))) {
67                 return NULL;
68         }
69
70         /* Attempt to find the endpoint given the name and domain provided */
71         snprintf(id, sizeof(id), "%s@%s", endpoint_name, domain_name);
72         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
73                 goto done;
74         }
75
76         /* See if an alias exists for the domain provided */
77         if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
78                 snprintf(id, sizeof(id), "%s@%s", endpoint_name, alias->domain);
79                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
80                         goto done;
81                 }
82         }
83
84         /* See if the transport this came in on has a provided domain */
85         if ((transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL)) &&
86                 (transport = ao2_callback(transports, 0, find_transport_in_use, rdata)) &&
87                 !ast_strlen_zero(transport->domain)) {
88                 snprintf(id, sizeof(id), "%s@%s", endpoint_name, transport->domain);
89                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
90                         goto done;
91                 }
92         }
93
94         /* Fall back to no domain */
95         endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name);
96
97 done:
98         if (endpoint) {
99                 if (!(endpoint->ident_method & AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME)) {
100                         ao2_ref(endpoint, -1);
101                         return NULL;
102                 }
103                 ast_debug(3, "Retrieved endpoint %s\n", ast_sorcery_object_get_id(endpoint));
104         }
105         return endpoint;
106 }
107
108 static struct ast_sip_endpoint_identifier username_identifier = {
109         .identify_endpoint = username_identify,
110 };
111
112 static int load_module(void)
113 {
114         ast_sip_register_endpoint_identifier(&username_identifier);
115         return AST_MODULE_LOAD_SUCCESS;
116 }
117
118 static int unload_module(void)
119 {
120         ast_sip_unregister_endpoint_identifier(&username_identifier);
121         return 0;
122 }
123
124 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "SIP username endpoint identifier",
125                 .load = load_module,
126                 .unload = unload_module,
127                 .load_pri = AST_MODPRI_APP_DEPEND,
128                );