Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> |
| 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 | */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 19 | #include <pjsip-ua/sip_regc.h> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 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> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 27 | #include <pjsip/sip_errno.h> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 28 | #include <pj/assert.h> |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 29 | #include <pj/guid.h> |
| 30 | #include <pj/os.h> |
| 31 | #include <pj/pool.h> |
| 32 | #include <pj/log.h> |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 33 | #include <pj/rand.h> |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 34 | #include <pj/string.h> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 35 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 36 | |
| 37 | #define REFRESH_TIMER 1 |
| 38 | #define DELAY_BEFORE_REFRESH 5 |
| 39 | #define THIS_FILE "sip_regc.c" |
| 40 | |
| 41 | /** |
| 42 | * SIP client registration structure. |
| 43 | */ |
| 44 | struct pjsip_regc |
| 45 | { |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 46 | pj_pool_t *pool; |
| 47 | pjsip_endpoint *endpt; |
| 48 | pj_bool_t _delete_flag; |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 49 | pj_bool_t has_tsx; |
| 50 | pj_int32_t busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 51 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 52 | void *token; |
| 53 | pjsip_regc_cb *cb; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 54 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 55 | pj_str_t str_srv_url; |
| 56 | pjsip_uri *srv_url; |
| 57 | pjsip_cid_hdr *cid_hdr; |
| 58 | pjsip_cseq_hdr *cseq_hdr; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 59 | pj_str_t from_uri; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 60 | pjsip_from_hdr *from_hdr; |
| 61 | pjsip_to_hdr *to_hdr; |
| 62 | char *contact_buf; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 63 | pjsip_generic_string_hdr *contact_hdr; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 64 | pjsip_expires_hdr *expires_hdr; |
| 65 | pjsip_contact_hdr *unreg_contact_hdr; |
| 66 | pjsip_expires_hdr *unreg_expires_hdr; |
| 67 | pj_uint32_t expires; |
| 68 | pjsip_route_hdr route_set; |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 69 | pjsip_hdr hdr_list; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 70 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 71 | /* Authorization sessions. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 72 | pjsip_auth_clt_sess auth_sess; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 73 | |
| 74 | /* Auto refresh registration. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 75 | pj_bool_t auto_reg; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 76 | pj_time_val last_reg; |
| 77 | pj_time_val next_reg; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 78 | pj_timer_entry timer; |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame^] | 79 | |
| 80 | /* Transport selector */ |
| 81 | pjsip_tpselector tp_sel; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
| 85 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 86 | PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token, |
| 87 | pjsip_regc_cb *cb, |
| 88 | pjsip_regc **p_regc) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 89 | { |
| 90 | pj_pool_t *pool; |
| 91 | pjsip_regc *regc; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 92 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 93 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 94 | /* Verify arguments. */ |
| 95 | PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 96 | |
| 97 | pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 98 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 99 | |
| 100 | regc = pj_pool_zalloc(pool, sizeof(struct pjsip_regc)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 101 | |
| 102 | regc->pool = pool; |
| 103 | regc->endpt = endpt; |
| 104 | regc->token = token; |
| 105 | regc->cb = cb; |
| 106 | regc->contact_buf = pj_pool_alloc(pool, PJSIP_REGC_CONTACT_BUF_SIZE); |
| 107 | regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED; |
| 108 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 109 | status = pjsip_auth_clt_init(®c->auth_sess, endpt, regc->pool, 0); |
| 110 | if (status != PJ_SUCCESS) |
| 111 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 112 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 113 | pj_list_init(®c->route_set); |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 114 | pj_list_init(®c->hdr_list); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 115 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 116 | /* Done */ |
| 117 | *p_regc = regc; |
| 118 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 122 | PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 123 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 124 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
| 125 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 126 | if (regc->has_tsx || regc->busy) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 127 | regc->_delete_flag = 1; |
| 128 | regc->cb = NULL; |
| 129 | } else { |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame^] | 130 | pjsip_tpselector_dec_ref(®c->tp_sel); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 131 | pjsip_endpt_release_pool(regc->endpt, regc->pool); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 132 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 133 | |
| 134 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 138 | PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc, |
| 139 | pjsip_regc_info *info ) |
| 140 | { |
| 141 | PJ_ASSERT_RETURN(regc && info, PJ_EINVAL); |
| 142 | |
| 143 | info->server_uri = regc->str_srv_url; |
| 144 | info->client_uri = regc->from_uri; |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 145 | info->is_busy = (regc->busy || regc->has_tsx); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 146 | info->auto_reg = regc->auto_reg; |
| 147 | info->interval = regc->expires; |
| 148 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 149 | if (regc->has_tsx) |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 150 | info->next_reg = 0; |
| 151 | else if (regc->auto_reg == 0) |
| 152 | info->next_reg = 0; |
| 153 | else if (regc->expires < 0) |
| 154 | info->next_reg = regc->expires; |
| 155 | else { |
| 156 | pj_time_val now, next_reg; |
| 157 | |
| 158 | next_reg = regc->next_reg; |
| 159 | pj_gettimeofday(&now); |
| 160 | PJ_TIME_VAL_SUB(next_reg, now); |
| 161 | info->next_reg = next_reg.sec; |
| 162 | } |
| 163 | |
| 164 | return PJ_SUCCESS; |
| 165 | } |
| 166 | |
| 167 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 168 | PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc) |
| 169 | { |
| 170 | return regc->pool; |
| 171 | } |
| 172 | |
| 173 | static void set_expires( pjsip_regc *regc, pj_uint32_t expires) |
| 174 | { |
| 175 | if (expires != regc->expires) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 176 | regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 177 | } else { |
| 178 | regc->expires_hdr = NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static pj_status_t set_contact( pjsip_regc *regc, |
| 184 | int contact_cnt, |
| 185 | const pj_str_t contact[] ) |
| 186 | { |
| 187 | int i; |
| 188 | char *s; |
| 189 | const pj_str_t contact_STR = { "Contact", 7}; |
| 190 | |
| 191 | /* Concatenate contacts. */ |
| 192 | for (i=0, s=regc->contact_buf; i<contact_cnt; ++i) { |
| 193 | if ((s-regc->contact_buf) + contact[i].slen + 2 > PJSIP_REGC_CONTACT_BUF_SIZE) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 194 | return PJSIP_EURITOOLONG; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 195 | } |
| 196 | pj_memcpy(s, contact[i].ptr, contact[i].slen); |
| 197 | s += contact[i].slen; |
| 198 | |
| 199 | if (i != contact_cnt - 1) { |
| 200 | *s++ = ','; |
| 201 | *s++ = ' '; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /* Set "Contact" header. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 206 | regc->contact_hdr = pjsip_generic_string_hdr_create(regc->pool, |
| 207 | &contact_STR, |
| 208 | NULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 209 | regc->contact_hdr->hvalue.ptr = regc->contact_buf; |
| 210 | regc->contact_hdr->hvalue.slen = (s - regc->contact_buf); |
| 211 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 212 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
| 216 | PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc, |
| 217 | const pj_str_t *srv_url, |
| 218 | const pj_str_t *from_url, |
| 219 | const pj_str_t *to_url, |
| 220 | int contact_cnt, |
| 221 | const pj_str_t contact[], |
| 222 | pj_uint32_t expires) |
| 223 | { |
| 224 | pj_str_t tmp; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 225 | pj_status_t status; |
| 226 | |
| 227 | PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url && |
| 228 | contact_cnt && contact && expires, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 229 | |
| 230 | /* Copy server URL. */ |
| 231 | pj_strdup_with_null(regc->pool, ®c->str_srv_url, srv_url); |
| 232 | |
| 233 | /* Set server URL. */ |
| 234 | tmp = regc->str_srv_url; |
| 235 | regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0); |
| 236 | if (regc->srv_url == NULL) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 237 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Set "From" header. */ |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 241 | pj_strdup_with_null(regc->pool, ®c->from_uri, from_url); |
| 242 | tmp = regc->from_uri; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 243 | regc->from_hdr = pjsip_from_hdr_create(regc->pool); |
| 244 | regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen, |
| 245 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 246 | if (!regc->from_hdr->uri) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 247 | PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s", |
| 248 | from_url->slen, from_url->ptr)); |
| 249 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* Set "To" header. */ |
| 253 | pj_strdup_with_null(regc->pool, &tmp, to_url); |
| 254 | regc->to_hdr = pjsip_to_hdr_create(regc->pool); |
| 255 | regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen, |
| 256 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 257 | if (!regc->to_hdr->uri) { |
| 258 | PJ_LOG(4,(THIS_FILE, "regc: invalid target URI %.*s", to_url->slen, to_url->ptr)); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 259 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | /* Set "Contact" header. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 264 | status = set_contact( regc, contact_cnt, contact); |
| 265 | if (status != PJ_SUCCESS) |
| 266 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 267 | |
| 268 | /* Set "Expires" header, if required. */ |
| 269 | set_expires( regc, expires); |
| 270 | |
| 271 | /* Set "Call-ID" header. */ |
| 272 | regc->cid_hdr = pjsip_cid_hdr_create(regc->pool); |
| 273 | pj_create_unique_string(regc->pool, ®c->cid_hdr->id); |
| 274 | |
| 275 | /* Set "CSeq" header. */ |
| 276 | regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool); |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 277 | regc->cseq_hdr->cseq = pj_rand() % 0xFFFF; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 278 | pjsip_method_set( ®c->cseq_hdr->method, PJSIP_REGISTER_METHOD); |
| 279 | |
| 280 | /* Create "Contact" header used in unregistration. */ |
| 281 | regc->unreg_contact_hdr = pjsip_contact_hdr_create(regc->pool); |
| 282 | regc->unreg_contact_hdr->star = 1; |
| 283 | |
| 284 | /* Create "Expires" header used in unregistration. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 285 | regc->unreg_expires_hdr = pjsip_expires_hdr_create( regc->pool, 0); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 286 | |
| 287 | /* Done. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 288 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc, |
| 292 | int count, |
| 293 | const pjsip_cred_info cred[] ) |
| 294 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 295 | PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL); |
| 296 | return pjsip_auth_clt_set_credentials(®c->auth_sess, count, cred); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 299 | PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc, |
| 300 | const pjsip_route_hdr *route_set) |
| 301 | { |
| 302 | const pjsip_route_hdr *chdr; |
| 303 | |
| 304 | PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL); |
| 305 | |
| 306 | pj_list_init(®c->route_set); |
| 307 | |
| 308 | chdr = route_set->next; |
| 309 | while (chdr != route_set) { |
| 310 | pj_list_push_back(®c->route_set, pjsip_hdr_clone(regc->pool, chdr)); |
| 311 | chdr = chdr->next; |
| 312 | } |
| 313 | |
| 314 | return PJ_SUCCESS; |
| 315 | } |
| 316 | |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame^] | 317 | |
| 318 | /* |
| 319 | * Bind client registration to a specific transport/listener. |
| 320 | */ |
| 321 | PJ_DEF(pj_status_t) pjsip_regc_set_transport( pjsip_regc *regc, |
| 322 | const pjsip_tpselector *sel) |
| 323 | { |
| 324 | PJ_ASSERT_RETURN(regc && sel, PJ_EINVAL); |
| 325 | |
| 326 | pjsip_tpselector_dec_ref(®c->tp_sel); |
| 327 | pj_memcpy(®c->tp_sel, sel, sizeof(*sel)); |
| 328 | pjsip_tpselector_add_ref(®c->tp_sel); |
| 329 | |
| 330 | return PJ_SUCCESS; |
| 331 | } |
| 332 | |
| 333 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 334 | PJ_DEF(pj_status_t) pjsip_regc_add_headers( pjsip_regc *regc, |
| 335 | const pjsip_hdr *hdr_list) |
| 336 | { |
| 337 | const pjsip_hdr *hdr; |
| 338 | |
| 339 | PJ_ASSERT_RETURN(regc && hdr_list, PJ_EINVAL); |
| 340 | |
| 341 | //This is "add" operation, so don't remove headers. |
| 342 | //pj_list_init(®c->hdr_list); |
| 343 | |
| 344 | hdr = hdr_list->next; |
| 345 | while (hdr != hdr_list) { |
| 346 | pj_list_push_back(®c->hdr_list, pjsip_hdr_clone(regc->pool, hdr)); |
| 347 | hdr = hdr->next; |
| 348 | } |
| 349 | |
| 350 | return PJ_SUCCESS; |
| 351 | } |
| 352 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 353 | static pj_status_t create_request(pjsip_regc *regc, |
| 354 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 355 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 356 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 357 | pjsip_tx_data *tdata; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 358 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 359 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 360 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 361 | /* Create the request. */ |
| 362 | status = pjsip_endpt_create_request_from_hdr( regc->endpt, |
| 363 | &pjsip_register_method, |
| 364 | regc->srv_url, |
| 365 | regc->from_hdr, |
| 366 | regc->to_hdr, |
| 367 | NULL, |
| 368 | regc->cid_hdr, |
| 369 | regc->cseq_hdr->cseq, |
| 370 | NULL, |
| 371 | &tdata); |
| 372 | if (status != PJ_SUCCESS) |
| 373 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 374 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 375 | /* Add cached authorization headers. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 376 | pjsip_auth_clt_init_req( ®c->auth_sess, tdata ); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 377 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 378 | /* Add Route headers from route set, ideally after Via header */ |
| 379 | if (!pj_list_empty(®c->route_set)) { |
| 380 | pjsip_hdr *route_pos; |
| 381 | const pjsip_route_hdr *route; |
| 382 | |
| 383 | route_pos = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 384 | if (!route_pos) |
| 385 | route_pos = &tdata->msg->hdr; |
| 386 | |
| 387 | route = regc->route_set.next; |
| 388 | while (route != ®c->route_set) { |
| 389 | pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, route); |
| 390 | pj_list_insert_after(route_pos, new_hdr); |
| 391 | route_pos = new_hdr; |
| 392 | route = route->next; |
| 393 | } |
| 394 | } |
| 395 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 396 | /* Add additional request headers */ |
| 397 | if (!pj_list_empty(®c->hdr_list)) { |
| 398 | const pjsip_hdr *hdr; |
| 399 | |
| 400 | hdr = regc->hdr_list.next; |
| 401 | while (hdr != ®c->hdr_list) { |
| 402 | pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, hdr); |
| 403 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 404 | hdr = hdr->next; |
| 405 | } |
| 406 | } |
| 407 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 408 | /* Done. */ |
| 409 | *p_tdata = tdata; |
| 410 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 414 | PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg, |
| 415 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 416 | { |
| 417 | pjsip_msg *msg; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 418 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 419 | pjsip_tx_data *tdata; |
| 420 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 421 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 422 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 423 | status = create_request(regc, &tdata); |
| 424 | if (status != PJ_SUCCESS) |
| 425 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 426 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 427 | /* Add Contact header. */ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 428 | msg = tdata->msg; |
Benny Prijono | 4093f7c | 2006-09-13 23:48:45 +0000 | [diff] [blame] | 429 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 430 | regc->contact_hdr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 431 | if (regc->expires_hdr) |
Benny Prijono | 4093f7c | 2006-09-13 23:48:45 +0000 | [diff] [blame] | 432 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 433 | regc->expires_hdr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 434 | |
| 435 | if (regc->timer.id != 0) { |
| 436 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 437 | regc->timer.id = 0; |
| 438 | } |
| 439 | |
| 440 | regc->auto_reg = autoreg; |
| 441 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 442 | /* Done */ |
| 443 | *p_tdata = tdata; |
| 444 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 448 | PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc, |
| 449 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 450 | { |
| 451 | pjsip_tx_data *tdata; |
| 452 | pjsip_msg *msg; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 453 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 454 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 455 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 456 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 457 | if (regc->timer.id != 0) { |
| 458 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 459 | regc->timer.id = 0; |
| 460 | } |
| 461 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 462 | status = create_request(regc, &tdata); |
| 463 | if (status != PJ_SUCCESS) |
| 464 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 465 | |
| 466 | msg = tdata->msg; |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 467 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 468 | regc->contact_hdr)); |
| 469 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr); |
| 470 | |
| 471 | *p_tdata = tdata; |
| 472 | return PJ_SUCCESS; |
| 473 | } |
| 474 | |
| 475 | PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc, |
| 476 | pjsip_tx_data **p_tdata) |
| 477 | { |
| 478 | pjsip_tx_data *tdata; |
| 479 | pjsip_msg *msg; |
| 480 | pj_status_t status; |
| 481 | |
| 482 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 483 | |
| 484 | if (regc->timer.id != 0) { |
| 485 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 486 | regc->timer.id = 0; |
| 487 | } |
| 488 | |
| 489 | status = create_request(regc, &tdata); |
| 490 | if (status != PJ_SUCCESS) |
| 491 | return status; |
| 492 | |
| 493 | msg = tdata->msg; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 494 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_contact_hdr); |
| 495 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr); |
| 496 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 497 | *p_tdata = tdata; |
| 498 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc, |
| 503 | int contact_cnt, |
| 504 | const pj_str_t contact[] ) |
| 505 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 506 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 507 | return set_contact( regc, contact_cnt, contact ); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc, |
| 512 | pj_uint32_t expires ) |
| 513 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 514 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 515 | set_expires( regc, expires ); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 516 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 520 | static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code, |
| 521 | const pj_str_t *reason, |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 522 | pjsip_rx_data *rdata, pj_int32_t expiration, |
| 523 | int contact_cnt, pjsip_contact_hdr *contact[]) |
| 524 | { |
| 525 | struct pjsip_regc_cbparam cbparam; |
| 526 | |
| 527 | |
| 528 | cbparam.regc = regc; |
| 529 | cbparam.token = regc->token; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 530 | cbparam.status = status; |
| 531 | cbparam.code = st_code; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 532 | cbparam.reason = *reason; |
| 533 | cbparam.rdata = rdata; |
| 534 | cbparam.contact_cnt = contact_cnt; |
| 535 | cbparam.expiration = expiration; |
| 536 | if (contact_cnt) { |
| 537 | pj_memcpy( cbparam.contact, contact, |
| 538 | contact_cnt*sizeof(pjsip_contact_hdr*)); |
| 539 | } |
| 540 | |
| 541 | (*regc->cb)(&cbparam); |
| 542 | } |
| 543 | |
| 544 | static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
| 545 | struct pj_timer_entry *entry) |
| 546 | { |
| 547 | pjsip_regc *regc = entry->user_data; |
| 548 | pjsip_tx_data *tdata; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 549 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 550 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 551 | PJ_UNUSED_ARG(timer_heap); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 552 | |
| 553 | entry->id = 0; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 554 | status = pjsip_regc_register(regc, 1, &tdata); |
| 555 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 556 | status = pjsip_regc_send(regc, tdata); |
| 557 | } |
| 558 | |
| 559 | if (status != PJ_SUCCESS) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 560 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 561 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 562 | call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | static void tsx_callback(void *token, pjsip_event *event) |
| 567 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 568 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 569 | pjsip_regc *regc = token; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 570 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 571 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 572 | /* Decrement pending transaction counter. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 573 | pj_assert(regc->has_tsx); |
| 574 | regc->has_tsx = PJ_FALSE; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 575 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 576 | /* If registration data has been deleted by user then remove registration |
| 577 | * data from transaction's callback, and don't call callback. |
| 578 | */ |
| 579 | if (regc->_delete_flag) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 580 | |
| 581 | /* Nothing to do */ |
| 582 | ; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 583 | |
| 584 | } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 585 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
| 586 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 587 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 588 | pjsip_tx_data *tdata; |
| 589 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 590 | status = pjsip_auth_clt_reinit_req( ®c->auth_sess, |
| 591 | rdata, |
| 592 | tsx->last_tx, |
| 593 | &tdata); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 594 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 595 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 596 | status = pjsip_regc_send(regc, tdata); |
| 597 | } |
| 598 | |
| 599 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 600 | call_callback(regc, status, tsx->status_code, |
| 601 | &rdata->msg_info.msg->line.status.reason, |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 602 | rdata, -1, 0, NULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 603 | } |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 604 | |
| 605 | return; |
| 606 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 607 | } else { |
| 608 | int contact_cnt = 0; |
| 609 | pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT]; |
| 610 | pjsip_rx_data *rdata; |
| 611 | pj_int32_t expiration = 0xFFFF; |
| 612 | |
| 613 | if (tsx->status_code/100 == 2) { |
| 614 | int i; |
| 615 | pjsip_contact_hdr *hdr; |
| 616 | pjsip_msg *msg; |
| 617 | pjsip_expires_hdr *expires; |
| 618 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 619 | rdata = event->body.tsx_state.src.rdata; |
| 620 | msg = rdata->msg_info.msg; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 621 | hdr = pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL); |
| 622 | while (hdr) { |
| 623 | contact[contact_cnt++] = hdr; |
| 624 | hdr = hdr->next; |
| 625 | if (hdr == (void*)&msg->hdr) |
| 626 | break; |
| 627 | hdr = pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, hdr); |
| 628 | } |
| 629 | |
| 630 | expires = pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 631 | |
| 632 | if (expires) |
| 633 | expiration = expires->ivalue; |
| 634 | |
| 635 | for (i=0; i<contact_cnt; ++i) { |
| 636 | hdr = contact[i]; |
| 637 | if (hdr->expires >= 0 && hdr->expires < expiration) |
| 638 | expiration = contact[i]->expires; |
| 639 | } |
| 640 | |
| 641 | if (regc->auto_reg && expiration != 0 && expiration != 0xFFFF) { |
| 642 | pj_time_val delay = { 0, 0}; |
| 643 | |
| 644 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
| 645 | if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED && |
| 646 | delay.sec > (pj_int32_t)regc->expires) |
| 647 | { |
| 648 | delay.sec = regc->expires; |
| 649 | } |
| 650 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 651 | delay.sec = DELAY_BEFORE_REFRESH; |
| 652 | regc->timer.cb = ®c_refresh_timer_cb; |
| 653 | regc->timer.id = REFRESH_TIMER; |
| 654 | regc->timer.user_data = regc; |
| 655 | pjsip_endpt_schedule_timer( regc->endpt, ®c->timer, &delay); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 656 | pj_gettimeofday(®c->last_reg); |
| 657 | regc->next_reg = regc->last_reg; |
| 658 | regc->next_reg.sec += delay.sec; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | } else { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 662 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 663 | event->body.tsx_state.src.rdata : NULL; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 666 | /* Increment busy flag temporarily to prevent regc from |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 667 | * being destroyed. |
| 668 | */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 669 | ++regc->busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 670 | |
| 671 | /* Call callback. */ |
| 672 | if (expiration == 0xFFFF) expiration = -1; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 673 | call_callback(regc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 674 | (rdata ? &rdata->msg_info.msg->line.status.reason |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 675 | : pjsip_get_status_text(tsx->status_code)), |
| 676 | rdata, expiration, |
| 677 | contact_cnt, contact); |
| 678 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 679 | /* Decrement busy flag */ |
| 680 | --regc->busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 684 | if (regc->_delete_flag && regc->busy==0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 685 | pjsip_regc_destroy(regc); |
| 686 | } |
| 687 | } |
| 688 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 689 | PJ_DEF(pj_status_t) pjsip_regc_send(pjsip_regc *regc, pjsip_tx_data *tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 690 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 691 | pj_status_t status; |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 692 | pjsip_cseq_hdr *cseq_hdr; |
| 693 | pj_uint32_t cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 694 | |
| 695 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 696 | if (regc->has_tsx) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 697 | PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another " |
| 698 | "transaction pending")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 699 | pjsip_tx_data_dec_ref( tdata ); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 700 | return PJSIP_EBUSY; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | /* Invalidate message buffer. */ |
| 704 | pjsip_tx_data_invalidate_msg(tdata); |
| 705 | |
| 706 | /* Increment CSeq */ |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 707 | cseq = ++regc->cseq_hdr->cseq; |
| 708 | cseq_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
| 709 | cseq_hdr->cseq = cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 710 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 711 | /* Increment pending transaction first, since transaction callback |
| 712 | * may be called even before send_request() returns! |
| 713 | */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 714 | regc->has_tsx = PJ_TRUE; |
| 715 | ++regc->busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 716 | status = pjsip_endpt_send_request(regc->endpt, tdata, -1, regc, &tsx_callback); |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 717 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 718 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 719 | } |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 720 | --regc->busy; |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 721 | |
| 722 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 723 | if (regc->_delete_flag && regc->busy==0) { |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 724 | pjsip_regc_destroy(regc); |
| 725 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 726 | |
| 727 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | |