Asterisk - The Open Source Telephony Project  21.4.1
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  if (!ast_sip_is_uri_sip_sips(from)) {
37  return -1;
38  }
39  ast_copy_pj_str(domain, ast_sip_pjsip_uri_get_hostname(from), domain_size);
40  return 0;
41 }
42 
43 static int find_transport_state_in_use(void *obj, void *arg, int flags)
44 {
45  struct ast_sip_transport_state *transport_state = obj;
46  pjsip_rx_data *rdata = arg;
47 
48  if (transport_state->transport == rdata->tp_info.transport
49  || (transport_state->factory
50  && !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;
53  }
54 
55  return 0;
56 }
57 
58 #define DOMAIN_NAME_LEN 255
59 
60 static struct ast_sip_endpoint *anonymous_identify(pjsip_rx_data *rdata)
61 {
62  char domain_name[DOMAIN_NAME_LEN + 1];
63  struct ast_sip_endpoint *endpoint;
64 
65  if (get_endpoint_details(rdata, domain_name, sizeof(domain_name))) {
66  return NULL;
67  }
68 
69  if (!ast_sip_get_disable_multi_domain()) {
70  struct ast_sip_domain_alias *alias;
71  struct ao2_container *transport_states;
72  struct ast_sip_transport_state *transport_state = NULL;
73  struct ast_sip_transport *transport = NULL;
74  char id[sizeof("anonymous@") + DOMAIN_NAME_LEN];
75 
76  /* Attempt to find the endpoint given the name and domain provided */
77  snprintf(id, sizeof(id), "anonymous@%s", domain_name);
78  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
79  if (endpoint) {
80  goto done;
81  }
82 
83  /* See if an alias exists for the domain provided */
84  alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias",
85  domain_name);
86  if (alias) {
87  snprintf(id, sizeof(id), "anonymous@%s", alias->domain);
88  ao2_ref(alias, -1);
89  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
90  if (endpoint) {
91  goto done;
92  }
93  }
94 
95  /* See if the transport this came in on has a provided domain */
96  if ((transport_states = ast_sip_get_transport_states())
97  && (transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, rdata))
98  && (transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))
99  && !ast_strlen_zero(transport->domain)) {
100  snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
101  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
102  }
103  ao2_cleanup(transport);
104  ao2_cleanup(transport_state);
105  ao2_cleanup(transport_states);
106  if (endpoint) {
107  goto done;
108  }
109  }
110 
111  /* Fall back to no domain */
112  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", "anonymous");
113 
114 done:
115  if (endpoint) {
116  ast_debug(3, "Retrieved anonymous endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
117  }
118  return endpoint;
119 }
120 
121 static struct ast_sip_endpoint_identifier anonymous_identifier = {
122  .identify_endpoint = anonymous_identify,
123 };
124 
125 static int load_module(void)
126 {
127  ast_sip_register_endpoint_identifier_with_name(&anonymous_identifier, "anonymous");
129 }
130 
131 static int unload_module(void)
132 {
133  ast_sip_unregister_endpoint_identifier(&anonymous_identifier);
134  return 0;
135 }
136 
137 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PJSIP Anonymous endpoint identifier",
138  .support_level = AST_MODULE_SUPPORT_CORE,
139  .load = load_module,
140  .unload = unload_module,
141  .requires = "res_pjsip",
142 );
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container, as described below.
Definition: astobj2.h:1693
struct ast_sip_endpoint *(* identify_endpoint)(pjsip_rx_data *rdata)
Callback used to identify the source of a message. See ast_sip_identify_endpoint for more details...
Definition: res_pjsip.h:1279
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
Structure for SIP transport information.
Definition: res_pjsip.h:119
struct pjsip_transport * transport
Transport itself.
Definition: res_pjsip.h:121
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
#define ast_debug(level,...)
Log a DEBUG message.
An entity with which Asterisk communicates.
Definition: res_pjsip.h:949
Transport to bind to.
Definition: res_pjsip.h:221
struct pjsip_tpfactory * factory
Transport factory.
Definition: res_pjsip.h:123
const ast_string_field domain
Definition: res_pjsip.h:241
An entity responsible for identifying the source of a SIP message.
Definition: res_pjsip.h:1274
const ast_string_field domain
Definition: res_pjsip.h:323
Generic container type.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.