Changeset 141 for pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c
- Timestamp:
- Feb 7, 2006 6:48:01 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c
r65 r141 17 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 18 */ 19 #include <pjsip _mod_ua/sip_reg.h>19 #include <pjsip-ua/sip_regc.h> 20 20 #include <pjsip/sip_endpoint.h> 21 21 #include <pjsip/sip_parser.h> … … 25 25 #include <pjsip/sip_util.h> 26 26 #include <pjsip/sip_auth_msg.h> 27 #include <pjsip/sip_errno.h> 27 28 #include <pj/pool.h> 28 29 #include <pj/string.h> 29 30 #include <pj/guid.h> 30 31 #include <pj/log.h> 32 #include <pj/assert.h> 33 31 34 32 35 #define REFRESH_TIMER 1 … … 42 45 pjsip_endpoint *endpt; 43 46 pj_bool_t _delete_flag; 44 int pending_tsx;47 int pending_tsx; 45 48 46 49 void *token; 47 50 pjsip_regc_cb *cb; 48 51 49 pj_str_t str_srv_url;52 pj_str_t str_srv_url; 50 53 pjsip_uri *srv_url; 51 54 pjsip_cid_hdr *cid_hdr; … … 60 63 pj_uint32_t expires; 61 64 62 /* Credentials. */63 int cred_count;64 pjsip_cred_info *cred_info;65 66 65 /* Authorization sessions. */ 67 pjsip_auth_ session auth_sess_list;66 pjsip_auth_clt_sess auth_sess; 68 67 69 68 /* Auto refresh registration. */ … … 74 73 75 74 76 PJ_DEF(pjsip_regc*) pjsip_regc_create( pjsip_endpoint *endpt, void *token, 77 pjsip_regc_cb *cb) 75 PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token, 76 pjsip_regc_cb *cb, 77 pjsip_regc **p_regc) 78 78 { 79 79 pj_pool_t *pool; 80 80 pjsip_regc *regc; 81 82 if (cb == NULL) 83 return NULL; 81 pj_status_t status; 82 83 /* Verify arguments. */ 84 PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL); 84 85 85 86 pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024); 86 regc = pj_pool_calloc(pool, 1, sizeof(struct pjsip_regc)); 87 PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); 88 89 regc = pj_pool_zalloc(pool, sizeof(struct pjsip_regc)); 87 90 88 91 regc->pool = pool; … … 93 96 regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED; 94 97 95 pj_list_init(®c->auth_sess_list); 96 97 return regc; 98 } 99 100 101 PJ_DEF(void) pjsip_regc_destroy(pjsip_regc *regc) 98 status = pjsip_auth_clt_init(®c->auth_sess, endpt, regc->pool, 0); 99 if (status != PJ_SUCCESS) 100 return status; 101 102 /* Done */ 103 *p_regc = regc; 104 return PJ_SUCCESS; 105 } 106 107 108 PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc) 102 109 { 103 110 if (regc->pending_tsx) { … … 105 112 regc->cb = NULL; 106 113 } else { 107 pjsip_endpt_destroy_pool(regc->endpt, regc->pool); 108 } 114 pjsip_endpt_release_pool(regc->endpt, regc->pool); 115 } 116 117 return PJ_SUCCESS; 109 118 } 110 119 … … 118 127 { 119 128 if (expires != regc->expires) { 120 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool); 121 regc->expires_hdr->ivalue = expires; 129 regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires); 122 130 } else { 123 131 regc->expires_hdr = NULL; … … 137 145 for (i=0, s=regc->contact_buf; i<contact_cnt; ++i) { 138 146 if ((s-regc->contact_buf) + contact[i].slen + 2 > PJSIP_REGC_CONTACT_BUF_SIZE) { 139 return -1;147 return PJSIP_EURITOOLONG; 140 148 } 141 149 pj_memcpy(s, contact[i].ptr, contact[i].slen); … … 149 157 150 158 /* Set "Contact" header. */ 151 regc->contact_hdr = pjsip_generic_string_hdr_create( regc->pool, &contact_STR); 159 regc->contact_hdr = pjsip_generic_string_hdr_create(regc->pool, 160 &contact_STR, 161 NULL); 152 162 regc->contact_hdr->hvalue.ptr = regc->contact_buf; 153 163 regc->contact_hdr->hvalue.slen = (s - regc->contact_buf); 154 164 155 return 0;165 return PJ_SUCCESS; 156 166 } 157 167 … … 166 176 { 167 177 pj_str_t tmp; 178 pj_status_t status; 179 180 PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url && 181 contact_cnt && contact && expires, PJ_EINVAL); 168 182 169 183 /* Copy server URL. */ … … 174 188 regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0); 175 189 if (regc->srv_url == NULL) { 176 return -1;190 return PJSIP_EINVALIDURI; 177 191 } 178 192 … … 183 197 PJSIP_PARSE_URI_AS_NAMEADDR); 184 198 if (!regc->from_hdr->uri) { 185 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s", from_url->slen, from_url->ptr)); 186 return -1; 199 PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s", 200 from_url->slen, from_url->ptr)); 201 return PJSIP_EINVALIDURI; 187 202 } 188 203 … … 194 209 if (!regc->to_hdr->uri) { 195 210 PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s", to_url->slen, to_url->ptr)); 196 return -1;211 return PJSIP_EINVALIDURI; 197 212 } 198 213 199 214 200 215 /* Set "Contact" header. */ 201 if (set_contact( regc, contact_cnt, contact) != 0) 202 return -1; 216 status = set_contact( regc, contact_cnt, contact); 217 if (status != PJ_SUCCESS) 218 return status; 203 219 204 220 /* Set "Expires" header, if required. */ … … 219 235 220 236 /* Create "Expires" header used in unregistration. */ 221 regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool); 222 regc->unreg_expires_hdr->ivalue = 0; 237 regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool, 0); 223 238 224 239 /* Done. */ 225 return 0;240 return PJ_SUCCESS; 226 241 } 227 242 … … 230 245 const pjsip_cred_info cred[] ) 231 246 { 232 if (count > 0) { 233 regc->cred_info = pj_pool_alloc(regc->pool, count * sizeof(pjsip_cred_info)); 234 pj_memcpy(regc->cred_info, cred, count * sizeof(pjsip_cred_info)); 235 } 236 regc->cred_count = count; 237 return 0; 238 } 239 240 static pjsip_tx_data *create_request(pjsip_regc *regc) 241 { 247 PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL); 248 return pjsip_auth_clt_set_credentials(®c->auth_sess, count, cred); 249 } 250 251 static pj_status_t create_request(pjsip_regc *regc, 252 pjsip_tx_data **p_tdata) 253 { 254 pj_status_t status; 242 255 pjsip_tx_data *tdata; 256 257 PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); 258 259 /* Create the request. */ 260 status = pjsip_endpt_create_request_from_hdr( regc->endpt, 261 &pjsip_register_method, 262 regc->srv_url, 263 regc->from_hdr, 264 regc->to_hdr, 265 NULL, 266 regc->cid_hdr, 267 regc->cseq_hdr->cseq, 268 NULL, 269 &tdata); 270 if (status != PJ_SUCCESS) 271 return status; 272 273 /* Add cached authorization headers. */ 274 pjsip_auth_clt_init_req( ®c->auth_sess, tdata ); 275 276 /* Done. */ 277 *p_tdata = tdata; 278 return PJ_SUCCESS; 279 } 280 281 282 PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg, 283 pjsip_tx_data **p_tdata) 284 { 243 285 pjsip_msg *msg; 244 245 /* Create transmit data. */ 246 tdata = pjsip_endpt_create_tdata(regc->endpt); 247 if (!tdata) { 248 return NULL; 249 } 250 251 /* Create request message. */ 252 msg = pjsip_msg_create(tdata->pool, PJSIP_REQUEST_MSG); 253 tdata->msg = msg; 254 255 /* Initialize request line. */ 256 pjsip_method_set(&msg->line.req.method, PJSIP_REGISTER_METHOD); 257 msg->line.req.uri = regc->srv_url; 258 259 /* Add headers. */ 260 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->from_hdr); 261 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->to_hdr); 262 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->cid_hdr); 263 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->cseq_hdr); 264 265 /* Add cached authorization headers. */ 266 pjsip_auth_init_req( regc->pool, tdata, ®c->auth_sess_list, 267 regc->cred_count, regc->cred_info ); 268 269 /* Add reference counter to transmit data. */ 270 pjsip_tx_data_add_ref(tdata); 271 272 return tdata; 273 } 274 275 276 PJ_DEF(pjsip_tx_data*) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg) 277 { 278 pjsip_msg *msg; 286 pj_status_t status; 279 287 pjsip_tx_data *tdata; 280 288 281 tdata = create_request(regc); 282 if (!tdata) 283 return NULL; 284 289 status = create_request(regc, &tdata); 290 if (status != PJ_SUCCESS) 291 return status; 292 293 /* Add Contact header. */ 285 294 msg = tdata->msg; 286 295 pjsip_msg_add_hdr(msg, (pjsip_hdr*) regc->contact_hdr); … … 295 304 regc->auto_reg = autoreg; 296 305 297 return tdata; 298 } 299 300 301 PJ_DEF(pjsip_tx_data*) pjsip_regc_unregister(pjsip_regc *regc) 306 /* Done */ 307 *p_tdata = tdata; 308 return PJ_SUCCESS; 309 } 310 311 312 PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc, 313 pjsip_tx_data **p_tdata) 302 314 { 303 315 pjsip_tx_data *tdata; 304 316 pjsip_msg *msg; 317 pj_status_t status; 305 318 306 319 if (regc->timer.id != 0) { … … 309 322 } 310 323 311 tdata = create_request(regc);312 if ( !tdata)313 return NULL;324 status = create_request(regc, &tdata); 325 if (status != PJ_SUCCESS) 326 return status; 314 327 315 328 msg = tdata->msg; … … 317 330 pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr); 318 331 319 return tdata; 332 *p_tdata = tdata; 333 return PJ_SUCCESS; 320 334 } 321 335 … … 333 347 { 334 348 set_expires( regc, expires ); 335 return 0;349 return PJ_SUCCESS; 336 350 } 337 351 … … 364 378 pjsip_regc *regc = entry->user_data; 365 379 pjsip_tx_data *tdata; 380 pj_status_t status; 366 381 367 PJ_UNUSED_ARG(timer_heap) 382 PJ_UNUSED_ARG(timer_heap); 368 383 369 384 entry->id = 0; 370 tdata = pjsip_regc_register(regc, 1);371 if ( tdata) {385 status = pjsip_regc_register(regc, 1, &tdata); 386 if (status == PJ_SUCCESS) { 372 387 pjsip_regc_send(regc, tdata); 373 388 } else { 374 pj_str_t reason = pj_str("Unable to create txdata"); 389 char errmsg[PJ_ERR_MSG_SIZE]; 390 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); 375 391 call_callback(regc, -1, &reason, NULL, -1, 0, NULL); 376 392 } … … 379 395 static void tsx_callback(void *token, pjsip_event *event) 380 396 { 397 pj_status_t status; 381 398 pjsip_regc *regc = token; 382 pjsip_transaction *tsx = event-> obj.tsx;399 pjsip_transaction *tsx = event->body.tsx_state.tsx; 383 400 384 401 /* If registration data has been deleted by user then remove registration … … 391 408 tsx->status_code == PJSIP_SC_UNAUTHORIZED) 392 409 { 393 pjsip_rx_data *rdata = event-> src.rdata;410 pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; 394 411 pjsip_tx_data *tdata; 395 412 396 tdata = pjsip_auth_reinit_req( regc->endpt,397 regc->pool, ®c->auth_sess_list,398 regc->cred_count, regc->cred_info,399 tsx->last_tx, event->src.rdata);400 401 if ( tdata) {413 status = pjsip_auth_clt_reinit_req( ®c->auth_sess, 414 rdata, 415 tsx->last_tx, 416 &tdata); 417 418 if (status == PJ_SUCCESS) { 402 419 --regc->pending_tsx; 403 420 pjsip_regc_send(regc, tdata); 404 421 return; 405 422 } else { 406 call_callback(regc, tsx->status_code, &rdata->msg ->line.status.reason,423 call_callback(regc, tsx->status_code, &rdata->msg_info.msg->line.status.reason, 407 424 rdata, -1, 0, NULL); 408 425 --regc->pending_tsx; … … 420 437 pjsip_expires_hdr *expires; 421 438 422 rdata = event-> src.rdata;423 msg = rdata->msg ;439 rdata = event->body.tsx_state.src.rdata; 440 msg = rdata->msg_info.msg; 424 441 hdr = pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL); 425 442 while (hdr) { … … 460 477 461 478 } else { 462 rdata = (event->src_type==PJSIP_EVENT_RX_MSG) ? event->src.rdata : NULL; 479 rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? 480 event->body.tsx_state.src.rdata : NULL; 463 481 } 464 482 … … 467 485 if (expiration == 0xFFFF) expiration = -1; 468 486 call_callback(regc, tsx->status_code, 469 (rdata ? &rdata->msg ->line.status.reason487 (rdata ? &rdata->msg_info.msg->line.status.reason 470 488 : pjsip_get_status_text(tsx->status_code)), 471 489 rdata, expiration, … … 481 499 } 482 500 483 PJ_DEF( void) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata)484 { 485 int status;501 PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata) 502 { 503 pj_status_t status; 486 504 487 505 /* Make sure we don't have pending transaction. */ … … 490 508 call_callback(regc, -1, &reason, NULL, -1, 0, NULL); 491 509 pjsip_tx_data_dec_ref( tdata ); 492 return ;510 return PJ_EINVALIDOP; 493 511 } 494 512 … … 501 519 /* Send. */ 502 520 status = pjsip_endpt_send_request(regc->endpt, tdata, -1, regc, &tsx_callback); 503 if (status== 0)521 if (status==PJ_SUCCESS) 504 522 ++regc->pending_tsx; 505 523 else { 506 pj_str_t reason = pj_str("Unable to send request."); 524 char errmsg[PJ_ERR_MSG_SIZE]; 525 pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); 507 526 call_callback(regc, status, &reason, NULL, -1, 0, NULL); 508 527 } 509 } 510 511 528 529 return status; 530 } 531 532
Note: See TracChangeset
for help on using the changeset viewer.