| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
#define MIN_REFRESH_MSEC 500 |
| 45 |
#define THIS_FILE "publishc.c" |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 106 |
pjsip_auth_clt_sess auth_sess; |
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
PJ_DEF(pj_status_t) pjsip_publishc_init_module(pjsip_endpoint *endpt) |
| 139 |
{ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 276 |
pj_strdup_with_null(pubc->pool, &pubc->event, event); |
| 277 |
|
| 278 |
|
| 279 |
pj_strdup_with_null(pubc->pool, &pubc->str_target_uri, target_uri); |
| 280 |
|
| 281 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 309 |
set_expires( pubc, expires); |
| 310 |
|
| 311 |
|
| 312 |
pubc->cid_hdr = pjsip_cid_hdr_create(pubc->pool); |
| 313 |
pj_create_unique_string(pubc->pool, &pubc->cid_hdr->id); |
| 314 |
|
| 315 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 425 |
pjsip_auth_clt_init_req( &pubc->auth_sess, tdata ); |
| 426 |
|
| 427 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 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 |
|
| 616 |
|
| 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 |
|
| 637 |
pj_assert(pubc->pending_tsx > 0); |
| 638 |
--pubc->pending_tsx; |
| 639 |
|
| 640 |
|
| 641 |
++pubc->in_callback; |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if (pubc->_delete_flag) { |
| 647 |
|
| 648 |
|
| 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 |
|
| 659 |
|
| 660 |
el |
| 661 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 731 |
if (pubc->timer.id != 0) { |
| 732 |
pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 733 |
pubc->timer.id = 0; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 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 |
|
| 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 |
|
| 796 |
|
| 797 |
|
| 798 |
pj_list_erase(pp); |
| 799 |
pj_list_push_back(&pubc->pending_reqs_empty, pp); |
| 800 |
|
| 801 |
|
| 802 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 836 |
--pubc->in_callback; |
| 837 |
|
| 838 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 887 |
pjsip_tx_data_invalidate_msg(tdata); |
| 888 |
|
| 889 |
|
| 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 |
|
| 896 |
|
| 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 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
PJ_PERROR(4,(THIS_FILE, status, "Error sending request")); |
| 910 |
} |
| 911 |
|
| 912 |
return status; |
| 913 |
} |
| 914 |
|