loader: Add dependency fields to module structures.
[asterisk/asterisk.git] / res / res_pjsip_endpoint_identifier_anonymous.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  * Joshua Colp <jcolp@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*** MODULEINFO
21         <depend>pjproject</depend>
22         <depend>res_pjsip</depend>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/module.h"
32
33 static int get_endpoint_details(pjsip_rx_data *rdata, char *domain, size_t domain_size)
34 {
35         pjsip_uri *from = rdata->msg_info.from->uri;
36         pjsip_sip_uri *sip_from;
37         if (!PJSIP_URI_SCHEME_IS_SIP(from) && !PJSIP_URI_SCHEME_IS_SIPS(from)) {
38                 return -1;
39         }
40         sip_from = (pjsip_sip_uri *) pjsip_uri_get_uri(from);
41         ast_copy_pj_str(domain, &sip_from->host, domain_size);
42         return 0;
43 }
44
45 static int find_transport_state_in_use(void *obj, void *arg, int flags)
46 {
47         struct ast_sip_transport_state *transport_state = obj;
48         pjsip_rx_data *rdata = arg;
49
50         if (transport_state && ((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 #define DOMAIN_NAME_LEN 255
60
61 static struct ast_sip_endpoint *anonymous_identify(pjsip_rx_data *rdata)
62 {
63         char domain_name[DOMAIN_NAME_LEN + 1];
64         struct ast_sip_endpoint *endpoint;
65         RAII_VAR(struct ast_sip_domain_alias *, alias, NULL, ao2_cleanup);
66         RAII_VAR(struct ao2_container *, transport_states, NULL, ao2_cleanup);
67         RAII_VAR(struct ast_sip_transport_state *, transport_state, NULL, ao2_cleanup);
68         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
69
70         if (get_endpoint_details(rdata, domain_name, sizeof(domain_name))) {
71                 return NULL;
72         }
73
74         if (!ast_sip_get_disable_multi_domain()) {
75                 char id[sizeof("anonymous@") + DOMAIN_NAME_LEN];
76
77                 /* Attempt to find the endpoint given the name and domain provided */
78                 snprintf(id, sizeof(id), "anonymous@%s", domain_name);
79                 if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
80                         goto done;
81                 }
82
83                 /* See if an alias exists for the domain provided */
84                 if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
85                         snprintf(id, sizeof(id), "anonymous@%s", alias->domain);
86                         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
87                                 goto done;
88                         }
89                 }
90
91                 /* See if the transport this came in on has a provided domain */
92                 if ((transport_states = ast_sip_get_transport_states())
93                         && (transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, rdata))
94                         && (transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))
95                         && !ast_strlen_zero(transport->domain)) {
96                         snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
97                         if ((endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id))) {
98                                 goto done;
99                         }
100                 }
101         }
102
103         /* Fall back to no domain */
104         endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", "anonymous");
105
106 done:
107         if (endpoint) {
108                 ast_debug(3, "Retrieved anonymous endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
109         }
110         return endpoint;
111 }
112
113 static struct ast_sip_endpoint_identifier anonymous_identifier = {
114         .identify_endpoint = anonymous_identify,
115 };
116
117 static int load_module(void)
118 {
119         CHECK_PJSIP_MODULE_LOADED();
120
121         ast_sip_register_endpoint_identifier_with_name(&anonymous_identifier, "anonymous");
122         return AST_MODULE_LOAD_SUCCESS;
123 }
124
125 static int unload_module(void)
126 {
127         ast_sip_unregister_endpoint_identifier(&anonymous_identifier);
128         return 0;
129 }
130
131 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PJSIP Anonymous endpoint identifier",
132         .support_level = AST_MODULE_SUPPORT_CORE,
133         .load = load_module,
134         .unload = unload_module,
135         .requires = "res_pjsip",
136 );