Debian Codegraph @

Xpjproject/pjsip/src/pjsua-lib/pjsua_pres.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_pres.c"
24
25
26 static void subscribe_buddy(pjsua_buddy_id buddy_id, pj_bool_t presence);
27 static void unsubscribe_buddy(pjsua_buddy_id buddy_id, pj_bool_t presence);
28
29 /*
30 * Find buddy.
31 */
32 static pjsua_buddy_id find_buddy(const pjsip_uri *uri)
33 {
34 const pjsip_sip_uri *sip_uri;
35 unsigned i;
36
37 uri = (const pjsip_uri*) pjsip_uri_get_uri((pjsip_uri*)uri);
38
39 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))
40 return PJSUA_INVALID_ID;
41
42 sip_uri = (const pjsip_sip_uri*) uri;
43
44 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
45 const pjsua_buddy *b = &pjsua_var.buddy[i];
46
47 if (!pjsua_buddy_is_valid(i))
48 continue;
49
50 if (pj_stricmp(&sip_uri->user, &b->name)==0 &&
51 pj_stricmp(&sip_uri->host, &b->host)==0 &&
52 (sip_uri->port==(int)b->port || (sip_uri->port==0 && b->port==5060)))
53 {
54 /* Match */
55 return i;
56 }
57 }
58
59 return PJSUA_INVALID_ID;
60 }
61
62 #define LOCK_DIALOG 1
63 #define LOCK_PJSUA 2
64 #define LOCK_ALL (LOCK_DIALOG | LOCK_PJSUA)
65
66 /* Buddy lock object */
67 struct buddy_lock
68 {
69 pjsua_buddy *buddy;
70 pjsip_dialog *dlg;
71 pj_uint8_t flag;
72 };
73
74 /* Acquire lock to the specified buddy_id */
75 static pj_status_t lock_buddy(const char *title,
76 pjsua_buddy_id buddy_id,
77 struct buddy_lock *lck,
78 unsigned _unused_)
79 {
80 enum { MAX_RETRY=50 };
81 pj_bool_t has_pjsua_lock = PJ_FALSE;
82 unsigned retry;
83
84 PJ_UNUSED_ARG(_unused_);
85
86 pj_bzero(lck, sizeof(*lck));
87
88 for (retry=0; retry<MAX_RETRY; ++retry) {
89
90 if (PJSUA_TRY_LOCK() != PJ_SUCCESS) {
91 pj_thread_sleep(retry/10);
92 continue;
93 }
94
95 has_pjsua_lock = PJ_TRUE;
96 lck->flag = LOCK_PJSUA;
97 lck->buddy = &pjsua_var.buddy[buddy_id];
98
99 if (lck->buddy->dlg == NULL)
100 return PJ_SUCCESS;
101
102 if (pjsip_dlg_try_inc_lock(lck->buddy->dlg) != PJ_SUCCESS) {
103 lck->flag = 0;
104 lck->buddy = NULL;
105 has_pjsua_lock = PJ_FALSE;
106 PJSUA_UNLOCK();
107 pj_thread_sleep(retry/10);
108 continue;
109 }
110
111 lck->dlg = lck->buddy->dlg;
112 lck->flag = LOCK_DIALOG;
113 PJSUA_UNLOCK();
114
115 break;
116 }
117
118 if (lck->flag == 0) {
119 if (has_pjsua_lock == PJ_FALSE)
120 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex "
121 "(possibly system has deadlocked) in %s",
122 title));
123 else
124 PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex "
125 "(possibly system has deadlocked) in %s",
126 title));
127 return PJ_ETIMEDOUT;
128 }
129
130 return PJ_SUCCESS;
131 }
132
133 /* Release buddy lock */
134 static void unlock_buddy(struct buddy_lock *lck)
135 {
136 if (lck->flag & LOCK_DIALOG)
137 pjsip_dlg_dec_lock(lck->dlg);
138
139 if (lck->flag & LOCK_PJSUA)
140 PJSUA_UNLOCK();
141 }
142
143
144 /*
145 * Get total number of buddies.
146 */
147 PJ_DEF(unsigned) pjsua_get_buddy_count(void)
148 {
149 return pjsua_var.buddy_cnt;
150 }
151
152
153 /*
154 * Find buddy.
155 */
156 PJ_DEF(pjsua_buddy_id) pjsua_buddy_find(const pj_str_t *uri_str)
157 {
158 pj_str_t input;
159 pj_pool_t *pool;
160 pjsip_uri *uri;
161 pjsua_buddy_id buddy_id;
162
163 pool = pjsua_pool_create("buddyfind", 512, 512);
164 pj_strdup_with_null(pool, &input, uri_str);
165
166 uri = pjsip_parse_uri(pool, input.ptr, input.slen, 0);
167 if (!uri)
168 buddy_id = PJSUA_INVALID_ID;
169 else {
170 PJSUA_LOCK();
171 buddy_id = find_buddy(uri);
172 PJSUA_UNLOCK();
173 }
174
175 pj_pool_release(pool);
176
177 return buddy_id;
178 }
179
180
181 /*
182 * Check if buddy ID is valid.
183 */
184 PJ_DEF(pj_bool_t) pjsua_buddy_is_valid(pjsua_buddy_id buddy_id)
185 {
186 return buddy_id>=0 && buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy) &&
187 pjsua_var.buddy[buddy_id].uri.slen != 0;
188 }
189
190
191 /*
192 * Enum buddy IDs.
193 */
194 PJ_DEF(pj_status_t) pjsua_enum_buddies( pjsua_buddy_id ids[],
195 unsigned *count)
196 {
197 unsigned i, c;
198
199 PJ_ASSERT_RETURN(ids && count, PJ_EINVAL);
200
201 PJSUA_LOCK();
202
203 for (i=0, c=0; c<*count && i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
204 if (!pjsua_var.buddy[i].uri.slen)
205 continue;
206 ids[c] = i;
207 ++c;
208 }
209
210 *count = c;
211
212 PJSUA_UNLOCK();
213
214 return PJ_SUCCESS;
215 }
216
217
218 /*
219 * Get detailed buddy info.
220 */
221 PJ_DEF(pj_status_t) pjsua_buddy_get_info( pjsua_buddy_id buddy_id,
222 pjsua_buddy_info *info)
223 {
224 pj_size_t total=0;
225 struct buddy_lock lck;
226 pjsua_buddy *buddy;
227 pj_status_t status;
228
229 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
230
231 pj_bzero(info, sizeof(pjsua_buddy_info));
232
233 status = lock_buddy("pjsua_buddy_get_info()", buddy_id, &lck, 0);
234 if (status != PJ_SUCCESS)
235 return status;
236
237 buddy = lck.buddy;
238 info->id = buddy->index;
239 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
240 unlock_buddy(&lck);
241 return PJ_SUCCESS;
242 }
243
244 /* uri */
245 info->uri.ptr = info->buf_ + total;
246 pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
247 total += info->uri.slen;
248
249 /* acc id */
250 info->acc_id = buddy->acc_id;
251
252 /* contact */
253 if (total < sizeof(info->buf_)) {
254 info->contact.ptr = info->buf_ + total;
255 pj_strncpy(&info->contact, &buddy->contact, sizeof(info->buf_) - total);
256 total += info->contact.slen;
257 } else {
258 info->contact = pj_str("");
259 }
260
261 /* Presence status */
262 pj_memcpy(&info->pres_status, &buddy->status, sizeof(pjsip_pres_status));
263
264 /* status and status text */
265 if (buddy->sub == NULL || buddy->status.info_cnt==0) {
266 info->status = PJSUA_BUDDY_STATUS_UNKNOWN;
267 info->status_text = pj_str("?");
268 } else if (pjsua_var.buddy[buddy_id].status.info[0].basic_open) {
269 info->status = PJSUA_BUDDY_STATUS_ONLINE;
270
271 /* copy RPID information */
272 info->rpid = buddy->status.info[0].rpid;
273
274 if (info->rpid.note.slen)
275 info->status_text = info->rpid.note;
276 else
277 info->status_text = pj_str("Online");
278
279 } else {
280 info->status = PJSUA_BUDDY_STATUS_OFFLINE;
281 info->rpid = buddy->status.info[0].rpid;
282
283 if (info->rpid.note.slen)
284 info->status_text = info->rpid.note;
285 else
286 info->status_text = pj_str("Offline");
287 }
288
289 /* monitor pres */
290 info->monitor_pres = buddy->monitor;
291
292 /* subscription state and termination reason */
293 info->sub_term_code = buddy->term_code;
294 if (buddy->sub) {
295 info->sub_state = pjsip_evsub_get_state(buddy->sub);
296 info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
297 if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
298 total < sizeof(info->buf_))
299 {
300 info->sub_term_reason.ptr = info->buf_ + total;
301 pj_strncpy(&info->sub_term_reason,
302 pjsip_evsub_get_termination_reason(buddy->sub),
303 sizeof(info->buf_) - total);
304 total += info->sub_term_reason.slen;
305 } else {
306 info->sub_term_reason = pj_str("");
307 }
308 } else if (total < sizeof(info->buf_)) {
309 info->sub_state_name = "NULL";
310 info->sub_term_reason.ptr = info->buf_ + total;
311 pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
312 sizeof(info->buf_) - total);
313 total += info->sub_term_reason.slen;
314 } else {
315 info->sub_state_name = "NULL";
316 info->sub_term_reason = pj_str("");
317 }
318
319 unlock_buddy(&lck);
320 return PJ_SUCCESS;
321 }
322
323 /*
324 * Get detailed buddy dialog event info.
325 */
326 PJ_DEF(pj_status_t)
327 pjsua_buddy_get_dlg_event_info( pjsua_buddy_id buddy_id,
328 pjsua_buddy_dlg_event_info *info)
329 {
330 pj_ssize_t total=0;
331 struct buddy_lock lck;
332 pjsua_buddy *buddy;
333 pj_status_t status;
334
335 #define COPY_TO_BUF(status, field) \
336 if (status.field.slen > 0) { \
337 if (total + status.field.slen < sizeof(info->buf_)) { \
338 info->field.ptr = info->buf_ + total; \
339 pj_strncpy(&info->field, &status.field, status.field.slen); \
340 total += info->field.slen; \
341 } else { \
342 PJ_LOG(4, (THIS_FILE, "Insufficient buffer when copying %s", \
343 #field)); \
344 } \
345 }
346
347 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
348
349 pj_bzero(info, sizeof(pjsua_buddy_dlg_event_info));
350
351 status = lock_buddy("pjsua_buddy_get_dlg_event_info()", buddy_id, &lck, 0);
352 if (status != PJ_SUCCESS)
353 return status;
354
355 buddy = lck.buddy;
356 info->id = buddy->index;
357 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
358 unlock_buddy(&lck);
359 return PJ_SUCCESS;
360 }
361
362 /* uri */
363 info->uri.ptr = info->buf_ + total;
364 pj_strncpy(&info->uri, &buddy->uri, sizeof(info->buf_)-total);
365 total += info->uri.slen;
366
367 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_info_state);
368 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_info_entity);
369
370 if (buddy->dlg_ev_status.info[0].dialog_node) {
371 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_id);
372 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_call_id);
373 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_remote_tag);
374 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_local_tag);
375 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_direction);
376 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_state);
377 COPY_TO_BUF(buddy->dlg_ev_status.info[0], dialog_duration);
378
379 COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_identity);
380 COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_identity_display);
381 COPY_TO_BUF(buddy->dlg_ev_status.info[0], local_target_uri);
382 COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_identity);
383 COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_identity_display);
384 COPY_TO_BUF(buddy->dlg_ev_status.info[0], remote_target_uri);
385 }
386
387 /* subscription state and termination reason */
388 info->sub_term_code = buddy->term_code;
389 if (buddy->sub) {
390 info->sub_state = pjsip_evsub_get_state(buddy->sub);
391 info->sub_state_name = pjsip_evsub_get_state_name(buddy->sub);
392 if (info->sub_state == PJSIP_EVSUB_STATE_TERMINATED &&
393 total < sizeof(info->buf_))
394 {
395 info->sub_term_reason.ptr = info->buf_ + total;
396 pj_strncpy(&info->sub_term_reason,
397 pjsip_evsub_get_termination_reason(buddy->sub),
398 sizeof(info->buf_) - total);
399 total += info->sub_term_reason.slen;
400 } else {
401 info->sub_term_reason = pj_str("");
402 }
403 } else if (total < sizeof(info->buf_)) {
404 info->sub_state_name = "NULL";
405 info->sub_term_reason.ptr = info->buf_ + total;
406 pj_strncpy(&info->sub_term_reason, &buddy->term_reason,
407 sizeof(info->buf_) - total);
408 total += info->sub_term_reason.slen;
409 } else {
410 info->sub_state_name = "NULL";
411 info->sub_term_reason = pj_str("");
412 }
413
414 unlock_buddy(&lck);
415 return PJ_SUCCESS;
416 }
417
418 /*
419 * Set the user data associated with the buddy object.
420 */
421 PJ_DEF(pj_status_t) pjsua_buddy_set_user_data( pjsua_buddy_id buddy_id,
422 void *user_data)
423 {
424 //struct buddy_lock lck;
425 //pj_status_t status;
426
427 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
428
429 /* Locking may fail and validity check above should be sufficient.
430 * Other similar functions also skip locking.
431 */
432 //status = lock_buddy("pjsua_buddy_set_user_data()", buddy_id, &lck, 0);
433 //if (status != PJ_SUCCESS)
434 // return status;
435
436 pjsua_var.buddy[buddy_id].user_data = user_data;
437
438 //unlock_buddy(&lck);
439
440 return PJ_SUCCESS;
441 }
442
443
444 /*
445 * Get the user data associated with the budy object.
446 */
447 PJ_DEF(void*) pjsua_buddy_get_user_data(pjsua_buddy_id buddy_id)
448 {
449 //struct buddy_lock lck;
450 //pj_status_t status;
451 void *user_data;
452
453 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), NULL);
454
455 /* Locking may fail and application has no idea whether the user_data
456 * is really NULL or locking failed, so we skip locking here.
457 * Other similar functions also skip locking.
458 */
459 //status = lock_buddy("pjsua_buddy_get_user_data()", buddy_id, &lck, 0);
460 //if (status != PJ_SUCCESS)
461 // return NULL;
462
463 user_data = pjsua_var.buddy[buddy_id].user_data;
464
465 //unlock_buddy(&lck);
466
467 return user_data;
468 }
469
470
471 /*
472 * Reset buddy descriptor.
473 */
474 static void reset_buddy(pjsua_buddy_id id)
475 {
476 pj_pool_t *pool = pjsua_var.buddy[id].pool;
477 pj_bzero(&pjsua_var.buddy[id], sizeof(pjsua_var.buddy[id]));
478 pjsua_var.buddy[id].pool = pool;
479 pjsua_var.buddy[id].index = id;
480 pjsua_var.buddy[id].acc_id = PJSUA_INVALID_ID;
481 }
482
483
484 /*
485 * Add new buddy.
486 */
487 PJ_DEF(pj_status_t) pjsua_buddy_add( const pjsua_buddy_config *cfg,
488 pjsua_buddy_id *p_buddy_id)
489 {
490 pjsip_name_addr *url;
491 pjsua_buddy *buddy;
492 pjsip_sip_uri *sip_uri;
493 int index;
494 pj_str_t tmp;
495
496 PJ_ASSERT_RETURN(pjsua_var.buddy_cnt <=
497 PJ_ARRAY_SIZE(pjsua_var.buddy),
498 PJ_ETOOMANY);
499
500 PJ_LOG(4,(THIS_FILE, "Adding buddy: %.*s",
501 (int)cfg->uri.slen, cfg->uri.ptr));
502 pj_log_push_indent();
503
504 PJSUA_LOCK();
505
506 /* Find empty slot */
507 for (index=0; index<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++index) {
508 if (pjsua_var.buddy[index].uri.slen == 0)
509 break;
510 }
511
512 /* Expect to find an empty slot */
513 if (index == PJ_ARRAY_SIZE(pjsua_var.buddy)) {
514 PJSUA_UNLOCK();
515 /* This shouldn't happen */
516 pj_assert(!"index < PJ_ARRAY_SIZE(pjsua_var.buddy)");
517 pj_log_pop_indent();
518 return PJ_ETOOMANY;
519 }
520
521 buddy = &pjsua_var.buddy[index];
522
523 /* Create pool for this buddy */
524 if (buddy->pool) {
525 pj_pool_reset(buddy->pool);
526 } else {
527 char name[PJ_MAX_OBJ_NAME];
528 pj_ansi_snprintf(name, sizeof(name), "buddy%03d", index);
529 buddy->pool = pjsua_pool_create(name, 512, 256);
530 }
531
532 /* Init buffers for presence subscription status */
533 buddy->term_reason.ptr = (char*)
534 pj_pool_alloc(buddy->pool,
535 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
536
537 /* Get name and display name for buddy */
538 pj_strdup_with_null(buddy->pool, &tmp, &cfg->uri);
539 url = (pjsip_name_addr*)pjsip_parse_uri(buddy->pool, tmp.ptr, tmp.slen,
540 PJSIP_PARSE_URI_AS_NAMEADDR);
541
542 if (url == NULL) {
543 pjsua_perror(THIS_FILE, "Unable to add buddy", PJSIP_EINVALIDURI);
544 pj_pool_release(buddy->pool);
545 buddy->pool = NULL;
546 PJSUA_UNLOCK();
547 pj_log_pop_indent();
548 return PJSIP_EINVALIDURI;
549 }
550
551 /* Only support SIP schemes */
552 if (!PJSIP_URI_SCHEME_IS_SIP(url) && !PJSIP_URI_SCHEME_IS_SIPS(url)) {
553 pj_pool_release(buddy->pool);
554 buddy->pool = NULL;
555 PJSUA_UNLOCK();
556 pj_log_pop_indent();
557 return PJSIP_EINVALIDSCHEME;
558 }
559
560 /* Reset buddy, to make sure everything is cleared with default
561 * values
562 */
563 reset_buddy(index);
564
565 /* Save URI */
566 pjsua_var.buddy[index].uri = tmp;
567
568 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(url->uri);
569 pjsua_var.buddy[index].name = sip_uri->user;
570 pjsua_var.buddy[index].display = url->display;
571 pjsua_var.buddy[index].host = sip_uri->host;
572 pjsua_var.buddy[index].port = sip_uri->port;
573 pjsua_var.buddy[index].monitor = cfg->subscribe;
574 pjsua_var.buddy[index].acc_id = cfg->acc_id;
575 if (pjsua_var.buddy[index].port == 0)
576 pjsua_var.buddy[index].port = 5060;
577
578 /* Save user data */
579 pjsua_var.buddy[index].user_data = (void*)cfg->user_data;
580
581 if (p_buddy_id)
582 *p_buddy_id = index;
583
584 pjsua_var.buddy_cnt++;
585
586 PJSUA_UNLOCK();
587
588 if (cfg->acc_id != PJSUA_INVALID_ID) {
589 PJ_LOG(4,(THIS_FILE, "Buddy %d added for account %d.", index,
590 cfg->acc_id));
591 } else {
592 PJ_LOG(4,(THIS_FILE, "Buddy %d added.", index));
593 }
594
595 if (cfg->subscribe) {
596 pjsua_buddy_subscribe_pres(index, cfg->subscribe);
597 } else if (cfg->subscribe_dlg_event) {
598 pjsua_buddy_subscribe_dlg_event(index, cfg->subscribe_dlg_event);
599 }
600
601 pj_log_pop_indent();
602 return PJ_SUCCESS;
603 }
604
605 /*
606 * Delete buddy.
607 */
608 PJ_DEF(pj_status_t) pjsua_buddy_del(pjsua_buddy_id buddy_id)
609 {
610 struct buddy_lock lck;
611 pj_status_t status;
612
613 PJ_ASSERT_RETURN(buddy_id>=0 &&
614 buddy_id<(int)PJ_ARRAY_SIZE(pjsua_var.buddy),
615 PJ_EINVAL);
616
617 if (pjsua_var.buddy[buddy_id].uri.slen == 0) {
618 return PJ_SUCCESS;
619 }
620
621 status = lock_buddy("pjsua_buddy_del()", buddy_id, &lck, 0);
622 if (status != PJ_SUCCESS)
623 return status;
624
625 PJ_LOG(4,(THIS_FILE, "Buddy %d: deleting..", buddy_id));
626 pj_log_push_indent();
627
628 /* Unsubscribe */
629 if (pjsua_var.buddy[buddy_id].presence) {
630 pjsua_buddy_subscribe_pres(buddy_id, PJ_FALSE);
631 } else {
632 pjsua_buddy_subscribe_dlg_event(buddy_id, PJ_FALSE);
633 }
634
635 /* Not interested with further events for this buddy */
636 if (pjsua_var.buddy[buddy_id].sub) {
637 pjsip_evsub_set_mod_data(pjsua_var.buddy[buddy_id].sub,
638 pjsua_var.mod.id, NULL);
639 }
640
641 /* Remove buddy */
642 pjsua_var.buddy[buddy_id].uri.slen = 0;
643 pjsua_var.buddy_cnt--;
644
645 /* Clear timer */
646 if (pjsua_var.buddy[buddy_id].timer.id) {
647 pjsua_cancel_timer(&pjsua_var.buddy[buddy_id].timer);
648 pjsua_var.buddy[buddy_id].timer.id = PJ_FALSE;
649 }
650
651 /* Reset buddy struct */
652 reset_buddy(buddy_id);
653
654 unlock_buddy(&lck);
655 pj_log_pop_indent();
656 return PJ_SUCCESS;
657 }
658
659 /*
660 * Enable/disable buddy's presence monitoring.
661 */
662 PJ_DEF(pj_status_t) pjsua_buddy_subscribe_pres( pjsua_buddy_id buddy_id,
663 pj_bool_t subscribe)
664 {
665 struct buddy_lock lck;
666 pj_status_t status;
667
668 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
669
670 status = lock_buddy("pjsua_buddy_subscribe_pres()", buddy_id, &lck, 0);
671 if (status != PJ_SUCCESS)
672 return status;
673
674 pj_log_push_indent();
675
676 lck.buddy->monitor = subscribe;
677
678 pjsua_buddy_update_pres(buddy_id);
679
680 unlock_buddy(&lck);
681 pj_log_pop_indent();
682 return PJ_SUCCESS;
683 }
684
685 /*
686 * Enable/disable buddy's dialog event monitoring.
687 */
688
689 PJ_DEF(pj_status_t) pjsua_buddy_subscribe_dlg_event(pjsua_buddy_id buddy_id,
690 pj_bool_t subscribe)
691 {
692 struct buddy_lock lck;
693 pj_status_t status;
694
695 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
696
697 status = lock_buddy("pjsua_buddy_subscribe_dlg_event()", buddy_id, &lck, 0);
698 if (status != PJ_SUCCESS)
699 return status;
700
701 pj_log_push_indent();
702
703 lck.buddy->monitor = subscribe;
704
705 pjsua_buddy_update_dlg_event(buddy_id);
706
707 unlock_buddy(&lck);
708 pj_log_pop_indent();
709 return PJ_SUCCESS;
710 }
711
712 /*
713 * Update buddy's presence.
714 */
715 PJ_DEF(pj_status_t) pjsua_buddy_update_pres(pjsua_buddy_id buddy_id)
716 {
717 struct buddy_lock lck;
718 pj_status_t status;
719
720 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
721
722 status = lock_buddy("pjsua_buddy_update_pres()", buddy_id, &lck, 0);
723 if (status != PJ_SUCCESS)
724 return status;
725
726 PJ_LOG(4,(THIS_FILE, "Buddy %d: updating presence..", buddy_id));
727 pj_log_push_indent();
728
729 /* Is this an unsubscribe request? */
730 if (!lck.buddy->monitor) {
731 unsubscribe_buddy(buddy_id, PJ_TRUE);
732 unlock_buddy(&lck);
733 pj_log_pop_indent();
734 return PJ_SUCCESS;
735 }
736
737 /* Ignore if presence is already active for the buddy */
738 if (lck.buddy->sub) {
739 unlock_buddy(&lck);
740 pj_log_pop_indent();
741 return PJ_SUCCESS;
742 }
743
744 /* Initiate presence subscription */
745 subscribe_buddy(buddy_id, PJ_TRUE);
746
747 unlock_buddy(&lck);
748 pj_log_pop_indent();
749 return PJ_SUCCESS;
750 }
751
752 /*
753 * Update buddy's dlg event.
754 */
755 PJ_DEF(pj_status_t) pjsua_buddy_update_dlg_event(pjsua_buddy_id buddy_id)
756 {
757 struct buddy_lock lck;
758 pj_status_t status;
759
760 PJ_ASSERT_RETURN(pjsua_buddy_is_valid(buddy_id), PJ_EINVAL);
761
762 status = lock_buddy("pjsua_buddy_update_dlg_event()", buddy_id, &lck, 0);
763 if (status != PJ_SUCCESS)
764 return status;
765
766 PJ_LOG(4,(THIS_FILE, "Buddy %d: updating dialog event..", buddy_id));
767 pj_log_push_indent();
768
769 /* Is this an unsubscribe request? */
770 if (!lck.buddy->monitor) {
771 unsubscribe_buddy(buddy_id, PJ_FALSE);
772 unlock_buddy(&lck);
773 pj_log_pop_indent();
774 return PJ_SUCCESS;
775 }
776
777 /* Ignore if dialog event is already active for the buddy */
778 if (lck.buddy->sub) {
779 unlock_buddy(&lck);
780 pj_log_pop_indent();
781 return PJ_SUCCESS;
782 }
783
784 /* Initiate dialog event subscription */
785 subscribe_buddy(buddy_id, PJ_FALSE);
786
787 unlock_buddy(&lck);
788 pj_log_pop_indent();
789 return PJ_SUCCESS;
790 }
791 /*
792 * Dump presence subscriptions to log file.
793 */
794 PJ_DEF(void) pjsua_pres_dump(pj_bool_t verbose)
795 {
796 unsigned acc_id;
797 unsigned i;
798
799
800 PJSUA_LOCK();
801
802 /*
803 * When no detail is required, just dump number of server and client
804 * subscriptions.
805 */
806 if (verbose == PJ_FALSE) {
807
808 int count = 0;
809
810 for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
811
812 if (!pjsua_var.acc[acc_id].valid)
813 continue;
814
815 if (!pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
816 struct pjsua_srv_pres *uapres;
817
818 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
819 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
820 ++count;
821 uapres = uapres->next;
822 }
823 }
824 }
825
826 PJ_LOG(3,(THIS_FILE, "Number of server/UAS subscriptions: %d",
827 count));
828
829 count = 0;
830
831 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
832 if (pjsua_var.buddy[i].uri.slen == 0)
833 continue;
834 if (pjsua_var.buddy[i].sub) {
835 ++count;
836 }
837 }
838
839 PJ_LOG(3,(THIS_FILE, "Number of client/UAC subscriptions: %d",
840 count));
841 PJSUA_UNLOCK();
842 return;
843 }
844
845
846 /*
847 * Dumping all server (UAS) subscriptions
848 */
849 PJ_LOG(3,(THIS_FILE, "Dumping pjsua server subscriptions:"));
850
851 for (acc_id=0; acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
852
853 if (!pjsua_var.acc[acc_id].valid)
854 continue;
855
856 PJ_LOG(3,(THIS_FILE, " %.*s",
857 (int)pjsua_var.acc[acc_id].cfg.id.slen,
858 pjsua_var.acc[acc_id].cfg.id.ptr));
859
860 if (pj_list_empty(&pjsua_var.acc[acc_id].pres_srv_list)) {
861
862 PJ_LOG(3,(THIS_FILE, " - none - "));
863
864 } else {
865 struct pjsua_srv_pres *uapres;
866
867 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
868 while (uapres != &pjsua_var.acc[acc_id].pres_srv_list) {
869
870 PJ_LOG(3,(THIS_FILE, " %10s %s",
871 pjsip_evsub_get_state_name(uapres->sub),
872 uapres->remote));
873
874 uapres = uapres->next;
875 }
876 }
877 }
878
879 /*
880 * Dumping all client (UAC) subscriptions
881 */
882 PJ_LOG(3,(THIS_FILE, "Dumping pjsua client subscriptions:"));
883
884 if (pjsua_var.buddy_cnt == 0) {
885
886 PJ_LOG(3,(THIS_FILE, " - no buddy list - "));
887
888 } else {
889 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
890
891 if (pjsua_var.buddy[i].uri.slen == 0)
892 continue;
893
894 if (pjsua_var.buddy[i].sub) {
895 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
896 pjsip_evsub_get_state_name(pjsua_var.buddy[i].sub),
897 (int)pjsua_var.buddy[i].uri.slen,
898 pjsua_var.buddy[i].uri.ptr));
899 } else {
900 PJ_LOG(3,(THIS_FILE, " %10s %.*s",
901 "(null)",
902 (int)pjsua_var.buddy[i].uri.slen,
903 pjsua_var.buddy[i].uri.ptr));
904 }
905 }
906 }
907
908 PJSUA_UNLOCK();
909 }
910
911
912 /***************************************************************************
913 * Server subscription.
914 */
915
916 /* Proto */
917 static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata);
918
919 /* The module instance. */
920 static pjsip_module mod_pjsua_pres =
921 {
922 NULL, NULL, /* prev, next. */
923 { "mod-pjsua-pres", 14 }, /* Name. */
924 -1, /* Id */
925 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
926 NULL, /* load() */
927 NULL, /* start() */
928 NULL, /* stop() */
929 NULL, /* unload() */
930 &pres_on_rx_request, /* on_rx_request() */
931 NULL, /* on_rx_response() */
932 NULL, /* on_tx_request. */
933 NULL, /* on_tx_response() */
934 NULL, /* on_tsx_state() */
935
936 };
937
938
939 /* Callback called when *server* subscription state has changed. */
940 static void pres_evsub_on_srv_state( pjsip_evsub *sub, pjsip_event *event)
941 {
942 pjsua_srv_pres *uapres;
943
944 PJ_UNUSED_ARG(event);
945
946 PJSUA_LOCK();
947
948 uapres = (pjsua_srv_pres*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
949 if (uapres) {
950 pjsip_evsub_state state;
951
952 PJ_LOG(4,(THIS_FILE, "Server subscription to %s is %s",
953 uapres->remote, pjsip_evsub_get_state_name(sub)));
954 pj_log_push_indent();
955
956 state = pjsip_evsub_get_state(sub);
957
958 if (pjsua_var.ua_cfg.cb.on_srv_subscribe_state) {
959 pj_str_t from;
960
961 from = uapres->dlg->remote.info_str;
962 (*pjsua_var.ua_cfg.cb.on_srv_subscribe_state)(uapres->acc_id,
963 uapres, &from,
964 state, event);
965 }
966
967 if (state == PJSIP_EVSUB_STATE_TERMINATED) {
968 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
969 pj_list_erase(uapres);
970 }
971 pj_log_pop_indent();
972 }
973
974 PJSUA_UNLOCK();
975 }
976
977 /* This is called when request is received.
978 * We need to check for incoming SUBSCRIBE request.
979 */
980 static pj_bool_t pres_on_rx_request(pjsip_rx_data *rdata)
981 {
982 int acc_id;
983 pjsua_acc *acc;
984 pj_str_t contact;
985 pjsip_method *req_method = &rdata->msg_info.msg->line.req.method;
986 pjsua_srv_pres *uapres;
987 pjsip_evsub *sub;
988 pjsip_evsub_user pres_cb;
989 pjsip_dialog *dlg;
990 pjsip_status_code st_code;
991 pj_str_t reason;
992 pjsip_expires_hdr *expires_hdr;
993 pjsua_msg_data msg_data;
994 pjsip_tpselector tp_sel;
995 pj_status_t status;
996
997 if (pjsip_method_cmp(req_method, pjsip_get_subscribe_method()) != 0)
998 return PJ_FALSE;
999
1000 /* Incoming SUBSCRIBE: */
1001
1002 /* Don't want to accept the request if shutdown is in progress */
1003 if (pjsua_var.thread_quit_flag) {
1004 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
1005 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
1006 NULL, NULL);
1007 return PJ_TRUE;
1008 }
1009
1010 PJSUA_LOCK();
1011
1012 /* Find which account for the incoming request. */
1013 acc_id = pjsua_acc_find_for_incoming(rdata);
1014 if (acc_id == PJSUA_INVALID_ID) {
1015 PJ_LOG(2, (THIS_FILE,
1016 "Unable to process incoming message %s "
1017 "due to no available account",
1018 pjsip_rx_data_get_info(rdata)));
1019
1020 PJSUA_UNLOCK();
1021 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
1022 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
1023 NULL, NULL);
1024 pj_log_pop_indent();
1025 return PJ_TRUE;
1026 }
1027 acc = &pjsua_var.acc[acc_id];
1028
1029 PJ_LOG(4,(THIS_FILE, "Creating server subscription, using account %d",
1030 acc_id));
1031 pj_log_push_indent();
1032
1033 /* Create suitable Contact header */
1034 if (acc->contact.slen) {
1035 contact = acc->contact;
1036 } else {
1037 status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact,
1038 acc_id, rdata);
1039 if (status != PJ_SUCCESS) {
1040 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
1041 status);
1042 PJSUA_UNLOCK();
1043 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
1044 NULL, NULL);
1045 pj_log_pop_indent();
1046 return PJ_TRUE;
1047 }
1048 }
1049
1050 /* Create UAS dialog: */
1051 status = pjsip_dlg_create_uas_and_inc_lock(pjsip_ua_instance(), rdata,
1052 &contact, &dlg);
1053 if (status != PJ_SUCCESS) {
1054 pjsua_perror(THIS_FILE,
1055 "Unable to create UAS dialog for subscription",
1056 status);
1057 PJSUA_UNLOCK();
1058 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
1059 NULL, NULL);
1060 pj_log_pop_indent();
1061 return PJ_TRUE;
1062 }
1063
1064 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
1065 pjsip_dlg_set_via_sent_by(dlg, &acc->via_addr, acc->via_tp);
1066 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
1067 !pjsua_sip_acc_is_using_upnp(acc_id))
1068 {
1069 /* Choose local interface to use in Via if acc is not using
1070 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
1071 */
1072 char target_buf[PJSIP_MAX_URL_SIZE];
1073 pj_str_t target;
1074 pjsip_host_port via_addr;
1075 const void *via_tp;
1076
1077 target.ptr = target_buf;
1078 target.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI,
1079 dlg->target,
1080 target_buf, sizeof(target_buf));
1081 if (target.slen < 0) target.slen = 0;
1082
1083 if (pjsua_acc_get_uac_addr(acc_id, dlg->pool, &target,
1084 &via_addr, NULL, NULL,
1085 &via_tp) == PJ_SUCCESS)
1086 {
1087 pjsip_dlg_set_via_sent_by(dlg, &via_addr,
1088 (pjsip_transport*)via_tp);
1089 }
1090 }
1091
1092 /* Set credentials and preference. */
1093 pjsip_auth_clt_set_credentials(&dlg->auth_sess, acc->cred_cnt, acc->cred);
1094 pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref);
1095
1096 /* Init callback: */
1097 pj_bzero(&pres_cb, sizeof(pres_cb));
1098 pres_cb.on_evsub_state = &pres_evsub_on_srv_state;
1099
1100 /* Create server presence subscription: */
1101 status = pjsip_pres_create_uas( dlg, &pres_cb, rdata, &sub);
1102 if (status != PJ_SUCCESS) {
1103 int code = PJSIP_ERRNO_TO_SIP_STATUS(status);
1104 pjsip_tx_data *tdata;
1105
1106 pjsua_perror(THIS_FILE, "Unable to create server subscription",
1107 status);
1108
1109 if (code==599 || code > 699 || code < 300) {
1110 code = 400;
1111 }
1112
1113 status = pjsip_dlg_create_response(dlg, rdata, code, NULL, &tdata);
1114 if (status == PJ_SUCCESS) {
1115 status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
1116 tdata);
1117 }
1118
1119 pjsip_dlg_dec_lock(dlg);
1120 PJSUA_UNLOCK();
1121 pj_log_pop_indent();
1122 return PJ_TRUE;
1123 }
1124
1125 /* Subscription has been created, decrement & release dlg lock */
1126 pjsip_dlg_dec_lock(dlg);
1127
1128 /* Set dialog's transport based on acc's config. */
1129 pjsua_init_tpselector(acc_id, &tp_sel);
1130 pjsip_dlg_set_transport(dlg, &tp_sel);
1131
1132 /* Attach our data to the subscription: */
1133 uapres = PJ_POOL_ALLOC_T(dlg->pool, pjsua_srv_pres);
1134 uapres->sub = sub;
1135 uapres->remote = (char*) pj_pool_alloc(dlg->pool, PJSIP_MAX_URL_SIZE);
1136 uapres->acc_id = acc_id;
1137 uapres->dlg = dlg;
1138 status = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, dlg->remote.info->uri,
1139 uapres->remote, PJSIP_MAX_URL_SIZE);
1140 if (status < 1)
1141 pj_ansi_strxcpy(uapres->remote, "<-- url is too long-->",
1142 PJSIP_MAX_URL_SIZE);
1143 else
1144 uapres->remote[status] = '\0';
1145
1146 pjsip_evsub_add_header(sub, &acc->cfg.sub_hdr_list);
1147 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, uapres);
1148
1149 /* Add server subscription to the list: */
1150 pj_list_push_back(&pjsua_var.acc[acc_id].pres_srv_list, uapres);
1151
1152
1153 /* Capture the value of Expires header. */
1154 expires_hdr = (pjsip_expires_hdr*)
1155 pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES,
1156 NULL);
1157 if (expires_hdr)
1158 uapres->expires = expires_hdr->ivalue;
1159 else
1160 uapres->expires = PJSIP_EXPIRES_NOT_SPECIFIED;
1161
1162 st_code = (pjsip_status_code)200;
1163 reason = pj_str("OK");
1164 pjsua_msg_data_init(&msg_data);
1165
1166 /* Notify application callback, if any */
1167 if (pjsua_var.ua_cfg.cb.on_incoming_subscribe) {
1168 pjsua_buddy_id buddy_id;
1169
1170 buddy_id = find_buddy(rdata->msg_info.from->uri);
1171
1172 (*pjsua_var.ua_cfg.cb.on_incoming_subscribe)(acc_id, uapres, buddy_id,
1173 &dlg->remote.info_str,
1174 rdata, &st_code, &reason,
1175 &msg_data);
1176 }
1177
1178 /* Handle rejection case */
1179 if (st_code >= 300) {
1180 pjsip_tx_data *tdata;
1181
1182 /* Create response */
1183 status = pjsip_dlg_create_response(dlg, rdata, st_code,
1184 &reason, &tdata);
1185 if (status != PJ_SUCCESS) {
1186 pjsua_perror(THIS_FILE, "Error creating response", status);
1187 pj_list_erase(uapres);
1188 pjsip_pres_terminate(sub, PJ_FALSE);
1189 PJSUA_UNLOCK();
1190 pj_log_pop_indent();
1191 return PJ_FALSE;
1192 }
1193
1194 /* Add header list, if any */
1195 pjsua_process_msg_data(tdata, &msg_data);
1196
1197 /* Send the response */
1198 status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata),
1199 tdata);
1200 if (status != PJ_SUCCESS) {
1201 pjsua_perror(THIS_FILE, "Error sending response", status);
1202 /* This is not fatal */
1203 }
1204
1205 /* Terminate presence subscription */
1206 pj_list_erase(uapres);
1207 pjsip_pres_terminate(sub, PJ_FALSE);
1208 PJSUA_UNLOCK();
1209 pj_log_pop_indent();
1210 return PJ_TRUE;
1211 }
1212
1213 /* Create and send 2xx response to the SUBSCRIBE request: */
1214 status = pjsip_pres_accept(sub, rdata, st_code, &msg_data.hdr_list);
1215 if (status != PJ_SUCCESS) {
1216 pjsua_perror(THIS_FILE, "Unable to accept presence subscription",
1217 status);
1218 pj_list_erase(uapres);
1219 pjsip_pres_terminate(sub, PJ_FALSE);
1220 PJSUA_UNLOCK();
1221 pj_log_pop_indent();
1222 return PJ_FALSE;
1223 }
1224
1225 /* If code is 200, send NOTIFY now */
1226 if (st_code == 200) {
1227 pjsua_pres_notify(acc_id, uapres, PJSIP_EVSUB_STATE_ACTIVE,
1228 NULL, NULL, PJ_TRUE, &msg_data);
1229 }
1230
1231 /* Done: */
1232 PJSUA_UNLOCK();
1233 pj_log_pop_indent();
1234 return PJ_TRUE;
1235 }
1236
1237
1238 /*
1239 * Send NOTIFY.
1240 */
1241 PJ_DEF(pj_status_t) pjsua_pres_notify( pjsua_acc_id acc_id,
1242 pjsua_srv_pres *srv_pres,
1243 pjsip_evsub_state ev_state,
1244 const pj_str_t *state_str,
1245 const pj_str_t *reason,
1246 pj_bool_t with_body,
1247 const pjsua_msg_data *msg_data)
1248 {
1249 pjsua_acc *acc;
1250 pjsip_pres_status pres_status;
1251 pjsua_buddy_id buddy_id;
1252 pjsip_tx_data *tdata;
1253 pj_status_t status;
1254
1255 /* Check parameters */
1256 PJ_ASSERT_RETURN(acc_id!=-1 && srv_pres, PJ_EINVAL);
1257
1258 /* Check that account ID is valid */
1259 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
1260 PJ_EINVAL);
1261 /* Check that account is valid */
1262 PJ_ASSERT_RETURN(pjsua_var.acc[acc_id].valid, PJ_EINVALIDOP);
1263
1264 PJ_LOG(4,(THIS_FILE, "Acc %d: sending NOTIFY for srv_pres=0x%p..",
1265 acc_id, srv_pres));
1266 pj_log_push_indent();
1267
1268 PJSUA_LOCK();
1269
1270 acc = &pjsua_var.acc[acc_id];
1271
1272 /* Check that the server presence subscription is still valid */
1273 if (pj_list_find_node(&acc->pres_srv_list, srv_pres) == NULL) {
1274 /* Subscription has been terminated */
1275 PJSUA_UNLOCK();
1276 pj_log_pop_indent();
1277 return PJ_EINVALIDOP;
1278 }
1279
1280 /* Set our online status: */
1281 pj_bzero(&pres_status, sizeof(pres_status));
1282 pres_status.info_cnt = 1;
1283 pres_status.info[0].basic_open = acc->online_status;
1284 pres_status.info[0].id = acc->cfg.pidf_tuple_id;
1285 //Both pjsua_var.local_uri and pjsua_var.contact_uri are enclosed in "<" and ">"
1286 //causing XML parsing to fail.
1287 //pres_status.info[0].contact = pjsua_var.local_uri;
1288 /* add RPID information */
1289 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1290 sizeof(pjrpid_element));
1291
1292 pjsip_pres_set_status(srv_pres->sub, &pres_status);
1293
1294 /* Check expires value. If it's zero, send our presense state but
1295 * set subscription state to TERMINATED.
1296 */
1297 if (srv_pres->expires == 0)
1298 ev_state = PJSIP_EVSUB_STATE_TERMINATED;
1299
1300 /* Create and send the NOTIFY to active subscription: */
1301 status = pjsip_pres_notify(srv_pres->sub, ev_state, state_str,
1302 reason, &tdata);
1303 if (status == PJ_SUCCESS) {
1304 /* Force removal of message body if msg_body==FALSE */
1305 if (!with_body) {
1306 tdata->msg->body = NULL;
1307 }
1308 pjsua_process_msg_data(tdata, msg_data);
1309 status = pjsip_pres_send_request( srv_pres->sub, tdata);
1310 }
1311
1312 if (status != PJ_SUCCESS) {
1313 pjsua_perror(THIS_FILE, "Unable to create/send NOTIFY",
1314 status);
1315 pj_list_erase(srv_pres);
1316 pjsip_pres_terminate(srv_pres->sub, PJ_FALSE);
1317 PJSUA_UNLOCK();
1318 pj_log_pop_indent();
1319 return status;
1320 }
1321
1322
1323 /* Subscribe to buddy's presence if we're not subscribed */
1324 buddy_id = find_buddy(srv_pres->dlg->remote.info->uri);
1325 if (buddy_id != PJSUA_INVALID_ID) {
1326 pjsua_buddy *b = &pjsua_var.buddy[buddy_id];
1327 if (b->monitor && b->sub == NULL) {
1328 PJ_LOG(4,(THIS_FILE, "Received SUBSCRIBE from buddy %d, "
1329 "activating outgoing subscription", buddy_id));
1330 subscribe_buddy(buddy_id, PJ_TRUE);
1331 }
1332 }
1333
1334 PJSUA_UNLOCK();
1335 pj_log_pop_indent();
1336 return PJ_SUCCESS;
1337 }
1338
1339
1340 /*
1341 * Client presence publication callback.
1342 */
1343 static void publish_cb(struct pjsip_publishc_cbparam *param)
1344 {
1345 pjsua_acc *acc = (pjsua_acc*) param->token;
1346
1347 if (param->code/100 != 2 || param->status != PJ_SUCCESS) {
1348
1349 pjsip_publishc_destroy(param->pubc);
1350 acc->publish_sess = NULL;
1351
1352 if (param->status != PJ_SUCCESS) {
1353 char errmsg[PJ_ERR_MSG_SIZE];
1354
1355 pj_strerror(param->status, errmsg, sizeof(errmsg));
1356 PJ_LOG(1,(THIS_FILE,
1357 "Client publication (PUBLISH) failed, status=%d, msg=%s",
1358 param->status, errmsg));
1359 } else if (param->code == 412) {
1360 /* 412 (Conditional Request Failed)
1361 * The PUBLISH refresh has failed, retry with new one.
1362 */
1363 pjsua_pres_init_publish_acc(acc->index);
1364
1365 } else {
1366 PJ_LOG(1,(THIS_FILE,
1367 "Client publication (PUBLISH) failed (%d/%.*s)",
1368 param->code, (int)param->reason.slen,
1369 param->reason.ptr));
1370 }
1371
1372 } else {
1373 if (param->expiration < 1) {
1374 /* Could happen if server "forgot" to include Expires header
1375 * in the response. We will not renew, so destroy the pubc.
1376 */
1377 pjsip_publishc_destroy(param->pubc);
1378 acc->publish_sess = NULL;
1379 }
1380 }
1381 }
1382
1383
1384 /*
1385 * Send PUBLISH request.
1386 */
1387 static pj_status_t send_publish(int acc_id, pj_bool_t active)
1388 {
1389 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1390 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1391 pjsip_pres_status pres_status;
1392 pjsip_tx_data *tdata;
1393 pj_status_t status;
1394
1395 PJ_LOG(5,(THIS_FILE, "Acc %d: sending %sPUBLISH..",
1396 acc_id, (active ? "" : "un-")));
1397 pj_log_push_indent();
1398
1399 /* Create PUBLISH request */
1400 if (active) {
1401 char *bpos;
1402 pj_str_t entity;
1403
1404 status = pjsip_publishc_publish(acc->publish_sess, PJ_TRUE, &tdata);
1405 if (status != PJ_SUCCESS) {
1406 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1407 goto on_error;
1408 }
1409
1410 /* Set our online status: */
1411 pj_bzero(&pres_status, sizeof(pres_status));
1412 pres_status.info_cnt = 1;
1413 pres_status.info[0].basic_open = acc->online_status;
1414 pres_status.info[0].id = acc->cfg.pidf_tuple_id;
1415 /* .. including RPID information */
1416 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1417 sizeof(pjrpid_element));
1418
1419 /* Be careful not to send PIDF with presence entity ID containing
1420 * "<" character.
1421 */
1422 if ((bpos=pj_strchr(&acc_cfg->id, '<')) != NULL) {
1423 char *epos = pj_strchr(&acc_cfg->id, '>');
1424 if (epos - bpos < 2) {
1425 pj_assert(!"Unexpected invalid URI");
1426 status = PJSIP_EINVALIDURI;
1427 goto on_error;
1428 }
1429 entity.ptr = bpos+1;
1430 entity.slen = epos - bpos - 1;
1431 } else {
1432 entity = acc_cfg->id;
1433 }
1434
1435 /* Create and add PIDF message body */
1436 status = pjsip_pres_create_pidf(tdata->pool, &pres_status,
1437 &entity, &tdata->msg->body);
1438 if (status != PJ_SUCCESS) {
1439 pjsua_perror(THIS_FILE, "Error creating PIDF for PUBLISH request",
1440 status);
1441 pjsip_tx_data_dec_ref(tdata);
1442 goto on_error;
1443 }
1444 } else {
1445 status = pjsip_publishc_unpublish(acc->publish_sess, &tdata);
1446 if (status != PJ_SUCCESS) {
1447 pjsua_perror(THIS_FILE, "Error creating PUBLISH request", status);
1448 goto on_error;
1449 }
1450 }
1451
1452 /* Add headers etc */
1453 pjsua_process_msg_data(tdata, NULL);
1454
1455 /* Set Via sent-by */
1456 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
1457 pjsip_publishc_set_via_sent_by(acc->publish_sess, &acc->via_addr,
1458 acc->via_tp);
1459 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
1460 !pjsua_sip_acc_is_using_upnp(acc_id))
1461 {
1462 /* Choose local interface to use in Via if acc is not using
1463 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
1464 */
1465 pjsip_host_port via_addr;
1466 const void *via_tp;
1467
1468 if (pjsua_acc_get_uac_addr(acc_id, acc->pool, &acc_cfg->id,
1469 &via_addr, NULL, NULL,
1470 &via_tp) == PJ_SUCCESS)
1471 {
1472 pjsip_publishc_set_via_sent_by(acc->publish_sess, &via_addr,
1473 (pjsip_transport*)via_tp);
1474 }
1475 }
1476
1477 /* Send the PUBLISH request */
1478 status = pjsip_publishc_send(acc->publish_sess, tdata);
1479 if (status == PJ_EPENDING) {
1480 PJ_LOG(3,(THIS_FILE, "Previous request is in progress, "
1481 "PUBLISH request is queued"));
1482 } else if (status != PJ_SUCCESS) {
1483 pjsua_perror(THIS_FILE, "Error sending PUBLISH request", status);
1484 goto on_error;
1485 }
1486
1487 acc->publish_state = acc->online_status;
1488 pj_log_pop_indent();
1489 return PJ_SUCCESS;
1490
1491 on_error:
1492 if (acc->publish_sess) {
1493 pjsip_publishc_destroy(acc->publish_sess);
1494 acc->publish_sess = NULL;
1495 }
1496 pj_log_pop_indent();
1497 return status;
1498 }
1499
1500
1501 /* Create client publish session */
1502 pj_status_t pjsua_pres_init_publish_acc(int acc_id)
1503 {
1504 const pj_str_t STR_PRESENCE = { "presence", 8 };
1505 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1506 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1507 pj_status_t status;
1508
1509 /* Create and init client publication session */
1510 if (acc_cfg->publish_enabled) {
1511
1512 /* Create client publication */
1513 status = pjsip_publishc_create(pjsua_var.endpt, &acc_cfg->publish_opt,
1514 acc, &publish_cb,
1515 &acc->publish_sess);
1516 if (status != PJ_SUCCESS) {
1517 acc->publish_sess = NULL;
1518 return status;
1519 }
1520
1521 /* Initialize client publication */
1522 status = pjsip_publishc_init(acc->publish_sess, &STR_PRESENCE,
1523 &acc_cfg->id, &acc_cfg->id,
1524 &acc_cfg->id,
1525 PJSUA_PUBLISH_EXPIRATION);
1526 if (status != PJ_SUCCESS) {
1527 acc->publish_sess = NULL;
1528 return status;
1529 }
1530
1531 /* Add credential for authentication */
1532 if (acc->cred_cnt) {
1533 pjsip_publishc_set_credentials(acc->publish_sess, acc->cred_cnt,
1534 acc->cred);
1535 }
1536
1537 /* Set shared auth session for PUBLISH */
1538 if (acc->cfg.use_shared_auth) {
1539 pjsip_publishc_set_auth_sess(acc->publish_sess,
1540 &acc->shared_auth_sess);
1541 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
1542 pjsip_auth_clt_async_setting async_opt;
1543 pj_bzero(&async_opt, sizeof(async_opt));
1544 async_opt.cb = &pjsua_auth_on_challenge;
1545 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
1546 pjsip_auth_clt_async_configure(
1547 pjsip_publishc_get_auth_sess(acc->publish_sess), &async_opt);
1548 }
1549
1550 /* Set route-set */
1551 pjsip_publishc_set_route_set(acc->publish_sess, &acc->route_set);
1552
1553 /* Send initial PUBLISH request */
1554 if (acc->online_status != 0) {
1555 status = send_publish(acc_id, PJ_TRUE);
1556 if (status != PJ_SUCCESS)
1557 return status;
1558 }
1559
1560 } else {
1561 acc->publish_sess = NULL;
1562 }
1563
1564 return PJ_SUCCESS;
1565 }
1566
1567
1568 /* Init presence for account */
1569 pj_status_t pjsua_pres_init_acc(int acc_id)
1570 {
1571 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1572
1573 /* Init presence subscription */
1574 pj_list_init(&acc->pres_srv_list);
1575
1576 return PJ_SUCCESS;
1577 }
1578
1579
1580 /* Unpublish presence publication */
1581 void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags)
1582 {
1583 if (acc->publish_sess) {
1584 pjsua_acc_config *acc_cfg = &acc->cfg;
1585
1586 acc->online_status = PJ_FALSE;
1587 acc_cfg->publish_enabled = PJ_FALSE;
1588
1589 if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
1590 send_publish(acc->index, PJ_FALSE);
1591 }
1592
1593 /* By ticket #364, don't destroy the session yet (let the callback
1594 destroy it)
1595 if (acc->publish_sess) {
1596 pjsip_publishc_destroy(acc->publish_sess);
1597 acc->publish_sess = NULL;
1598 }
1599 */
1600 }
1601 }
1602
1603 /* Terminate server subscription for the account */
1604 void pjsua_pres_delete_acc(int acc_id, unsigned flags)
1605 {
1606 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1607 pjsua_srv_pres *uapres;
1608
1609 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
1610
1611 /* Notify all subscribers that we're no longer available */
1612 while (uapres != &acc->pres_srv_list) {
1613
1614 pjsip_pres_status pres_status;
1615 pj_str_t reason = { "noresource", 10 };
1616 pjsua_srv_pres *next;
1617 pjsip_tx_data *tdata;
1618
1619 next = uapres->next;
1620
1621 pjsip_pres_get_status(uapres->sub, &pres_status);
1622
1623 pres_status.info[0].basic_open = pjsua_var.acc[acc_id].online_status;
1624 pjsip_pres_set_status(uapres->sub, &pres_status);
1625
1626 if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
1627 if (pjsip_pres_notify(uapres->sub,
1628 PJSIP_EVSUB_STATE_TERMINATED, NULL,
1629 &reason, &tdata)==PJ_SUCCESS)
1630 {
1631 pjsip_pres_send_request(uapres->sub, tdata);
1632 }
1633 } else {
1634 pjsip_pres_terminate(uapres->sub, PJ_FALSE);
1635 }
1636
1637 uapres = next;
1638 }
1639
1640 /* Clear server presence subscription list because account might be reused
1641 * later. */
1642 pj_list_init(&acc->pres_srv_list);
1643
1644 /* Terminate presence publication, if any */
1645 if (acc->cfg.publish_enabled)
1646 pjsua_pres_unpublish(acc, flags);
1647 }
1648
1649
1650 /* Update server subscription (e.g. when our online status has changed) */
1651 void pjsua_pres_update_acc(int acc_id, pj_bool_t force)
1652 {
1653 pjsua_acc *acc = &pjsua_var.acc[acc_id];
1654 pjsua_acc_config *acc_cfg = &pjsua_var.acc[acc_id].cfg;
1655 pjsua_srv_pres *uapres;
1656
1657 uapres = pjsua_var.acc[acc_id].pres_srv_list.next;
1658
1659 while (uapres != &acc->pres_srv_list) {
1660
1661 pjsip_pres_status pres_status;
1662 pjsip_tx_data *tdata;
1663
1664 pjsip_pres_get_status(uapres->sub, &pres_status);
1665
1666 /* Only send NOTIFY once subscription is active. Some subscriptions
1667 * may still be in NULL (when app is adding a new buddy while in the
1668 * on_incoming_subscribe() callback) or PENDING (when user approval is
1669 * being requested) state and we don't send NOTIFY to these subs until
1670 * the user accepted the request.
1671 */
1672 if (pjsip_evsub_get_state(uapres->sub)==PJSIP_EVSUB_STATE_ACTIVE &&
1673 (force || pres_status.info[0].basic_open != acc->online_status))
1674 {
1675
1676 pres_status.info[0].basic_open = acc->online_status;
1677 pj_memcpy(&pres_status.info[0].rpid, &acc->rpid,
1678 sizeof(pjrpid_element));
1679
1680 pjsip_pres_set_status(uapres->sub, &pres_status);
1681
1682 if (pjsip_pres_current_notify(uapres->sub, &tdata)==PJ_SUCCESS) {
1683 pjsua_process_msg_data(tdata, NULL);
1684 pjsip_pres_send_request(uapres->sub, tdata);
1685 }
1686 }
1687
1688 uapres = uapres->next;
1689 }
1690
1691 /* Send PUBLISH if required. We only do this when we have a PUBLISH
1692 * session. If we don't have a PUBLISH session, then it could be
1693 * that we're waiting until registration has completed before we
1694 * send the first PUBLISH.
1695 */
1696 if (acc_cfg->publish_enabled && acc->publish_sess) {
1697 if (force || acc->publish_state != acc->online_status) {
1698 send_publish(acc_id, PJ_TRUE);
1699 }
1700 }
1701 }
1702
1703
1704
1705 /***************************************************************************
1706 * Client subscription.
1707 */
1708
1709 static void buddy_timer_cb(pj_timer_heap_t *th, pj_timer_entry *entry)
1710 {
1711 pjsua_buddy *buddy = (pjsua_buddy*)entry->user_data;
1712
1713 PJ_UNUSED_ARG(th);
1714
1715 entry->id = PJ_FALSE;
1716 if (buddy->presence)
1717 pjsua_buddy_update_pres(buddy->index);
1718 else
1719 pjsua_buddy_update_dlg_event(buddy->index);
1720 }
1721
1722 /* Reschedule subscription refresh timer or terminate the subscription
1723 * refresh timer for the specified buddy.
1724 */
1725 static void buddy_resubscribe(pjsua_buddy *buddy, pj_bool_t resched,
1726 unsigned msec_interval)
1727 {
1728 if (buddy->timer.id) {
1729 pjsua_cancel_timer(&buddy->timer);
1730 buddy->timer.id = PJ_FALSE;
1731 }
1732
1733 if (resched) {
1734 pj_time_val delay;
1735
1736 PJ_LOG(4,(THIS_FILE,
1737 "Resubscribing buddy id %u in %u ms (reason: %.*s)",
1738 buddy->index, msec_interval,
1739 (int)buddy->term_reason.slen,
1740 buddy->term_reason.ptr));
1741
1742 pj_timer_entry_init(&buddy->timer, 0, buddy, &buddy_timer_cb);
1743 delay.sec = 0;
1744 delay.msec = msec_interval;
1745 pj_time_val_normalize(&delay);
1746
1747 if (pjsua_schedule_timer(&buddy->timer, &delay)==PJ_SUCCESS)
1748 buddy->timer.id = PJ_TRUE;
1749 }
1750 }
1751
1752 /* Callback called when *client* subscription state has changed. */
1753 static void pjsua_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
1754 {
1755 pjsua_buddy *buddy;
1756
1757 PJ_UNUSED_ARG(event);
1758
1759 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1760 * a dialog attached to it, lock_buddy() will use the dialog
1761 * lock, which we are currently holding!
1762 */
1763 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1764 if (buddy) {
1765 PJ_LOG(4,(THIS_FILE,
1766 "%s subscription to %.*s is %s",
1767 buddy->presence? "Presence": "Dialog event",
1768 (int)pjsua_var.buddy[buddy->index].uri.slen,
1769 pjsua_var.buddy[buddy->index].uri.ptr,
1770 pjsip_evsub_get_state_name(sub)));
1771 pj_log_push_indent();
1772
1773 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1774 int resub_delay = -1;
1775
1776 if (buddy->term_reason.ptr == NULL) {
1777 buddy->term_reason.ptr = (char*)
1778 pj_pool_alloc(buddy->pool,
1779 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
1780 }
1781 pj_strncpy(&buddy->term_reason,
1782 pjsip_evsub_get_termination_reason(sub),
1783 PJSUA_BUDDY_SUB_TERM_REASON_LEN);
1784
1785 buddy->term_code = 200;
1786
1787 /* Determine whether to resubscribe automatically */
1788 if (event && event->type==PJSIP_EVENT_TSX_STATE) {
1789 const pjsip_transaction *tsx = event->body.tsx_state.tsx;
1790 if (pjsip_method_cmp(&tsx->method,
1791 pjsip_get_subscribe_method())==0)
1792 {
1793 buddy->term_code = tsx->status_code;
1794 switch (tsx->status_code) {
1795 case PJSIP_SC_CALL_TSX_DOES_NOT_EXIST:
1796 /* 481: we refreshed too late? resubscribe
1797 * immediately.
1798 */
1799 /* But this must only happen when the 481 is received
1800 * on subscription refresh request. We MUST NOT try to
1801 * resubscribe automatically if the 481 is received
1802 * on the initial SUBSCRIBE (if server returns this
1803 * response for some reason).
1804 */
1805 if (buddy->dlg->remote.contact)
1806 resub_delay = 500;
1807 break;
1808 }
1809 } else if (pjsip_method_cmp(&tsx->method,
1810 pjsip_get_notify_method())==0)
1811 {
1812 if (pj_stricmp2(&buddy->term_reason, "deactivated")==0 ||
1813 pj_stricmp2(&buddy->term_reason, "timeout")==0) {
1814 /* deactivated: The subscription has been terminated,
1815 * but the subscriber SHOULD retry immediately with
1816 * a new subscription.
1817 */
1818 /* timeout: The subscription has been terminated
1819 * because it was not refreshed before it expired.
1820 * Clients MAY re-subscribe immediately. The
1821 * "retry-after" parameter has no semantics for
1822 * "timeout".
1823 */
1824 resub_delay = 500;
1825 }
1826 else if (pj_stricmp2(&buddy->term_reason, "probation")==0||
1827 pj_stricmp2(&buddy->term_reason, "giveup")==0) {
1828 /* probation: The subscription has been terminated,
1829 * but the client SHOULD retry at some later time.
1830 * If a "retry-after" parameter is also present, the
1831 * client SHOULD wait at least the number of seconds
1832 * specified by that parameter before attempting to re-
1833 * subscribe.
1834 */
1835 /* giveup: The subscription has been terminated because
1836 * the notifier could not obtain authorization in a
1837 * timely fashion. If a "retry-after" parameter is
1838 * also present, the client SHOULD wait at least the
1839 * number of seconds specified by that parameter before
1840 * attempting to re-subscribe; otherwise, the client
1841 * MAY retry immediately, but will likely get put back
1842 * into pending state.
1843 */
1844 const pjsip_sub_state_hdr *sub_hdr;
1845 pj_str_t sub_state = { "Subscription-State", 18 };
1846 const pjsip_msg *msg;
1847
1848 msg = event->body.tsx_state.src.rdata->msg_info.msg;
1849 sub_hdr = (const pjsip_sub_state_hdr*)
1850 pjsip_msg_find_hdr_by_name(msg, &sub_state,
1851 NULL);
1852 if (sub_hdr && sub_hdr->retry_after > 0)
1853 resub_delay = sub_hdr->retry_after * 1000;
1854 }
1855
1856 }
1857 }
1858
1859 /* For other cases of subscription termination, if resubscribe
1860 * timer is not set, schedule with default expiration (plus minus
1861 * some random value, to avoid sending SUBSCRIBEs all at once)
1862 */
1863 if (resub_delay == -1) {
1864 pj_assert(PJSUA_PRES_TIMER >= 3);
1865 resub_delay = PJSUA_PRES_TIMER*1000 - 2500 + (pj_rand()%5000);
1866 }
1867
1868 buddy_resubscribe(buddy, PJ_TRUE, resub_delay);
1869
1870 } else {
1871 /* This will clear the last termination code/reason */
1872 buddy->term_code = 0;
1873 buddy->term_reason.slen = 0;
1874 }
1875
1876 /* Call callbacks */
1877 if (buddy->presence) {
1878 if (pjsua_var.ua_cfg.cb.on_buddy_evsub_state) {
1879 (*pjsua_var.ua_cfg.cb.on_buddy_evsub_state)(buddy->index, sub,
1880 event);
1881 }
1882
1883 if (pjsua_var.ua_cfg.cb.on_buddy_state)
1884 (*pjsua_var.ua_cfg.cb.on_buddy_state)(buddy->index);
1885 } else {
1886 if (pjsua_var.ua_cfg.cb.on_buddy_evsub_dlg_event_state) {
1887 (*pjsua_var.ua_cfg.cb.on_buddy_evsub_dlg_event_state)(
1888 buddy->index, sub, event);
1889 }
1890
1891 if (pjsua_var.ua_cfg.cb.on_buddy_dlg_event_state)
1892 (*pjsua_var.ua_cfg.cb.on_buddy_dlg_event_state)(buddy->index);
1893 }
1894
1895 /* Clear subscription */
1896 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
1897 buddy->sub = NULL;
1898 buddy->status.info_cnt = 0;
1899 pj_bzero(&buddy->dlg_ev_status, sizeof(buddy->dlg_ev_status));
1900 buddy->dlg = NULL;
1901 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
1902 }
1903
1904 pj_log_pop_indent();
1905 }
1906 }
1907
1908 /* Callback when transaction state has changed. */
1909 static void pjsua_evsub_on_tsx_state(pjsip_evsub *sub,
1910 pjsip_transaction *tsx,
1911 pjsip_event *event)
1912 {
1913 pjsua_buddy *buddy;
1914 pjsip_contact_hdr *contact_hdr;
1915
1916 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1917 * a dialog attached to it, lock_buddy() will use the dialog
1918 * lock, which we are currently holding!
1919 */
1920 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1921 if (!buddy) {
1922 return;
1923 }
1924
1925 /* We only use this to update buddy's Contact, when it's not
1926 * set.
1927 */
1928 if (buddy->contact.slen != 0) {
1929 /* Contact already set */
1930 return;
1931 }
1932
1933 /* Only care about 2xx response to outgoing SUBSCRIBE */
1934 if (tsx->status_code/100 != 2 ||
1935 tsx->role != PJSIP_UAC_ROLE ||
1936 event->type != PJSIP_EVENT_RX_MSG ||
1937 pjsip_method_cmp(&tsx->method, pjsip_get_subscribe_method())!=0)
1938 {
1939 return;
1940 }
1941
1942 /* Find contact header. */
1943 contact_hdr = (pjsip_contact_hdr*)
1944 pjsip_msg_find_hdr(event->body.rx_msg.rdata->msg_info.msg,
1945 PJSIP_H_CONTACT, NULL);
1946 if (!contact_hdr || !contact_hdr->uri) {
1947 return;
1948 }
1949
1950 buddy->contact.ptr = (char*)
1951 pj_pool_alloc(buddy->pool, PJSIP_MAX_URL_SIZE);
1952 buddy->contact.slen = pjsip_uri_print( PJSIP_URI_IN_CONTACT_HDR,
1953 contact_hdr->uri,
1954 buddy->contact.ptr,
1955 PJSIP_MAX_URL_SIZE);
1956 if (buddy->contact.slen < 0)
1957 buddy->contact.slen = 0;
1958 }
1959
1960
1961 /* Callback called when we receive NOTIFY */
1962 static void pjsua_evsub_on_rx_notify(pjsip_evsub *sub,
1963 pjsip_rx_data *rdata,
1964 int *p_st_code,
1965 pj_str_t **p_st_text,
1966 pjsip_hdr *res_hdr,
1967 pjsip_msg_body **p_body)
1968 {
1969 pjsua_buddy *buddy;
1970
1971 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
1972 * a dialog attached to it, lock_buddy() will use the dialog
1973 * lock, which we are currently holding!
1974 */
1975 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
1976 if (buddy) {
1977 /* Update our info. */
1978 pjsip_pres_get_status(sub, &buddy->status);
1979 }
1980
1981 /* The default is to send 200 response to NOTIFY.
1982 * Just leave it there..
1983 */
1984 PJ_UNUSED_ARG(rdata);
1985 PJ_UNUSED_ARG(p_st_code);
1986 PJ_UNUSED_ARG(p_st_text);
1987 PJ_UNUSED_ARG(res_hdr);
1988 PJ_UNUSED_ARG(p_body);
1989 }
1990
1991 /* Callback called when we receive dialog monitoring NOTIFY */
1992 static void pjsua_evsub_on_rx_dlg_event_notify(pjsip_evsub *sub,
1993 pjsip_rx_data *rdata,
1994 int *p_st_code,
1995 pj_str_t **p_st_text,
1996 pjsip_hdr *res_hdr,
1997 pjsip_msg_body **p_body)
1998 {
1999 pjsua_buddy *buddy;
2000
2001 /* Note: #937: no need to acquire PJSUA_LOCK here. Since the buddy has
2002 * a dialog attached to it, lock_buddy() will use the dialog
2003 * lock, which we are currently holding!
2004 */
2005 buddy = (pjsua_buddy*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
2006 if (buddy) {
2007 /* Update our info. */
2008 pjsip_dlg_event_get_status(sub, &buddy->dlg_ev_status);
2009 }
2010
2011 /* The default is to send 200 response to NOTIFY.
2012 * Just leave it there..
2013 */
2014 PJ_UNUSED_ARG(rdata);
2015 PJ_UNUSED_ARG(p_st_code);
2016 PJ_UNUSED_ARG(p_st_text);
2017 PJ_UNUSED_ARG(res_hdr);
2018 PJ_UNUSED_ARG(p_body);
2019 }
2020
2021 static void subscribe_buddy(pjsua_buddy_id buddy_id,
2022 pj_bool_t presence)
2023 {
2024 pjsip_evsub_user pres_callback;
2025 pjsip_evsub_user dlg_event_callback;
2026 pj_pool_t *tmp_pool = NULL;
2027 pjsua_buddy *buddy;
2028 int acc_id;
2029 pjsua_acc *acc;
2030 pj_str_t contact;
2031 pjsip_tx_data *tdata;
2032 pjsip_tpselector tp_sel;
2033 pj_status_t status;
2034 const char *sub_str = presence? "presence": "dialog event";
2035 pjsip_dialog *dlg;
2036
2037 /* Event subscription callback. */
2038 pj_bzero(&pres_callback, sizeof(pres_callback));
2039 pres_callback.on_evsub_state = &pjsua_evsub_on_state;
2040 pres_callback.on_tsx_state = &pjsua_evsub_on_tsx_state;
2041 pres_callback.on_rx_notify = &pjsua_evsub_on_rx_notify;
2042
2043 /* Event subscription callback. */
2044 pj_bzero(&dlg_event_callback, sizeof(dlg_event_callback));
2045 dlg_event_callback.on_evsub_state = &pjsua_evsub_on_state;
2046 dlg_event_callback.on_tsx_state = &pjsua_evsub_on_tsx_state;
2047 dlg_event_callback.on_rx_notify = &pjsua_evsub_on_rx_dlg_event_notify;
2048
2049 buddy = &pjsua_var.buddy[buddy_id];
2050 acc_id = (buddy->acc_id != PJSUA_INVALID_ID)? buddy->acc_id:
2051 pjsua_acc_find_for_outgoing(&buddy->uri);
2052 if (!pjsua_acc_is_valid(acc_id)) {
2053 PJ_LOG(4,(THIS_FILE, "Buddy %d: subscription failed, account %d is "
2054 "invalid!", buddy_id, acc_id));
2055 return;
2056 }
2057
2058 acc = &pjsua_var.acc[acc_id];
2059
2060 PJ_LOG(4,(THIS_FILE, "Buddy %d: subscribing %s,using account %d..",
2061 buddy_id, sub_str, acc_id));
2062 pj_log_push_indent();
2063
2064 /* Generate suitable Contact header unless one is already set in
2065 * the account
2066 */
2067 if (acc->contact.slen) {
2068 contact = acc->contact;
2069 } else {
2070 tmp_pool = pjsua_pool_create("tmpbuddy", 512, 256);
2071
2072 status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
2073 acc_id, &buddy->uri);
2074 if (status != PJ_SUCCESS) {
2075 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
2076 status);
2077 pj_pool_release(tmp_pool);
2078 pj_log_pop_indent();
2079 return;
2080 }
2081 }
2082
2083 /* Create UAC dialog */
2084 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
2085 &acc->cfg.id,
2086 &contact,
2087 &buddy->uri,
2088 NULL, &buddy->dlg);
2089 if (status != PJ_SUCCESS) {
2090 pjsua_perror(THIS_FILE, "Unable to create dialog",
2091 status);
2092 if (tmp_pool) pj_pool_release(tmp_pool);
2093 pj_log_pop_indent();
2094 return;
2095 }
2096
2097 /* Increment the dialog's lock otherwise when presence session creation
2098 * fails the dialog will be destroyed prematurely.
2099 */
2100 pjsip_dlg_inc_lock(buddy->dlg);
2101
2102 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
2103 pjsip_dlg_set_via_sent_by(buddy->dlg, &acc->via_addr, acc->via_tp);
2104 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
2105 !pjsua_sip_acc_is_using_upnp(acc_id))
2106 {
2107 /* Choose local interface to use in Via if acc is not using
2108 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
2109 */
2110 pjsip_host_port via_addr;
2111 const void *via_tp;
2112
2113 if (pjsua_acc_get_uac_addr(acc_id, buddy->dlg->pool, &buddy->uri,
2114 &via_addr, NULL, NULL,
2115 &via_tp) == PJ_SUCCESS)
2116 {
2117 pjsip_dlg_set_via_sent_by(buddy->dlg, &via_addr,
2118 (pjsip_transport*)via_tp);
2119 }
2120 }
2121
2122
2123 if (presence) {
2124 status = pjsip_pres_create_uac(buddy->dlg, &pres_callback,
2125 PJSIP_EVSUB_NO_EVENT_ID, &buddy->sub);
2126 } else {
2127 status = pjsip_dlg_event_create_uac(buddy->dlg, &dlg_event_callback,
2128 PJSIP_EVSUB_NO_EVENT_ID,
2129 &buddy->sub);
2130 }
2131
2132 if (status != PJ_SUCCESS) {
2133 buddy->sub = NULL;
2134 pjsua_perror(THIS_FILE, "Unable to create subscription client",
2135 status);
2136 /* This should destroy the dialog since there's no session
2137 * referencing it
2138 */
2139 pjsip_dlg_dec_lock(buddy->dlg);
2140 if (tmp_pool) pj_pool_release(tmp_pool);
2141 pj_log_pop_indent();
2142 return;
2143 }
2144
2145 /* Set dialog's transport based on acc's config. */
2146 pjsua_init_tpselector(acc_id, &tp_sel);
2147 pjsip_dlg_set_transport(buddy->dlg, &tp_sel);
2148
2149 /* Set route-set */
2150 if (!pj_list_empty(&acc->route_set)) {
2151 pjsip_dlg_set_route_set(buddy->dlg, &acc->route_set);
2152 }
2153
2154 if (acc->cfg.use_shared_auth) {
2155 pjsip_dlg_set_auth_sess(buddy->dlg, &acc->shared_auth_sess);
2156 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
2157 pjsip_auth_clt_async_setting async_opt;
2158 pj_bzero(&async_opt, sizeof(async_opt));
2159 async_opt.cb = &pjsua_auth_on_challenge;
2160 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
2161 pjsip_auth_clt_async_configure(&buddy->dlg->auth_sess, &async_opt);
2162 }
2163
2164 /* Set credentials */
2165 if (acc->cred_cnt) {
2166 pjsip_auth_clt_set_credentials( &buddy->dlg->auth_sess,
2167 acc->cred_cnt, acc->cred);
2168 }
2169
2170 /* Set authentication preference */
2171 pjsip_auth_clt_set_prefs(&buddy->dlg->auth_sess, &acc->cfg.auth_pref);
2172
2173 pjsip_evsub_add_header(buddy->sub, &acc->cfg.sub_hdr_list);
2174 pjsip_evsub_set_mod_data(buddy->sub, pjsua_var.mod.id, buddy);
2175
2176 if (presence) {
2177 status = pjsip_pres_initiate(buddy->sub, PJSIP_EXPIRES_NOT_SPECIFIED,
2178 &tdata);
2179 } else {
2180 status = pjsip_dlg_event_initiate(buddy->sub,
2181 PJSIP_EXPIRES_NOT_SPECIFIED, &tdata);
2182 }
2183 if (status != PJ_SUCCESS) {
2184 pjsip_dlg_dec_lock(buddy->dlg);
2185 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
2186 buddy->sub = NULL;
2187 pjsua_perror(THIS_FILE, "Unable to create initial SUBSCRIBE",
2188 status);
2189 if (tmp_pool) pj_pool_release(tmp_pool);
2190 pj_log_pop_indent();
2191 return;
2192 }
2193 buddy->presence = presence;
2194 pjsua_process_msg_data(tdata, NULL);
2195
2196 /* Send request. Note that if the send operation fails sync-ly, e.g:
2197 * gethostbyname() error, tsx callback may have been invoked which may
2198 * get the subscription terminated and the buddy states reset, we need
2199 * to be prepared for such scenario here.
2200 */
2201 dlg = buddy->dlg;
2202
2203 if (presence) {
2204 status = pjsip_pres_send_request(buddy->sub, tdata);
2205 } else {
2206 status = pjsip_dlg_event_send_request(buddy->sub, tdata);
2207 }
2208 if (status != PJ_SUCCESS) {
2209 pjsip_dlg_dec_lock(dlg);
2210 if (buddy->sub)
2211 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
2212 buddy->sub = NULL;
2213 pjsua_perror(THIS_FILE, "Unable to send initial SUBSCRIBE",
2214 status);
2215 if (tmp_pool) pj_pool_release(tmp_pool);
2216 pj_log_pop_indent();
2217 return;
2218 }
2219
2220 pjsip_dlg_dec_lock(buddy->dlg);
2221 if (tmp_pool) pj_pool_release(tmp_pool);
2222 pj_log_pop_indent();
2223 }
2224
2225 static void unsubscribe_buddy(pjsua_buddy_id buddy_id,
2226 pj_bool_t presence)
2227 {
2228 pjsua_buddy *buddy;
2229 pjsip_tx_data *tdata;
2230 pj_status_t status;
2231 const char *sub_str = presence? "presence": "dialog event";
2232
2233 buddy = &pjsua_var.buddy[buddy_id];
2234
2235 if (buddy->sub == NULL)
2236 return;
2237
2238 if (pjsip_evsub_get_state(buddy->sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2239 buddy->sub = NULL;
2240 return;
2241 }
2242
2243 PJ_LOG(5,(THIS_FILE, "Buddy %d: unsubscribing %s..", buddy_id, sub_str));
2244 pj_log_push_indent();
2245
2246 if (presence) {
2247 status = pjsip_pres_initiate( buddy->sub, 0, &tdata);
2248 } else {
2249 status = pjsip_dlg_event_initiate( buddy->sub, 0, &tdata);
2250 }
2251 if (status == PJ_SUCCESS) {
2252 pjsua_process_msg_data(tdata, NULL);
2253 if (presence)
2254 status = pjsip_pres_send_request( buddy->sub, tdata );
2255 else
2256 status = pjsip_dlg_event_send_request(buddy->sub, tdata);
2257 }
2258
2259 if (status != PJ_SUCCESS && buddy->sub) {
2260 if (presence) {
2261 pjsip_pres_terminate(buddy->sub, PJ_FALSE);
2262 } else {
2263 pjsip_dlg_event_terminate(buddy->sub, PJ_FALSE);
2264 }
2265 buddy->sub = NULL;
2266 pjsua_perror(THIS_FILE, "Unable to unsubscribe", status);
2267 }
2268
2269 pj_log_pop_indent();
2270 }
2271
2272 /* It does what it says.. */
2273 static pj_status_t refresh_client_subscriptions(void)
2274 {
2275 unsigned i;
2276 pj_status_t status;
2277
2278 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2279 struct buddy_lock lck;
2280
2281 if (!pjsua_buddy_is_valid(i))
2282 continue;
2283
2284 status = lock_buddy("refresh_client_subscriptions()", i, &lck, 0);
2285 if (status != PJ_SUCCESS)
2286 return status;
2287
2288 if (pjsua_var.buddy[i].monitor && !pjsua_var.buddy[i].sub) {
2289 subscribe_buddy(i, PJ_TRUE);
2290
2291 } else if (!pjsua_var.buddy[i].monitor && pjsua_var.buddy[i].sub) {
2292 unsubscribe_buddy(i, PJ_TRUE);
2293
2294 }
2295
2296 unlock_buddy(&lck);
2297 }
2298
2299 return PJ_SUCCESS;
2300 }
2301
2302 /***************************************************************************
2303 * MWI
2304 */
2305 /* Callback called when *client* subscription state has changed. */
2306 static void mwi_evsub_on_state( pjsip_evsub *sub, pjsip_event *event)
2307 {
2308 pjsua_acc *acc;
2309
2310 PJ_UNUSED_ARG(event);
2311
2312 /* Note: #937: no need to acuire PJSUA_LOCK here. Since the buddy has
2313 * a dialog attached to it, lock_buddy() will use the dialog
2314 * lock, which we are currently holding!
2315 */
2316 acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
2317 if (!acc)
2318 return;
2319
2320 PJ_LOG(4,(THIS_FILE,
2321 "MWI subscription for %.*s is %s",
2322 (int)acc->cfg.id.slen, acc->cfg.id.ptr,
2323 pjsip_evsub_get_state_name(sub)));
2324
2325 /* Call callback */
2326 if (pjsua_var.ua_cfg.cb.on_mwi_state) {
2327 (*pjsua_var.ua_cfg.cb.on_mwi_state)(acc->index, sub);
2328 }
2329
2330 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2331 /* Clear subscription */
2332 acc->mwi_dlg = NULL;
2333 acc->mwi_sub = NULL;
2334 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
2335
2336 }
2337 }
2338
2339 /* Callback called when we receive NOTIFY */
2340 static void mwi_evsub_on_rx_notify(pjsip_evsub *sub,
2341 pjsip_rx_data *rdata,
2342 int *p_st_code,
2343 pj_str_t **p_st_text,
2344 pjsip_hdr *res_hdr,
2345 pjsip_msg_body **p_body)
2346 {
2347 pjsua_mwi_info mwi_info;
2348 pjsua_acc *acc;
2349
2350 PJ_UNUSED_ARG(p_st_code);
2351 PJ_UNUSED_ARG(p_st_text);
2352 PJ_UNUSED_ARG(res_hdr);
2353 PJ_UNUSED_ARG(p_body);
2354
2355 acc = (pjsua_acc*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id);
2356 if (!acc)
2357 return;
2358
2359 /* Construct mwi_info */
2360 pj_bzero(&mwi_info, sizeof(mwi_info));
2361 mwi_info.evsub = sub;
2362 mwi_info.rdata = rdata;
2363
2364 PJ_LOG(4,(THIS_FILE, "MWI got NOTIFY.."));
2365 pj_log_push_indent();
2366
2367 /* Call callback */
2368 if (pjsua_var.ua_cfg.cb.on_mwi_info) {
2369 (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc->index, &mwi_info);
2370 }
2371
2372 pj_log_pop_indent();
2373 }
2374
2375
2376 /* Event subscription callback. */
2377 static pjsip_evsub_user mwi_cb =
2378 {
2379 &mwi_evsub_on_state,
2380 NULL, /* on_tsx_state: not interested */
2381 NULL, /* on_rx_refresh: don't care about SUBSCRIBE refresh, unless
2382 * we want to authenticate
2383 */
2384
2385 &mwi_evsub_on_rx_notify,
2386
2387 NULL, /* on_client_refresh: Use default behaviour, which is to
2388 * refresh client subscription. */
2389
2390 NULL, /* on_server_timeout: Use default behaviour, which is to send
2391 * NOTIFY to terminate.
2392 */
2393 };
2394
2395 pj_status_t pjsua_start_mwi(pjsua_acc_id acc_id, pj_bool_t force_renew)
2396 {
2397 pjsua_acc *acc;
2398 pj_pool_t *tmp_pool = NULL;
2399 pj_str_t contact;
2400 pjsip_tx_data *tdata;
2401 pjsip_tpselector tp_sel;
2402 pj_status_t status = PJ_SUCCESS;
2403
2404 PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc)
2405 && pjsua_var.acc[acc_id].valid, PJ_EINVAL);
2406
2407 acc = &pjsua_var.acc[acc_id];
2408
2409 if (!acc->cfg.mwi_enabled || !acc->regc) {
2410 if (acc->mwi_sub) {
2411 /* Terminate MWI subscription */
2412 pjsip_evsub *sub = acc->mwi_sub;
2413
2414 /* Detach sub from this account */
2415 acc->mwi_sub = NULL;
2416 acc->mwi_dlg = NULL;
2417 pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL);
2418
2419 /* Unsubscribe */
2420 status = pjsip_mwi_initiate(sub, 0, &tdata);
2421 if (status == PJ_SUCCESS) {
2422 status = pjsip_mwi_send_request(sub, tdata);
2423 }
2424 }
2425 return status;
2426 }
2427
2428 /* Subscription is already active */
2429 if (acc->mwi_sub) {
2430 if (!force_renew)
2431 return PJ_SUCCESS;
2432
2433 /* Update MWI subscription */
2434 pj_assert(acc->mwi_dlg);
2435 pjsip_dlg_inc_lock(acc->mwi_dlg);
2436
2437 status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
2438 if (status == PJ_SUCCESS) {
2439 pjsua_process_msg_data(tdata, NULL);
2440 status = pjsip_pres_send_request(acc->mwi_sub, tdata);
2441 }
2442
2443 pjsip_dlg_dec_lock(acc->mwi_dlg);
2444 return status;
2445 }
2446
2447 PJ_LOG(4,(THIS_FILE, "Starting MWI subscription.."));
2448 pj_log_push_indent();
2449
2450 /* Generate suitable Contact header unless one is already set in
2451 * the account
2452 */
2453 if (acc->contact.slen) {
2454 contact = acc->contact;
2455 } else {
2456 tmp_pool = pjsua_pool_create("tmpmwi", 512, 256);
2457 status = pjsua_acc_create_uac_contact(tmp_pool, &contact,
2458 acc->index, &acc->cfg.id);
2459 if (status != PJ_SUCCESS) {
2460 pjsua_perror(THIS_FILE, "Unable to generate Contact header",
2461 status);
2462 goto on_return;
2463 }
2464 }
2465
2466 /* Create UAC dialog */
2467 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
2468 &acc->cfg.id,
2469 &contact,
2470 &acc->cfg.id,
2471 NULL, &acc->mwi_dlg);
2472 if (status != PJ_SUCCESS) {
2473 pjsua_perror(THIS_FILE, "Unable to create dialog", status);
2474 goto on_return;
2475 }
2476
2477 /* Increment the dialog's lock otherwise when presence session creation
2478 * fails the dialog will be destroyed prematurely.
2479 */
2480 pjsip_dlg_inc_lock(acc->mwi_dlg);
2481
2482 if (acc->cfg.allow_via_rewrite && acc->via_addr.host.slen > 0) {
2483 pjsip_dlg_set_via_sent_by(acc->mwi_dlg, &acc->via_addr, acc->via_tp);
2484 } else if (!pjsua_sip_acc_is_using_stun(acc_id) &&
2485 !pjsua_sip_acc_is_using_upnp(acc_id))
2486 {
2487 /* Choose local interface to use in Via if acc is not using
2488 * STUN nor UPnP. See https://github.com/pjsip/pjproject/issues/1412
2489 */
2490 pjsip_host_port via_addr;
2491 const void *via_tp;
2492
2493 if (pjsua_acc_get_uac_addr(acc_id, acc->mwi_dlg->pool, &acc->cfg.id,
2494 &via_addr, NULL, NULL,
2495 &via_tp) == PJ_SUCCESS)
2496 {
2497 pjsip_dlg_set_via_sent_by(acc->mwi_dlg, &via_addr,
2498 (pjsip_transport*)via_tp);
2499 }
2500 }
2501
2502 /* Create UAC subscription */
2503 status = pjsip_mwi_create_uac(acc->mwi_dlg, &mwi_cb,
2504 PJSIP_EVSUB_NO_EVENT_ID, &acc->mwi_sub);
2505 if (status != PJ_SUCCESS) {
2506 pjsua_perror(THIS_FILE, "Error creating MWI subscription", status);
2507 pjsip_dlg_dec_lock(acc->mwi_dlg);
2508 goto on_return;
2509 }
2510
2511 /* Set dialog's transport based on acc's config. */
2512 pjsua_init_tpselector(acc_id, &tp_sel);
2513 pjsip_dlg_set_transport(acc->mwi_dlg, &tp_sel);
2514
2515 /* Set route-set */
2516 if (!pj_list_empty(&acc->route_set)) {
2517 pjsip_dlg_set_route_set(acc->mwi_dlg, &acc->route_set);
2518 }
2519
2520 if (acc->cfg.use_shared_auth) {
2521 pjsip_dlg_set_auth_sess(acc->mwi_dlg, &acc->shared_auth_sess);
2522 } else if (pjsua_var.ua_cfg.cb.on_auth_challenge) {
2523 pjsip_auth_clt_async_setting async_opt;
2524 pj_bzero(&async_opt, sizeof(async_opt));
2525 async_opt.cb = &pjsua_auth_on_challenge;
2526 async_opt.user_data = (void*)(pj_ssize_t)acc->index;
2527 pjsip_auth_clt_async_configure(&acc->mwi_dlg->auth_sess, &async_opt);
2528 }
2529
2530 /* Set credentials */
2531 if (acc->cred_cnt) {
2532 pjsip_auth_clt_set_credentials( &acc->mwi_dlg->auth_sess,
2533 acc->cred_cnt, acc->cred);
2534 }
2535
2536 /* Set authentication preference */
2537 pjsip_auth_clt_set_prefs(&acc->mwi_dlg->auth_sess, &acc->cfg.auth_pref);
2538
2539 pjsip_evsub_set_mod_data(acc->mwi_sub, pjsua_var.mod.id, acc);
2540
2541 status = pjsip_mwi_initiate(acc->mwi_sub, acc->cfg.mwi_expires, &tdata);
2542 if (status != PJ_SUCCESS) {
2543 pjsip_dlg_dec_lock(acc->mwi_dlg);
2544 pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2545 acc->mwi_sub = NULL;
2546 acc->mwi_dlg = NULL;
2547 pjsua_perror(THIS_FILE, "Unable to create initial MWI SUBSCRIBE",
2548 status);
2549 goto on_return;
2550 }
2551
2552 pjsua_process_msg_data(tdata, NULL);
2553
2554 status = pjsip_pres_send_request(acc->mwi_sub, tdata);
2555 if (status != PJ_SUCCESS) {
2556 pjsip_dlg_dec_lock(acc->mwi_dlg);
2557 pjsip_pres_terminate(acc->mwi_sub, PJ_FALSE);
2558 acc->mwi_sub = NULL;
2559 acc->mwi_dlg = NULL;
2560 pjsua_perror(THIS_FILE, "Unable to send initial MWI SUBSCRIBE",
2561 status);
2562 goto on_return;
2563 }
2564
2565 pjsip_dlg_dec_lock(acc->mwi_dlg);
2566
2567 on_return:
2568 if (tmp_pool) pj_pool_release(tmp_pool);
2569
2570 pj_log_pop_indent();
2571 return status;
2572 }
2573
2574
2575 /***************************************************************************
2576 * Unsolicited MWI
2577 */
2578 static pj_bool_t unsolicited_mwi_on_rx_request(pjsip_rx_data *rdata)
2579 {
2580 pjsip_msg *msg = rdata->msg_info.msg;
2581 pj_str_t EVENT_HDR = { "Event", 5 };
2582 pj_str_t MWI = { "message-summary", 15 };
2583 pjsip_event_hdr *eh;
2584 pjsua_acc_id acc_id;
2585
2586 if (pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())!=0)
2587 {
2588 /* Only interested with NOTIFY request */
2589 return PJ_FALSE;
2590 }
2591
2592 eh = (pjsip_event_hdr*) pjsip_msg_find_hdr_by_name(msg, &EVENT_HDR, NULL);
2593 if (!eh) {
2594 /* Something wrong with the request, it has no Event hdr */
2595 return PJ_FALSE;
2596 }
2597
2598 if (pj_stricmp(&eh->event_type, &MWI) != 0) {
2599 /* Not MWI event */
2600 return PJ_FALSE;
2601 }
2602
2603 /* Find which account for the incoming request. */
2604 acc_id = pjsua_acc_find_for_incoming(rdata);
2605 if (acc_id == PJSUA_INVALID_ID) {
2606 const pj_str_t reason = pj_str("No account to handle");
2607
2608 PJ_LOG(2, (THIS_FILE,
2609 "Unable to process incoming message %s "
2610 "due to no available account",
2611 pjsip_rx_data_get_info(rdata)));
2612
2613 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
2614 PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, &reason,
2615 NULL, NULL);
2616
2617 return PJ_TRUE;
2618 }
2619
2620 PJ_LOG(4,(THIS_FILE, "Got unsolicited NOTIFY from %s:%d..",
2621 rdata->pkt_info.src_name, rdata->pkt_info.src_port));
2622 pj_log_push_indent();
2623
2624 /* Got unsolicited MWI request, respond with 200/OK first */
2625 pjsip_endpt_respond(pjsua_get_pjsip_endpt(), NULL, rdata, 200, NULL,
2626 NULL, NULL, NULL);
2627
2628
2629 /* Call callback */
2630 if (pjsua_var.ua_cfg.cb.on_mwi_info) {
2631 pjsua_mwi_info mwi_info;
2632
2633 pj_bzero(&mwi_info, sizeof(mwi_info));
2634 mwi_info.rdata = rdata;
2635
2636 (*pjsua_var.ua_cfg.cb.on_mwi_info)(acc_id, &mwi_info);
2637 }
2638
2639 pj_log_pop_indent();
2640 return PJ_TRUE;
2641 }
2642
2643 /* The module instance. */
2644 static pjsip_module pjsua_unsolicited_mwi_mod =
2645 {
2646 NULL, NULL, /* prev, next. */
2647 { "mod-unsolicited-mwi", 19 }, /* Name. */
2648 -1, /* Id */
2649 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
2650 NULL, /* load() */
2651 NULL, /* start() */
2652 NULL, /* stop() */
2653 NULL, /* unload() */
2654 &unsolicited_mwi_on_rx_request, /* on_rx_request() */
2655 NULL, /* on_rx_response() */
2656 NULL, /* on_tx_request. */
2657 NULL, /* on_tx_response() */
2658 NULL, /* on_tsx_state() */
2659 };
2660
2661 static pj_status_t enable_unsolicited_mwi(void)
2662 {
2663 pj_status_t status;
2664
2665 status = pjsip_endpt_register_module(pjsua_get_pjsip_endpt(),
2666 &pjsua_unsolicited_mwi_mod);
2667 if (status != PJ_SUCCESS)
2668 pjsua_perror(THIS_FILE, "Error registering unsolicited MWI module",
2669 status);
2670
2671 return status;
2672 }
2673
2674
2675
2676 /***************************************************************************/
2677
2678 /* Timer callback to re-create client subscription */
2679 static void pres_timer_cb(pj_timer_heap_t *th,
2680 pj_timer_entry *entry)
2681 {
2682 unsigned i;
2683 pj_time_val delay = { PJSUA_PRES_TIMER, 0 };
2684
2685 entry->id = PJ_FALSE;
2686
2687 /* Retry failed PUBLISH and MWI SUBSCRIBE requests */
2688 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2689 pjsua_acc *acc = &pjsua_var.acc[i];
2690
2691 /* Acc may not be ready yet, otherwise assertion will happen */
2692 if (!pjsua_acc_is_valid(i))
2693 continue;
2694
2695 /* Retry PUBLISH */
2696 if (acc->cfg.publish_enabled && acc->publish_sess==NULL)
2697 pjsua_pres_init_publish_acc(acc->index);
2698
2699 /* Re-subscribe MWI subscription if it's terminated prematurely */
2700 if (acc->cfg.mwi_enabled && !acc->mwi_sub)
2701 pjsua_start_mwi(acc->index, PJ_FALSE);
2702 }
2703
2704 /* #937: No need to do bulk client refresh, as buddies have their
2705 * own individual timer now.
2706 */
2707 //refresh_client_subscriptions();
2708
2709 pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, &delay);
2710 entry->id = PJ_TRUE;
2711
2712 PJ_UNUSED_ARG(th);
2713 }
2714
2715
2716 /*
2717 * Init presence
2718 */
2719 pj_status_t pjsua_pres_init()
2720 {
2721 unsigned i;
2722 pj_status_t status;
2723
2724 status = pjsip_endpt_register_module( pjsua_var.endpt, &mod_pjsua_pres);
2725 if (status != PJ_SUCCESS) {
2726 pjsua_perror(THIS_FILE, "Unable to register pjsua presence module",
2727 status);
2728 }
2729
2730 if (pjsua_var.ua_cfg.enable_unsolicited_mwi) {
2731 status = enable_unsolicited_mwi();
2732 if (status != PJ_SUCCESS)
2733 return status;
2734 }
2735
2736 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2737 reset_buddy(i);
2738 }
2739
2740 return status;
2741 }
2742
2743
2744 /*
2745 * Start presence subsystem.
2746 */
2747 pj_status_t pjsua_pres_start(void)
2748 {
2749 /* Start presence timer to re-subscribe to buddy's presence when
2750 * subscription has failed.
2751 */
2752 if (pjsua_var.pres_timer.id == PJ_FALSE) {
2753 pj_time_val pres_interval = {PJSUA_PRES_TIMER, 0};
2754
2755 pjsua_var.pres_timer.cb = &pres_timer_cb;
2756 pjsip_endpt_schedule_timer(pjsua_var.endpt, &pjsua_var.pres_timer,
2757 &pres_interval);
2758 pjsua_var.pres_timer.id = PJ_TRUE;
2759 }
2760
2761 return PJ_SUCCESS;
2762 }
2763
2764
2765 /*
2766 * Shutdown presence.
2767 */
2768 void pjsua_pres_shutdown(unsigned flags)
2769 {
2770 unsigned i;
2771
2772 PJ_LOG(4,(THIS_FILE, "Shutting down presence.."));
2773 pj_log_push_indent();
2774
2775 if (pjsua_var.pres_timer.id != 0) {
2776 pjsip_endpt_cancel_timer(pjsua_var.endpt, &pjsua_var.pres_timer);
2777 pjsua_var.pres_timer.id = PJ_FALSE;
2778 }
2779
2780 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2781 if (!pjsua_var.acc[i].valid)
2782 continue;
2783 pjsua_pres_delete_acc(i, flags);
2784 }
2785
2786 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
2787 pjsua_var.buddy[i].monitor = 0;
2788 }
2789
2790 if ((flags & PJSUA_DESTROY_NO_TX_MSG) == 0) {
2791 refresh_client_subscriptions();
2792
2793 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
2794 if (pjsua_var.acc[i].valid)
2795 pjsua_pres_update_acc(i, PJ_FALSE);
2796 }
2797 }
2798
2799 pj_log_pop_indent();
2800 }