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