Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_im.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pjsua-lib/pjsua.h>
20 #include <pjsua-lib/pjsua_internal.h>
21
22
23 #define THIS_FILE "pjsua_im.h"
24
25
26 /* Declare MESSAGE method */
27 /* We put PJSIP_MESSAGE_METHOD as the enum here, so that when
28 * somebody add that method into pjsip_method_e in sip_msg.h we
29 * will get an error here.
30 */
31 enum
32 {
33 PJSIP_MESSAGE_METHOD = PJSIP_OTHER_METHOD
34 };
35
36 const pjsip_method pjsip_message_method =
37 {
38 (pjsip_method_e) PJSIP_MESSAGE_METHOD,
39 { "MESSAGE", 7 }
40 };
41
42
43 /* Proto */
44 static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata);
45
46 /* Per-request auth context for async auth support.
47 * Allocated from tsx->pool; kept alive via grp_lock ref.
48 */
49 typedef struct im_auth_ctx {
50 pjsip_auth_clt_sess auth;
51 pjsip_auth_clt_async_impl_token token;
52 pjsua_im_data *im_data;
53 pjsip_endpt_send_callback send_cb;
54 } im_auth_ctx;
55
56 static pj_status_t im_async_auth_send_impl(
57 pjsip_auth_clt_sess *auth_sess,
58 void *user_data,
59 pjsip_tx_data *tdata);
60 static void im_async_auth_abandon_impl(
61 pjsip_auth_clt_sess *auth_sess,
62 void *user_data);
63
64
65 /* The module instance. */
66 static pjsip_module mod_pjsua_im =
67 {
68 NULL, NULL, /* prev, next. */
69 { "mod-pjsua-im", 12 }, /* Name. */
70 -1, /* Id */
71 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
72 NULL, /* load() */
73 NULL, /* start() */
74 NULL, /* stop() */
75 NULL, /* unload() */
76 &im_on_rx_request, /* on_rx_request() */
77 NULL, /* on_rx_response() */
78 NULL, /* on_tx_request. */
79 NULL, /* on_tx_response() */
80 NULL, /* on_tsx_state() */
81
82 };
83
84
85 /* MIME constants. */
86 static const pj_str_t STR_MIME_APP = { "application", 11 };
87 static const pj_str_t STR_MIME_ISCOMPOSING = { "im-iscomposing+xml", 18 };
88
89
90 /* Check if content type is acceptable */
91 #if 0
92 static pj_bool_t acceptable_message(const pjsip_media_type *mime)
93 {
94 const pj_str_t STR_MIME_TEXT = { "text", 4 };
95 const pj_str_t STR_MIME_PLAIN = { "plain", 5 };
96 return (pj_stricmp(&mime->type, &STR_MIME_TEXT)==0 &&
97 pj_stricmp(&mime->subtype, &STR_MIME_PLAIN)==0)
98 ||
99 (pj_stricmp(&mime->type, &STR_MIME_APP)==0 &&
100 pj_stricmp(&mime->subtype, &STR_MIME_ISCOMPOSING)==0);
101 }
102 #endif
103
104 /**
105 * Create Accept header for MESSAGE.
106 */
107 pjsip_accept_hdr* pjsua_im_create_accept(pj_pool_t *pool)
108 {
109 /* Create Accept header. */
110 pjsip_accept_hdr *accept;
111
112 accept = pjsip_accept_hdr_create(pool);
113 accept->values[0] = pj_str("text/plain");
114 accept->values[1] = pj_str("application/im-iscomposing+xml");
115 accept->count = 2;
116
117 return accept;
118 }
119
120 /**
121 * Private: check if we can accept the message.
122 */
123 pj_bool_t pjsua_im_accept_pager(pjsip_rx_data *rdata,
124 pjsip_accept_hdr **p_accept_hdr)
125 {
126 /* Some UA sends text/html, so this check will break */
127 #if 0
128 pjsip_ctype_hdr *ctype;
129 pjsip_msg *msg;
130
131 msg = rdata->msg_info.msg;
132
133 /* Request MUST have message body, with Content-Type equal to
134 * "text/plain".
135 */
136 ctype = (pjsip_ctype_hdr*)
137 pjsip_msg_find_hdr(msg, PJSIP_H_CONTENT_TYPE, NULL);
138 if (msg->body == NULL || ctype == NULL ||
139 !acceptable_message(&ctype->media))
140 {
141 /* Create Accept header. */
142 if (p_accept_hdr)
143 *p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool);
144
145 return PJ_FALSE;
146 }
147 #elif 0
148 pjsip_msg *msg;
149
150 msg = rdata->msg_info.msg;
151 if (msg->body == NULL) {
152 /* Create Accept header. */
153 if (p_accept_hdr)
154 *p_accept_hdr = pjsua_im_create_accept(rdata->tp_info.pool);
155
156 return PJ_FALSE;
157 }
158 #else
159 /* Ticket #693: allow incoming MESSAGE without message body */
160 PJ_UNUSED_ARG(rdata);
161 PJ_UNUSED_ARG(p_accept_hdr);
162 #endif
163
164 return PJ_TRUE;
165 }
166
167 static pj_bool_t is_typing_indication(pjsip_rx_data *rdata)
168 {
169 pjsip_msg_body *body;
170 PJ_ASSERT_RETURN(rdata, PJ_FALSE);
171 PJ_ASSERT_RETURN(rdata->msg_info.msg, PJ_FALSE);
172
173 body = rdata->msg_info.msg->body;
174
175 if (body && pj_stricmp(&body->content_type.type, &STR_MIME_APP)==0 &&
176 pj_stricmp(&body->content_type.subtype, &STR_MIME_ISCOMPOSING)==0) {
177 return PJ_TRUE;
178 }
179 return PJ_FALSE;
180 }
181
182 /**
183 * Private: process pager message.
184 * This may trigger pjsua_ui_on_pager() or pjsua_ui_on_typing().
185 */
186 void pjsua_im_process_pager(int call_id, const pj_str_t *from,
187 const pj_str_t *to, pjsip_rx_data *rdata)
188 {
189 pjsip_contact_hdr *contact_hdr;
190 pj_str_t contact;
191 pjsip_msg_body *body = rdata->msg_info.msg->body;
192
193 #if 0
194 /* Ticket #693: allow incoming MESSAGE without message body */
195 /* Body MUST have been checked before */
196 pj_assert(body != NULL);
197 #endif
198
199
200 /* Build remote contact */
201 contact_hdr = (pjsip_contact_hdr*)
202 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
203 NULL);
204 if (contact_hdr && contact_hdr->uri) {
205 contact.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool,
206 PJSIP_MAX_URL_SIZE);
207 contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR,
208 contact_hdr->uri, contact.ptr,
209 PJSIP_MAX_URL_SIZE);
210 } else {
211 contact.slen = 0;
212 }
213
214 if (is_typing_indication(rdata) == PJ_TRUE) {
215 /* Expecting typing indication */
216 pj_status_t status;
217 pj_bool_t is_typing;
218
219 status = pjsip_iscomposing_parse(rdata->tp_info.pool, (char*)body->data,
220 body->len, &is_typing, NULL, NULL,
221 NULL );
222 if (status != PJ_SUCCESS) {
223 pjsua_perror(THIS_FILE, "Invalid MESSAGE body", status);
224 return;
225 }
226
227 if (pjsua_var.ua_cfg.cb.on_typing) {
228 (*pjsua_var.ua_cfg.cb.on_typing)(call_id, from, to, &contact,
229 is_typing);
230 }
231
232 if (pjsua_var.ua_cfg.cb.on_typing2) {
233 pjsua_acc_id acc_id;
234
235 if (call_id == PJSUA_INVALID_ID) {
236 acc_id = pjsua_acc_find_for_incoming(rdata);
237 } else {
238 pjsua_call *call = &pjsua_var.calls[call_id];
239 acc_id = call->acc_id;
240 }
241
242 if (acc_id != PJSUA_INVALID_ID) {
243 (*pjsua_var.ua_cfg.cb.on_typing2)(call_id, from, to, &contact,
244 is_typing, rdata, acc_id);
245 }
246 }
247
248 } else {
249 pj_str_t mime_type;
250 char buf[256];
251 pjsip_media_type *m;
252 pj_str_t text_body;
253
254 /* Save text body */
255 if (body) {
256 text_body.ptr = (char*)rdata->msg_info.msg->body->data;
257 text_body.slen = rdata->msg_info.msg->body->len;
258
259 /* Get mime type */
260 m = &rdata->msg_info.msg->body->content_type;
261 mime_type.ptr = buf;
262 mime_type.slen = pj_ansi_snprintf(buf, sizeof(buf),
263 "%.*s/%.*s",
264 (int)m->type.slen,
265 m->type.ptr,
266 (int)m->subtype.slen,
267 m->subtype.ptr);
268 if (mime_type.slen < 1)
269 mime_type.slen = 0;
270
271
272 } else {
273 text_body.ptr = mime_type.ptr = "";
274 text_body.slen = mime_type.slen = 0;
275 }
276
277 if (pjsua_var.ua_cfg.cb.on_pager) {
278 (*pjsua_var.ua_cfg.cb.on_pager)(call_id, from, to, &contact,
279 &mime_type, &text_body);
280 }
281
282 if (pjsua_var.ua_cfg.cb.on_pager2) {
283 pjsua_acc_id acc_id;
284
285 if (call_id == PJSUA_INVALID_ID) {
286 acc_id = pjsua_acc_find_for_incoming(rdata);
287 } else {
288 pjsua_call *call = &pjsua_var.calls[call_id];
289 acc_id = call->acc_id;
290 }
291
292 if (acc_id != PJSUA_INVALID_ID) {
293 (*pjsua_var.ua_cfg.cb.on_pager2)(call_id, from, to, &contact,
294 &mime_type, &text_body, rdata,
295 acc_id);
296 }
297 }
298 }
299 }
300
301
302 /*
303 * Handler to receive incoming MESSAGE
304 */
305 static pj_bool_t im_on_rx_request(pjsip_rx_data *rdata)
306 {
307 pjsua_acc_id acc_id = PJSUA_INVALID_ID;
308 pj_str_t from, to;
309 pjsip_accept_hdr *accept_hdr;
310 pjsip_msg *msg;
311
312 msg = rdata->msg_info.msg;
313
314 /* Only want to handle MESSAGE requests. */
315 if (pjsip_method_cmp(&msg->line.req.method, &pjsip_message_method) != 0) {
316 return PJ_FALSE;
317 }
318
319
320 /* Should not have any transaction attached to rdata. */
321 PJ_ASSERT_RETURN(pjsip_rdata_get_tsx(rdata)==NULL, PJ_FALSE);
322
323 /* Should not have any dialog attached to rdata. */
324 PJ_ASSERT_RETURN(pjsip_rdata_get_dlg(rdata)==NULL, PJ_FALSE);
325
326 /* Check if we can accept the message. */
327 if (!pjsua_im_accept_pager(rdata, &accept_hdr)) {
328 pjsip_hdr hdr_list;
329
330 pj_list_init(&hdr_list);
331 pj_list_push_back(&hdr_list, accept_hdr);
332
333 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
334 PJSIP_SC_NOT_ACCEPTABLE_HERE, NULL,
335 &hdr_list, NULL);
336 return PJ_TRUE;
337 }
338
339 acc_id = pjsua_acc_find_for_incoming(rdata);
340
341 if (acc_id != PJSUA_INVALID_ID &&
342 pjsua_var.acc[acc_id].cfg.auto_repond_sip_message == PJ_FALSE &&
343 is_typing_indication(rdata) == PJ_FALSE) {
344
345 pjsip_transaction *tsx;
346 pj_status_t status;
347
348 status = pjsip_tsx_create_uas(&mod_pjsua_im, rdata, &tsx);
349 if (status != PJ_SUCCESS) {
350 /* Fall back to immediate response if UAS creation fails */
351 pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL,
352 NULL, NULL, NULL);
353 return PJ_TRUE;
354 }
355
356 pjsip_tsx_recv_msg(tsx, rdata);
357 }
358 else
359 {
360 /* Respond with 200 OK */
361 pjsip_endpt_respond( pjsua_var.endpt, NULL, rdata, 200, NULL,
362 NULL, NULL, NULL);
363 }
364 /* For the source URI, we use Contact header if present, since
365 * Contact header contains the port number information. If this is
366 * not available, then use From header.
367 */
368 from.ptr = (char*)pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
369 from.slen = pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR,
370 rdata->msg_info.from->uri,
371 from.ptr, PJSIP_MAX_URL_SIZE);
372
373 if (from.slen < 1)
374 from = pj_str("<--URI is too long-->");
375
376 /* Build the To text. */
377 to.ptr = (char*) pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE);
378 to.slen = pjsip_uri_print( PJSIP_URI_IN_FROMTO_HDR,
379 rdata->msg_info.to->uri,
380 to.ptr, PJSIP_MAX_URL_SIZE);
381 if (to.slen < 1)
382 to = pj_str("<--URI is too long-->");
383
384 /* Process pager. */
385 pjsua_im_process_pager(PJSUA_INVALID_ID, &from, &to, rdata);
386
387 /* Done. */
388 return PJ_TRUE;
389 }
390
391
392 static pj_status_t im_async_auth_send_impl(
393 pjsip_auth_clt_sess *auth_sess,
394 void *user_data,
395 pjsip_tx_data *tdata)
396 {
397 im_auth_ctx *ctx = (im_auth_ctx *)user_data;
398 pjsip_endpoint *endpt = pjsua_var.endpt;
399 pjsip_endpt_send_callback send_cb;
400 pjsua_im_data *im_data;
401 pjsua_im_data *im_data2;
402
403 PJ_UNUSED_ARG(auth_sess);
404
405 send_cb = ctx->send_cb;
406 im_data = ctx->im_data;
407
408 im_data2 = pjsua_im_data_dup(tdata->pool, im_data);
409 PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++;
410
411 /* Clean up auth session; pool is freed by core layer via grp_lock */
412 pjsip_auth_clt_deinit(&ctx->auth);
413
414 return pjsip_endpt_send_request(endpt, tdata, -1, im_data2, send_cb);
415 }
416
417 /* Called when the application abandons a pending async IM auth challenge. */
418 static void im_async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess,
419 void *user_data)
420 {
421 im_auth_ctx *ctx = (im_auth_ctx *)user_data;
422
423 PJ_UNUSED_ARG(auth_sess);
424
425 /* Clean up auth session; pool is freed by core layer via grp_lock */
426 pjsip_auth_clt_deinit(&ctx->auth);
427 }
428
429
430 /* Outgoing IM callback. */
431 static void im_callback(void *token, pjsip_event *e)
432 {
433 pjsua_im_data *im_data = (pjsua_im_data*) token;
434
435 if (e->type == PJSIP_EVENT_TSX_STATE) {
436
437 pjsip_transaction *tsx = e->body.tsx_state.tsx;
438
439 /* Ignore provisional response, if any */
440 if (tsx->status_code < 200)
441 return;
442
443
444 /* Handle authentication challenges */
445 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG &&
446 (tsx->status_code == 401 || tsx->status_code == 407))
447 {
448 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
449 pjsip_tx_data *tdata;
450 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
451 im_auth_ctx *ctx;
452 pjsua_im_data *im_data2;
453 pj_status_t status;
454
455 PJ_LOG(4,(THIS_FILE, "Resending IM with authentication"));
456
457 ctx = PJ_POOL_ZALLOC_T(tsx->pool, im_auth_ctx);
458 ctx->im_data = im_data;
459 ctx->send_cb = &im_callback;
460
461 pjsip_auth_clt_init(&ctx->auth, pjsua_var.endpt, tsx->pool, 0);
462
463 /* Set shared or non-shared async auth for IM */
464 if (pjsua_var.acc[im_data->acc_id].cfg.use_shared_auth) {
465 pjsip_auth_clt_set_parent(&ctx->auth,
466 &pjsua_var.acc[im_data->acc_id].shared_auth_sess);
467 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
468 pjsip_auth_clt_async_setting async_opt;
469 pj_bzero(&async_opt, sizeof(async_opt));
470 async_opt.cb = &pjsua_auth_on_challenge;
471 async_opt.user_data = (void*)(pj_ssize_t)im_data->acc_id;
472 pjsip_auth_clt_async_configure(&ctx->auth, &async_opt);
473 }
474
475 pjsip_auth_clt_set_credentials(&ctx->auth,
476 pjsua_var.acc[im_data->acc_id].cred_cnt,
477 pjsua_var.acc[im_data->acc_id].cred);
478 pjsip_auth_clt_set_prefs(&ctx->auth,
479 &pjsua_var.acc[im_data->acc_id].cfg.auth_pref);
480
481 ctx->token.user_data = ctx;
482 ctx->token.send_impl = &im_async_auth_send_impl;
483 ctx->token.abandon_impl = &im_async_auth_abandon_impl;
484 ctx->token.grp_lock = tsx->grp_lock;
485 pj_grp_lock_add_ref(tsx->grp_lock);
486
487 pj_bzero(&chal_param, sizeof(chal_param));
488 chal_param.rdata = rdata;
489 chal_param.tdata = tsx->last_tx;
490 status = pjsip_auth_clt_async_impl_on_challenge(
491 &ctx->auth, &ctx->token,
492 &chal_param);
493 if (status == PJ_SUCCESS) {
494 /* Async auth dispatched; memory freed by core layer
495 * via grp_lock dec_ref.
496 */
497 return;
498 }
499
500 /* Async dispatch failed — release grp_lock ref and fall
501 * through to sync path.
502 */
503 pj_grp_lock_dec_ref(tsx->grp_lock);
504
505 /* Sync fallback */
506 status = pjsip_auth_clt_reinit_req(&ctx->auth, rdata,
507 tsx->last_tx, &tdata);
508 if (status == PJ_SUCCESS) {
509 /* Must duplicate im_data */
510 im_data2 = pjsua_im_data_dup(tdata->pool, im_data);
511
512 /* Increment CSeq */
513 PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++;
514
515 /* Re-send request */
516 status = pjsip_endpt_send_request(pjsua_var.endpt, tdata,
517 -1, im_data2,
518 &im_callback);
519 pjsip_auth_clt_deinit(&ctx->auth);
520 if (status == PJ_SUCCESS)
521 return;
522
523 /* Don't invoke callback if another callback (with auth,
524 * different tsx) has been called.
525 */
526 if (im_data2->acc_id == PJSUA_INVALID_ID)
527 return;
528 } else {
529 pjsip_auth_clt_deinit(&ctx->auth);
530 }
531 }
532
533 if (tsx->status_code/100 == 2) {
534 PJ_LOG(4,(THIS_FILE,
535 "Message \'%s\' delivered successfully",
536 im_data->body.ptr));
537 } else {
538 PJ_LOG(3,(THIS_FILE,
539 "Failed to deliver message \'%s\': %d/%.*s",
540 im_data->body.ptr,
541 tsx->status_code,
542 (int)tsx->status_text.slen,
543 tsx->status_text.ptr));
544 }
545
546 if (pjsua_var.ua_cfg.cb.on_pager_status) {
547 pj_str_t im_body = im_data->body;
548 if (im_body.slen==0) {
549 pjsip_msg_body *body = tsx->last_tx->msg->body;
550 pj_strset(&im_body, body->data, body->len);
551 }
552
553 pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id,
554 &im_data->to,
555 &im_body,
556 im_data->user_data,
557 (pjsip_status_code)
558 tsx->status_code,
559 &tsx->status_text);
560 }
561
562 if (pjsua_var.ua_cfg.cb.on_pager_status2) {
563 pjsip_rx_data *rdata;
564 pj_str_t im_body = im_data->body;
565
566 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
567 rdata = e->body.tsx_state.src.rdata;
568 else
569 rdata = NULL;
570
571 if (im_body.slen==0) {
572 pjsip_msg_body *body = tsx->last_tx->msg->body;
573 pj_strset(&im_body, body->data, body->len);
574 }
575
576 pjsua_var.ua_cfg.cb.on_pager_status2(im_data->call_id,
577 &im_data->to,
578 &im_body,
579 im_data->user_data,
580 (pjsip_status_code)
581 tsx->status_code,
582 &tsx->status_text,
583 tsx->last_tx,
584 rdata, im_data->acc_id);
585 }
586
587 /* Reset acc_id after invoking callback */
588 im_data->acc_id = PJSUA_INVALID_ID;
589 }
590 }
591
592
593 /* Outgoing typing indication callback.
594 * (used to reauthenticate request)
595 */
596 static void typing_callback(void *token, pjsip_event *e)
597 {
598 pjsua_im_data *im_data = (pjsua_im_data*) token;
599
600 if (e->type == PJSIP_EVENT_TSX_STATE) {
601
602 pjsip_transaction *tsx = e->body.tsx_state.tsx;
603
604 /* Ignore provisional response, if any */
605 if (tsx->status_code < 200)
606 return;
607
608 /* Handle authentication challenges */
609 if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG &&
610 (tsx->status_code == 401 || tsx->status_code == 407))
611 {
612 pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
613 pjsip_tx_data *tdata;
614 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
615 im_auth_ctx *ctx;
616 pj_status_t status;
617
618 PJ_LOG(4,(THIS_FILE, "Resending IM with authentication"));
619
620 ctx = PJ_POOL_ZALLOC_T(tsx->pool, im_auth_ctx);
621 ctx->im_data = im_data;
622 ctx->send_cb = &typing_callback;
623
624 pjsip_auth_clt_init(&ctx->auth, pjsua_var.endpt, tsx->pool, 0);
625
626 /* Set shared or non-shared async auth for IM */
627 if (pjsua_var.acc[im_data->acc_id].cfg.use_shared_auth) {
628 pjsip_auth_clt_set_parent(&ctx->auth,
629 &pjsua_var.acc[im_data->acc_id].shared_auth_sess);
630 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
631 pjsip_auth_clt_async_setting async_opt;
632 pj_bzero(&async_opt, sizeof(async_opt));
633 async_opt.cb = &pjsua_auth_on_challenge;
634 async_opt.user_data = (void*)(pj_ssize_t)im_data->acc_id;
635 pjsip_auth_clt_async_configure(&ctx->auth, &async_opt);
636 }
637
638 pjsip_auth_clt_set_credentials(&ctx->auth,
639 pjsua_var.acc[im_data->acc_id].cred_cnt,
640 pjsua_var.acc[im_data->acc_id].cred);
641 pjsip_auth_clt_set_prefs(&ctx->auth,
642 &pjsua_var.acc[im_data->acc_id].cfg.auth_pref);
643
644 ctx->token.user_data = ctx;
645 ctx->token.send_impl = &im_async_auth_send_impl;
646 ctx->token.abandon_impl = &im_async_auth_abandon_impl;
647 ctx->token.grp_lock = tsx->grp_lock;
648 pj_grp_lock_add_ref(tsx->grp_lock);
649
650 pj_bzero(&chal_param, sizeof(chal_param));
651 chal_param.rdata = rdata;
652 chal_param.tdata = tsx->last_tx;
653 status = pjsip_auth_clt_async_impl_on_challenge(
654 &ctx->auth, &ctx->token,
655 &chal_param);
656 if (status == PJ_SUCCESS) {
657 /* Async auth dispatched; memory freed by core layer
658 * via grp_lock dec_ref.
659 */
660 return;
661 }
662
663 /* Async dispatch failed — release grp_lock ref and fall
664 * through to sync path.
665 */
666 pj_grp_lock_dec_ref(tsx->grp_lock);
667
668 /* Sync fallback */
669 status = pjsip_auth_clt_reinit_req(&ctx->auth, rdata,
670 tsx->last_tx, &tdata);
671 if (status == PJ_SUCCESS) {
672 pjsua_im_data *im_data2;
673
674 /* Must duplicate im_data */
675 im_data2 = pjsua_im_data_dup(tdata->pool, im_data);
676
677 /* Increment CSeq */
678 PJSIP_MSG_CSEQ_HDR(tdata->msg)->cseq++;
679
680 /* Re-send request */
681 status = pjsip_endpt_send_request(pjsua_var.endpt, tdata,
682 -1, im_data2,
683 &typing_callback);
684 pjsip_auth_clt_deinit(&ctx->auth);
685 if (status == PJ_SUCCESS)
686 return;
687 } else {
688 pjsip_auth_clt_deinit(&ctx->auth);
689 }
690 }
691
692 }
693 }
694
695
696 /*
697 * Send instant messaging outside dialog, using the specified account for
698 * route set and authentication.
699 */
700 PJ_DEF(pj_status_t) pjsua_im_send( pjsua_acc_id acc_id,
701 const pj_str_t *to,
702 const pj_str_t *mime_type,
703 const pj_str_t *content,
704 const pjsua_msg_data *msg_data,
705 void *user_data)
706 {
707 pjsip_tx_data *tdata;
708 const pj_str_t mime_text_plain = pj_str("text/plain");
709 pjsip_media_type media_type;
710 pjsua_im_data *im_data;
711 pjsua_acc *acc;
712 pj_bool_t content_in_msg_data;
713 pjsip_tpselector tp_sel;
714 pj_status_t status;
715
716 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
717 PJ_EINVAL);
718
719 content_in_msg_data = msg_data && (msg_data->msg_body.slen ||
720 msg_data->multipart_ctype.type.slen);
721
722 /* To and message body must be specified. */
723 PJ_ASSERT_RETURN(to && (content || content_in_msg_data), PJ_EINVAL);
724
725 acc = &pjsua_var.acc[acc_id];
726
727 /* Create request. */
728 status = pjsip_endpt_create_request(pjsua_var.endpt,
729 &pjsip_message_method,
730 (msg_data && msg_data->target_uri.slen?
731 &msg_data->target_uri: to),
732 (msg_data && msg_data->local_uri.slen?
733 &msg_data->local_uri: &acc->cfg.id),
734 to, NULL, NULL, -1, NULL, &tdata);
735 if (status != PJ_SUCCESS) {
736 pjsua_perror(THIS_FILE, "Unable to create request", status);
737 return status;
738 }
739
740 /* Set transmit data's transport based on acc's config. */
741 pjsua_init_tpselector(acc_id, &tp_sel);
742 pjsip_tx_data_set_transport(tdata, &tp_sel);
743
744 /* Add accept header. */
745 pjsip_msg_add_hdr( tdata->msg,
746 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
747
748 /* Create suitable Contact header unless a Contact header has been
749 * set in the account.
750 */
751 /* Ticket #1632: According to RFC 3428:
752 * MESSAGE requests do not initiate dialogs.
753 * User Agents MUST NOT insert Contact header fields into MESSAGE requests
754 */
755 /*
756 if (acc->contact.slen) {
757 contact = acc->contact;
758 } else {
759 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
760 if (status != PJ_SUCCESS) {
761 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
762 pjsip_tx_data_dec_ref(tdata);
763 return status;
764 }
765 }
766
767 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
768 pjsip_generic_string_hdr_create(tdata->pool,
769 &STR_CONTACT, &contact));
770 */
771
772 /* Create IM data to keep message details and give it back to
773 * application on the callback
774 */
775 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
776 im_data->acc_id = acc_id;
777 im_data->call_id = PJSUA_INVALID_ID;
778 pj_strdup_with_null(tdata->pool, &im_data->to, to);
779 im_data->user_data = user_data;
780
781
782 /* Add message body, if content is set */
783 if (content) {
784 pj_strdup_with_null(tdata->pool, &im_data->body, content);
785
786 /* Set default media type if none is specified */
787 if (mime_type == NULL) {
788 mime_type = &mime_text_plain;
789 }
790
791 /* Parse MIME type */
792 pjsua_parse_media_type(tdata->pool, mime_type, &media_type);
793
794 /* Add message body */
795 tdata->msg->body = pjsip_msg_body_create( tdata->pool, &media_type.type,
796 &media_type.subtype,
797 &im_data->body);
798 if (tdata->msg->body == NULL) {
799 pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM);
800 pjsip_tx_data_dec_ref(tdata);
801 return PJ_ENOMEM;
802 }
803 }
804
805 /* Add additional headers etc. */
806 pjsua_process_msg_data(tdata, msg_data);
807
808 /* Add route set */
809 pjsua_set_msg_route_set(tdata, &acc->route_set);
810
811 /* If via_addr is set, use this address for the Via header. */
812 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
813 tdata->via_addr = acc->via_addr;
814 tdata->via_tp = acc->via_tp;
815 }
816
817 /* Send request (statefully) */
818 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
819 im_data, &im_callback);
820 if (status != PJ_SUCCESS) {
821 pjsua_perror(THIS_FILE, "Unable to send request", status);
822 return status;
823 }
824
825 return PJ_SUCCESS;
826 }
827
828
829 /*
830 * Send typing indication outside dialog.
831 */
832 PJ_DEF(pj_status_t) pjsua_im_typing( pjsua_acc_id acc_id,
833 const pj_str_t *to,
834 pj_bool_t is_typing,
835 const pjsua_msg_data *msg_data)
836 {
837 pjsua_im_data *im_data;
838 pjsip_tx_data *tdata;
839 pjsua_acc *acc;
840 pjsip_tpselector tp_sel;
841 pj_status_t status;
842
843 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
844 PJ_EINVAL);
845
846 acc = &pjsua_var.acc[acc_id];
847
848 /* Create request. */
849 status = pjsip_endpt_create_request( pjsua_var.endpt, &pjsip_message_method,
850 to, &acc->cfg.id,
851 to, NULL, NULL, -1, NULL, &tdata);
852 if (status != PJ_SUCCESS) {
853 pjsua_perror(THIS_FILE, "Unable to create request", status);
854 return status;
855 }
856
857 /* Set transmit data's transport based on acc's config. */
858 pjsua_init_tpselector(acc_id, &tp_sel);
859 pjsip_tx_data_set_transport(tdata, &tp_sel);
860
861 /* Add accept header. */
862 pjsip_msg_add_hdr( tdata->msg,
863 (pjsip_hdr*)pjsua_im_create_accept(tdata->pool));
864
865
866 /* Create suitable Contact header unless a Contact header has been
867 * set in the account.
868 */
869 /* Ticket #1632: According to RFC 3428:
870 * MESSAGE requests do not initiate dialogs.
871 * User Agents MUST NOT insert Contact header fields into MESSAGE requests
872 */
873 /*
874 if (acc->contact.slen) {
875 contact = acc->contact;
876 } else {
877 status = pjsua_acc_create_uac_contact(tdata->pool, &contact, acc_id, to);
878 if (status != PJ_SUCCESS) {
879 pjsua_perror(THIS_FILE, "Unable to generate Contact header", status);
880 pjsip_tx_data_dec_ref(tdata);
881 return status;
882 }
883 }
884
885 pjsip_msg_add_hdr( tdata->msg, (pjsip_hdr*)
886 pjsip_generic_string_hdr_create(tdata->pool,
887 &STR_CONTACT, &contact));
888 */
889
890 /* Create "application/im-iscomposing+xml" msg body. */
891 tdata->msg->body = pjsip_iscomposing_create_body( tdata->pool, is_typing,
892 NULL, NULL, -1);
893
894 /* Add additional headers etc. */
895 pjsua_process_msg_data(tdata, msg_data);
896
897 /* Add route set */
898 pjsua_set_msg_route_set(tdata, &acc->route_set);
899
900 /* If via_addr is set, use this address for the Via header. */
901 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
902 tdata->via_addr = acc->via_addr;
903 tdata->via_tp = acc->via_tp;
904 }
905
906 /* Create data to reauthenticate */
907 im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data);
908 im_data->acc_id = acc_id;
909
910 /* Send request (statefully) */
911 status = pjsip_endpt_send_request( pjsua_var.endpt, tdata, -1,
912 im_data, &typing_callback);
913 if (status != PJ_SUCCESS) {
914 pjsua_perror(THIS_FILE, "Unable to send request", status);
915 return status;
916 }
917
918 return PJ_SUCCESS;
919 }
920
921
922 /*
923 * Init pjsua IM module.
924 */
925 pj_status_t pjsua_im_init(void)
926 {
927 const pj_str_t msg_tag = { "MESSAGE", 7 };
928 const pj_str_t STR_MIME_TEXT_PLAIN = { "text/plain", 10 };
929 const pj_str_t STR_MIME_APP_ISCOMPOSING =
930 { "application/im-iscomposing+xml", 30 };
931 pj_status_t status;
932
933 /* Register module */
934 status = pjsip_endpt_register_module(pjsua_var.endpt, &mod_pjsua_im);
935 if (status != PJ_SUCCESS)
936 return status;
937
938 /* Register support for MESSAGE method. */
939 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ALLOW,
940 NULL, 1, &msg_tag);
941
942 /* Register support for "application/im-iscomposing+xml" content */
943 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
944 NULL, 1, &STR_MIME_APP_ISCOMPOSING);
945
946 /* Register support for "text/plain" content */
947 pjsip_endpt_add_capability( pjsua_var.endpt, &mod_pjsua_im, PJSIP_H_ACCEPT,
948 NULL, 1, &STR_MIME_TEXT_PLAIN);
949
950 return PJ_SUCCESS;
951 }
952