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