2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
23 #include "asterisk/res_pjsip.h"
25 static int distribute(void *data);
26 static pj_bool_t distributor(pjsip_rx_data *rdata);
28 static pjsip_module distributor_mod = {
29 .name = {"Request Distributor", 19},
30 .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 6,
31 .on_rx_request = distributor,
32 .on_rx_response = distributor,
35 /*! Dialog-specific information the distributor uses */
36 struct distributor_dialog_data {
37 /* Serializer to distribute tasks to for this dialog */
38 struct ast_taskprocessor *serializer;
39 /* Endpoint associated with this dialog */
40 struct ast_sip_endpoint *endpoint;
46 * \note Call this with the dialog locked
48 static struct distributor_dialog_data *distributor_dialog_data_alloc(pjsip_dialog *dlg)
50 struct distributor_dialog_data *dist;
52 dist = PJ_POOL_ZALLOC_T(dlg->pool, struct distributor_dialog_data);
53 pjsip_dlg_set_mod_data(dlg, distributor_mod.id, dist);
58 void ast_sip_dialog_set_serializer(pjsip_dialog *dlg, struct ast_taskprocessor *serializer)
60 struct distributor_dialog_data *dist;
61 SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
63 dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
65 dist = distributor_dialog_data_alloc(dlg);
67 dist->serializer = serializer;
70 void ast_sip_dialog_set_endpoint(pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
72 struct distributor_dialog_data *dist;
73 SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
75 dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
77 dist = distributor_dialog_data_alloc(dlg);
79 dist->endpoint = endpoint;
82 struct ast_sip_endpoint *ast_sip_dialog_get_endpoint(pjsip_dialog *dlg)
84 struct distributor_dialog_data *dist;
85 SCOPED_LOCK(lock, dlg, pjsip_dlg_inc_lock, pjsip_dlg_dec_lock);
87 dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
88 if (!dist || !dist->endpoint) {
91 ao2_ref(dist->endpoint, +1);
92 return dist->endpoint;
95 static pjsip_dialog *find_dialog(pjsip_rx_data *rdata)
98 pjsip_transaction *tsx;
101 pj_str_t *remote_tag;
103 if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
104 local_tag = &rdata->msg_info.to->tag;
105 remote_tag = &rdata->msg_info.from->tag;
107 local_tag = &rdata->msg_info.from->tag;
108 remote_tag = &rdata->msg_info.to->tag;
111 /* We can only call the convenient method for
113 * 2) non-CANCEL requests
114 * 3) CANCEL requests with a to-tag
116 if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG ||
117 pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) ||
118 rdata->msg_info.to->tag.slen != 0) {
119 return pjsip_ua_find_dialog(&rdata->msg_info.cid->id, local_tag,
120 remote_tag, PJ_TRUE);
123 /* Incoming CANCEL without a to-tag can't use same method for finding the
124 * dialog. Instead, we have to find the matching INVITE transaction and
125 * then get the dialog from the transaction
127 pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAS,
128 pjsip_get_invite_method(), rdata);
130 tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
132 ast_log(LOG_ERROR, "Could not find matching INVITE transaction for CANCEL request\n");
136 dlg = pjsip_tsx_get_dlg(tsx);
138 #ifdef HAVE_PJ_TRANSACTION_GRP_LOCK
139 pj_grp_lock_release(tsx->grp_lock);
141 pj_mutex_unlock(tsx->mutex);
148 pjsip_dlg_inc_lock(dlg);
152 static pj_bool_t distributor(pjsip_rx_data *rdata)
154 pjsip_dialog *dlg = find_dialog(rdata);
155 struct distributor_dialog_data *dist = NULL;
156 struct ast_taskprocessor *serializer = NULL;
157 pjsip_rx_data *clone;
160 dist = pjsip_dlg_get_mod_data(dlg, distributor_mod.id);
162 serializer = dist->serializer;
166 if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG && (
167 !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) ||
168 !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) &&
170 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 481, NULL, NULL, NULL);
174 pjsip_rx_data_clone(rdata, 0, &clone);
177 clone->endpt_info.mod_data[distributor_mod.id] = dist->endpoint;
180 ast_sip_push_task(serializer, distribute, clone);
184 pjsip_dlg_dec_lock(dlg);
190 static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata);
192 static pjsip_module endpoint_mod = {
193 .name = {"Endpoint Identifier", 19},
194 .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 3,
195 .on_rx_request = endpoint_lookup,
198 static struct ast_sip_auth *artificial_auth;
200 static int create_artificial_auth(void)
202 if (!(artificial_auth = ast_sorcery_alloc(
203 ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, "artificial"))) {
204 ast_log(LOG_ERROR, "Unable to create artificial auth\n");
208 ast_string_field_set(artificial_auth, realm, "asterisk");
209 ast_string_field_set(artificial_auth, auth_user, "");
210 ast_string_field_set(artificial_auth, auth_pass, "");
211 artificial_auth->type = AST_SIP_AUTH_TYPE_ARTIFICIAL;
215 struct ast_sip_auth *ast_sip_get_artificial_auth(void)
217 ao2_ref(artificial_auth, +1);
218 return artificial_auth;
221 static struct ast_sip_endpoint *artificial_endpoint;
223 static int create_artificial_endpoint(void)
225 if (!(artificial_endpoint = ast_sorcery_alloc(
226 ast_sip_get_sorcery(), "endpoint", NULL))) {
230 artificial_endpoint->inbound_auths.num = 1;
234 struct ast_sip_endpoint *ast_sip_get_artificial_endpoint(void)
236 ao2_ref(artificial_endpoint, +1);
237 return artificial_endpoint;
240 static void log_unidentified_request(pjsip_rx_data *rdata)
242 char from_buf[PJSIP_MAX_URL_SIZE];
243 char callid_buf[PJSIP_MAX_URL_SIZE];
244 pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, rdata->msg_info.from->uri, from_buf, PJSIP_MAX_URL_SIZE);
245 ast_copy_pj_str(callid_buf, &rdata->msg_info.cid->id, PJSIP_MAX_URL_SIZE);
246 ast_log(LOG_NOTICE, "Request from '%s' failed for '%s:%d' (callid: %s) - No matching endpoint found\n",
247 from_buf, rdata->pkt_info.src_name, rdata->pkt_info.src_port, callid_buf);
250 static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata)
252 struct ast_sip_endpoint *endpoint;
253 int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
255 endpoint = rdata->endpt_info.mod_data[distributor_mod.id];
257 /* Bumping the refcount makes refcounting consistent whether an endpoint
258 * is looked up or not */
259 ao2_ref(endpoint, +1);
261 endpoint = ast_sip_identify_endpoint(rdata);
264 if (!endpoint && !is_ack) {
265 char name[AST_UUID_STR_LEN] = "";
266 pjsip_uri *from = rdata->msg_info.from->uri;
268 /* always use an artificial endpoint - per discussion no reason
269 to have "alwaysauthreject" as an option. It is felt using it
270 was a bug fix and it is not needed since we are not worried about
271 breaking old stuff and we really don't want to enable the discovery
273 endpoint = ast_sip_get_artificial_endpoint();
275 if (PJSIP_URI_SCHEME_IS_SIP(from) || PJSIP_URI_SCHEME_IS_SIPS(from)) {
276 pjsip_sip_uri *sip_from = pjsip_uri_get_uri(from);
277 ast_copy_pj_str(name, &sip_from->user, sizeof(name));
280 log_unidentified_request(rdata);
281 ast_sip_report_invalid_endpoint(name, rdata);
283 rdata->endpt_info.mod_data[endpoint_mod.id] = endpoint;
287 static pj_bool_t authenticate(pjsip_rx_data *rdata)
289 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
290 int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
292 ast_assert(endpoint != NULL);
294 if (!is_ack && ast_sip_requires_authentication(endpoint, rdata)) {
295 pjsip_tx_data *tdata;
296 pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, 401, NULL, &tdata);
297 switch (ast_sip_check_authentication(endpoint, rdata, tdata)) {
298 case AST_SIP_AUTHENTICATION_CHALLENGE:
299 /* Send the 401 we created for them */
300 ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata);
301 pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);
303 case AST_SIP_AUTHENTICATION_SUCCESS:
304 ast_sip_report_auth_success(endpoint, rdata);
305 pjsip_tx_data_dec_ref(tdata);
307 case AST_SIP_AUTHENTICATION_FAILED:
308 ast_sip_report_auth_failed_challenge_response(endpoint, rdata);
309 pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL);
311 case AST_SIP_AUTHENTICATION_ERROR:
312 ast_sip_report_auth_failed_challenge_response(endpoint, rdata);
313 pjsip_tx_data_dec_ref(tdata);
314 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
322 static pjsip_module auth_mod = {
323 .name = {"Request Authenticator", 21},
324 .priority = PJSIP_MOD_PRIORITY_APPLICATION - 1,
325 .on_rx_request = authenticate,
328 static int distribute(void *data)
330 static pjsip_process_rdata_param param = {
331 .start_mod = &distributor_mod,
332 .idx_after_start = 1,
335 pjsip_rx_data *rdata = data;
336 int is_request = rdata->msg_info.msg->type == PJSIP_REQUEST_MSG;
337 int is_ack = is_request ? rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD : 0;
338 struct ast_sip_endpoint *endpoint;
340 pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), rdata, ¶m, &handled);
341 if (!handled && is_request && !is_ack) {
342 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 501, NULL, NULL, NULL);
345 /* The endpoint_mod stores an endpoint reference in the mod_data of rdata. This
346 * is the only appropriate spot to actually decrement the reference.
348 endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
349 ao2_cleanup(endpoint);
350 pjsip_rx_data_free_cloned(rdata);
354 struct ast_sip_endpoint *ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata)
356 struct ast_sip_endpoint *endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
358 ao2_ref(endpoint, +1);
363 int ast_sip_initialize_distributor(void)
365 if (create_artificial_endpoint() || create_artificial_auth()) {
369 if (ast_sip_register_service(&distributor_mod)) {
372 if (ast_sip_register_service(&endpoint_mod)) {
375 if (ast_sip_register_service(&auth_mod)) {
382 void ast_sip_destroy_distributor(void)
384 ast_sip_unregister_service(&distributor_mod);
385 ast_sip_unregister_service(&endpoint_mod);
386 ast_sip_unregister_service(&auth_mod);
388 ao2_cleanup(artificial_auth);
389 ao2_cleanup(artificial_endpoint);