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