Asterisk - The Open Source Telephony Project  18.5.0
res_pjsip_endpoint_identifier_anonymous.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <[email protected]>
7  * Joshua Colp <[email protected]>
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 == rdata->tp_info.transport
51  || (transport_state->factory
52  && !pj_strcmp(&transport_state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host)
53  && transport_state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) {
54  return CMP_MATCH;
55  }
56 
57  return 0;
58 }
59 
60 #define DOMAIN_NAME_LEN 255
61 
62 static struct ast_sip_endpoint *anonymous_identify(pjsip_rx_data *rdata)
63 {
64  char domain_name[DOMAIN_NAME_LEN + 1];
65  struct ast_sip_endpoint *endpoint;
66 
67  if (get_endpoint_details(rdata, domain_name, sizeof(domain_name))) {
68  return NULL;
69  }
70 
72  struct ast_sip_domain_alias *alias;
74  struct ast_sip_transport_state *transport_state = NULL;
75  struct ast_sip_transport *transport = NULL;
76  char id[sizeof("anonymous@") + DOMAIN_NAME_LEN];
77 
78  /* Attempt to find the endpoint given the name and domain provided */
79  snprintf(id, sizeof(id), "anonymous@%s", domain_name);
80  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
81  if (endpoint) {
82  goto done;
83  }
84 
85  /* See if an alias exists for the domain provided */
86  alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias",
87  domain_name);
88  if (alias) {
89  snprintf(id, sizeof(id), "anonymous@%s", alias->domain);
90  ao2_ref(alias, -1);
91  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
92  if (endpoint) {
93  goto done;
94  }
95  }
96 
97  /* See if the transport this came in on has a provided domain */
98  if ((transport_states = ast_sip_get_transport_states())
99  && (transport_state = ao2_callback(transport_states, 0, find_transport_state_in_use, rdata))
100  && (transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_state->id))
101  && !ast_strlen_zero(transport->domain)) {
102  snprintf(id, sizeof(id), "anonymous@%s", transport->domain);
103  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
104  }
105  ao2_cleanup(transport);
106  ao2_cleanup(transport_state);
107  ao2_cleanup(transport_states);
108  if (endpoint) {
109  goto done;
110  }
111  }
112 
113  /* Fall back to no domain */
114  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", "anonymous");
115 
116 done:
117  if (endpoint) {
118  ast_debug(3, "Retrieved anonymous endpoint '%s'\n", ast_sorcery_object_get_id(endpoint));
119  }
120  return endpoint;
121 }
122 
125 };
126 
127 static int load_module(void)
128 {
129  ast_sip_register_endpoint_identifier_with_name(&anonymous_identifier, "anonymous");
131 }
132 
133 static int unload_module(void)
134 {
135  ast_sip_unregister_endpoint_identifier(&anonymous_identifier);
136  return 0;
137 }
138 
139 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PJSIP Anonymous endpoint identifier",
140  .support_level = AST_MODULE_SUPPORT_CORE,
141  .load = load_module,
142  .unload = unload_module,
143  .requires = "res_pjsip",
144 );
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
static struct ast_sip_endpoint * anonymous_identify(pjsip_rx_data *rdata)
static struct ao2_container * transport_states
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5240
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:998
#define NULL
Definition: resample.c:96
Domain data structure.
Definition: sip.h:888
#define ast_strlen_zero(foo)
Definition: strings.h:52
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
int done
Definition: test_amihooks.c:48
static struct ast_sip_endpoint_identifier anonymous_identifier
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
Structure for SIP transport information.
Definition: res_pjsip.h:87
struct pjsip_transport * transport
Transport itself.
Definition: res_pjsip.h:89
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
unsigned int ast_sip_get_disable_multi_domain(void)
Retrieve the system setting &#39;disable multi domain&#39;.
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
static int load_module(void)
struct ao2_container * ast_sip_get_transport_states(void)
Retrieves all transport states.
Transport to bind to.
Definition: res_pjsip.h:171
static int find_transport_state_in_use(void *obj, void *arg, int flags)
struct pjsip_tpfactory * factory
Transport factory.
Definition: res_pjsip.h:91
const ast_string_field domain
Definition: res_pjsip.h:191
static int unload_module(void)
An entity responsible for identifying the source of a SIP message.
Definition: res_pjsip.h:993
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int get_endpoint_details(pjsip_rx_data *rdata, char *domain, size_t domain_size)
const ast_string_field domain
Definition: res_pjsip.h:265
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_sip_register_endpoint_identifier_with_name(struct ast_sip_endpoint_identifier *identifier, const char *name)
Register a SIP endpoint identifier with a name.
Definition: res_pjsip.c:3431
void ast_sip_unregister_endpoint_identifier(struct ast_sip_endpoint_identifier *identifier)
Unregister a SIP endpoint identifier.
Definition: res_pjsip.c:3513
Generic container type.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.