Debian Codegraph @

Xpjproject/pjsip/src/pjsip-ua/sip_reg.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 <pjsip-ua/sip_regc.h>
20 #include <pjsip/sip_endpoint.h>
21 #include <pjsip/sip_parser.h>
22 #include <pjsip/sip_module.h>
23 #include <pjsip/sip_transaction.h>
24 #include <pjsip/sip_event.h>
25 #include <pjsip/sip_util.h>
26 #include <pjsip/sip_auth_msg.h>
27 #include <pjsip/sip_errno.h>
28 #include <pj/assert.h>
29 #include <pj/guid.h>
30 #include <pj/lock.h>
31 #include <pj/os.h>
32 #include <pj/pool.h>
33 #include <pj/log.h>
34 #include <pj/rand.h>
35 #include <pj/string.h>
36
37
38 #define REFRESH_TIMER 1
39 #define DELAY_BEFORE_REFRESH PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH
40
41 /* This defines the minimum value for the refresh timer in milliseconds.
42 * If the calculated expiry for the refresh timer falls below this; it will be clamped to this value.
43 */
44 #define MIN_REFRESH_MSEC 500
45 #define THIS_FILE "sip_reg.c"
46
47 /* Outgoing transaction timeout when server sends 100 but never replies
48 * with final response. Value is in MILLISECONDS!
49 */
50 #define REGC_TSX_TIMEOUT 33000
51
52 #define NOEXP PJSIP_REGC_EXPIRATION_NOT_SPECIFIED
53
54 static const pj_str_t XUID_PARAM_NAME = { "x-uid", 5 };
55
56
57 /* Current/pending operation */
58 enum regc_op
59 {
60 REGC_IDLE,
61 REGC_REGISTERING,
62 REGC_UNREGISTERING
63 };
64
65 /**
66 * SIP client registration structure.
67 */
68 struct pjsip_regc
69 {
70 pj_pool_t *pool;
71 pjsip_endpoint *endpt;
72 pj_lock_t *lock;
73 pj_bool_t _delete_flag;
74 pj_bool_t has_tsx;
75 pj_atomic_t *busy_ctr;
76 enum regc_op current_op;
77
78 pj_bool_t add_xuid_param;
79
80 void *token;
81 pjsip_regc_cb *cb;
82 pjsip_regc_tsx_cb *tsx_cb;
83
84 pj_str_t str_srv_url;
85 pjsip_uri *srv_url;
86 pjsip_cid_hdr *cid_hdr;
87 pjsip_cseq_hdr *cseq_hdr;
88 pj_str_t from_uri;
89 pjsip_from_hdr *from_hdr;
90 pjsip_to_hdr *to_hdr;
91 pjsip_contact_hdr contact_hdr_list;
92 pjsip_contact_hdr removed_contact_hdr_list;
93 pjsip_expires_hdr *expires_hdr;
94 pj_uint32_t expires;
95 pj_uint32_t expires_requested;
96 pj_uint32_t delay_before_refresh;
97 pjsip_route_hdr route_set;
98 pjsip_hdr hdr_list;
99 pjsip_host_port via_addr;
100 const void *via_tp;
101
102 /* Authorization sessions. */
103 pjsip_auth_clt_sess auth_sess;
104
105 /* Auto refresh registration. */
106 pj_bool_t auto_reg;
107 pj_time_val last_reg;
108 pj_time_val next_reg;
109 pj_timer_entry timer;
110
111 /* Transport selector */
112 pjsip_tpselector tp_sel;
113
114 /* Last transport used. We acquire the transport to keep
115 * it open.
116 */
117 pjsip_transport *last_transport;
118
119 /* Transport being used by pending transaction, for informational purpose,
120 * we don't keep this transport.
121 */
122 pjsip_transport *info_transport;
123 };
124
125
126 /* Declaration of async auth send implementation */
127 static pj_status_t async_auth_send_impl(pjsip_auth_clt_sess *auth_sess,
128 void *user_data,
129 pjsip_tx_data *tdata);
130
131 /* Declaration of async auth abandon implementation */
132 static void async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess,
133 void *user_data);
134
135
136 PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token,
137 pjsip_regc_cb *cb,
138 pjsip_regc **p_regc)
139 {
140 pj_pool_t *pool;
141 pjsip_regc *regc;
142 pj_status_t status;
143
144 /* Verify arguments. */
145 PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL);
146
147 pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024);
148 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
149
150 regc = PJ_POOL_ZALLOC_T(pool, pjsip_regc);
151
152 regc->pool = pool;
153 regc->endpt = endpt;
154 regc->token = token;
155 regc->cb = cb;
156 regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
157 regc->add_xuid_param = pjsip_cfg()->regc.add_xuid_param;
158
159 status = pj_lock_create_recursive_mutex(pool, pool->obj_name,
160 &regc->lock);
161 if (status != PJ_SUCCESS) {
162 pj_pool_release(pool);
163 return status;
164 }
165
166 status = pj_atomic_create(pool, 0, &regc->busy_ctr);
167 if (status != PJ_SUCCESS) {
168 pj_lock_destroy(regc->lock);
169 pj_pool_release(pool);
170 return status;
171 }
172
173 status = pjsip_auth_clt_init(&regc->auth_sess, endpt, regc->pool, 0);
174 if (status != PJ_SUCCESS)
175 return status;
176
177 pj_list_init(&regc->route_set);
178 pj_list_init(&regc->hdr_list);
179 pj_list_init(&regc->contact_hdr_list);
180 pj_list_init(&regc->removed_contact_hdr_list);
181
182 /* Done */
183 *p_regc = regc;
184 return PJ_SUCCESS;
185 }
186
187 PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc)
188 {
189 return pjsip_regc_destroy2(regc, PJ_TRUE);
190 }
191
192 PJ_DEF(pj_status_t) pjsip_regc_destroy2(pjsip_regc *regc, pj_bool_t force)
193 {
194 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
195
196 pj_lock_acquire(regc->lock);
197
198 if (!force && regc->has_tsx) {
199 pj_lock_release(regc->lock);
200 return PJ_EBUSY;
201 }
202
203 if (regc->has_tsx || pj_atomic_get(regc->busy_ctr) != 0) {
204 regc->_delete_flag = 1;
205 regc->cb = NULL;
206 pj_lock_release(regc->lock);
207 } else {
208 pjsip_tpselector_dec_ref(&regc->tp_sel);
209 if (regc->last_transport) {
210 pjsip_transport_dec_ref(regc->last_transport);
211 regc->last_transport = NULL;
212 }
213 if (regc->timer.id != 0) {
214 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
215 regc->timer.id = 0;
216 }
217 pj_atomic_destroy(regc->busy_ctr);
218 pj_lock_release(regc->lock);
219 pj_lock_destroy(regc->lock);
220 regc->lock = NULL;
221
222 pjsip_auth_clt_deinit(&regc->auth_sess);
223 pjsip_endpt_release_pool(regc->endpt, regc->pool);
224 }
225
226 return PJ_SUCCESS;
227 }
228
229
230 PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc,
231 pjsip_regc_info *info )
232 {
233 PJ_ASSERT_RETURN(regc && info, PJ_EINVAL);
234
235 pj_lock_acquire(regc->lock);
236
237 info->server_uri = regc->str_srv_url;
238 info->client_uri = regc->from_uri;
239 info->is_busy = (pj_atomic_get(regc->busy_ctr) || regc->has_tsx);
240 info->auto_reg = regc->auto_reg;
241 info->interval = regc->expires;
242 info->transport = regc->has_tsx? regc->info_transport :
243 regc->last_transport;
244
245 if (regc->has_tsx)
246 info->next_reg = 0;
247 else if (regc->auto_reg == 0)
248 info->next_reg = 0;
249 else if (regc->expires == PJSIP_REGC_EXPIRATION_NOT_SPECIFIED)
250 info->next_reg = regc->expires;
251 else {
252 pj_time_val now, next_reg;
253
254 next_reg = regc->next_reg;
255 pj_gettimeofday(&now);
256 if (PJ_TIME_VAL_GT(next_reg, now)) {
257 PJ_TIME_VAL_SUB(next_reg, now);
258 info->next_reg = next_reg.sec;
259 } else {
260 info->next_reg = 0;
261 }
262 }
263
264 pj_lock_release(regc->lock);
265
266 return PJ_SUCCESS;
267 }
268
269
270 PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc)
271 {
272 return regc->pool;
273 }
274
275 static void set_expires( pjsip_regc *regc, pj_uint32_t expires)
276 {
277 if (expires != regc->expires) {
278 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires);
279 } else {
280 regc->expires_hdr = NULL;
281 }
282 }
283
284
285 static pj_status_t set_contact( pjsip_regc *regc,
286 int contact_cnt,
287 const pj_str_t contact[] )
288 {
289 const pj_str_t CONTACT = { "Contact", 7 };
290 pjsip_contact_hdr *h;
291 int i;
292
293 /* Save existing contact list to removed_contact_hdr_list and
294 * clear contact_hdr_list.
295 */
296 pj_list_merge_last(&regc->removed_contact_hdr_list,
297 &regc->contact_hdr_list);
298
299 /* Set the expiration of Contacts in to removed_contact_hdr_list
300 * zero.
301 */
302 h = regc->removed_contact_hdr_list.next;
303 while (h != &regc->removed_contact_hdr_list) {
304 h->expires = 0;
305 h = h->next;
306 }
307
308 /* Process new contacts */
309 for (i=0; i<contact_cnt; ++i) {
310 pjsip_contact_hdr *hdr;
311 pj_str_t tmp;
312
313 pj_strdup_with_null(regc->pool, &tmp, &contact[i]);
314 hdr = (pjsip_contact_hdr*)
315 pjsip_parse_hdr(regc->pool, &CONTACT, tmp.ptr, tmp.slen, NULL);
316 if (hdr == NULL) {
317 PJ_LOG(4,(THIS_FILE, "Invalid Contact: \"%.*s\"",
318 (int)tmp.slen, tmp.ptr));
319 return PJSIP_EINVALIDURI;
320 }
321
322 /* Find the new contact in old contact list. If found, remove
323 * the old header from the old header list.
324 */
325 h = regc->removed_contact_hdr_list.next;
326 while (h != &regc->removed_contact_hdr_list) {
327 int rc;
328
329 rc = pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR,
330 h->uri, hdr->uri);
331 if (rc == 0) {
332 /* Match */
333 pj_list_erase(h);
334 break;
335 }
336
337 h = h->next;
338 }
339
340 /* If add_xuid_param option is enabled and Contact URI is sip/sips,
341 * add xuid parameter to assist matching the Contact URI in the
342 * REGISTER response later.
343 */
344 if (regc->add_xuid_param && (PJSIP_URI_SCHEME_IS_SIP(hdr->uri) ||
345 PJSIP_URI_SCHEME_IS_SIPS(hdr->uri)))
346 {
347 pjsip_param *xuid_param;
348 pjsip_sip_uri *sip_uri;
349
350 xuid_param = PJ_POOL_ZALLOC_T(regc->pool, pjsip_param);
351 xuid_param->name = XUID_PARAM_NAME;
352 pj_create_unique_string(regc->pool, &xuid_param->value);
353
354 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(hdr->uri);
355 pj_list_push_back(&sip_uri->other_param, xuid_param);
356 }
357
358 pj_list_push_back(&regc->contact_hdr_list, hdr);
359 }
360
361 return PJ_SUCCESS;
362 }
363
364
365 PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc,
366 const pj_str_t *srv_url,
367 const pj_str_t *from_url,
368 const pj_str_t *to_url,
369 int contact_cnt,
370 const pj_str_t contact[],
371 pj_uint32_t expires)
372 {
373 pj_str_t tmp;
374 pj_status_t status;
375
376 PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url &&
377 expires, PJ_EINVAL);
378
379 /* Copy server URL. */
380 pj_strdup_with_null(regc->pool, &regc->str_srv_url, srv_url);
381
382 /* Set server URL. */
383 tmp = regc->str_srv_url;
384 regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0);
385 if (regc->srv_url == NULL) {
386 return PJSIP_EINVALIDURI;
387 }
388
389 /* Set "From" header. */
390 pj_strdup_with_null(regc->pool, &regc->from_uri, from_url);
391 tmp = regc->from_uri;
392 regc->from_hdr = pjsip_from_hdr_create(regc->pool);
393 regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
394 PJSIP_PARSE_URI_AS_NAMEADDR);
395 if (!regc->from_hdr->uri) {
396 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s",
397 (int)from_url->slen, from_url->ptr));
398 return PJSIP_EINVALIDURI;
399 }
400
401 /* Set "To" header. */
402 pj_strdup_with_null(regc->pool, &tmp, to_url);
403 regc->to_hdr = pjsip_to_hdr_create(regc->pool);
404 regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen,
405 PJSIP_PARSE_URI_AS_NAMEADDR);
406 if (!regc->to_hdr->uri) {
407 PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s",
408 (int)to_url->slen, to_url->ptr));
409 return PJSIP_EINVALIDURI;
410 }
411
412
413 /* Set "Contact" header. */
414 status = set_contact( regc, contact_cnt, contact);
415 if (status != PJ_SUCCESS)
416 return status;
417
418 /* Set "Expires" header, if required. */
419 set_expires( regc, expires);
420 regc->delay_before_refresh = DELAY_BEFORE_REFRESH;
421
422 /* Set "Call-ID" header. */
423 regc->cid_hdr = pjsip_cid_hdr_create(regc->pool);
424 pj_create_unique_string(regc->pool, &regc->cid_hdr->id);
425
426 /* Set "CSeq" header. */
427 regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool);
428 regc->cseq_hdr->cseq = pj_rand() % 0xFFFF;
429 pjsip_method_set( &regc->cseq_hdr->method, PJSIP_REGISTER_METHOD);
430
431 /* Done. */
432 return PJ_SUCCESS;
433 }
434
435 PJ_DEF(void) pjsip_regc_add_ref( pjsip_regc *regc )
436 {
437 pj_assert(regc);
438 pj_atomic_inc(regc->busy_ctr);
439 }
440
441 PJ_DEF(pj_status_t) pjsip_regc_dec_ref( pjsip_regc *regc )
442 {
443 pj_assert(regc);
444 if (pj_atomic_dec_and_get(regc->busy_ctr)==0 && regc->_delete_flag) {
445 pjsip_regc_destroy(regc);
446 return PJ_EGONE;
447 }
448
449 return PJ_SUCCESS;
450 }
451
452 PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc,
453 int count,
454 const pjsip_cred_info cred[] )
455 {
456 PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL);
457 return pjsip_auth_clt_set_credentials(&regc->auth_sess, count, cred);
458 }
459
460 PJ_DEF(pj_status_t) pjsip_regc_set_prefs( pjsip_regc *regc,
461 const pjsip_auth_clt_pref *pref)
462 {
463 PJ_ASSERT_RETURN(regc && pref, PJ_EINVAL);
464 return pjsip_auth_clt_set_prefs(&regc->auth_sess, pref);
465 }
466
467 PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc,
468 const pjsip_route_hdr *route_set)
469 {
470 const pjsip_route_hdr *chdr;
471
472 PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL);
473
474 pj_list_init(&regc->route_set);
475
476 chdr = route_set->next;
477 while (chdr != route_set) {
478 pj_list_push_back(&regc->route_set, pjsip_hdr_clone(regc->pool, chdr));
479 chdr = chdr->next;
480 }
481
482 return PJ_SUCCESS;
483 }
484
485
486 /*
487 * Bind client registration to a specific transport/listener.
488 */
489 PJ_DEF(pj_status_t) pjsip_regc_set_transport( pjsip_regc *regc,
490 const pjsip_tpselector *sel)
491 {
492 PJ_ASSERT_RETURN(regc && sel, PJ_EINVAL);
493
494 pjsip_tpselector_dec_ref(&regc->tp_sel);
495 pj_memcpy(&regc->tp_sel, sel, sizeof(*sel));
496 pjsip_tpselector_add_ref(&regc->tp_sel);
497
498 return PJ_SUCCESS;
499 }
500
501 /* Release transport */
502 PJ_DEF(pj_status_t) pjsip_regc_release_transport(pjsip_regc *regc)
503 {
504 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
505 if (regc->last_transport) {
506 pjsip_transport_dec_ref(regc->last_transport);
507 regc->last_transport = NULL;
508 }
509 if (regc->info_transport) {
510 regc->info_transport = NULL;
511 }
512 return PJ_SUCCESS;
513 }
514
515
516 PJ_DEF(pj_status_t) pjsip_regc_add_headers( pjsip_regc *regc,
517 const pjsip_hdr *hdr_list)
518 {
519 const pjsip_hdr *hdr;
520
521 PJ_ASSERT_RETURN(regc && hdr_list, PJ_EINVAL);
522
523 //This is "add" operation, so don't remove headers.
524 //pj_list_init(&regc->hdr_list);
525
526 hdr = hdr_list->next;
527 while (hdr != hdr_list) {
528 pj_list_push_back(&regc->hdr_list, pjsip_hdr_clone(regc->pool, hdr));
529 hdr = hdr->next;
530 }
531
532 return PJ_SUCCESS;
533 }
534
535 static pj_status_t create_request(pjsip_regc *regc,
536 pjsip_tx_data **p_tdata)
537 {
538 pj_status_t status;
539 pjsip_tx_data *tdata;
540
541 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
542
543 /* Create the request. */
544 status = pjsip_endpt_create_request_from_hdr( regc->endpt,
545 pjsip_get_register_method(),
546 regc->srv_url,
547 regc->from_hdr,
548 regc->to_hdr,
549 NULL,
550 regc->cid_hdr,
551 regc->cseq_hdr->cseq,
552 NULL,
553 &tdata);
554 if (status != PJ_SUCCESS)
555 return status;
556
557 /* Add cached authorization headers. */
558 pjsip_auth_clt_init_req( &regc->auth_sess, tdata );
559
560 /* Add Route headers from route set, ideally after Via header */
561 if (!pj_list_empty(&regc->route_set)) {
562 pjsip_hdr *route_pos;
563 const pjsip_route_hdr *route;
564
565 route_pos = (pjsip_hdr*)
566 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
567 if (!route_pos)
568 route_pos = &tdata->msg->hdr;
569
570 route = regc->route_set.next;
571 while (route != &regc->route_set) {
572 pjsip_hdr *new_hdr = (pjsip_hdr*)
573 pjsip_hdr_clone(tdata->pool, route);
574 pj_list_insert_after(route_pos, new_hdr);
575 route_pos = new_hdr;
576 route = route->next;
577 }
578 }
579
580 /* Add additional request headers */
581 if (!pj_list_empty(&regc->hdr_list)) {
582 const pjsip_hdr *hdr;
583
584 hdr = regc->hdr_list.next;
585 while (hdr != &regc->hdr_list) {
586 pjsip_hdr *new_hdr = (pjsip_hdr*)
587 pjsip_hdr_clone(tdata->pool, hdr);
588 pjsip_msg_add_hdr(tdata->msg, new_hdr);
589 hdr = hdr->next;
590 }
591 }
592
593 /* Done. */
594 *p_tdata = tdata;
595 return PJ_SUCCESS;
596 }
597
598
599 PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg,
600 pjsip_tx_data **p_tdata)
601 {
602 pjsip_msg *msg;
603 pjsip_contact_hdr *hdr;
604 const pjsip_hdr *h_allow;
605 pj_status_t status;
606 pjsip_tx_data *tdata;
607
608 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
609
610 pj_lock_acquire(regc->lock);
611
612 regc->expires_requested = 1;
613
614 status = create_request(regc, &tdata);
615 if (status != PJ_SUCCESS) {
616 pj_lock_release(regc->lock);
617 return status;
618 }
619
620 msg = tdata->msg;
621
622 /* Add Contact headers. */
623 hdr = regc->contact_hdr_list.next;
624 while (hdr != &regc->contact_hdr_list) {
625 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
626 pjsip_hdr_clone(tdata->pool, hdr));
627 hdr = hdr->next;
628 }
629
630 /* Also add bindings which are to be removed */
631 while (!pj_list_empty(&regc->removed_contact_hdr_list)) {
632 hdr = regc->removed_contact_hdr_list.next;
633 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
634 pjsip_hdr_clone(tdata->pool, hdr));
635 pj_list_erase(hdr);
636 }
637
638
639 if (regc->expires_hdr)
640 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
641 pjsip_hdr_clone(tdata->pool,
642 regc->expires_hdr));
643
644 if (regc->timer.id != 0) {
645 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
646 regc->timer.id = 0;
647 }
648
649 /* Add Allow header (https://github.com/pjsip/pjproject/issues/1039) */
650 h_allow = pjsip_endpt_get_capability(regc->endpt, PJSIP_H_ALLOW, NULL);
651 if (h_allow) {
652 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
653 pjsip_hdr_clone(tdata->pool, h_allow));
654
655 }
656
657 regc->auto_reg = autoreg;
658
659 pj_lock_release(regc->lock);
660
661 /* Done */
662 *p_tdata = tdata;
663 return PJ_SUCCESS;
664 }
665
666
667 PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc,
668 pjsip_tx_data **p_tdata)
669 {
670 pjsip_tx_data *tdata;
671 pjsip_msg *msg;
672 pjsip_hdr *hdr;
673 pj_status_t status;
674
675 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
676
677 pj_lock_acquire(regc->lock);
678
679 if (regc->timer.id != 0) {
680 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
681 regc->timer.id = 0;
682 }
683
684 regc->expires_requested = 0;
685
686 status = create_request(regc, &tdata);
687 if (status != PJ_SUCCESS) {
688 pj_lock_release(regc->lock);
689 return status;
690 }
691
692 msg = tdata->msg;
693
694 /* Add Contact headers. */
695 hdr = (pjsip_hdr*)regc->contact_hdr_list.next;
696 while ((void*)hdr != (void*)&regc->contact_hdr_list) {
697 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
698 pjsip_hdr_clone(tdata->pool, hdr));
699 hdr = hdr->next;
700 }
701
702 /* Also add bindings which are to be removed */
703 while (!pj_list_empty(&regc->removed_contact_hdr_list)) {
704 hdr = (pjsip_hdr*)regc->removed_contact_hdr_list.next;
705 pjsip_msg_add_hdr(msg, (pjsip_hdr*)
706 pjsip_hdr_clone(tdata->pool, hdr));
707 pj_list_erase(hdr);
708 }
709
710 /* Add Expires:0 header */
711 hdr = (pjsip_hdr*) pjsip_expires_hdr_create(tdata->pool, 0);
712 pjsip_msg_add_hdr(msg, hdr);
713
714 pj_lock_release(regc->lock);
715
716 *p_tdata = tdata;
717 return PJ_SUCCESS;
718 }
719
720 PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc,
721 pjsip_tx_data **p_tdata)
722 {
723 pjsip_tx_data *tdata;
724 pjsip_contact_hdr *hcontact;
725 pjsip_hdr *hdr;
726 pjsip_msg *msg;
727 pj_status_t status;
728
729 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL);
730
731 pj_lock_acquire(regc->lock);
732
733 if (regc->timer.id != 0) {
734 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
735 regc->timer.id = 0;
736 }
737
738 status = create_request(regc, &tdata);
739 if (status != PJ_SUCCESS) {
740 pj_lock_release(regc->lock);
741 return status;
742 }
743
744 msg = tdata->msg;
745
746 /* Clear removed_contact_hdr_list */
747 pj_list_init(&regc->removed_contact_hdr_list);
748
749 /* Add Contact:* header */
750 hcontact = pjsip_contact_hdr_create(tdata->pool);
751 hcontact->star = 1;
752 pjsip_msg_add_hdr(msg, (pjsip_hdr*)hcontact);
753
754 /* Add Expires:0 header */
755 hdr = (pjsip_hdr*) pjsip_expires_hdr_create(tdata->pool, 0);
756 pjsip_msg_add_hdr(msg, hdr);
757
758 pj_lock_release(regc->lock);
759
760 *p_tdata = tdata;
761 return PJ_SUCCESS;
762 }
763
764
765 PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc,
766 int contact_cnt,
767 const pj_str_t contact[] )
768 {
769 pj_status_t status;
770
771 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
772
773 pj_lock_acquire(regc->lock);
774 status = set_contact( regc, contact_cnt, contact );
775 pj_lock_release(regc->lock);
776
777 return status;
778 }
779
780
781 PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc,
782 pj_uint32_t expires )
783 {
784 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
785
786 pj_lock_acquire(regc->lock);
787 set_expires( regc, expires );
788 pj_lock_release(regc->lock);
789
790 return PJ_SUCCESS;
791 }
792
793 static void cbparam_init( struct pjsip_regc_cbparam *cbparam,
794 pjsip_regc *regc,
795 pj_status_t status, int st_code,
796 const pj_str_t *reason,
797 pjsip_rx_data *rdata, pj_uint32_t expiration,
798 int contact_cnt, pjsip_contact_hdr *contact[],
799 pj_bool_t is_unreg)
800 {
801 cbparam->regc = regc;
802 cbparam->token = regc->token;
803 cbparam->status = status;
804 cbparam->code = st_code;
805 cbparam->reason = *reason;
806 cbparam->rdata = rdata;
807 cbparam->contact_cnt = contact_cnt;
808 cbparam->expiration = (expiration != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED?
809 expiration: regc->expires_requested);
810 cbparam->is_unreg = is_unreg;
811 if (contact_cnt) {
812 pj_memcpy( cbparam->contact, contact,
813 contact_cnt*sizeof(pjsip_contact_hdr*));
814 }
815 }
816
817 static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code,
818 const pj_str_t *reason,
819 pjsip_rx_data *rdata, pj_uint32_t expiration,
820 int contact_cnt, pjsip_contact_hdr *contact[],
821 pj_bool_t is_unreg)
822 {
823 struct pjsip_regc_cbparam cbparam;
824
825 if (!regc->cb)
826 return;
827
828 cbparam_init(&cbparam, regc, status, st_code, reason, rdata, expiration,
829 contact_cnt, contact, is_unreg);
830 (*regc->cb)(&cbparam);
831 }
832
833 static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap,
834 struct pj_timer_entry *entry)
835 {
836 pjsip_regc *regc = (pjsip_regc*) entry->user_data;
837 pjsip_tx_data *tdata;
838 pj_status_t status;
839
840 PJ_UNUSED_ARG(timer_heap);
841
842 /* Temporarily increase busy flag to prevent regc from being deleted
843 * in pjsip_regc_send() or in the callback
844 */
845 pjsip_regc_add_ref(regc);
846
847 entry->id = 0;
848 status = pjsip_regc_register(regc, 1, &tdata);
849 if (status == PJ_SUCCESS) {
850 status = pjsip_regc_send(regc, tdata);
851 }
852
853 if (status != PJ_SUCCESS && regc->cb) {
854 char errmsg[PJ_ERR_MSG_SIZE];
855 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
856 call_callback(regc, status, 400, &reason, NULL, NOEXP, 0, NULL,
857 PJ_FALSE);
858 }
859
860 /* Delete the record if user destroy regc during the callback. */
861 pjsip_regc_dec_ref(regc);
862 }
863
864 static void schedule_registration ( pjsip_regc *regc, pj_uint32_t expiration )
865 {
866 if (regc->auto_reg && expiration > 0 && expiration != NOEXP) {
867 const pj_time_val min_delay = {MIN_REFRESH_MSEC / 1000,
868 MIN_REFRESH_MSEC % 1000};
869 pj_time_val delay = {0, 0};
870
871 pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(regc->endpt),
872 &regc->timer, 0);
873
874 /* prevent underflow in case the remote answers with an unexpectedly low Expires field */
875 if (expiration > regc->delay_before_refresh) {
876 delay.sec = expiration - regc->delay_before_refresh;
877 }
878 if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED &&
879 delay.sec > (pj_int32_t)regc->expires) {
880 delay.sec = regc->expires;
881 }
882 if (delay.sec < DELAY_BEFORE_REFRESH) {
883 if (expiration <= DELAY_BEFORE_REFRESH) {
884 /* Very small expiration: refresh before expiry. */
885 delay.sec = (expiration > 1) ? (long)expiration - 1 : 0;
886 delay.msec = (expiration > 1) ? 0 : 500;
887 } else {
888 delay.sec = DELAY_BEFORE_REFRESH;
889 }
890 }
891
892 /* make sure we don't go below the minimum */
893 if (PJ_TIME_VAL_LT(delay, min_delay)) {
894 delay = min_delay;
895 }
896 regc->timer.cb = &regc_refresh_timer_cb;
897 regc->timer.id = REFRESH_TIMER;
898 regc->timer.user_data = regc;
899 pjsip_endpt_schedule_timer( regc->endpt, &regc->timer, &delay);
900 pj_gettimeofday(&regc->last_reg);
901 regc->next_reg = regc->last_reg;
902 PJ_TIME_VAL_ADD(regc->next_reg, delay);
903 }
904 }
905
906 PJ_DEF(pj_status_t) pjsip_regc_set_reg_tsx_cb( pjsip_regc *regc,
907 pjsip_regc_tsx_cb *tsx_cb)
908 {
909 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
910 regc->tsx_cb = tsx_cb;
911 return PJ_SUCCESS;
912 }
913
914
915 PJ_DEF(pj_status_t) pjsip_regc_set_via_sent_by( pjsip_regc *regc,
916 pjsip_host_port *via_addr,
917 pjsip_transport *via_tp)
918 {
919 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
920
921 if (!via_addr)
922 pj_bzero(&regc->via_addr, sizeof(regc->via_addr));
923 else {
924 if (pj_strcmp(&regc->via_addr.host, &via_addr->host))
925 pj_strdup(regc->pool, &regc->via_addr.host, &via_addr->host);
926 regc->via_addr.port = via_addr->port;
927 }
928 regc->via_tp = via_tp;
929
930 return PJ_SUCCESS;
931 }
932
933 PJ_DEF(pj_status_t)
934 pjsip_regc_set_delay_before_refresh( pjsip_regc *regc,
935 pj_uint32_t delay )
936 {
937 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
938
939 if (delay > regc->expires)
940 return PJ_ETOOBIG;
941
942 pj_lock_acquire(regc->lock);
943
944 if (regc->delay_before_refresh != delay)
945 {
946 regc->delay_before_refresh = delay;
947
948 if (regc->timer.id != 0) {
949 /* Cancel registration timer */
950 pjsip_endpt_cancel_timer(regc->endpt, &regc->timer);
951 regc->timer.id = 0;
952
953 /* Schedule next registration */
954 schedule_registration(regc, regc->expires);
955 }
956 }
957
958 pj_lock_release(regc->lock);
959
960 return PJ_SUCCESS;
961 }
962
963
964 static pj_uint32_t calculate_response_expiration(const pjsip_regc *regc,
965 const pjsip_rx_data *rdata,
966 unsigned *contact_cnt,
967 unsigned max_contact,
968 pjsip_contact_hdr *contacts[])
969 {
970 pj_uint32_t expiration = NOEXP;
971 const pjsip_msg *msg = rdata->msg_info.msg;
972 const pjsip_hdr *hdr;
973
974 /* Enumerate all Contact headers in the response */
975 *contact_cnt = 0;
976 for (hdr=msg->hdr.next; hdr!=&msg->hdr; hdr=hdr->next) {
977 if (hdr->type == PJSIP_H_CONTACT &&
978 *contact_cnt < max_contact)
979 {
980 contacts[*contact_cnt] = (pjsip_contact_hdr*)hdr;
981 ++(*contact_cnt);
982 }
983 }
984
985 if (regc->current_op == REGC_REGISTERING) {
986 pj_bool_t has_our_contact = PJ_FALSE;
987 const pjsip_expires_hdr *expires;
988
989 /* Get Expires header */
990 expires = (const pjsip_expires_hdr*)
991 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
992
993 /* Try to find the Contact URIs that we register, in the response
994 * to get the expires value. We'll try both with comparing the URI
995 * and comparing the extension param only.
996 */
997 if (pjsip_cfg()->regc.check_contact || regc->add_xuid_param) {
998 unsigned i;
999 for (i=0; i<*contact_cnt; ++i) {
1000 const pjsip_contact_hdr *our_hdr;
1001
1002 our_hdr = (const pjsip_contact_hdr*)
1003 regc->contact_hdr_list.next;
1004
1005 /* Match with our Contact header(s) */
1006 while ((void*)our_hdr != (void*)&regc->contact_hdr_list) {
1007
1008 const pjsip_uri *uri1, *uri2;
1009 pj_bool_t matched = PJ_FALSE;
1010
1011 /* Exclude the display name when comparing the URI
1012 * since server may not return it.
1013 */
1014 uri1 = (const pjsip_uri*)
1015 pjsip_uri_get_uri(contacts[i]->uri);
1016 uri2 = (const pjsip_uri*)
1017 pjsip_uri_get_uri(our_hdr->uri);
1018
1019 /* First try with exact matching, according to RFC 3261
1020 * Section 19.1.4 URI Comparison
1021 */
1022 if (pjsip_cfg()->regc.check_contact) {
1023 matched = pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR,
1024 uri1, uri2)==0;
1025 }
1026
1027 /* If no match is found, try with matching the extension
1028 * parameter only if extension parameter was added.
1029 */
1030 if (!matched && regc->add_xuid_param &&
1031 (PJSIP_URI_SCHEME_IS_SIP(uri1) ||
1032 PJSIP_URI_SCHEME_IS_SIPS(uri1)) &&
1033 (PJSIP_URI_SCHEME_IS_SIP(uri2) ||
1034 PJSIP_URI_SCHEME_IS_SIPS(uri2)))
1035 {
1036 const pjsip_sip_uri *sip_uri1, *sip_uri2;
1037 const pjsip_param *p1, *p2;
1038
1039 sip_uri1 = (const pjsip_sip_uri*)uri1;
1040 sip_uri2 = (const pjsip_sip_uri*)uri2;
1041
1042 p1 = pjsip_param_cfind(&sip_uri1->other_param,
1043 &XUID_PARAM_NAME);
1044 p2 = pjsip_param_cfind(&sip_uri2->other_param,
1045 &XUID_PARAM_NAME);
1046 matched = p1 && p2 &&
1047 pj_strcmp(&p1->value, &p2->value)==0;
1048
1049 }
1050
1051 if (matched) {
1052 has_our_contact = PJ_TRUE;
1053
1054 if (contacts[i]->expires != PJSIP_EXPIRES_NOT_SPECIFIED
1055 && contacts[i]->expires < expiration)
1056 {
1057 /* Get the lowest expiration time. */
1058 expiration = contacts[i]->expires;
1059 }
1060
1061 break;
1062 }
1063
1064 our_hdr = our_hdr->next;
1065
1066 } /* while ((void.. */
1067
1068 } /* for (i=.. */
1069
1070 /* If matching Contact header(s) are found but the
1071 * header doesn't contain expires parameter, get the
1072 * expiration value from the Expires header. And
1073 * if Expires header is not present, get the expiration
1074 * value from the request.
1075 */
1076 if (has_our_contact && expiration == NOEXP) {
1077 if (expires) {
1078 expiration = expires->ivalue;
1079 } else if (regc->expires_hdr) {
1080 expiration = regc->expires_hdr->ivalue;
1081 } else {
1082 /* We didn't request explicit expiration value,
1083 * and server doesn't specify it either. This
1084 * shouldn't happen unless we have a broken
1085 * registrar.
1086 */
1087 expiration = 3600;
1088 }
1089 }
1090
1091 }
1092
1093 /* If we still couldn't get matching Contact header(s), it means
1094 * there must be something wrong with the registrar (e.g. it may
1095 * have modified the URI's in the response, which is prohibited).
1096 */
1097 if (expiration==NOEXP) {
1098 /* If the number of Contact headers in the response matches
1099 * ours, they're all probably ours. Get the expiration
1100 * from there if this is the case, or from Expires header
1101 * if we don't have exact Contact header count, or
1102 * from the request as the last resort.
1103 */
1104 pj_size_t our_contact_cnt;
1105
1106 our_contact_cnt = pj_list_size(&regc->contact_hdr_list);
1107
1108 if (*contact_cnt == our_contact_cnt && *contact_cnt &&
1109 contacts[0]->expires != PJSIP_EXPIRES_NOT_SPECIFIED)
1110 {
1111 expiration = contacts[0]->expires;
1112 } else if (expires)
1113 expiration = expires->ivalue;
1114 else if (regc->expires_hdr)
1115 expiration = regc->expires_hdr->ivalue;
1116 else
1117 expiration = 3600;
1118 }
1119
1120 } else {
1121 /* Just assume that the unregistration has been successful. */
1122 expiration = 0;
1123 }
1124
1125 /* Must have expiration value by now */
1126 pj_assert(expiration != NOEXP);
1127
1128 return expiration;
1129 }
1130
1131
1132 /* Sending authentication */
1133 static pj_status_t async_auth_send_impl(pjsip_auth_clt_sess *auth_sess,
1134 void *user_data,
1135 pjsip_tx_data *tdata)
1136 {
1137 pj_status_t status;
1138 pjsip_regc *regc = (pjsip_regc*)user_data;
1139
1140 PJ_UNUSED_ARG(auth_sess);
1141
1142 status = pjsip_regc_send(regc, tdata);
1143 if (status != PJ_SUCCESS) {
1144 char errmsg[PJ_ERR_MSG_SIZE];
1145 pj_bool_t is_unreg = (regc->current_op == REGC_UNREGISTERING);
1146 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
1147 /* call_callback() is invoked here without holding regc->lock.
1148 * This function is called either from within the async challenge
1149 * callback (lock already released before the callback fires) or
1150 * from outside it (no lock held by the caller), so this is safe
1151 * and consistent with the rest of the codebase.
1152 */
1153 call_callback(regc, status, 0, &reason, NULL, NOEXP, 0, NULL,
1154 is_unreg);
1155 }
1156
1157 return status;
1158 }
1159
1160
1161 /* Called when the application abandons a pending async auth challenge. */
1162 static void async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess,
1163 void *user_data)
1164 {
1165 pjsip_regc *regc = (pjsip_regc*)user_data;
1166 pj_bool_t is_unreg = (regc->current_op == REGC_UNREGISTERING);
1167 pj_str_t reason = { "Authentication abandoned", 24 };
1168
1169 PJ_UNUSED_ARG(auth_sess);
1170
1171 /* Called without regc->lock held (same convention as send_impl). */
1172 call_callback(regc, PJ_ECANCELLED, PJSIP_SC_UNAUTHORIZED, &reason,
1173 NULL, NOEXP, 0, NULL, is_unreg);
1174 }
1175
1176
1177 static void regc_tsx_callback(void *token, pjsip_event *event)
1178 {
1179 pj_status_t status;
1180 pjsip_regc *regc = (pjsip_regc*) token;
1181 pjsip_transaction *tsx = event->body.tsx_state.tsx;
1182 pj_bool_t handled = PJ_TRUE;
1183 pj_bool_t update_contact = PJ_FALSE;
1184
1185 pjsip_regc_add_ref(regc);
1186 pj_lock_acquire(regc->lock);
1187
1188 /* Decrement pending transaction counter. */
1189 pj_assert(regc->has_tsx);
1190 regc->has_tsx = PJ_FALSE;
1191
1192 /* Add reference to the transport */
1193 if (tsx->transport != regc->last_transport) {
1194 if (regc->last_transport) {
1195 pjsip_transport_dec_ref(regc->last_transport);
1196 regc->last_transport = NULL;
1197 }
1198
1199 if (tsx->transport) {
1200 regc->last_transport = tsx->transport;
1201 pjsip_transport_add_ref(regc->last_transport);
1202 }
1203 }
1204
1205 if (regc->_delete_flag == 0 && regc->tsx_cb &&
1206 regc->current_op == REGC_REGISTERING)
1207 {
1208 struct pjsip_regc_tsx_cb_param param;
1209
1210 param.contact_cnt = -1;
1211 cbparam_init(&param.cbparam, regc, PJ_SUCCESS, tsx->status_code,
1212 &tsx->status_text,
1213 (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
1214 event->body.tsx_state.src.rdata : NULL,
1215 NOEXP, 0, NULL, PJ_FALSE);
1216
1217 /* Call regc tsx callback before handling any response */
1218 pj_lock_release(regc->lock);
1219 (*regc->tsx_cb)(&param);
1220 pj_lock_acquire(regc->lock);
1221
1222 if (param.contact_cnt >= 0) {
1223 /* Since we receive non-2xx response, it means that (some) contact
1224 * bindings haven't been established so we can safely remove these
1225 * contact headers. This is to avoid removing non-existent contact
1226 * bindings later.
1227 */
1228 if (tsx->status_code/100 != 2) {
1229 pjsip_contact_hdr *h;
1230
1231 h = regc->contact_hdr_list.next;
1232 while (h != &regc->contact_hdr_list) {
1233 pjsip_contact_hdr *next = h->next;
1234
1235 if (h->expires == PJSIP_EXPIRES_NOT_SPECIFIED) {
1236 pj_list_erase(h);
1237 }
1238 h = next;
1239 }
1240 }
1241
1242 /* Update contact address */
1243 pjsip_regc_update_contact(regc, param.contact_cnt, param.contact);
1244 update_contact = PJ_TRUE;
1245 }
1246 }
1247
1248 /* Handle 401/407 challenge (even when _delete_flag is set) */
1249 if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
1250 tsx->status_code == PJSIP_SC_UNAUTHORIZED)
1251 {
1252 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
1253 pjsip_tx_data *tdata;
1254 pj_bool_t is_unreg;
1255 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
1256
1257 /* Capture is_unreg but keep current_op alive so that
1258 * async auth callbacks can read it if needed. It will be
1259 * reset to REGC_IDLE below (sync fallback) or by
1260 * pjsip_regc_send() on the async success path.
1261 */
1262 is_unreg = (regc->current_op == REGC_UNREGISTERING);
1263
1264 if (update_contact) {
1265 pjsip_msg *msg;
1266 pjsip_hdr *hdr, *ins_hdr;
1267 pjsip_contact_hdr *chdr;
1268
1269 /* Delete Contact headers, but we shouldn't delete headers
1270 * which are supposed to remove contact bindings since
1271 * we cannot reconstruct those headers.
1272 */
1273 msg = tsx->last_tx->msg;
1274 hdr = msg->hdr.next;
1275 ins_hdr = &msg->hdr;
1276 while (hdr != &msg->hdr) {
1277 pjsip_hdr *next = hdr->next;
1278
1279 if (hdr->type == PJSIP_H_CONTACT) {
1280 chdr = (pjsip_contact_hdr *)hdr;
1281 if (chdr->expires != 0) {
1282 pj_list_erase(hdr);
1283 ins_hdr = next;
1284 }
1285 }
1286 hdr = next;
1287 }
1288
1289 /* Add Contact headers. */
1290 chdr = regc->contact_hdr_list.next;
1291 while (chdr != &regc->contact_hdr_list) {
1292 pj_list_insert_before(ins_hdr, (pjsip_hdr*)
1293 pjsip_hdr_clone(tsx->last_tx->pool, chdr));
1294 chdr = chdr->next;
1295 }
1296
1297 /* Also add bindings which are to be removed */
1298 while (!pj_list_empty(&regc->removed_contact_hdr_list)) {
1299 chdr = regc->removed_contact_hdr_list.next;
1300 pj_list_insert_before(ins_hdr, (pjsip_hdr*)
1301 pjsip_hdr_clone(tsx->last_tx->pool, chdr));
1302 pj_list_erase(chdr);
1303 }
1304 }
1305
1306 if (regc->_delete_flag != 0) {
1307 pjsip_auth_clt_set_parent(&regc->auth_sess, NULL);
1308 }
1309
1310 /* Check if application handles the authentication.
1311 * Release the lock to prevent deadlock in case application calls
1312 * pjsip_auth_clt_async_send_req() synchronously from within the
1313 * callback (see ticket #2260 and #1247).
1314 * It should be safe to do this since the regc's refcount has been
1315 * incremented.
1316 */
1317 {
1318 pjsip_auth_clt_async_impl_token *auth_token;
1319 auth_token = PJ_POOL_ZALLOC_T(tsx->pool,
1320 pjsip_auth_clt_async_impl_token);
1321 auth_token->user_data = regc;
1322 auth_token->send_impl = &async_auth_send_impl;
1323 auth_token->abandon_impl = &async_auth_abandon_impl;
1324 auth_token->grp_lock = tsx->grp_lock;
1325 pj_grp_lock_add_ref(tsx->grp_lock);
1326
1327 pj_bzero(&chal_param, sizeof(chal_param));
1328 chal_param.rdata = rdata;
1329 chal_param.tdata = tsx->last_tx;
1330 pj_lock_release(regc->lock);
1331 status = pjsip_auth_clt_async_impl_on_challenge(
1332 &regc->auth_sess,
1333 auth_token, &chal_param);
1334 pj_lock_acquire(regc->lock);
1335 if (status != PJ_SUCCESS)
1336 pj_grp_lock_dec_ref(tsx->grp_lock);
1337 }
1338 if (status != PJ_SUCCESS) {
1339 /* Application does not handle the authentication, so let's
1340 * handle it here now. Reset current_op now since we deferred
1341 * it above for the async path.
1342 */
1343 regc->current_op = REGC_IDLE;
1344
1345 /* Reinit request with auth response. */
1346 status = pjsip_auth_clt_reinit_req( &regc->auth_sess,
1347 rdata,
1348 tsx->last_tx,
1349 &tdata);
1350
1351 /* Send the request. */
1352 if (status == PJ_SUCCESS && tdata) {
1353 /* Need to unlock the regc temporarily while sending the
1354 * message to prevent deadlock (see ticket #2260 and #1247).
1355 * It should be safe to do this since the regc's refcount has
1356 * been incremented.
1357 */
1358 pj_lock_release(regc->lock);
1359 status = pjsip_regc_send(regc, tdata);
1360 pj_lock_acquire(regc->lock);
1361 }
1362
1363 if (status != PJ_SUCCESS) {
1364 /* Only call callback if application is still interested
1365 * in it.
1366 */
1367 if (regc->_delete_flag == 0) {
1368 /* Should be safe to release the lock temporarily.
1369 * We do this to avoid deadlock.
1370 */
1371 pj_lock_release(regc->lock);
1372 call_callback(regc, status, tsx->status_code,
1373 &rdata->msg_info.msg->line.status.reason,
1374 rdata, NOEXP, 0, NULL, is_unreg);
1375 pj_lock_acquire(regc->lock);
1376 }
1377 }
1378 }
1379
1380 } else if (regc->_delete_flag) {
1381
1382 /* User has called pjsip_regc_destroy(), so don't call callback.
1383 * This regc will be destroyed later in this function.
1384 */
1385
1386 /* Just reset current op */
1387 regc->current_op = REGC_IDLE;
1388
1389 } else if (tsx->status_code == PJSIP_SC_INTERVAL_TOO_BRIEF &&
1390 regc->current_op == REGC_REGISTERING)
1391 {
1392 /* Handle 423 response automatically:
1393 * - set requested expiration to Min-Expires header, ONLY IF
1394 * the original request is a registration (as opposed to
1395 * unregistration) and the requested expiration was indeed
1396 * lower than Min-Expires)
1397 * - resend the request
1398 */
1399 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
1400 pjsip_min_expires_hdr *me_hdr;
1401 pjsip_tx_data *tdata;
1402 pj_uint32_t min_exp;
1403
1404 /* reset current op */
1405 regc->current_op = REGC_IDLE;
1406
1407 /* Update requested expiration */
1408 me_hdr = (pjsip_min_expires_hdr*)
1409 pjsip_msg_find_hdr(rdata->msg_info.msg,
1410 PJSIP_H_MIN_EXPIRES, NULL);
1411 if (me_hdr) {
1412 min_exp = me_hdr->ivalue;
1413 } else {
1414 /* Broken server, Min-Expires doesn't exist.
1415 * Just guestimate then, BUT ONLY if if this is the
1416 * first time we received such response.
1417 */
1418 enum {
1419 /* Note: changing this value would require changing couple of
1420 * Python test scripts.
1421 */
1422 UNSPECIFIED_MIN_EXPIRES = 3601
1423 };
1424 if (!regc->expires_hdr ||
1425 regc->expires_hdr->ivalue != UNSPECIFIED_MIN_EXPIRES)
1426 {
1427 min_exp = UNSPECIFIED_MIN_EXPIRES;
1428 } else {
1429 handled = PJ_FALSE;
1430 PJ_LOG(4,(THIS_FILE, "Registration failed: 423 response "
1431 "without Min-Expires header is invalid"));
1432 goto handle_err;
1433 }
1434 }
1435
1436 if (regc->expires_hdr && regc->expires_hdr->ivalue >= min_exp) {
1437 /* But we already send with greater expiration time, why does
1438 * the server send us with 423? Oh well, just fail the request.
1439 */
1440 handled = PJ_FALSE;
1441 PJ_LOG(4,(THIS_FILE, "Registration failed: invalid "
1442 "Min-Expires header value in response"));
1443 goto handle_err;
1444 }
1445
1446 set_expires(regc, min_exp);
1447
1448 status = pjsip_regc_register(regc, regc->auto_reg, &tdata);
1449 if (status == PJ_SUCCESS) {
1450 /* Need to unlock the regc temporarily while sending the message
1451 * to prevent deadlock (see ticket #2260 and #1247).
1452 * It should be safe to do this since the regc's refcount has been
1453 * incremented.
1454 */
1455 pj_lock_release(regc->lock);
1456 status = pjsip_regc_send(regc, tdata);
1457 pj_lock_acquire(regc->lock);
1458 }
1459
1460 if (status != PJ_SUCCESS) {
1461 /* Only call callback if application is still interested
1462 * in it.
1463 */
1464 if (!regc->_delete_flag) {
1465 /* Should be safe to release the lock temporarily.
1466 * We do this to avoid deadlock.
1467 */
1468 pj_lock_release(regc->lock);
1469 call_callback(regc, status, tsx->status_code,
1470 &rdata->msg_info.msg->line.status.reason,
1471 rdata, NOEXP, 0, NULL, PJ_FALSE);
1472 pj_lock_acquire(regc->lock);
1473 }
1474 }
1475
1476 } else {
1477 handled = PJ_FALSE;
1478 }
1479
1480 handle_err:
1481 if (!handled) {
1482 pjsip_rx_data *rdata;
1483 pj_uint32_t expiration = NOEXP;
1484 unsigned contact_cnt = 0;
1485 pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT];
1486 pj_bool_t is_unreg;
1487
1488 if (tsx->status_code/100 == 2) {
1489
1490 rdata = event->body.tsx_state.src.rdata;
1491
1492 /* Calculate expiration */
1493 expiration = calculate_response_expiration(regc, rdata,
1494 &contact_cnt,
1495 PJSIP_REGC_MAX_CONTACT,
1496 contact);
1497
1498 #if PJSIP_REGISTER_ALLOW_EXP_REFRESH
1499 if (expiration == 0 && regc->current_op != REGC_UNREGISTERING) {
1500 /* Response contain expires contact param 0, allow client to
1501 * continue refresh registration.
1502 * Refer to: https://github.com/pjsip/pjproject/pull/2809
1503 */
1504 const pjsip_msg *msg = rdata->msg_info.msg;
1505 const pjsip_expires_hdr *expires;
1506 expires = (const pjsip_expires_hdr*) pjsip_msg_find_hdr(msg,
1507 PJSIP_H_EXPIRES, NULL);
1508
1509 if (expires) {
1510 expiration = expires->ivalue;
1511 } else if (regc->expires_hdr && regc->expires_hdr->ivalue) {
1512 expiration = regc->expires_hdr->ivalue;
1513 } else {
1514 expiration = 3600;
1515 }
1516 PJ_LOG(4, (THIS_FILE, "Modify response's expiration from 0 "
1517 "to %d", expiration));
1518 }
1519 #endif
1520 /* Schedule next registration */
1521 schedule_registration(regc, expiration);
1522
1523 } else {
1524 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
1525 event->body.tsx_state.src.rdata : NULL;
1526 }
1527
1528 /* Update registration */
1529 // if (expiration==NOEXP) expiration=-1;
1530 regc->expires = expiration;
1531
1532 /* Mark operation as complete */
1533 is_unreg = (regc->current_op == REGC_UNREGISTERING);
1534 regc->current_op = REGC_IDLE;
1535
1536 /* Call callback. */
1537 /* Should be safe to release the lock temporarily.
1538 * We do this to avoid deadlock.
1539 */
1540 pj_lock_release(regc->lock);
1541 call_callback(regc, PJ_SUCCESS, tsx->status_code,
1542 (rdata ? &rdata->msg_info.msg->line.status.reason
1543 : &tsx->status_text),
1544 rdata, expiration,
1545 contact_cnt, contact, is_unreg);
1546 pj_lock_acquire(regc->lock);
1547 }
1548
1549 pj_lock_release(regc->lock);
1550
1551 /* Delete the record if user destroy regc during the callback. */
1552 pjsip_regc_dec_ref(regc);
1553 }
1554
1555 PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata)
1556 {
1557 pj_status_t status;
1558 pjsip_cseq_hdr *cseq_hdr;
1559 pjsip_expires_hdr *expires_hdr;
1560 pj_int32_t cseq;
1561
1562 pjsip_regc_add_ref(regc);
1563 pj_lock_acquire(regc->lock);
1564
1565 /* Make sure we don't have pending transaction. */
1566 if (regc->has_tsx) {
1567 PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another "
1568 "transaction pending"));
1569 pjsip_tx_data_dec_ref( tdata );
1570 pj_lock_release(regc->lock);
1571 pj_atomic_dec(regc->busy_ctr);
1572 return PJSIP_EBUSY;
1573 }
1574
1575 /* Just regc->has_tsx check above should be enough. This assertion check
1576 * may cause problem, e.g: when regc_tsx_callback() invokes callback,
1577 * lock is released and 'has_tsx' is set to FALSE and 'current_op' has
1578 * not been updated to REGC_IDLE yet.
1579 */
1580 //pj_assert(regc->current_op == REGC_IDLE);
1581
1582 /* Invalidate message buffer. */
1583 pjsip_tx_data_invalidate_msg(tdata);
1584
1585 /* Increment CSeq */
1586 cseq = ++regc->cseq_hdr->cseq;
1587 cseq_hdr = (pjsip_cseq_hdr*)
1588 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
1589 cseq_hdr->cseq = cseq;
1590
1591 /* Find Expires header */
1592 expires_hdr = (pjsip_expires_hdr*)
1593 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_EXPIRES, NULL);
1594
1595 /* Bind to transport selector */
1596 pjsip_tx_data_set_transport(tdata, &regc->tp_sel);
1597
1598 regc->has_tsx = PJ_TRUE;
1599
1600 /* Set current operation based on the value of Expires header */
1601 if (expires_hdr && expires_hdr->ivalue==0)
1602 regc->current_op = REGC_UNREGISTERING;
1603 else
1604 regc->current_op = REGC_REGISTERING;
1605
1606 if (expires_hdr && expires_hdr->ivalue)
1607 regc->expires_requested = expires_hdr->ivalue;
1608
1609 /* Prevent deletion of tdata, e.g: when something wrong in sending,
1610 * we need tdata to retrieve the transport.
1611 */
1612 pjsip_tx_data_add_ref(tdata);
1613
1614 /* If via_addr is set, use this address for the Via header. */
1615 if (regc->via_addr.host.slen > 0) {
1616 tdata->via_addr = regc->via_addr;
1617 tdata->via_tp = regc->via_tp;
1618 }
1619
1620 /* Need to unlock the regc temporarily while sending the message to
1621 * prevent deadlock (https://github.com/pjsip/pjproject/issues/1247).
1622 * It should be safe to do this since the regc's refcount has been
1623 * incremented.
1624 */
1625 pj_lock_release(regc->lock);
1626
1627 /* Now send the message */
1628 status = pjsip_endpt_send_request(regc->endpt, tdata, REGC_TSX_TIMEOUT,
1629 regc, &regc_tsx_callback);
1630
1631 /* Reacquire the lock */
1632 pj_lock_acquire(regc->lock);
1633
1634 if (status!=PJ_SUCCESS) {
1635 /* On failure, regc_tsx_callback() may not be called, so we need
1636 * to reset regc->has_tsx here (see also ticket #1936).
1637 * But note that we are releasing the lock when sending the request
1638 * above, so there can be a race with another registration send.
1639 */
1640 if (cseq == regc->cseq_hdr->cseq) {
1641 regc->has_tsx = PJ_FALSE;
1642 }
1643
1644 PJ_PERROR(4,(THIS_FILE, status, "Error sending request"));
1645 }
1646
1647 /* Get last transport used and add reference to it */
1648 //if (tdata->tp_info.transport != regc->last_transport &&
1649 // status==PJ_SUCCESS)
1650 //{
1651 // if (regc->last_transport) {
1652 // pjsip_transport_dec_ref(regc->last_transport);
1653 // regc->last_transport = NULL;
1654 // }
1655
1656 // if (tdata->tp_info.transport) {
1657 // regc->last_transport = tdata->tp_info.transport;
1658 // pjsip_transport_add_ref(regc->last_transport);
1659 // }
1660 //}
1661 // Update: don't add_ref() or use the transport info from tdata other than
1662 // for informational purpose (e.g: comparing the pointers to check
1663 // if a disconnected transport is the registration transport), see
1664 // pjsip_tx_data.tp_info docs about its valid period.
1665 // Note that the send operation may be async and transport may
1666 // have been destroyed here.
1667 regc->info_transport = status==PJ_SUCCESS? tdata->tp_info.transport :
1668 NULL;
1669
1670 /* Release tdata */
1671 pjsip_tx_data_dec_ref(tdata);
1672
1673 pj_lock_release(regc->lock);
1674
1675 /* Delete the record if user destroy regc during the callback. */
1676 pjsip_regc_dec_ref(regc);
1677
1678 return status;
1679 }
1680
1681 PJ_DEF(pj_status_t) pjsip_regc_set_auth_sess( pjsip_regc *regc,
1682 pjsip_auth_clt_sess *session )
1683 {
1684 PJ_ASSERT_RETURN(regc, PJ_EINVAL);
1685 return pjsip_auth_clt_set_parent(&regc->auth_sess, session);
1686 }
1687
1688
1689 PJ_DEF(pjsip_auth_clt_sess*) pjsip_regc_get_auth_sess( pjsip_regc *regc )
1690 {
1691 PJ_ASSERT_RETURN(regc, NULL);
1692 return &regc->auth_sess;
1693 }