Debian Codegraph @

Xpjproject/pjsip/src/pjsip-simple/publishc.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-simple/publish.h>
20 #include <pjsip/sip_auth.h>
21 #include <pjsip/sip_endpoint.h>
22 #include <pjsip/sip_errno.h>
23 #include <pjsip/sip_event.h>
24 #include <pjsip/sip_msg.h>
25 #include <pjsip/sip_transaction.h>
26 #include <pjsip/sip_uri.h>
27 #include <pjsip/sip_util.h>
28 #include <pj/assert.h>
29 #include <pj/guid.h>
30 #include <pj/log.h>
31 #include <pj/os.h>
32 #include <pj/pool.h>
33 #include <pj/rand.h>
34 #include <pj/string.h>
35 #include <pj/timer.h>
36
37
38 #define REFRESH_TIMER 1
39 #define DELAY_BEFORE_REFRESH PJSIP_PUBLISHC_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 "publishc.c"
46
47
48 /* Let's define this enum, so that it'll trigger compilation error
49 * when somebody define the same enum in sip_msg.h
50 */
51 enum
52 {
53 PJSIP_PUBLISH_METHOD = PJSIP_OTHER_METHOD,
54 };
55
56 const pjsip_method pjsip_publish_method =
57 {
58 (pjsip_method_e)PJSIP_PUBLISH_METHOD,
59 { "PUBLISH", 7 }
60 };
61
62
63 /**
64 * Pending request list.
65 */
66 typedef struct pending_publish
67 {
68 PJ_DECL_LIST_MEMBER(struct pending_publish);
69 pjsip_tx_data *tdata;
70 } pending_publish;
71
72
73 /**
74 * SIP client publication structure.
75 */
76 struct pjsip_publishc
77 {
78 pj_pool_t *pool;
79 pjsip_endpoint *endpt;
80 pj_bool_t _delete_flag;
81 int pending_tsx;
82 pj_bool_t in_callback;
83 pj_mutex_t *mutex;
84
85 pjsip_publishc_opt opt;
86 void *token;
87 pjsip_publishc_cb *cb;
88
89 pj_str_t event;
90 pj_str_t str_target_uri;
91 pjsip_uri *target_uri;
92 pjsip_cid_hdr *cid_hdr;
93 pjsip_cseq_hdr *cseq_hdr;
94 pj_str_t from_uri;
95 pjsip_from_hdr *from_hdr;
96 pjsip_to_hdr *to_hdr;
97 pj_str_t etag;
98 pjsip_expires_hdr *expires_hdr;
99 pj_uint32_t expires;
100 pjsip_route_hdr route_set;
101 pjsip_hdr usr_hdr;
102 pjsip_host_port via_addr;
103 const void *via_tp;
104
105 /* Authorization sessions. */
106 pjsip_auth_clt_sess auth_sess;
107
108 /* Auto refresh publication. */
109 pj_bool_t auto_refresh;
110 pj_time_val last_refresh;
111 pj_time_val next_refresh;
112 pj_timer_entry timer;
113
114 /* Pending PUBLISH request */
115 pending_publish pending_reqs;
116 pending_publish pending_reqs_empty;
117 };
118
119
120 static pj_status_t pubc_async_auth_send_impl(
121 pjsip_auth_clt_sess *auth_sess,
122 void *user_data,
123 pjsip_tx_data *tdata);
124 static void pubc_async_auth_abandon_impl(
125 pjsip_auth_clt_sess *auth_sess,
126 void *user_data);
127
128 PJ_DEF(void) pjsip_publishc_opt_default(pjsip_publishc_opt *opt)
129 {
130 pj_bzero(opt, sizeof(*opt));
131 opt->queue_request = PJSIP_PUBLISHC_QUEUE_REQUEST;
132 }
133
134
135 /*
136 * Initialize client publication module.
137 */
138 PJ_DEF(pj_status_t) pjsip_publishc_init_module(pjsip_endpoint *endpt)
139 {
140 /* Note:
141 Commented out the capability registration below, since it's
142 wrong to include PUBLISH in Allow header of INVITE requests/
143 responses.
144
145 13.2.1 Creating the Initial INVITE
146 An Allow header field (Section 20.5) SHOULD be present in the
147 INVITE. It indicates what methods can be invoked within a dialog
148
149 20.5 Allow
150 The Allow header field lists the set of methods supported by the
151 UA generating the message.
152
153 While the semantic of Allow header in non-dialog requests is unclear,
154 it's probably best not to include PUBLISH in Allow header for now
155 until we can find out how to customize the inclusion of methods in
156 Allow header for in-dialog vs out-dialog requests.
157
158 return pjsip_endpt_add_capability( endpt, NULL, PJSIP_H_ALLOW, NULL,
159 1, &pjsip_publish_method.name);
160 */
161 PJ_UNUSED_ARG(endpt);
162 return PJ_SUCCESS;
163 }
164
165
166 PJ_DEF(pj_status_t) pjsip_publishc_create( pjsip_endpoint *endpt,
167 const pjsip_publishc_opt *opt,
168 void *token,
169 pjsip_publishc_cb *cb,
170 pjsip_publishc **p_pubc)
171 {
172 pj_pool_t *pool;
173 pjsip_publishc *pubc;
174 pjsip_publishc_opt default_opt;
175 pj_status_t status;
176
177 /* Verify arguments. */
178 PJ_ASSERT_RETURN(endpt && cb && p_pubc, PJ_EINVAL);
179
180 pool = pjsip_endpt_create_pool(endpt, "pubc%p", 1024, 1024);
181 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
182
183 pubc = PJ_POOL_ZALLOC_T(pool, pjsip_publishc);
184
185 pubc->pool = pool;
186 pubc->endpt = endpt;
187 pubc->token = token;
188 pubc->cb = cb;
189 pubc->expires = PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED;
190
191 if (!opt) {
192 pjsip_publishc_opt_default(&default_opt);
193 opt = &default_opt;
194 }
195 pj_memcpy(&pubc->opt, opt, sizeof(*opt));
196 pj_list_init(&pubc->pending_reqs);
197 pj_list_init(&pubc->pending_reqs_empty);
198
199 status = pj_mutex_create_recursive(pubc->pool, "pubc%p", &pubc->mutex);
200 if (status != PJ_SUCCESS) {
201 pj_pool_release(pool);
202 return status;
203 }
204
205 status = pjsip_auth_clt_init(&pubc->auth_sess, endpt, pubc->pool, 0);
206 if (status != PJ_SUCCESS) {
207 pj_mutex_destroy(pubc->mutex);
208 pj_pool_release(pool);
209 return status;
210 }
211
212 pj_list_init(&pubc->route_set);
213 pj_list_init(&pubc->usr_hdr);
214
215 /* Done */
216 *p_pubc = pubc;
217 return PJ_SUCCESS;
218 }
219
220
221 PJ_DEF(pj_status_t) pjsip_publishc_destroy(pjsip_publishc *pubc)
222 {
223 PJ_ASSERT_RETURN(pubc, PJ_EINVAL);
224
225 if (pubc->pending_tsx || pubc->in_callback) {
226 pubc->_delete_flag = 1;
227 pubc->cb = NULL;
228 } else {
229 /* Cancel existing timer, if any */
230 if (pubc->timer.id != 0) {
231 pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer);
232 pubc->timer.id = 0;
233 }
234
235 if (pubc->mutex)
236 pj_mutex_destroy(pubc->mutex);
237
238 pjsip_auth_clt_deinit(&pubc->auth_sess);
239 pjsip_endpt_release_pool(pubc->endpt, pubc->pool);
240 }
241
242 return PJ_SUCCESS;
243 }
244
245
246 PJ_DEF(pj_pool_t*) pjsip_publishc_get_pool(pjsip_publishc *pubc)
247 {
248 return pubc->pool;
249 }
250
251 static void set_expires( pjsip_publishc *pubc, pj_uint32_t expires)
252 {
253 if (expires != pubc->expires &&
254 expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED)
255 {
256 pubc->expires_hdr = pjsip_expires_hdr_create(pubc->pool, expires);
257 } else {
258 pubc->expires_hdr = NULL;
259 }
260 }
261
262
263 PJ_DEF(pj_status_t) pjsip_publishc_init(pjsip_publishc *pubc,
264 const pj_str_t *event,
265 const pj_str_t *target_uri,
266 const pj_str_t *from_uri,
267 const pj_str_t *to_uri,
268 pj_uint32_t expires)
269 {
270 pj_str_t tmp;
271
272 PJ_ASSERT_RETURN(pubc && event && target_uri && from_uri && to_uri &&
273 expires, PJ_EINVAL);
274
275 /* Copy event type */
276 pj_strdup_with_null(pubc->pool, &pubc->event, event);
277
278 /* Copy server URL. */
279 pj_strdup_with_null(pubc->pool, &pubc->str_target_uri, target_uri);
280
281 /* Set server URL. */
282 tmp = pubc->str_target_uri;
283 pubc->target_uri = pjsip_parse_uri( pubc->pool, tmp.ptr, tmp.slen, 0);
284 if (pubc->target_uri == NULL) {
285 return PJSIP_EINVALIDURI;
286 }
287
288 /* Set "From" header. */
289 pj_strdup_with_null(pubc->pool, &pubc->from_uri, from_uri);
290 tmp = pubc->from_uri;
291 pubc->from_hdr = pjsip_from_hdr_create(pubc->pool);
292 pubc->from_hdr->uri = pjsip_parse_uri(pubc->pool, tmp.ptr, tmp.slen,
293 PJSIP_PARSE_URI_AS_NAMEADDR);
294 if (!pubc->from_hdr->uri) {
295 return PJSIP_EINVALIDURI;
296 }
297
298 /* Set "To" header. */
299 pj_strdup_with_null(pubc->pool, &tmp, to_uri);
300 pubc->to_hdr = pjsip_to_hdr_create(pubc->pool);
301 pubc->to_hdr->uri = pjsip_parse_uri(pubc->pool, tmp.ptr, tmp.slen,
302 PJSIP_PARSE_URI_AS_NAMEADDR);
303 if (!pubc->to_hdr->uri) {
304 return PJSIP_EINVALIDURI;
305 }
306
307
308 /* Set "Expires" header, if required. */
309 set_expires( pubc, expires);
310
311 /* Set "Call-ID" header. */
312 pubc->cid_hdr = pjsip_cid_hdr_create(pubc->pool);
313 pj_create_unique_string(pubc->pool, &pubc->cid_hdr->id);
314
315 /* Set "CSeq" header. */
316 pubc->cseq_hdr = pjsip_cseq_hdr_create(pubc->pool);
317 pubc->cseq_hdr->cseq = pj_rand() % 0xFFFF;
318 pjsip_method_set( &pubc->cseq_hdr->method, PJSIP_REGISTER_METHOD);
319
320 /* Done. */
321 return PJ_SUCCESS;
322 }
323
324 PJ_DEF(pj_status_t) pjsip_publishc_set_credentials( pjsip_publishc *pubc,
325 int count,
326 const pjsip_cred_info cred[] )
327 {
328 PJ_ASSERT_RETURN(pubc && count && cred, PJ_EINVAL);
329 return pjsip_auth_clt_set_credentials(&pubc->auth_sess, count, cred);
330 }
331
332 PJ_DEF(pj_status_t) pjsip_publishc_set_auth_sess(
333 pjsip_publishc *pubc,
334 pjsip_auth_clt_sess *session)
335 {
336 PJ_ASSERT_RETURN(pubc, PJ_EINVAL);
337 return pjsip_auth_clt_set_parent(&pubc->auth_sess, session);
338 }
339
340 PJ_DEF(pjsip_auth_clt_sess*) pjsip_publishc_get_auth_sess(
341 pjsip_publishc *pubc)
342 {
343 PJ_ASSERT_RETURN(pubc, NULL);
344 return &pubc->auth_sess;
345 }
346
347 PJ_DEF(pj_status_t) pjsip_publishc_set_route_set( pjsip_publishc *pubc,
348 const pjsip_route_hdr *route_set)
349 {
350 const pjsip_route_hdr *chdr;
351
352 PJ_ASSERT_RETURN(pubc && route_set, PJ_EINVAL);
353
354 pj_list_init(&pubc->route_set);
355
356 chdr = route_set->next;
357 while (chdr != route_set) {
358 pj_list_push_back(&pubc->route_set, pjsip_hdr_clone(pubc->pool, chdr));
359 chdr = chdr->next;
360 }
361
362 return PJ_SUCCESS;
363 }
364
365 PJ_DEF(pj_status_t) pjsip_publishc_set_headers( pjsip_publishc *pubc,
366 const pjsip_hdr *hdr_list)
367 {
368 const pjsip_hdr *h;
369
370 PJ_ASSERT_RETURN(pubc && hdr_list, PJ_EINVAL);
371
372 pj_list_init(&pubc->usr_hdr);
373 h = hdr_list->next;
374 while (h != hdr_list) {
375 pj_list_push_back(&pubc->usr_hdr, pjsip_hdr_clone(pubc->pool, h));
376 h = h->next;
377 }
378
379 return PJ_SUCCESS;
380 }
381
382 PJ_DEF(pj_status_t) pjsip_publishc_set_via_sent_by(pjsip_publishc *pubc,
383 pjsip_host_port *via_addr,
384 pjsip_transport *via_tp)
385 {
386 PJ_ASSERT_RETURN(pubc, PJ_EINVAL);
387
388 if (!via_addr)
389 pj_bzero(&pubc->via_addr, sizeof(pubc->via_addr));
390 else {
391 if (pj_strcmp(&pubc->via_addr.host, &via_addr->host))
392 pj_strdup(pubc->pool, &pubc->via_addr.host, &via_addr->host);
393 pubc->via_addr.port = via_addr->port;
394 }
395 pubc->via_tp = via_tp;
396
397 return PJ_SUCCESS;
398 }
399
400 static pj_status_t create_request(pjsip_publishc *pubc,
401 pjsip_tx_data **p_tdata)
402 {
403 const pj_str_t STR_EVENT = { "Event", 5 };
404 pj_status_t status;
405 pjsip_generic_string_hdr *hdr;
406 pjsip_tx_data *tdata;
407
408 PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL);
409
410 /* Create the request. */
411 status = pjsip_endpt_create_request_from_hdr( pubc->endpt,
412 &pjsip_publish_method,
413 pubc->target_uri,
414 pubc->from_hdr,
415 pubc->to_hdr,
416 NULL,
417 pubc->cid_hdr,
418 pubc->cseq_hdr->cseq,
419 NULL,
420 &tdata);
421 if (status != PJ_SUCCESS)
422 return status;
423
424 /* Add cached authorization headers. */
425 pjsip_auth_clt_init_req( &pubc->auth_sess, tdata );
426
427 /* Add Route headers from route set, ideally after Via header */
428 if (!pj_list_empty(&pubc->route_set)) {
429 pjsip_hdr *route_pos;
430 const pjsip_route_hdr *route;
431
432 route_pos = (pjsip_hdr*)
433 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
434 if (!route_pos)
435 route_pos = &tdata->msg->hdr;
436
437 route = pubc->route_set.next;
438 while (route != &pubc->route_set) {
439 pjsip_hdr *new_hdr = (pjsip_hdr*)
440 pjsip_hdr_shallow_clone(tdata->pool, route);
441 pj_list_insert_after(route_pos, new_hdr);
442 route_pos = new_hdr;
443 route = route->next;
444 }
445 }
446
447 /* Add Event header */
448 hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_EVENT,
449 &pubc->event);
450 if (hdr)
451 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
452
453
454 /* Add SIP-If-Match if we have etag */
455 if (pubc->etag.slen) {
456 const pj_str_t STR_HNAME = { "SIP-If-Match", 12 };
457
458 hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_HNAME,
459 &pubc->etag);
460 if (hdr)
461 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
462 }
463
464 /* Add user headers */
465 if (!pj_list_empty(&pubc->usr_hdr)) {
466 const pjsip_hdr *uhdr;
467
468 uhdr = pubc->usr_hdr.next;
469 while (uhdr != &pubc->usr_hdr) {
470 pjsip_hdr *new_hdr = (pjsip_hdr*)
471 pjsip_hdr_shallow_clone(tdata->pool, uhdr);
472 pjsip_msg_add_hdr(tdata->msg, new_hdr);
473 uhdr = uhdr->next;
474 }
475 }
476
477
478 /* Done. */
479 *p_tdata = tdata;
480 return PJ_SUCCESS;
481 }
482
483
484 PJ_DEF(pj_status_t) pjsip_publishc_publish(pjsip_publishc *pubc,
485 pj_bool_t auto_refresh,
486 pjsip_tx_data **p_tdata)
487 {
488 pj_status_t status;
489 pjsip_tx_data *tdata;
490
491 PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL);
492
493 status = create_request(pubc, &tdata);
494 if (status != PJ_SUCCESS)
495 return status;
496
497 /* Add Expires header */
498 if (pubc->expires_hdr) {
499 pjsip_hdr *dup;
500
501 dup = (pjsip_hdr*)
502 pjsip_hdr_shallow_clone(tdata->pool, pubc->expires_hdr);
503 if (dup)
504 pjsip_msg_add_hdr(tdata->msg, dup);
505 }
506
507 /* Cancel existing timer */
508 if (pubc->timer.id != 0) {
509 pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer);
510 pubc->timer.id = 0;
511 }
512
513 pubc->auto_refresh = auto_refresh;
514
515 /* Done */
516 *p_tdata = tdata;
517 return PJ_SUCCESS;
518 }
519
520
521 PJ_DEF(pj_status_t) pjsip_publishc_unpublish(pjsip_publishc *pubc,
522 pjsip_tx_data **p_tdata)
523 {
524 pjsip_tx_data *tdata;
525 pjsip_msg *msg;
526 pjsip_expires_hdr *expires;
527 pj_status_t status;
528
529 PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL);
530
531 if (pubc->timer.id != 0) {
532 pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer);
533 pubc->timer.id = 0;
534 }
535
536 status = create_request(pubc, &tdata);
537 if (status != PJ_SUCCESS)
538 return status;
539
540 msg = tdata->msg;
541
542 /* Add Expires:0 header */
543 expires = pjsip_expires_hdr_create(tdata->pool, 0);
544 pjsip_msg_add_hdr( msg, (pjsip_hdr*)expires);
545
546 *p_tdata = tdata;
547 return PJ_SUCCESS;
548 }
549
550
551 PJ_DEF(pj_status_t) pjsip_publishc_update_expires( pjsip_publishc *pubc,
552 pj_uint32_t expires )
553 {
554 PJ_ASSERT_RETURN(pubc, PJ_EINVAL);
555 set_expires( pubc, expires );
556 return PJ_SUCCESS;
557 }
558
559
560 static void call_callback(pjsip_publishc *pubc, pj_status_t status,
561 int st_code, const pj_str_t *reason,
562 pjsip_rx_data *rdata, pj_uint32_t expiration)
563 {
564 struct pjsip_publishc_cbparam cbparam;
565
566
567 cbparam.pubc = pubc;
568 cbparam.token = pubc->token;
569 cbparam.status = status;
570 cbparam.code = st_code;
571 cbparam.reason = *reason;
572 cbparam.rdata = rdata;
573 cbparam.expiration = expiration;
574
575 (*pubc->cb)(&cbparam);
576 }
577
578 static void pubc_refresh_timer_cb( pj_timer_heap_t *timer_heap,
579 struct pj_timer_entry *entry)
580 {
581 pjsip_publishc *pubc = (pjsip_publishc*) entry->user_data;
582 pjsip_tx_data *tdata;
583 pj_status_t status;
584
585 PJ_UNUSED_ARG(timer_heap);
586
587 entry->id = 0;
588 status = pjsip_publishc_publish(pubc, 1, &tdata);
589 if (status != PJ_SUCCESS) {
590 char errmsg[PJ_ERR_MSG_SIZE];
591 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg));
592 call_callback(pubc, status, 400, &reason, NULL,
593 PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED);
594 return;
595 }
596
597 status = pjsip_publishc_send(pubc, tdata);
598 /* No need to call callback as it should have been called */
599 }
600
601 /* Async auth send callback: resend the authenticated PUBLISH request.
602 * user_data points to pjsip_publishc*. No lock is held by the caller
603 * (publishc does not use dialog-based locking).
604 */
605 static pj_status_t pubc_async_auth_send_impl(
606 pjsip_auth_clt_sess *auth_sess,
607 void *user_data,
608 pjsip_tx_data *tdata)
609 {
610 pjsip_publishc *pubc = (pjsip_publishc *)user_data;
611 PJ_UNUSED_ARG(auth_sess);
612 return pjsip_publishc_send(pubc, tdata);
613 }
614
615 /* Async auth abandon callback: notify the app that PUBLISH auth failed.
616 * user_data points to pjsip_publishc*. No lock is held by the caller.
617 */
618 static void pubc_async_auth_abandon_impl(pjsip_auth_clt_sess *auth_sess,
619 void *user_data)
620 {
621 pjsip_publishc *pubc = (pjsip_publishc *)user_data;
622 pj_str_t reason = { "Authentication abandoned", 24 };
623
624 PJ_UNUSED_ARG(auth_sess);
625
626 call_callback(pubc, PJ_ECANCELLED, PJSIP_SC_UNAUTHORIZED, &reason,
627 NULL, PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED);
628 }
629
630 static void tsx_callback(void *token, pjsip_event *event)
631 {
632 pj_status_t status;
633 pjsip_publishc *pubc = (pjsip_publishc*) token;
634 pjsip_transaction *tsx = event->body.tsx_state.tsx;
635
636 /* Decrement pending transaction counter. */
637 pj_assert(pubc->pending_tsx > 0);
638 --pubc->pending_tsx;
639
640 /* Mark that we're in callback to prevent deletion (#1164) */
641 ++pubc->in_callback;
642
643 /* If publication data has been deleted by user then remove publication
644 * data from transaction's callback, and don't call callback.
645 */
646 if (pubc->_delete_flag) {
647
648 /* Nothing to do */
649 ;
650
651 } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED ||
652 tsx->status_code == PJSIP_SC_UNAUTHORIZED)
653 {
654 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata;
655 pjsip_tx_data *tdata;
656 pjsip_auth_clt_async_on_chal_param chal_param = { 0 };
657
658 /* Per-challenge token allocated from tsx->pool, kept alive
659 * by the grp_lock ref until consumed (send or abandon).
660 * No lock is held here — publishc uses endpoint-level
661 * send_request, not dialog-based locking.
662 */
663 {
664 pjsip_auth_clt_async_impl_token *auth_token;
665 auth_token = PJ_POOL_ZALLOC_T(tsx->pool,
666 pjsip_auth_clt_async_impl_token);
667 auth_token->user_data = pubc;
668 auth_token->send_impl = &pubc_async_auth_send_impl;
669 auth_token->abandon_impl = &pubc_async_auth_abandon_impl;
670 auth_token->grp_lock = tsx->grp_lock;
671 pj_grp_lock_add_ref(tsx->grp_lock);
672
673 pj_bzero(&chal_param, sizeof(chal_param));
674 chal_param.rdata = rdata;
675 chal_param.tdata = tsx->last_tx;
676 status = pjsip_auth_clt_async_impl_on_challenge(
677 &pubc->auth_sess,
678 auth_token, &chal_param);
679 if (status != PJ_SUCCESS)
680 pj_grp_lock_dec_ref(tsx->grp_lock);
681 }
682 if (status != PJ_SUCCESS) {
683 status = pjsip_auth_clt_reinit_req(&pubc->auth_sess, rdata,
684 tsx->last_tx, &tdata);
685 if (status != PJ_SUCCESS) {
686 call_callback(pubc, status, tsx->status_code,
687 &rdata->msg_info.msg->line.status.reason,
688 rdata, PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED);
689 } else {
690 status = pjsip_publishc_send(pubc, tdata);
691 }
692 }
693
694 } else {
695 pjsip_rx_data *rdata;
696 pj_uint32_t expiration = PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED;
697
698 if (tsx->status_code/100 == 2) {
699 pjsip_msg *msg;
700 pjsip_expires_hdr *expires;
701 pjsip_generic_string_hdr *etag_hdr;
702 const pj_str_t STR_ETAG = { "SIP-ETag", 8 };
703
704 rdata = event->body.tsx_state.src.rdata;
705 msg = rdata->msg_info.msg;
706
707 /* Save ETag value */
708 etag_hdr = (pjsip_generic_string_hdr*)
709 pjsip_msg_find_hdr_by_name(msg, &STR_ETAG, NULL);
710 if (etag_hdr) {
711 pj_strdup(pubc->pool, &pubc->etag, &etag_hdr->hvalue);
712 } else {
713 pubc->etag.slen = 0;
714 }
715
716 /* Update expires value */
717 expires = (pjsip_expires_hdr*)
718 pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
719
720 if (pubc->auto_refresh && expires)
721 expiration = expires->ivalue;
722
723 if (pubc->auto_refresh && expiration!=0 &&
724 expiration!=PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED)
725 {
726 const pj_time_val min_delay = {MIN_REFRESH_MSEC / 1000,
727 MIN_REFRESH_MSEC % 1000};
728 pj_time_val delay = {0, 0};
729
730 /* Cancel existing timer, if any */
731 if (pubc->timer.id != 0) {
732 pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer);
733 pubc->timer.id = 0;
734 }
735
736 /* prevent underflow in case the remote answers with an unexpectedly low expiry value */
737 if (expiration > DELAY_BEFORE_REFRESH) {
738 delay.sec = expiration - DELAY_BEFORE_REFRESH;
739 }
740 if (pubc->expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED &&
741 delay.sec > (pj_int32_t)pubc->expires)
742 {
743 delay.sec = pubc->expires;
744 }
745 if (delay.sec < DELAY_BEFORE_REFRESH) {
746 if (expiration <= DELAY_BEFORE_REFRESH) {
747 /* Very small expiration: refresh before expiry. */
748 delay.sec = (expiration > 1) ? (long)expiration - 1 : 0;
749 delay.msec = (expiration > 1) ? 0 : 500;
750 } else {
751 delay.sec = DELAY_BEFORE_REFRESH;
752 }
753 }
754
755 /* make sure we don't go below the minimum */
756 if (PJ_TIME_VAL_LT(delay, min_delay)) {
757 delay = min_delay;
758 }
759 pubc->timer.cb = &pubc_refresh_timer_cb;
760 pubc->timer.id = REFRESH_TIMER;
761 pubc->timer.user_data = pubc;
762 pjsip_endpt_schedule_timer( pubc->endpt, &pubc->timer, &delay);
763 pj_gettimeofday(&pubc->last_refresh);
764 pubc->next_refresh = pubc->last_refresh;
765 PJ_TIME_VAL_ADD(pubc->next_refresh, delay);
766 }
767
768 } else {
769 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ?
770 event->body.tsx_state.src.rdata : NULL;
771 }
772
773
774 /* Call callback. */
775 // if (expiration == 0xFFFF) expiration = -1;
776
777 /* Temporarily increment pending_tsx to prevent callback from
778 * destroying pubc.
779 */
780 ++pubc->pending_tsx;
781
782 call_callback(pubc, PJ_SUCCESS, tsx->status_code,
783 (rdata ? &rdata->msg_info.msg->line.status.reason
784 : pjsip_get_status_text(tsx->status_code)),
785 rdata, expiration);
786
787 --pubc->pending_tsx;
788
789 /* If we have pending request(s), send them now */
790 pj_mutex_lock(pubc->mutex);
791 while (!pj_list_empty(&pubc->pending_reqs)) {
792 pending_publish *pp = pubc->pending_reqs.next;
793 pjsip_tx_data *tdata = pp->tdata;
794
795 /* Remove the request from pending request list,
796 * and keep the unused entry into pending_reqs_empty pool.
797 */
798 pj_list_erase(pp);
799 pj_list_push_back(&pubc->pending_reqs_empty, pp);
800
801 /* Add SIP-If-Match if we have etag and the request doesn't have
802 * one (https://github.com/pjsip/pjproject/issues/996)
803 */
804 if (pubc->etag.slen) {
805 const pj_str_t STR_HNAME = { "SIP-If-Match", 12 };
806 pjsip_generic_string_hdr *sim_hdr;
807
808 sim_hdr = (pjsip_generic_string_hdr*)
809 pjsip_msg_find_hdr_by_name(tdata->msg, &STR_HNAME, NULL);
810 if (!sim_hdr) {
811 /* Create the header */
812 sim_hdr = pjsip_generic_string_hdr_create(tdata->pool,
813 &STR_HNAME,
814 &pubc->etag);
815 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sim_hdr);
816
817 } else {
818 /* Update */
819 if (pj_strcmp(&pubc->etag, &sim_hdr->hvalue))
820 pj_strdup(tdata->pool, &sim_hdr->hvalue, &pubc->etag);
821 }
822 }
823
824 status = pjsip_publishc_send(pubc, tdata);
825 if (status == PJ_EPENDING) {
826 pj_assert(!"Not expected");
827 pjsip_tx_data_dec_ref(tdata);
828 } else if (status == PJ_SUCCESS) {
829 break;
830 }
831 }
832 pj_mutex_unlock(pubc->mutex);
833 }
834
835 /* No longer in callback. */
836 --pubc->in_callback;
837
838 /* Delete the record if user destroy pubc during the callback. */
839 if (pubc->_delete_flag && pubc->pending_tsx==0) {
840 pjsip_publishc_destroy(pubc);
841 }
842 }
843
844
845 PJ_DEF(pj_status_t) pjsip_publishc_send(pjsip_publishc *pubc,
846 pjsip_tx_data *tdata)
847 {
848 pj_status_t status;
849 pjsip_cseq_hdr *cseq_hdr;
850 pj_uint32_t cseq;
851
852 PJ_ASSERT_RETURN(pubc && tdata, PJ_EINVAL);
853
854 /* Make sure we don't have pending transaction. */
855 pj_mutex_lock(pubc->mutex);
856 if (pubc->pending_tsx) {
857 if (pubc->opt.queue_request) {
858 pending_publish *pp = NULL;
859 if (pj_list_empty(&pubc->pending_reqs_empty)) {
860 pp = PJ_POOL_ZALLOC_T(pubc->pool, pending_publish);
861 } else {
862 pp = pubc->pending_reqs_empty.next;
863 pj_list_erase(pp);
864 }
865 pp->tdata = tdata;
866 pj_list_push_back(&pubc->pending_reqs, pp);
867 pj_mutex_unlock(pubc->mutex);
868 PJ_LOG(4,(THIS_FILE, "Request is queued, pubc has another "
869 "transaction pending"));
870 return PJ_EPENDING;
871 } else {
872 pjsip_tx_data_dec_ref(tdata);
873 pj_mutex_unlock(pubc->mutex);
874 PJ_LOG(4,(THIS_FILE, "Unable to send request, pubc has another "
875 "transaction pending"));
876 return PJ_EBUSY;
877 }
878 }
879
880 /* If via_addr is set, use this address for the Via header. */
881 if (pubc->via_addr.host.slen > 0) {
882 tdata->via_addr = pubc->via_addr;
883 tdata->via_tp = pubc->via_tp;
884 }
885
886 /* Invalidate message buffer. */
887 pjsip_tx_data_invalidate_msg(tdata);
888
889 /* Increment CSeq */
890 cseq = ++pubc->cseq_hdr->cseq;
891 cseq_hdr = (pjsip_cseq_hdr*)
892 pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
893 cseq_hdr->cseq = cseq;
894
895 /* Increment pending transaction first, since transaction callback
896 * may be called even before send_request() returns!
897 */
898 ++pubc->pending_tsx;
899
900 pj_mutex_unlock(pubc->mutex);
901
902 status = pjsip_endpt_send_request(pubc->endpt, tdata, -1, pubc,
903 &tsx_callback);
904 if (status!=PJ_SUCCESS) {
905 // no need to decrement, callback has been called and it should
906 // already decremented pending_tsx. Decrementing this here may
907 // cause accessing freed memory location.
908 //--pubc->pending_tsx;
909 PJ_PERROR(4,(THIS_FILE, status, "Error sending request"));
910 }
911
912 return status;
913 }
914