Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 20 | #include <pjsip-ua/sip_regc.h> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 21 | #include <pjsip/sip_endpoint.h> |
| 22 | #include <pjsip/sip_parser.h> |
| 23 | #include <pjsip/sip_module.h> |
| 24 | #include <pjsip/sip_transaction.h> |
| 25 | #include <pjsip/sip_event.h> |
| 26 | #include <pjsip/sip_util.h> |
| 27 | #include <pjsip/sip_auth_msg.h> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 28 | #include <pjsip/sip_errno.h> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 29 | #include <pj/assert.h> |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 30 | #include <pj/guid.h> |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 31 | #include <pj/lock.h> |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 32 | #include <pj/os.h> |
| 33 | #include <pj/pool.h> |
| 34 | #include <pj/log.h> |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 35 | #include <pj/rand.h> |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 36 | #include <pj/string.h> |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 37 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 38 | |
| 39 | #define REFRESH_TIMER 1 |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 40 | #define DELAY_BEFORE_REFRESH PJSIP_REGISTER_CLIENT_DELAY_BEFORE_REFRESH |
Benny Prijono | 315999b | 2007-09-30 10:58:36 +0000 | [diff] [blame] | 41 | #define THIS_FILE "sip_reg.c" |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 42 | |
Benny Prijono | 0f35f91 | 2007-02-05 18:59:31 +0000 | [diff] [blame] | 43 | /* Outgoing transaction timeout when server sends 100 but never replies |
| 44 | * with final response. Value is in MILISECONDS! |
| 45 | */ |
| 46 | #define REGC_TSX_TIMEOUT 33000 |
| 47 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 48 | enum { NOEXP = 0x1FFFFFFF }; |
| 49 | |
| 50 | static const pj_str_t XUID_PARAM_NAME = { "x-uid", 5 }; |
| 51 | |
| 52 | |
| 53 | /* Current/pending operation */ |
| 54 | enum regc_op |
| 55 | { |
| 56 | REGC_IDLE, |
| 57 | REGC_REGISTERING, |
| 58 | REGC_UNREGISTERING |
| 59 | }; |
Benny Prijono | 0f35f91 | 2007-02-05 18:59:31 +0000 | [diff] [blame] | 60 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 61 | /** |
| 62 | * SIP client registration structure. |
| 63 | */ |
| 64 | struct pjsip_regc |
| 65 | { |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 66 | pj_pool_t *pool; |
| 67 | pjsip_endpoint *endpt; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 68 | pj_lock_t *lock; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 69 | pj_bool_t _delete_flag; |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 70 | pj_bool_t has_tsx; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 71 | pj_atomic_t *busy_ctr; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 72 | enum regc_op current_op; |
| 73 | |
| 74 | pj_bool_t add_xuid_param; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 75 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 76 | void *token; |
| 77 | pjsip_regc_cb *cb; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 78 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 79 | pj_str_t str_srv_url; |
| 80 | pjsip_uri *srv_url; |
| 81 | pjsip_cid_hdr *cid_hdr; |
| 82 | pjsip_cseq_hdr *cseq_hdr; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 83 | pj_str_t from_uri; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 84 | pjsip_from_hdr *from_hdr; |
| 85 | pjsip_to_hdr *to_hdr; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 86 | pjsip_contact_hdr contact_hdr_list; |
| 87 | pjsip_contact_hdr removed_contact_hdr_list; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 88 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 89 | pj_uint32_t expires; |
| 90 | pjsip_route_hdr route_set; |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 91 | pjsip_hdr hdr_list; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 92 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 93 | /* Authorization sessions. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 94 | pjsip_auth_clt_sess auth_sess; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 95 | |
| 96 | /* Auto refresh registration. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 97 | pj_bool_t auto_reg; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 98 | pj_time_val last_reg; |
| 99 | pj_time_val next_reg; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 100 | pj_timer_entry timer; |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame] | 101 | |
| 102 | /* Transport selector */ |
| 103 | pjsip_tpselector tp_sel; |
Benny Prijono | bdc093f | 2007-10-04 09:48:25 +0000 | [diff] [blame] | 104 | |
| 105 | /* Last transport used. We acquire the transport to keep |
| 106 | * it open. |
| 107 | */ |
| 108 | pjsip_transport *last_transport; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 112 | PJ_DEF(pj_status_t) pjsip_regc_create( pjsip_endpoint *endpt, void *token, |
| 113 | pjsip_regc_cb *cb, |
| 114 | pjsip_regc **p_regc) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 115 | { |
| 116 | pj_pool_t *pool; |
| 117 | pjsip_regc *regc; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 118 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 119 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 120 | /* Verify arguments. */ |
| 121 | PJ_ASSERT_RETURN(endpt && cb && p_regc, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 122 | |
| 123 | pool = pjsip_endpt_create_pool(endpt, "regc%p", 1024, 1024); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 124 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 125 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 126 | regc = PJ_POOL_ZALLOC_T(pool, pjsip_regc); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 127 | |
| 128 | regc->pool = pool; |
| 129 | regc->endpt = endpt; |
| 130 | regc->token = token; |
| 131 | regc->cb = cb; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 132 | regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 133 | regc->add_xuid_param = pjsip_cfg()->regc.add_xuid_param; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 134 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 135 | status = pj_lock_create_recursive_mutex(pool, pool->obj_name, |
| 136 | ®c->lock); |
| 137 | if (status != PJ_SUCCESS) { |
| 138 | pj_pool_release(pool); |
| 139 | return status; |
| 140 | } |
| 141 | |
| 142 | status = pj_atomic_create(pool, 0, ®c->busy_ctr); |
| 143 | if (status != PJ_SUCCESS) { |
| 144 | pj_lock_destroy(regc->lock); |
| 145 | pj_pool_release(pool); |
| 146 | return status; |
| 147 | } |
| 148 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 149 | status = pjsip_auth_clt_init(®c->auth_sess, endpt, regc->pool, 0); |
| 150 | if (status != PJ_SUCCESS) |
| 151 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 152 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 153 | pj_list_init(®c->route_set); |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 154 | pj_list_init(®c->hdr_list); |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 155 | pj_list_init(®c->contact_hdr_list); |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 156 | pj_list_init(®c->removed_contact_hdr_list); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 157 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 158 | /* Done */ |
| 159 | *p_regc = regc; |
| 160 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 164 | PJ_DEF(pj_status_t) pjsip_regc_destroy(pjsip_regc *regc) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 165 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 166 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
| 167 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 168 | pj_lock_acquire(regc->lock); |
| 169 | if (regc->has_tsx || pj_atomic_get(regc->busy_ctr) != 0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 170 | regc->_delete_flag = 1; |
| 171 | regc->cb = NULL; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 172 | pj_lock_release(regc->lock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 173 | } else { |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame] | 174 | pjsip_tpselector_dec_ref(®c->tp_sel); |
Benny Prijono | bdc093f | 2007-10-04 09:48:25 +0000 | [diff] [blame] | 175 | if (regc->last_transport) { |
| 176 | pjsip_transport_dec_ref(regc->last_transport); |
| 177 | regc->last_transport = NULL; |
| 178 | } |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 179 | if (regc->timer.id != 0) { |
| 180 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 181 | regc->timer.id = 0; |
| 182 | } |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 183 | pj_atomic_destroy(regc->busy_ctr); |
| 184 | pj_lock_release(regc->lock); |
| 185 | pj_lock_destroy(regc->lock); |
| 186 | regc->lock = NULL; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 187 | pjsip_endpt_release_pool(regc->endpt, regc->pool); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 188 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 189 | |
| 190 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 194 | PJ_DEF(pj_status_t) pjsip_regc_get_info( pjsip_regc *regc, |
| 195 | pjsip_regc_info *info ) |
| 196 | { |
| 197 | PJ_ASSERT_RETURN(regc && info, PJ_EINVAL); |
| 198 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 199 | pj_lock_acquire(regc->lock); |
| 200 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 201 | info->server_uri = regc->str_srv_url; |
| 202 | info->client_uri = regc->from_uri; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 203 | info->is_busy = (pj_atomic_get(regc->busy_ctr) || regc->has_tsx); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 204 | info->auto_reg = regc->auto_reg; |
| 205 | info->interval = regc->expires; |
| 206 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 207 | if (regc->has_tsx) |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 208 | info->next_reg = 0; |
| 209 | else if (regc->auto_reg == 0) |
| 210 | info->next_reg = 0; |
| 211 | else if (regc->expires < 0) |
| 212 | info->next_reg = regc->expires; |
| 213 | else { |
| 214 | pj_time_val now, next_reg; |
| 215 | |
| 216 | next_reg = regc->next_reg; |
| 217 | pj_gettimeofday(&now); |
| 218 | PJ_TIME_VAL_SUB(next_reg, now); |
| 219 | info->next_reg = next_reg.sec; |
| 220 | } |
| 221 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 222 | pj_lock_release(regc->lock); |
| 223 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 224 | return PJ_SUCCESS; |
| 225 | } |
| 226 | |
| 227 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 228 | PJ_DEF(pj_pool_t*) pjsip_regc_get_pool(pjsip_regc *regc) |
| 229 | { |
| 230 | return regc->pool; |
| 231 | } |
| 232 | |
| 233 | static void set_expires( pjsip_regc *regc, pj_uint32_t expires) |
| 234 | { |
| 235 | if (expires != regc->expires) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 236 | regc->expires_hdr = pjsip_expires_hdr_create(regc->pool, expires); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 237 | } else { |
| 238 | regc->expires_hdr = NULL; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | static pj_status_t set_contact( pjsip_regc *regc, |
| 244 | int contact_cnt, |
| 245 | const pj_str_t contact[] ) |
| 246 | { |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 247 | const pj_str_t CONTACT = { "Contact", 7 }; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 248 | pjsip_contact_hdr *h; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 249 | int i; |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 250 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 251 | /* Save existing contact list to removed_contact_hdr_list and |
| 252 | * clear contact_hdr_list. |
| 253 | */ |
| 254 | pj_list_merge_last(®c->removed_contact_hdr_list, |
| 255 | ®c->contact_hdr_list); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 256 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 257 | /* Set the expiration of Contacts in to removed_contact_hdr_list |
| 258 | * zero. |
| 259 | */ |
| 260 | h = regc->removed_contact_hdr_list.next; |
| 261 | while (h != ®c->removed_contact_hdr_list) { |
| 262 | h->expires = 0; |
| 263 | h = h->next; |
| 264 | } |
| 265 | |
| 266 | /* Process new contacts */ |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 267 | for (i=0; i<contact_cnt; ++i) { |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 268 | pjsip_contact_hdr *hdr; |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 269 | pj_str_t tmp; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 270 | |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 271 | pj_strdup_with_null(regc->pool, &tmp, &contact[i]); |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 272 | hdr = (pjsip_contact_hdr*) |
Benny Prijono | 91a5a3a | 2007-09-24 21:16:48 +0000 | [diff] [blame] | 273 | pjsip_parse_hdr(regc->pool, &CONTACT, tmp.ptr, tmp.slen, NULL); |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 274 | if (hdr == NULL) { |
Nanang Izzuddin | 5af37ff | 2009-08-05 18:41:23 +0000 | [diff] [blame] | 275 | PJ_LOG(4,(THIS_FILE, "Invalid Contact: \"%.*s\"", |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 276 | (int)tmp.slen, tmp.ptr)); |
| 277 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 278 | } |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 279 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 280 | /* Find the new contact in old contact list. If found, remove |
| 281 | * the old header from the old header list. |
| 282 | */ |
| 283 | h = regc->removed_contact_hdr_list.next; |
| 284 | while (h != ®c->removed_contact_hdr_list) { |
| 285 | int rc; |
| 286 | |
| 287 | rc = pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR, |
| 288 | h->uri, hdr->uri); |
| 289 | if (rc == 0) { |
| 290 | /* Match */ |
| 291 | pj_list_erase(h); |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | h = h->next; |
| 296 | } |
| 297 | |
| 298 | /* If add_xuid_param option is enabled and Contact URI is sip/sips, |
| 299 | * add xuid parameter to assist matching the Contact URI in the |
| 300 | * REGISTER response later. |
| 301 | */ |
| 302 | if (regc->add_xuid_param && (PJSIP_URI_SCHEME_IS_SIP(hdr->uri) || |
| 303 | PJSIP_URI_SCHEME_IS_SIPS(hdr->uri))) |
| 304 | { |
| 305 | pjsip_param *xuid_param; |
| 306 | pjsip_sip_uri *sip_uri; |
| 307 | |
| 308 | xuid_param = PJ_POOL_ZALLOC_T(regc->pool, pjsip_param); |
| 309 | xuid_param->name = XUID_PARAM_NAME; |
| 310 | pj_create_unique_string(regc->pool, &xuid_param->value); |
| 311 | |
Benny Prijono | 89ac2b4 | 2008-06-13 12:52:56 +0000 | [diff] [blame] | 312 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(hdr->uri); |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 313 | pj_list_push_back(&sip_uri->other_param, xuid_param); |
| 314 | } |
| 315 | |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 316 | pj_list_push_back(®c->contact_hdr_list, hdr); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 319 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | PJ_DEF(pj_status_t) pjsip_regc_init( pjsip_regc *regc, |
| 324 | const pj_str_t *srv_url, |
| 325 | const pj_str_t *from_url, |
| 326 | const pj_str_t *to_url, |
| 327 | int contact_cnt, |
| 328 | const pj_str_t contact[], |
| 329 | pj_uint32_t expires) |
| 330 | { |
| 331 | pj_str_t tmp; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 332 | pj_status_t status; |
| 333 | |
| 334 | PJ_ASSERT_RETURN(regc && srv_url && from_url && to_url && |
| 335 | contact_cnt && contact && expires, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 336 | |
| 337 | /* Copy server URL. */ |
| 338 | pj_strdup_with_null(regc->pool, ®c->str_srv_url, srv_url); |
| 339 | |
| 340 | /* Set server URL. */ |
| 341 | tmp = regc->str_srv_url; |
| 342 | regc->srv_url = pjsip_parse_uri( regc->pool, tmp.ptr, tmp.slen, 0); |
| 343 | if (regc->srv_url == NULL) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 344 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /* Set "From" header. */ |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 348 | pj_strdup_with_null(regc->pool, ®c->from_uri, from_url); |
| 349 | tmp = regc->from_uri; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 350 | regc->from_hdr = pjsip_from_hdr_create(regc->pool); |
| 351 | regc->from_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen, |
| 352 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 353 | if (!regc->from_hdr->uri) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 354 | PJ_LOG(4,(THIS_FILE, "regc: invalid source URI %.*s", |
| 355 | from_url->slen, from_url->ptr)); |
| 356 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* Set "To" header. */ |
| 360 | pj_strdup_with_null(regc->pool, &tmp, to_url); |
| 361 | regc->to_hdr = pjsip_to_hdr_create(regc->pool); |
| 362 | regc->to_hdr->uri = pjsip_parse_uri(regc->pool, tmp.ptr, tmp.slen, |
| 363 | PJSIP_PARSE_URI_AS_NAMEADDR); |
| 364 | if (!regc->to_hdr->uri) { |
| 365 | 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] | 366 | return PJSIP_EINVALIDURI; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | |
| 370 | /* Set "Contact" header. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 371 | status = set_contact( regc, contact_cnt, contact); |
| 372 | if (status != PJ_SUCCESS) |
| 373 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 374 | |
| 375 | /* Set "Expires" header, if required. */ |
| 376 | set_expires( regc, expires); |
| 377 | |
| 378 | /* Set "Call-ID" header. */ |
| 379 | regc->cid_hdr = pjsip_cid_hdr_create(regc->pool); |
| 380 | pj_create_unique_string(regc->pool, ®c->cid_hdr->id); |
| 381 | |
| 382 | /* Set "CSeq" header. */ |
| 383 | regc->cseq_hdr = pjsip_cseq_hdr_create(regc->pool); |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 384 | regc->cseq_hdr->cseq = pj_rand() % 0xFFFF; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 385 | pjsip_method_set( ®c->cseq_hdr->method, PJSIP_REGISTER_METHOD); |
| 386 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 387 | /* Done. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 388 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | PJ_DEF(pj_status_t) pjsip_regc_set_credentials( pjsip_regc *regc, |
| 392 | int count, |
| 393 | const pjsip_cred_info cred[] ) |
| 394 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 395 | PJ_ASSERT_RETURN(regc && count && cred, PJ_EINVAL); |
| 396 | return pjsip_auth_clt_set_credentials(®c->auth_sess, count, cred); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 399 | PJ_DEF(pj_status_t) pjsip_regc_set_prefs( pjsip_regc *regc, |
| 400 | const pjsip_auth_clt_pref *pref) |
| 401 | { |
| 402 | PJ_ASSERT_RETURN(regc && pref, PJ_EINVAL); |
| 403 | return pjsip_auth_clt_set_prefs(®c->auth_sess, pref); |
| 404 | } |
| 405 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 406 | PJ_DEF(pj_status_t) pjsip_regc_set_route_set( pjsip_regc *regc, |
| 407 | const pjsip_route_hdr *route_set) |
| 408 | { |
| 409 | const pjsip_route_hdr *chdr; |
| 410 | |
| 411 | PJ_ASSERT_RETURN(regc && route_set, PJ_EINVAL); |
| 412 | |
| 413 | pj_list_init(®c->route_set); |
| 414 | |
| 415 | chdr = route_set->next; |
| 416 | while (chdr != route_set) { |
| 417 | pj_list_push_back(®c->route_set, pjsip_hdr_clone(regc->pool, chdr)); |
| 418 | chdr = chdr->next; |
| 419 | } |
| 420 | |
| 421 | return PJ_SUCCESS; |
| 422 | } |
| 423 | |
Benny Prijono | 720d0a8 | 2007-01-12 06:37:35 +0000 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * Bind client registration to a specific transport/listener. |
| 427 | */ |
| 428 | PJ_DEF(pj_status_t) pjsip_regc_set_transport( pjsip_regc *regc, |
| 429 | const pjsip_tpselector *sel) |
| 430 | { |
| 431 | PJ_ASSERT_RETURN(regc && sel, PJ_EINVAL); |
| 432 | |
| 433 | pjsip_tpselector_dec_ref(®c->tp_sel); |
| 434 | pj_memcpy(®c->tp_sel, sel, sizeof(*sel)); |
| 435 | pjsip_tpselector_add_ref(®c->tp_sel); |
| 436 | |
| 437 | return PJ_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 441 | PJ_DEF(pj_status_t) pjsip_regc_add_headers( pjsip_regc *regc, |
| 442 | const pjsip_hdr *hdr_list) |
| 443 | { |
| 444 | const pjsip_hdr *hdr; |
| 445 | |
| 446 | PJ_ASSERT_RETURN(regc && hdr_list, PJ_EINVAL); |
| 447 | |
| 448 | //This is "add" operation, so don't remove headers. |
| 449 | //pj_list_init(®c->hdr_list); |
| 450 | |
| 451 | hdr = hdr_list->next; |
| 452 | while (hdr != hdr_list) { |
| 453 | pj_list_push_back(®c->hdr_list, pjsip_hdr_clone(regc->pool, hdr)); |
| 454 | hdr = hdr->next; |
| 455 | } |
| 456 | |
| 457 | return PJ_SUCCESS; |
| 458 | } |
| 459 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 460 | static pj_status_t create_request(pjsip_regc *regc, |
| 461 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 462 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 463 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 464 | pjsip_tx_data *tdata; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 465 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 466 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 467 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 468 | /* Create the request. */ |
| 469 | status = pjsip_endpt_create_request_from_hdr( regc->endpt, |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 470 | pjsip_get_register_method(), |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 471 | regc->srv_url, |
| 472 | regc->from_hdr, |
| 473 | regc->to_hdr, |
| 474 | NULL, |
| 475 | regc->cid_hdr, |
| 476 | regc->cseq_hdr->cseq, |
| 477 | NULL, |
| 478 | &tdata); |
| 479 | if (status != PJ_SUCCESS) |
| 480 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 481 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 482 | /* Add cached authorization headers. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 483 | pjsip_auth_clt_init_req( ®c->auth_sess, tdata ); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 484 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 485 | /* Add Route headers from route set, ideally after Via header */ |
| 486 | if (!pj_list_empty(®c->route_set)) { |
| 487 | pjsip_hdr *route_pos; |
| 488 | const pjsip_route_hdr *route; |
| 489 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 490 | route_pos = (pjsip_hdr*) |
| 491 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 492 | if (!route_pos) |
| 493 | route_pos = &tdata->msg->hdr; |
| 494 | |
| 495 | route = regc->route_set.next; |
| 496 | while (route != ®c->route_set) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 497 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 498 | pjsip_hdr_shallow_clone(tdata->pool, route); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 499 | pj_list_insert_after(route_pos, new_hdr); |
| 500 | route_pos = new_hdr; |
| 501 | route = route->next; |
| 502 | } |
| 503 | } |
| 504 | |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 505 | /* Add additional request headers */ |
| 506 | if (!pj_list_empty(®c->hdr_list)) { |
| 507 | const pjsip_hdr *hdr; |
| 508 | |
| 509 | hdr = regc->hdr_list.next; |
| 510 | while (hdr != ®c->hdr_list) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 511 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 512 | pjsip_hdr_shallow_clone(tdata->pool, hdr); |
Benny Prijono | 8fc6de0 | 2006-11-11 21:25:55 +0000 | [diff] [blame] | 513 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 514 | hdr = hdr->next; |
| 515 | } |
| 516 | } |
| 517 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 518 | /* Done. */ |
| 519 | *p_tdata = tdata; |
| 520 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 524 | PJ_DEF(pj_status_t) pjsip_regc_register(pjsip_regc *regc, pj_bool_t autoreg, |
| 525 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 526 | { |
| 527 | pjsip_msg *msg; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 528 | pjsip_contact_hdr *hdr; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 529 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 530 | pjsip_tx_data *tdata; |
| 531 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 532 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 533 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 534 | pj_lock_acquire(regc->lock); |
| 535 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 536 | status = create_request(regc, &tdata); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 537 | if (status != PJ_SUCCESS) { |
| 538 | pj_lock_release(regc->lock); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 539 | return status; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 540 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 541 | |
| 542 | msg = tdata->msg; |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 543 | |
| 544 | /* Add Contact headers. */ |
| 545 | hdr = regc->contact_hdr_list.next; |
| 546 | while (hdr != ®c->contact_hdr_list) { |
| 547 | pjsip_msg_add_hdr(msg, (pjsip_hdr*) |
| 548 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 549 | hdr = hdr->next; |
| 550 | } |
| 551 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 552 | /* Also add bindings which are to be removed */ |
| 553 | while (!pj_list_empty(®c->removed_contact_hdr_list)) { |
| 554 | hdr = regc->removed_contact_hdr_list.next; |
| 555 | pjsip_msg_add_hdr(msg, (pjsip_hdr*) |
| 556 | pjsip_hdr_clone(tdata->pool, hdr)); |
| 557 | pj_list_erase(hdr); |
| 558 | } |
| 559 | |
| 560 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 561 | if (regc->expires_hdr) |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 562 | pjsip_msg_add_hdr(msg, (pjsip_hdr*) |
| 563 | pjsip_hdr_shallow_clone(tdata->pool, |
Benny Prijono | 4093f7c | 2006-09-13 23:48:45 +0000 | [diff] [blame] | 564 | regc->expires_hdr)); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 565 | |
| 566 | if (regc->timer.id != 0) { |
| 567 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 568 | regc->timer.id = 0; |
| 569 | } |
| 570 | |
| 571 | regc->auto_reg = autoreg; |
| 572 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 573 | pj_lock_release(regc->lock); |
| 574 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 575 | /* Done */ |
| 576 | *p_tdata = tdata; |
| 577 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 581 | PJ_DEF(pj_status_t) pjsip_regc_unregister(pjsip_regc *regc, |
| 582 | pjsip_tx_data **p_tdata) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 583 | { |
| 584 | pjsip_tx_data *tdata; |
| 585 | pjsip_msg *msg; |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 586 | pjsip_hdr *hdr; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 587 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 588 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 589 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 590 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 591 | pj_lock_acquire(regc->lock); |
| 592 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 593 | if (regc->timer.id != 0) { |
| 594 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 595 | regc->timer.id = 0; |
| 596 | } |
| 597 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 598 | status = create_request(regc, &tdata); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 599 | if (status != PJ_SUCCESS) { |
| 600 | pj_lock_release(regc->lock); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 601 | return status; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 602 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 603 | |
| 604 | msg = tdata->msg; |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 605 | |
| 606 | /* Add Contact headers. */ |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 607 | hdr = (pjsip_hdr*)regc->contact_hdr_list.next; |
Benny Prijono | 4304725 | 2008-06-04 14:44:29 +0000 | [diff] [blame] | 608 | while ((void*)hdr != (void*)®c->contact_hdr_list) { |
Benny Prijono | e56b5b1 | 2007-09-11 09:04:51 +0000 | [diff] [blame] | 609 | pjsip_msg_add_hdr(msg, (pjsip_hdr*) |
| 610 | pjsip_hdr_shallow_clone(tdata->pool, hdr)); |
| 611 | hdr = hdr->next; |
| 612 | } |
| 613 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 614 | /* Also add bindings which are to be removed */ |
| 615 | while (!pj_list_empty(®c->removed_contact_hdr_list)) { |
| 616 | hdr = (pjsip_hdr*)regc->removed_contact_hdr_list.next; |
| 617 | pjsip_msg_add_hdr(msg, (pjsip_hdr*) |
| 618 | pjsip_hdr_clone(tdata->pool, hdr)); |
| 619 | pj_list_erase(hdr); |
| 620 | } |
| 621 | |
| 622 | /* Add Expires:0 header */ |
| 623 | hdr = (pjsip_hdr*) pjsip_expires_hdr_create(tdata->pool, 0); |
| 624 | pjsip_msg_add_hdr(msg, hdr); |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 625 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 626 | pj_lock_release(regc->lock); |
| 627 | |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 628 | *p_tdata = tdata; |
| 629 | return PJ_SUCCESS; |
| 630 | } |
| 631 | |
| 632 | PJ_DEF(pj_status_t) pjsip_regc_unregister_all(pjsip_regc *regc, |
| 633 | pjsip_tx_data **p_tdata) |
| 634 | { |
| 635 | pjsip_tx_data *tdata; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 636 | pjsip_contact_hdr *hcontact; |
| 637 | pjsip_hdr *hdr; |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 638 | pjsip_msg *msg; |
| 639 | pj_status_t status; |
| 640 | |
| 641 | PJ_ASSERT_RETURN(regc && p_tdata, PJ_EINVAL); |
| 642 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 643 | pj_lock_acquire(regc->lock); |
| 644 | |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 645 | if (regc->timer.id != 0) { |
| 646 | pjsip_endpt_cancel_timer(regc->endpt, ®c->timer); |
| 647 | regc->timer.id = 0; |
| 648 | } |
| 649 | |
| 650 | status = create_request(regc, &tdata); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 651 | if (status != PJ_SUCCESS) { |
| 652 | pj_lock_release(regc->lock); |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 653 | return status; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 654 | } |
Benny Prijono | dfc4c48 | 2006-12-02 07:25:29 +0000 | [diff] [blame] | 655 | |
| 656 | msg = tdata->msg; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 657 | |
| 658 | /* Clear removed_contact_hdr_list */ |
| 659 | pj_list_init(®c->removed_contact_hdr_list); |
| 660 | |
| 661 | /* Add Contact:* header */ |
| 662 | hcontact = pjsip_contact_hdr_create(tdata->pool); |
| 663 | hcontact->star = 1; |
| 664 | pjsip_msg_add_hdr(msg, (pjsip_hdr*)hcontact); |
| 665 | |
| 666 | /* Add Expires:0 header */ |
| 667 | hdr = (pjsip_hdr*) pjsip_expires_hdr_create(tdata->pool, 0); |
| 668 | pjsip_msg_add_hdr(msg, hdr); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 669 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 670 | pj_lock_release(regc->lock); |
| 671 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 672 | *p_tdata = tdata; |
| 673 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
| 677 | PJ_DEF(pj_status_t) pjsip_regc_update_contact( pjsip_regc *regc, |
| 678 | int contact_cnt, |
| 679 | const pj_str_t contact[] ) |
| 680 | { |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 681 | pj_status_t status; |
| 682 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 683 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 684 | |
| 685 | pj_lock_acquire(regc->lock); |
| 686 | status = set_contact( regc, contact_cnt, contact ); |
| 687 | pj_lock_release(regc->lock); |
| 688 | |
| 689 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | PJ_DEF(pj_status_t) pjsip_regc_update_expires( pjsip_regc *regc, |
| 694 | pj_uint32_t expires ) |
| 695 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 696 | PJ_ASSERT_RETURN(regc, PJ_EINVAL); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 697 | |
| 698 | pj_lock_acquire(regc->lock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 699 | set_expires( regc, expires ); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 700 | pj_lock_release(regc->lock); |
| 701 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 702 | return PJ_SUCCESS; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 706 | static void call_callback(pjsip_regc *regc, pj_status_t status, int st_code, |
| 707 | const pj_str_t *reason, |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 708 | pjsip_rx_data *rdata, pj_int32_t expiration, |
| 709 | int contact_cnt, pjsip_contact_hdr *contact[]) |
| 710 | { |
| 711 | struct pjsip_regc_cbparam cbparam; |
| 712 | |
| 713 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 714 | if (!regc->cb) |
| 715 | return; |
| 716 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 717 | cbparam.regc = regc; |
| 718 | cbparam.token = regc->token; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 719 | cbparam.status = status; |
| 720 | cbparam.code = st_code; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 721 | cbparam.reason = *reason; |
| 722 | cbparam.rdata = rdata; |
| 723 | cbparam.contact_cnt = contact_cnt; |
| 724 | cbparam.expiration = expiration; |
| 725 | if (contact_cnt) { |
| 726 | pj_memcpy( cbparam.contact, contact, |
| 727 | contact_cnt*sizeof(pjsip_contact_hdr*)); |
| 728 | } |
| 729 | |
| 730 | (*regc->cb)(&cbparam); |
| 731 | } |
| 732 | |
| 733 | static void regc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
| 734 | struct pj_timer_entry *entry) |
| 735 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 736 | pjsip_regc *regc = (pjsip_regc*) entry->user_data; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 737 | pjsip_tx_data *tdata; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 738 | pj_status_t status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 739 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 740 | PJ_UNUSED_ARG(timer_heap); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 741 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 742 | /* Temporarily increase busy flag to prevent regc from being deleted |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 743 | * in pjsip_regc_send() or in the callback |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 744 | */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 745 | pj_atomic_inc(regc->busy_ctr); |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 746 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 747 | entry->id = 0; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 748 | status = pjsip_regc_register(regc, 1, &tdata); |
| 749 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 750 | status = pjsip_regc_send(regc, tdata); |
| 751 | } |
| 752 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 753 | if (status != PJ_SUCCESS && regc->cb) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 754 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 755 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 756 | call_callback(regc, status, 400, &reason, NULL, -1, 0, NULL); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 757 | } |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 758 | |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 759 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 760 | if (pj_atomic_dec_and_get(regc->busy_ctr)==0 && regc->_delete_flag) { |
Benny Prijono | f265180 | 2007-01-26 17:13:56 +0000 | [diff] [blame] | 761 | pjsip_regc_destroy(regc); |
| 762 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 765 | static pj_int32_t calculate_response_expiration(const pjsip_regc *regc, |
| 766 | const pjsip_rx_data *rdata, |
| 767 | unsigned *contact_cnt, |
| 768 | unsigned max_contact, |
| 769 | pjsip_contact_hdr *contacts[]) |
| 770 | { |
| 771 | pj_int32_t expiration = NOEXP; |
| 772 | const pjsip_msg *msg = rdata->msg_info.msg; |
| 773 | const pjsip_hdr *hdr; |
| 774 | |
| 775 | /* Enumerate all Contact headers in the response */ |
| 776 | *contact_cnt = 0; |
| 777 | for (hdr=msg->hdr.next; hdr!=&msg->hdr; hdr=hdr->next) { |
| 778 | if (hdr->type == PJSIP_H_CONTACT && |
| 779 | *contact_cnt < max_contact) |
| 780 | { |
| 781 | contacts[*contact_cnt] = (pjsip_contact_hdr*)hdr; |
| 782 | ++(*contact_cnt); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | if (regc->current_op == REGC_REGISTERING) { |
| 787 | pj_bool_t has_our_contact = PJ_FALSE; |
| 788 | const pjsip_expires_hdr *expires; |
| 789 | |
| 790 | /* Get Expires header */ |
| 791 | expires = (const pjsip_expires_hdr*) |
| 792 | pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
| 793 | |
| 794 | /* Try to find the Contact URIs that we register, in the response |
| 795 | * to get the expires value. We'll try both with comparing the URI |
| 796 | * and comparing the extension param only. |
| 797 | */ |
| 798 | if (pjsip_cfg()->regc.check_contact || regc->add_xuid_param) { |
| 799 | unsigned i; |
| 800 | for (i=0; i<*contact_cnt; ++i) { |
| 801 | const pjsip_contact_hdr *our_hdr; |
| 802 | |
| 803 | our_hdr = (const pjsip_contact_hdr*) |
| 804 | regc->contact_hdr_list.next; |
| 805 | |
| 806 | /* Match with our Contact header(s) */ |
| 807 | while ((void*)our_hdr != (void*)®c->contact_hdr_list) { |
| 808 | |
| 809 | const pjsip_uri *uri1, *uri2; |
| 810 | pj_bool_t matched = PJ_FALSE; |
| 811 | |
| 812 | /* Exclude the display name when comparing the URI |
| 813 | * since server may not return it. |
| 814 | */ |
| 815 | uri1 = (const pjsip_uri*) |
| 816 | pjsip_uri_get_uri(contacts[i]->uri); |
| 817 | uri2 = (const pjsip_uri*) |
| 818 | pjsip_uri_get_uri(our_hdr->uri); |
| 819 | |
| 820 | /* First try with exact matching, according to RFC 3261 |
| 821 | * Section 19.1.4 URI Comparison |
| 822 | */ |
| 823 | if (pjsip_cfg()->regc.check_contact) { |
| 824 | matched = pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR, |
| 825 | uri1, uri2)==0; |
| 826 | } |
| 827 | |
| 828 | /* If no match is found, try with matching the extension |
| 829 | * parameter only if extension parameter was added. |
| 830 | */ |
| 831 | if (!matched && regc->add_xuid_param && |
| 832 | (PJSIP_URI_SCHEME_IS_SIP(uri1) || |
| 833 | PJSIP_URI_SCHEME_IS_SIPS(uri1)) && |
| 834 | (PJSIP_URI_SCHEME_IS_SIP(uri2) || |
| 835 | PJSIP_URI_SCHEME_IS_SIPS(uri2))) |
| 836 | { |
| 837 | const pjsip_sip_uri *sip_uri1, *sip_uri2; |
| 838 | const pjsip_param *p1, *p2; |
| 839 | |
| 840 | sip_uri1 = (const pjsip_sip_uri*)uri1; |
| 841 | sip_uri2 = (const pjsip_sip_uri*)uri2; |
| 842 | |
| 843 | p1 = pjsip_param_cfind(&sip_uri1->other_param, |
| 844 | &XUID_PARAM_NAME); |
| 845 | p2 = pjsip_param_cfind(&sip_uri2->other_param, |
| 846 | &XUID_PARAM_NAME); |
| 847 | matched = p1 && p2 && |
| 848 | pj_strcmp(&p1->value, &p2->value)==0; |
| 849 | |
| 850 | } |
| 851 | |
| 852 | if (matched) { |
| 853 | has_our_contact = PJ_TRUE; |
| 854 | |
| 855 | if (contacts[i]->expires >= 0 && |
| 856 | contacts[i]->expires < expiration) |
| 857 | { |
| 858 | /* Get the lowest expiration time. */ |
| 859 | expiration = contacts[i]->expires; |
| 860 | } |
| 861 | |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | our_hdr = our_hdr->next; |
| 866 | |
| 867 | } /* while ((void.. */ |
| 868 | |
| 869 | } /* for (i=.. */ |
| 870 | |
| 871 | /* If matching Contact header(s) are found but the |
| 872 | * header doesn't contain expires parameter, get the |
| 873 | * expiration value from the Expires header. And |
| 874 | * if Expires header is not present, get the expiration |
| 875 | * value from the request. |
| 876 | */ |
| 877 | if (has_our_contact && expiration == NOEXP) { |
| 878 | if (expires) { |
| 879 | expiration = expires->ivalue; |
| 880 | } else if (regc->expires_hdr) { |
| 881 | expiration = regc->expires_hdr->ivalue; |
| 882 | } else { |
| 883 | /* We didn't request explicit expiration value, |
| 884 | * and server doesn't specify it either. This |
| 885 | * shouldn't happen unless we have a broken |
| 886 | * registrar. |
| 887 | */ |
| 888 | expiration = 3600; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | } |
| 893 | |
| 894 | /* If we still couldn't get matching Contact header(s), it means |
| 895 | * there must be something wrong with the registrar (e.g. it may |
| 896 | * have modified the URI's in the response, which is prohibited). |
| 897 | */ |
| 898 | if (expiration==NOEXP) { |
| 899 | /* If the number of Contact headers in the response matches |
| 900 | * ours, they're all probably ours. Get the expiration |
| 901 | * from there if this is the case, or from Expires header |
| 902 | * if we don't have exact Contact header count, or |
| 903 | * from the request as the last resort. |
| 904 | */ |
| 905 | unsigned our_contact_cnt; |
| 906 | |
| 907 | our_contact_cnt = pj_list_size(®c->contact_hdr_list); |
| 908 | |
| 909 | if (*contact_cnt == our_contact_cnt && *contact_cnt && |
| 910 | contacts[0]->expires >= 0) |
| 911 | { |
| 912 | expiration = contacts[0]->expires; |
| 913 | } else if (expires) |
| 914 | expiration = expires->ivalue; |
| 915 | else if (regc->expires_hdr) |
| 916 | expiration = regc->expires_hdr->ivalue; |
| 917 | else |
| 918 | expiration = 3600; |
| 919 | } |
| 920 | |
| 921 | } else { |
| 922 | /* Just assume that the unregistration has been successful. */ |
| 923 | expiration = 0; |
| 924 | } |
| 925 | |
| 926 | /* Must have expiration value by now */ |
| 927 | pj_assert(expiration != NOEXP); |
| 928 | |
| 929 | return expiration; |
| 930 | } |
| 931 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 932 | static void tsx_callback(void *token, pjsip_event *event) |
| 933 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 934 | pj_status_t status; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 935 | pjsip_regc *regc = (pjsip_regc*) token; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 936 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 937 | |
| 938 | pj_atomic_inc(regc->busy_ctr); |
| 939 | pj_lock_acquire(regc->lock); |
| 940 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 941 | /* Decrement pending transaction counter. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 942 | pj_assert(regc->has_tsx); |
| 943 | regc->has_tsx = PJ_FALSE; |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 944 | |
Benny Prijono | bdc093f | 2007-10-04 09:48:25 +0000 | [diff] [blame] | 945 | /* Add reference to the transport */ |
| 946 | if (tsx->transport != regc->last_transport) { |
| 947 | if (regc->last_transport) { |
| 948 | pjsip_transport_dec_ref(regc->last_transport); |
| 949 | regc->last_transport = NULL; |
| 950 | } |
| 951 | |
| 952 | if (tsx->transport) { |
| 953 | regc->last_transport = tsx->transport; |
| 954 | pjsip_transport_add_ref(regc->last_transport); |
| 955 | } |
| 956 | } |
| 957 | |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 958 | /* Handle 401/407 challenge (even when _delete_flag is set) */ |
| 959 | if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 960 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 961 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 962 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 963 | pjsip_tx_data *tdata; |
| 964 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 965 | /* reset current op */ |
| 966 | regc->current_op = REGC_IDLE; |
| 967 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 968 | status = pjsip_auth_clt_reinit_req( ®c->auth_sess, |
| 969 | rdata, |
| 970 | tsx->last_tx, |
| 971 | &tdata); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 972 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 973 | if (status == PJ_SUCCESS) { |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 974 | status = pjsip_regc_send(regc, tdata); |
Benny Prijono | 3fe034a | 2007-06-01 12:51:07 +0000 | [diff] [blame] | 975 | } |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 976 | |
| 977 | if (status != PJ_SUCCESS) { |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 978 | |
| 979 | /* Only call callback if application is still interested |
| 980 | * in it. |
| 981 | */ |
| 982 | if (regc->_delete_flag == 0) { |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 983 | /* Should be safe to release the lock temporarily. |
| 984 | * We do this to avoid deadlock. |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 985 | */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 986 | pj_lock_release(regc->lock); |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 987 | call_callback(regc, status, tsx->status_code, |
| 988 | &rdata->msg_info.msg->line.status.reason, |
| 989 | rdata, -1, 0, NULL); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 990 | pj_lock_acquire(regc->lock); |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 991 | } |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 992 | } |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 993 | |
Benny Prijono | 5899023 | 2007-01-21 16:11:18 +0000 | [diff] [blame] | 994 | } else if (regc->_delete_flag) { |
| 995 | |
| 996 | /* User has called pjsip_regc_destroy(), so don't call callback. |
| 997 | * This regc will be destroyed later in this function. |
| 998 | */ |
| 999 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1000 | /* Just reset current op */ |
| 1001 | regc->current_op = REGC_IDLE; |
Benny Prijono | 2704258 | 2006-08-08 14:04:21 +0000 | [diff] [blame] | 1002 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1003 | } else { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1004 | pjsip_rx_data *rdata; |
Benny Prijono | e1c984f | 2007-11-04 02:05:13 +0000 | [diff] [blame] | 1005 | pj_int32_t expiration = NOEXP; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1006 | unsigned contact_cnt = 0; |
| 1007 | pjsip_contact_hdr *contact[PJSIP_REGC_MAX_CONTACT]; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1008 | |
| 1009 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1010 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1011 | rdata = event->body.tsx_state.src.rdata; |
Benny Prijono | e1c984f | 2007-11-04 02:05:13 +0000 | [diff] [blame] | 1012 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1013 | /* Calculate expiration */ |
| 1014 | expiration = calculate_response_expiration(regc, rdata, |
| 1015 | &contact_cnt, |
| 1016 | PJSIP_REGC_MAX_CONTACT, |
| 1017 | contact); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1018 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1019 | /* Mark operation as complete */ |
| 1020 | regc->current_op = REGC_IDLE; |
Benny Prijono | e1c984f | 2007-11-04 02:05:13 +0000 | [diff] [blame] | 1021 | |
| 1022 | /* Schedule next registration */ |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1023 | if (regc->auto_reg && expiration > 0) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1024 | pj_time_val delay = { 0, 0}; |
| 1025 | |
| 1026 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
| 1027 | if (regc->expires != PJSIP_REGC_EXPIRATION_NOT_SPECIFIED && |
| 1028 | delay.sec > (pj_int32_t)regc->expires) |
| 1029 | { |
| 1030 | delay.sec = regc->expires; |
| 1031 | } |
| 1032 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 1033 | delay.sec = DELAY_BEFORE_REFRESH; |
| 1034 | regc->timer.cb = ®c_refresh_timer_cb; |
| 1035 | regc->timer.id = REFRESH_TIMER; |
| 1036 | regc->timer.user_data = regc; |
| 1037 | pjsip_endpt_schedule_timer( regc->endpt, ®c->timer, &delay); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1038 | pj_gettimeofday(®c->last_reg); |
| 1039 | regc->next_reg = regc->last_reg; |
| 1040 | regc->next_reg.sec += delay.sec; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | } else { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1044 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 1045 | event->body.tsx_state.src.rdata : NULL; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1048 | /* Update registration */ |
| 1049 | if (expiration==NOEXP) expiration=-1; |
| 1050 | regc->expires = expiration; |
| 1051 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1052 | /* Call callback. */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1053 | /* Should be safe to release the lock temporarily. |
| 1054 | * We do this to avoid deadlock. |
| 1055 | */ |
| 1056 | pj_lock_release(regc->lock); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1057 | call_callback(regc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1058 | (rdata ? &rdata->msg_info.msg->line.status.reason |
Benny Prijono | 25bc205 | 2009-06-02 08:17:56 +0000 | [diff] [blame] | 1059 | : &tsx->status_text), |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1060 | rdata, expiration, |
| 1061 | contact_cnt, contact); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1062 | pj_lock_acquire(regc->lock); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1065 | pj_lock_release(regc->lock); |
| 1066 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1067 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1068 | if (pj_atomic_dec_and_get(regc->busy_ctr)==0 && regc->_delete_flag) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1069 | pjsip_regc_destroy(regc); |
| 1070 | } |
| 1071 | } |
| 1072 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1073 | 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] | 1074 | { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1075 | pj_status_t status; |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 1076 | pjsip_cseq_hdr *cseq_hdr; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1077 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 1078 | pj_uint32_t cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1079 | |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1080 | pj_atomic_inc(regc->busy_ctr); |
| 1081 | pj_lock_acquire(regc->lock); |
| 1082 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1083 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 1084 | if (regc->has_tsx) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 1085 | PJ_LOG(4,(THIS_FILE, "Unable to send request, regc has another " |
| 1086 | "transaction pending")); |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1087 | pjsip_tx_data_dec_ref( tdata ); |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1088 | pj_lock_release(regc->lock); |
| 1089 | pj_atomic_dec(regc->busy_ctr); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1090 | return PJSIP_EBUSY; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1093 | pj_assert(regc->current_op == REGC_IDLE); |
| 1094 | |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1095 | /* Invalidate message buffer. */ |
| 1096 | pjsip_tx_data_invalidate_msg(tdata); |
| 1097 | |
| 1098 | /* Increment CSeq */ |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 1099 | cseq = ++regc->cseq_hdr->cseq; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1100 | cseq_hdr = (pjsip_cseq_hdr*) |
| 1101 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
Benny Prijono | 59ca70f | 2006-02-22 22:18:58 +0000 | [diff] [blame] | 1102 | cseq_hdr->cseq = cseq; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1103 | |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1104 | /* Find Expires header */ |
| 1105 | expires_hdr = (pjsip_expires_hdr*) |
| 1106 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_EXPIRES, NULL); |
| 1107 | |
Benny Prijono | 37db51c | 2007-11-23 03:41:54 +0000 | [diff] [blame] | 1108 | /* Bind to transport selector */ |
| 1109 | pjsip_tx_data_set_transport(tdata, ®c->tp_sel); |
| 1110 | |
Benny Prijono | 25a86c7 | 2006-12-01 20:50:01 +0000 | [diff] [blame] | 1111 | regc->has_tsx = PJ_TRUE; |
Benny Prijono | dd742da | 2008-05-17 12:45:00 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* Set current operation based on the value of Expires header */ |
| 1114 | if (expires_hdr && expires_hdr->ivalue==0) |
| 1115 | regc->current_op = REGC_UNREGISTERING; |
| 1116 | else |
| 1117 | regc->current_op = REGC_REGISTERING; |
| 1118 | |
Benny Prijono | 0f35f91 | 2007-02-05 18:59:31 +0000 | [diff] [blame] | 1119 | status = pjsip_endpt_send_request(regc->endpt, tdata, REGC_TSX_TIMEOUT, |
| 1120 | regc, &tsx_callback); |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 1121 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 5b65687 | 2006-08-08 00:41:00 +0000 | [diff] [blame] | 1122 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 1123 | } |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1124 | |
| 1125 | pj_lock_release(regc->lock); |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 1126 | |
| 1127 | /* Delete the record if user destroy regc during the callback. */ |
Benny Prijono | 1189365 | 2008-06-06 22:52:48 +0000 | [diff] [blame] | 1128 | if (pj_atomic_dec_and_get(regc->busy_ctr)==0 && regc->_delete_flag) { |
Benny Prijono | 197cabf | 2006-10-16 20:05:27 +0000 | [diff] [blame] | 1129 | pjsip_regc_destroy(regc); |
| 1130 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1131 | |
| 1132 | return status; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |