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 | |
| 106 | regc = pj_pool_zalloc(pool, sizeof(struct 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; |
| 112 | regc->contact_buf = pj_pool_alloc(pool, PJSIP_REGC_CONTACT_BUF_SIZE); |
| 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 | |
| 389 | route_pos = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 390 | if (!route_pos) |
| 391 | route_pos = &tdata->msg->hdr; |
| 392 | |
| 393 | route = regc->route_set.next; |
| 394 | while (route != ®c->route_set) { |
| 395 | pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, route); |
| 396 | pj_list_insert_after(route_pos, new_hdr); |
| 397 | route_pos = new_hdr; |
| 398 | route = route->next; |
| 399 | } |
| 400 | } |
| 401 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 402 | /* Add additional request headers */ |
| 403 | if (!pj_list_empty(®c->hdr_list)) { |
| 404 | const pjsip_hdr *hdr; |
| 405 | |
| 406 | hdr = regc->hdr_list.next; |
| 407 | while (hdr != ®c->hdr_list) { |
| 408 | pjsip_hdr *new_hdr = pjsip_hdr_shallow_clone(tdata->pool, hdr); |
| 409 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 410 | hdr = hdr->next; |
| 411 | } |
| 412 | } |
| 413 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 414 | /* Done. */ |
| 415 | *p_tdata = tdata; |
| 416 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 420 | PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg, |
| 421 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 422 | { |
| 423 | pjsip_msg *msg; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 424 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 425 | pjsip_tx_data *tdata; |
| 426 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 427 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 428 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 429 | status = create_request(regc, &tdata); |
| 430 | if (status != PJ_SUCCESS) |
| 431 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 432 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 433 | /* Add Contact header. */ |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 434 | msg = tdata->msg; |
Benny Prijono | 4093f7c | 2006-09-13 23:48:45 +0000 | [diff] [blame] | 435 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 436 | regc->contact_hdr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 437 | if (regc->expires_hdr) |
Benny Prijono | 4093f7c | 2006-09-13 23:48:45 +0000 | [diff] [blame] | 438 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 439 | regc->expires_hdr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 440 | |
| 441 | if (regc->timer.id != 0) { |
| 442 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 443 | regc->timer.id = 0; |
| 444 | } |
| 445 | |
| 446 | regc->auto_reg = autoreg; |
| 447 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 448 | /* Done */ |
| 449 | *p_tdata = tdata; |
| 450 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 454 | PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc, |
| 455 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 456 | { |
| 457 | pjsip_tx_data *tdata; |
| 458 | pjsip_msg *msg; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 459 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 460 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 461 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 462 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 463 | if (regc->timer.id != 0) { |
| 464 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 465 | regc->timer.id = 0; |
| 466 | } |
| 467 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 468 | status = create_request(regc, &tdata); |
| 469 | if (status != PJ_SUCCESS) |
| 470 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 471 | |
| 472 | msg = tdata->msg; |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 473 | pjsip_msg_add_hdr(msg, pjsip_hdr_shallow_clone(tdata->pool, |
| 474 | regc->contact_hdr)); |
| 475 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr); |
| 476 | |
| 477 | *p_tdata = tdata; |
| 478 | return PJ_SUCCESS; |
| 479 | } |
| 480 | |
| 481 | PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc, |
| 482 | pjsip_tx_data **p_tdata) |
| 483 | { |
| 484 | pjsip_tx_data *tdata; |
| 485 | pjsip_msg *msg; |
| 486 | pj_status_t status; |
| 487 | |
| 488 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 489 | |
| 490 | if (regc->timer.id != 0) { |
| 491 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 492 | regc->timer.id = 0; |
| 493 | } |
| 494 | |
| 495 | status = create_request(regc, &tdata); |
| 496 | if (status != PJ_SUCCESS) |
| 497 | return status; |
| 498 | |
| 499 | msg = tdata->msg; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 500 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_contact_hdr); |
| 501 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr); |
| 502 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 503 | *p_tdata = tdata; |
| 504 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | |
| 508 | PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc, |
| 509 | int contact_cnt, |
| 510 | const pj_str_t contact[] ) |
| 511 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 512 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 513 | return set_contact( regc, contact_cnt, contact ); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc, |
| 518 | pj_uint32_t expires ) |
| 519 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 520 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 521 | set_expires( regc, expires ); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 522 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 526 | static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code, |
| 527 | const pj_str_t *reason, |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 528 | pjsip_rx_data *rdata, pj_int32_t expiration, |
| 529 | int contact_cnt, pjsip_contact_hdr *contact[]) |
| 530 | { |
| 531 | struct pjsip_regc_cbparam cbparam; |
| 532 | |
| 533 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 534 | if (!regc->cb) |
| 535 | return; |
| 536 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 537 | cbparam.regc = regc; |
| 538 | cbparam.token = regc->token; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 539 | cbparam.status = status; |
| 540 | cbparam.code = st_code; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 541 | cbparam.reason = *reason; |
| 542 | cbparam.rdata = rdata; |
| 543 | cbparam.contact_cnt = contact_cnt; |
| 544 | cbparam.expiration = expiration; |
| 545 | if (contact_cnt) { |
| 546 | pj_memcpy( cbparam.contact, contact, |
| 547 | contact_cnt*sizeof(pjsip_contact_hdr*)); |
| 548 | } |
| 549 | |
| 550 | (*regc->cb)(&cbparam); |
| 551 | } |
| 552 | |
| 553 | static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
| 554 | struct pj_timer_entry *entry) |
| 555 | { |
| 556 | pjsip_regc *regc = entry->user_data; |
| 557 | pjsip_tx_data *tdata; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 558 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 559 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 560 | PJ_UNUSED_ARG(timer_heap); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 561 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 562 | /* Temporarily increase busy flag to prevent regc from being deleted |
| 563 | * in pjsip_regc_send() |
| 564 | */ |
| 565 | regc->busy++; |
| 566 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 567 | entry->id = 0; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 568 | status = pjsip_regc_register(regc, 1, &tdata); |
| 569 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 570 | status = pjsip_regc_send(regc, tdata); |
| 571 | } |
| 572 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 573 | if (status != PJ_SUCCESS && regc->cb) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 574 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 575 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 576 | call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 577 | } |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 578 | |
| 579 | regc->busy--; |
| 580 | |
| 581 | /* Delete the record if user destroy regc during the callback. */ |
| 582 | if (regc->_delete_flag && regc->busy==0) { |
| 583 | pjsip_regc_destroy(regc); |
| 584 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void tsx_callback(void *token, pjsip_event *event) |
| 588 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 589 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 590 | pjsip_regc *regc = token; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 591 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 592 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 593 | /* Decrement pending transaction counter. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 594 | pj_assert(regc->has_tsx); |
| 595 | regc->has_tsx = PJ_FALSE; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 596 | |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 597 | /* Handle 401/407 challenge (even when _delete_flag is set) */ |
| 598 | if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 599 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 600 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 601 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 602 | pjsip_tx_data *tdata; |
| 603 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 604 | status = pjsip_auth_clt_reinit_req( ®c->auth_sess, |
| 605 | rdata, |
| 606 | tsx->last_tx, |
| 607 | &tdata); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 608 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 609 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 610 | status = pjsip_regc_send(regc, tdata); |
| 611 | } |
| 612 | |
| 613 | if (status != PJ_SUCCESS) { |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 614 | |
| 615 | /* Only call callback if application is still interested |
| 616 | * in it. |
| 617 | */ |
| 618 | if (regc->_delete_flag == 0) { |
| 619 | /* Increment busy flag temporarily to prevent regc from |
| 620 | * being destroyed. |
| 621 | */ |
| 622 | ++regc->busy; |
| 623 | |
| 624 | call_callback(regc, status, tsx->status_code, |
| 625 | &rdata->msg_info.msg->line.status.reason, |
| 626 | rdata, -1, 0, NULL); |
| 627 | |
| 628 | /* Decrement busy flag */ |
| 629 | --regc->busy; |
| 630 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 631 | } |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 632 | |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 633 | } else if (regc->_delete_flag) { |
| 634 | |
| 635 | /* User has called pjsip_regc_destroy(), so don't call callback. |
| 636 | * This regc will be destroyed later in this function. |
| 637 | */ |
| 638 | |
| 639 | /* Nothing to do */ |
| 640 | ; |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 641 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 642 | } else { |
| 643 | int contact_cnt = 0; |
| 644 | pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT]; |
| 645 | pjsip_rx_data *rdata; |
| 646 | pj_int32_t expiration = 0xFFFF; |
| 647 | |
| 648 | if (tsx->status_code/100 == 2) { |
| 649 | int i; |
| 650 | pjsip_contact_hdr *hdr; |
| 651 | pjsip_msg *msg; |
| 652 | pjsip_expires_hdr *expires; |
| 653 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 654 | rdata = event->body.tsx_state.src.rdata; |
| 655 | msg = rdata->msg_info.msg; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 656 | hdr = pjsip_msg_find_hdr( msg, PJSIP_H_CONTACT, NULL); |
| 657 | while (hdr) { |
| 658 | contact[contact_cnt++] = hdr; |
| 659 | hdr = hdr->next; |
| 660 | if (hdr == (void*)&msg->hdr) |
| 661 | break; |
| 662 | hdr = pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, hdr); |
| 663 | } |
| 664 | |
| 665 | expires = pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 666 | |
| 667 | if (expires) |
| 668 | expiration = expires->ivalue; |
| 669 | |
| 670 | for (i=0; i<contact_cnt; ++i) { |
| 671 | hdr = contact[i]; |
| 672 | if (hdr->expires >= 0 && hdr->expires < expiration) |
| 673 | expiration = contact[i]->expires; |
| 674 | } |
| 675 | |
| 676 | if (regc->auto_reg && expiration != 0 && expiration != 0xFFFF) { |
| 677 | pj_time_val delay = { 0, 0}; |
| 678 | |
| 679 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
| 680 | if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED && |
| 681 | delay.sec > (pj_int32_t)regc->expires) |
| 682 | { |
| 683 | delay.sec = regc->expires; |
| 684 | } |
| 685 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 686 | delay.sec = DELAY_BEFORE_REFRESH; |
| 687 | regc->timer.cb = ®c_refresh_timer_cb; |
| 688 | regc->timer.id = REFRESH_TIMER; |
| 689 | regc->timer.user_data = regc; |
| 690 | pjsip_endpt_schedule_timer( regc->endpt, ®c->timer, &delay); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 691 | pj_gettimeofday(®c->last_reg); |
| 692 | regc->next_reg = regc->last_reg; |
| 693 | regc->next_reg.sec += delay.sec; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | } else { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 697 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 698 | event->body.tsx_state.src.rdata : NULL; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 701 | /* Increment busy flag temporarily to prevent regc from |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 702 | * being destroyed. |
| 703 | */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 704 | ++regc->busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 705 | |
| 706 | /* Call callback. */ |
| 707 | if (expiration == 0xFFFF) expiration = -1; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 708 | call_callback(regc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 709 | (rdata ? &rdata->msg_info.msg->line.status.reason |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 710 | : pjsip_get_status_text(tsx->status_code)), |
| 711 | rdata, expiration, |
| 712 | contact_cnt, contact); |
| 713 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 714 | /* Decrement busy flag */ |
| 715 | --regc->busy; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 719 | if (regc->_delete_flag && regc->busy==0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 720 | pjsip_regc_destroy(regc); |
| 721 | } |
| 722 | } |
| 723 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 724 | 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] | 725 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 726 | pj_status_t status; |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 727 | pjsip_cseq_hdr *cseq_hdr; |
| 728 | pj_uint32_t cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 729 | |
| 730 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 731 | if (regc->has_tsx) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 732 | PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another " |
| 733 | "transaction pending")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 734 | pjsip_tx_data_dec_ref( tdata ); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 735 | return PJSIP_EBUSY; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* Invalidate message buffer. */ |
| 739 | pjsip_tx_data_invalidate_msg(tdata); |
| 740 | |
| 741 | /* Increment CSeq */ |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 742 | cseq = ++regc->cseq_hdr->cseq; |
| 743 | cseq_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
| 744 | cseq_hdr->cseq = cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 745 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 746 | /* Increment pending transaction first, since transaction callback |
| 747 | * may be called even before send_request() returns! |
| 748 | */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 749 | regc->has_tsx = PJ_TRUE; |
| 750 | ++regc->busy; |
Benny Prijono | 0f35f91 | 2007-02-05 18:59:31 +0000 | [diff] [blame] | 751 | status = pjsip_endpt_send_request(regc->endpt, tdata, REGC_TSX_TIMEOUT, |
| 752 | regc, &tsx_callback); |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 753 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 754 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 755 | } |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 756 | --regc->busy; |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 757 | |
| 758 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 759 | if (regc->_delete_flag && regc->busy==0) { |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 760 | pjsip_regc_destroy(regc); |
| 761 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 762 | |
| 763 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | |