Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 20 | #include <pjsip-simple/publish.h> |
| 21 | #include <pjsip/sip_auth.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 22 | #include <pjsip/sip_endpoint.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 23 | #include <pjsip/sip_errno.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 24 | #include <pjsip/sip_event.h> |
| 25 | #include <pjsip/sip_msg.h> |
| 26 | #include <pjsip/sip_transaction.h> |
| 27 | #include <pjsip/sip_uri.h> |
| 28 | #include <pjsip/sip_util.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 29 | #include <pj/assert.h> |
| 30 | #include <pj/guid.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 31 | #include <pj/log.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 32 | #include <pj/os.h> |
| 33 | #include <pj/pool.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 34 | #include <pj/rand.h> |
| 35 | #include <pj/string.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 36 | #include <pj/timer.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | #define REFRESH_TIMER 1 |
Benny Prijono | b4d22b0 | 2009-06-05 16:10:36 +0000 | [diff] [blame] | 40 | #define DELAY_BEFORE_REFRESH PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 41 | #define THIS_FILE "publishc.c" |
| 42 | |
| 43 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 44 | /* Let's define this enum, so that it'll trigger compilation error |
| 45 | * when somebody define the same enum in sip_msg.h |
| 46 | */ |
| 47 | enum |
| 48 | { |
| 49 | PJSIP_PUBLISH_METHOD = PJSIP_OTHER_METHOD, |
| 50 | }; |
| 51 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 52 | const pjsip_method pjsip_publish_method = |
| 53 | { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 54 | (pjsip_method_e)PJSIP_PUBLISH_METHOD, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 55 | { "PUBLISH", 7 } |
| 56 | }; |
| 57 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 58 | |
| 59 | /** |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 60 | * Pending request list. |
| 61 | */ |
| 62 | typedef struct pending_publish |
| 63 | { |
| 64 | PJ_DECL_LIST_MEMBER(pjsip_tx_data); |
| 65 | } pending_publish; |
| 66 | |
| 67 | |
| 68 | /** |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 69 | * SIP client publication structure. |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 70 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 71 | struct pjsip_publishc |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 72 | { |
| 73 | pj_pool_t *pool; |
| 74 | pjsip_endpoint *endpt; |
| 75 | pj_bool_t _delete_flag; |
| 76 | int pending_tsx; |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 77 | pj_mutex_t *mutex; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 78 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 79 | pjsip_publishc_opt opt; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 80 | void *token; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 81 | pjsip_publishc_cb *cb; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 82 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 83 | pj_str_t event; |
| 84 | pj_str_t str_target_uri; |
| 85 | pjsip_uri *target_uri; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 86 | pjsip_cid_hdr *cid_hdr; |
| 87 | pjsip_cseq_hdr *cseq_hdr; |
| 88 | pj_str_t from_uri; |
| 89 | pjsip_from_hdr *from_hdr; |
| 90 | pjsip_to_hdr *to_hdr; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 91 | pj_str_t etag; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 92 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 93 | pj_uint32_t expires; |
| 94 | pjsip_route_hdr route_set; |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 95 | pjsip_hdr usr_hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 96 | |
| 97 | /* Authorization sessions. */ |
| 98 | pjsip_auth_clt_sess auth_sess; |
| 99 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 100 | /* Auto refresh publication. */ |
| 101 | pj_bool_t auto_refresh; |
| 102 | pj_time_val last_refresh; |
| 103 | pj_time_val next_refresh; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 104 | pj_timer_entry timer; |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 105 | |
| 106 | /* Pending PUBLISH request */ |
| 107 | pending_publish pending_reqs; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 111 | PJ_DEF(void) pjsip_publishc_opt_default(pjsip_publishc_opt *opt) |
| 112 | { |
| 113 | pj_bzero(opt, sizeof(*opt)); |
| 114 | opt->queue_request = PJSIP_PUBLISHC_QUEUE_REQUEST; |
| 115 | } |
| 116 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 117 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Initialize client publication module. |
| 120 | */ |
| 121 | PJ_DEF(pj_status_t) pjsip_publishc_init_module(pjsip_endpoint *endpt) |
| 122 | { |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 123 | /* Note: |
| 124 | Commented out the capability registration below, since it's |
| 125 | wrong to include PUBLISH in Allow header of INVITE requests/ |
| 126 | responses. |
| 127 | |
| 128 | 13.2.1 Creating the Initial INVITE |
| 129 | An Allow header field (Section 20.5) SHOULD be present in the |
| 130 | INVITE. It indicates what methods can be invoked within a dialog |
| 131 | |
| 132 | 20.5 Allow |
| 133 | The Allow header field lists the set of methods supported by the |
| 134 | UA generating the message. |
| 135 | |
| 136 | While the semantic of Allow header in non-dialog requests is unclear, |
| 137 | it's probably best not to include PUBLISH in Allow header for now |
| 138 | until we can find out how to customize the inclusion of methods in |
| 139 | Allow header for in-dialog vs out-dialog requests. |
| 140 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 141 | return pjsip_endpt_add_capability( endpt, NULL, PJSIP_H_ALLOW, NULL, |
| 142 | 1, &pjsip_publish_method.name); |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 143 | */ |
Benny Prijono | 10d8dbd | 2008-07-13 13:12:36 +0000 | [diff] [blame] | 144 | PJ_UNUSED_ARG(endpt); |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 145 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | PJ_DEF(pj_status_t) pjsip_publishc_create( pjsip_endpoint *endpt, |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 150 | const pjsip_publishc_opt *opt, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 151 | void *token, |
| 152 | pjsip_publishc_cb *cb, |
| 153 | pjsip_publishc **p_pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 154 | { |
| 155 | pj_pool_t *pool; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 156 | pjsip_publishc *pubc; |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 157 | pjsip_publishc_opt default_opt; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 158 | pj_status_t status; |
| 159 | |
| 160 | /* Verify arguments. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 161 | PJ_ASSERT_RETURN(endpt && cb && p_pubc, PJ_EINVAL); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 162 | |
| 163 | pool = pjsip_endpt_create_pool(endpt, "pubc%p", 1024, 1024); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 164 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 165 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 166 | pubc = PJ_POOL_ZALLOC_T(pool, pjsip_publishc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 167 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 168 | pubc->pool = pool; |
| 169 | pubc->endpt = endpt; |
| 170 | pubc->token = token; |
| 171 | pubc->cb = cb; |
| 172 | pubc->expires = PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 173 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 174 | if (!opt) { |
| 175 | pjsip_publishc_opt_default(&default_opt); |
| 176 | opt = &default_opt; |
| 177 | } |
| 178 | pj_memcpy(&pubc->opt, opt, sizeof(*opt)); |
| 179 | pj_list_init(&pubc->pending_reqs); |
| 180 | |
| 181 | status = pj_mutex_create_recursive(pubc->pool, "pubc%p", &pubc->mutex); |
| 182 | if (status != PJ_SUCCESS) { |
| 183 | pj_pool_release(pool); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 184 | return status; |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | status = pjsip_auth_clt_init(&pubc->auth_sess, endpt, pubc->pool, 0); |
| 188 | if (status != PJ_SUCCESS) { |
| 189 | pj_mutex_destroy(pubc->mutex); |
| 190 | pj_pool_release(pool); |
| 191 | return status; |
| 192 | } |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 193 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 194 | pj_list_init(&pubc->route_set); |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 195 | pj_list_init(&pubc->usr_hdr); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 196 | |
| 197 | /* Done */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 198 | *p_pubc = pubc; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 199 | return PJ_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 203 | PJ_DEF(pj_status_t) pjsip_publishc_destroy(pjsip_publishc *pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 204 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 205 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 206 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 207 | if (pubc->pending_tsx) { |
| 208 | pubc->_delete_flag = 1; |
| 209 | pubc->cb = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 210 | } else { |
Benny Prijono | 6ad7766 | 2009-10-12 01:27:40 +0000 | [diff] [blame] | 211 | /* Cancel existing timer, if any */ |
| 212 | if (pubc->timer.id != 0) { |
| 213 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 214 | pubc->timer.id = 0; |
| 215 | } |
| 216 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 217 | if (pubc->mutex) |
| 218 | pj_mutex_destroy(pubc->mutex); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 219 | pjsip_endpt_release_pool(pubc->endpt, pubc->pool); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return PJ_SUCCESS; |
| 223 | } |
| 224 | |
| 225 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 226 | PJ_DEF(pj_pool_t*) pjsip_publishc_get_pool(pjsip_publishc *pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 227 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 228 | return pubc->pool; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 231 | static void set_expires( pjsip_publishc *pubc, pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 232 | { |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 233 | if (expires != pubc->expires && |
| 234 | expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED) |
| 235 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 236 | pubc->expires_hdr = pjsip_expires_hdr_create(pubc->pool, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 237 | } else { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 238 | pubc->expires_hdr = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 243 | PJ_DEF(pj_status_t) pjsip_publishc_init(pjsip_publishc *pubc, |
| 244 | const pj_str_t *event, |
| 245 | const pj_str_t *target_uri, |
| 246 | const pj_str_t *from_uri, |
| 247 | const pj_str_t *to_uri, |
| 248 | pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 249 | { |
| 250 | pj_str_t tmp; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 251 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 252 | PJ_ASSERT_RETURN(pubc && event && target_uri && from_uri && to_uri && |
| 253 | expires, PJ_EINVAL); |
| 254 | |
| 255 | /* Copy event type */ |
| 256 | pj_strdup_with_null(pubc->pool, &pubc->event, event); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 257 | |
| 258 | /* Copy server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 259 | pj_strdup_with_null(pubc->pool, &pubc->str_target_uri, target_uri); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 260 | |
| 261 | /* Set server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 262 | tmp = pubc->str_target_uri; |
| 263 | pubc->target_uri = pjsip_parse_uri( pubc->pool, tmp.ptr, tmp.slen, 0); |
| 264 | if (pubc->target_uri == NULL) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 265 | return PJSIP_EINVALIDURI; |
| 266 | } |
| 267 | |
| 268 | /* Set "From" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 269 | pj_strdup_with_null(pubc->pool, &pubc->from_uri, from_uri); |
| 270 | tmp = pubc->from_uri; |
| 271 | pubc->from_hdr = pjsip_from_hdr_create(pubc->pool); |
| 272 | pubc->from_hdr->uri = pjsip_parse_uri(pubc->pool, tmp.ptr, tmp.slen, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 273 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 274 | if (!pubc->from_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 275 | return PJSIP_EINVALIDURI; |
| 276 | } |
| 277 | |
| 278 | /* Set "To" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 279 | pj_strdup_with_null(pubc->pool, &tmp, to_uri); |
| 280 | pubc->to_hdr = pjsip_to_hdr_create(pubc->pool); |
| 281 | pubc->to_hdr->uri = pjsip_parse_uri(pubc->pool, tmp.ptr, tmp.slen, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 282 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 283 | if (!pubc->to_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 284 | return PJSIP_EINVALIDURI; |
| 285 | } |
| 286 | |
| 287 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 288 | /* Set "Expires" header, if required. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 289 | set_expires( pubc, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 290 | |
| 291 | /* Set "Call-ID" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 292 | pubc->cid_hdr = pjsip_cid_hdr_create(pubc->pool); |
| 293 | pj_create_unique_string(pubc->pool, &pubc->cid_hdr->id); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 294 | |
| 295 | /* Set "CSeq" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 296 | pubc->cseq_hdr = pjsip_cseq_hdr_create(pubc->pool); |
| 297 | pubc->cseq_hdr->cseq = pj_rand() % 0xFFFF; |
| 298 | pjsip_method_set( &pubc->cseq_hdr->method, PJSIP_REGISTER_METHOD); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 299 | |
| 300 | /* Done. */ |
| 301 | return PJ_SUCCESS; |
| 302 | } |
| 303 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 304 | PJ_DEF(pj_status_t) pjsip_publishc_set_credentials( pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 305 | int count, |
| 306 | const pjsip_cred_info cred[] ) |
| 307 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 308 | PJ_ASSERT_RETURN(pubc && count && cred, PJ_EINVAL); |
| 309 | return pjsip_auth_clt_set_credentials(&pubc->auth_sess, count, cred); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 312 | PJ_DEF(pj_status_t) pjsip_publishc_set_route_set( pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 313 | const pjsip_route_hdr *route_set) |
| 314 | { |
| 315 | const pjsip_route_hdr *chdr; |
| 316 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 317 | PJ_ASSERT_RETURN(pubc && route_set, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 318 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 319 | pj_list_init(&pubc->route_set); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 320 | |
| 321 | chdr = route_set->next; |
| 322 | while (chdr != route_set) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 323 | pj_list_push_back(&pubc->route_set, pjsip_hdr_clone(pubc->pool, chdr)); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 324 | chdr = chdr->next; |
| 325 | } |
| 326 | |
| 327 | return PJ_SUCCESS; |
| 328 | } |
| 329 | |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 330 | PJ_DEF(pj_status_t) pjsip_publishc_set_headers( pjsip_publishc *pubc, |
| 331 | const pjsip_hdr *hdr_list) |
| 332 | { |
| 333 | const pjsip_hdr *h; |
| 334 | |
| 335 | PJ_ASSERT_RETURN(pubc && hdr_list, PJ_EINVAL); |
| 336 | |
| 337 | pj_list_init(&pubc->usr_hdr); |
| 338 | h = hdr_list->next; |
| 339 | while (h != hdr_list) { |
| 340 | pj_list_push_back(&pubc->usr_hdr, pjsip_hdr_clone(pubc->pool, h)); |
| 341 | h = h->next; |
| 342 | } |
| 343 | |
| 344 | return PJ_SUCCESS; |
| 345 | } |
| 346 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 347 | static pj_status_t create_request(pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 348 | pjsip_tx_data **p_tdata) |
| 349 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 350 | const pj_str_t STR_EVENT = { "Event", 5 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 351 | pj_status_t status; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 352 | pjsip_generic_string_hdr *hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 353 | pjsip_tx_data *tdata; |
| 354 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 355 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 356 | |
| 357 | /* Create the request. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 358 | status = pjsip_endpt_create_request_from_hdr( pubc->endpt, |
| 359 | &pjsip_publish_method, |
| 360 | pubc->target_uri, |
| 361 | pubc->from_hdr, |
| 362 | pubc->to_hdr, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 363 | NULL, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 364 | pubc->cid_hdr, |
| 365 | pubc->cseq_hdr->cseq, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 366 | NULL, |
| 367 | &tdata); |
| 368 | if (status != PJ_SUCCESS) |
| 369 | return status; |
| 370 | |
| 371 | /* Add cached authorization headers. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 372 | pjsip_auth_clt_init_req( &pubc->auth_sess, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 373 | |
| 374 | /* Add Route headers from route set, ideally after Via header */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 375 | if (!pj_list_empty(&pubc->route_set)) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 376 | pjsip_hdr *route_pos; |
| 377 | const pjsip_route_hdr *route; |
| 378 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 379 | route_pos = (pjsip_hdr*) |
| 380 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 381 | if (!route_pos) |
| 382 | route_pos = &tdata->msg->hdr; |
| 383 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 384 | route = pubc->route_set.next; |
| 385 | while (route != &pubc->route_set) { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 386 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 387 | pjsip_hdr_shallow_clone(tdata->pool, route); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 388 | pj_list_insert_after(route_pos, new_hdr); |
| 389 | route_pos = new_hdr; |
| 390 | route = route->next; |
| 391 | } |
| 392 | } |
| 393 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 394 | /* Add Event header */ |
| 395 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_EVENT, |
| 396 | &pubc->event); |
| 397 | if (hdr) |
| 398 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 399 | |
| 400 | |
| 401 | /* Add SIP-If-Match if we have etag */ |
| 402 | if (pubc->etag.slen) { |
| 403 | const pj_str_t STR_HNAME = { "SIP-If-Match", 12 }; |
| 404 | |
| 405 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_HNAME, |
| 406 | &pubc->etag); |
| 407 | if (hdr) |
| 408 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 409 | } |
| 410 | |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 411 | /* Add user headers */ |
| 412 | if (!pj_list_empty(&pubc->usr_hdr)) { |
| 413 | const pjsip_hdr *hdr; |
| 414 | |
| 415 | hdr = pubc->usr_hdr.next; |
| 416 | while (hdr != &pubc->usr_hdr) { |
| 417 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 418 | pjsip_hdr_shallow_clone(tdata->pool, hdr); |
| 419 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 420 | hdr = hdr->next; |
| 421 | } |
| 422 | } |
| 423 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 424 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 425 | /* Done. */ |
| 426 | *p_tdata = tdata; |
| 427 | return PJ_SUCCESS; |
| 428 | } |
| 429 | |
| 430 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 431 | PJ_DEF(pj_status_t) pjsip_publishc_publish(pjsip_publishc *pubc, |
| 432 | pj_bool_t auto_refresh, |
| 433 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 434 | { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 435 | pj_status_t status; |
| 436 | pjsip_tx_data *tdata; |
| 437 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 438 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 439 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 440 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 441 | if (status != PJ_SUCCESS) |
| 442 | return status; |
| 443 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 444 | /* Add Expires header */ |
| 445 | if (pubc->expires_hdr) { |
| 446 | pjsip_hdr *dup; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 447 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 448 | dup = (pjsip_hdr*) |
| 449 | pjsip_hdr_shallow_clone(tdata->pool, pubc->expires_hdr); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 450 | if (dup) |
| 451 | pjsip_msg_add_hdr(tdata->msg, dup); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 454 | /* Cancel existing timer */ |
| 455 | if (pubc->timer.id != 0) { |
| 456 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 457 | pubc->timer.id = 0; |
| 458 | } |
| 459 | |
| 460 | pubc->auto_refresh = auto_refresh; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 461 | |
| 462 | /* Done */ |
| 463 | *p_tdata = tdata; |
| 464 | return PJ_SUCCESS; |
| 465 | } |
| 466 | |
| 467 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 468 | PJ_DEF(pj_status_t) pjsip_publishc_unpublish(pjsip_publishc *pubc, |
| 469 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 470 | { |
| 471 | pjsip_tx_data *tdata; |
| 472 | pjsip_msg *msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 473 | pjsip_expires_hdr *expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 474 | pj_status_t status; |
| 475 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 476 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 477 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 478 | if (pubc->timer.id != 0) { |
| 479 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 480 | pubc->timer.id = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 483 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 484 | if (status != PJ_SUCCESS) |
| 485 | return status; |
| 486 | |
| 487 | msg = tdata->msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 488 | |
| 489 | /* Add Expires:0 header */ |
| 490 | expires = pjsip_expires_hdr_create(tdata->pool, 0); |
| 491 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 492 | |
| 493 | *p_tdata = tdata; |
| 494 | return PJ_SUCCESS; |
| 495 | } |
| 496 | |
| 497 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 498 | PJ_DEF(pj_status_t) pjsip_publishc_update_expires( pjsip_publishc *pubc, |
| 499 | pj_uint32_t expires ) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 500 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 501 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
| 502 | set_expires( pubc, expires ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 503 | return PJ_SUCCESS; |
| 504 | } |
| 505 | |
| 506 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 507 | static void call_callback(pjsip_publishc *pubc, pj_status_t status, |
| 508 | int st_code, const pj_str_t *reason, |
| 509 | pjsip_rx_data *rdata, pj_int32_t expiration) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 510 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 511 | struct pjsip_publishc_cbparam cbparam; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 512 | |
| 513 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 514 | cbparam.pubc = pubc; |
| 515 | cbparam.token = pubc->token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 516 | cbparam.status = status; |
| 517 | cbparam.code = st_code; |
| 518 | cbparam.reason = *reason; |
| 519 | cbparam.rdata = rdata; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 520 | cbparam.expiration = expiration; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 521 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 522 | (*pubc->cb)(&cbparam); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 525 | static void pubc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 526 | struct pj_timer_entry *entry) |
| 527 | { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 528 | pjsip_publishc *pubc = (pjsip_publishc*) entry->user_data; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 529 | pjsip_tx_data *tdata; |
| 530 | pj_status_t status; |
| 531 | |
| 532 | PJ_UNUSED_ARG(timer_heap); |
| 533 | |
| 534 | entry->id = 0; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 535 | status = pjsip_publishc_publish(pubc, 1, &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame] | 536 | if (status != PJ_SUCCESS) { |
| 537 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 538 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
| 539 | call_callback(pubc, status, 400, &reason, NULL, -1); |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | status = pjsip_publishc_send(pubc, tdata); |
| 544 | /* No need to call callback as it should have been called */ |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static void tsx_callback(void *token, pjsip_event *event) |
| 548 | { |
| 549 | pj_status_t status; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 550 | pjsip_publishc *pubc = (pjsip_publishc*) token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 551 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
| 552 | |
| 553 | /* Decrement pending transaction counter. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 554 | pj_assert(pubc->pending_tsx > 0); |
| 555 | --pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 556 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 557 | /* If publication data has been deleted by user then remove publication |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 558 | * data from transaction's callback, and don't call callback. |
| 559 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 560 | if (pubc->_delete_flag) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 561 | |
| 562 | /* Nothing to do */ |
| 563 | ; |
| 564 | |
| 565 | } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 566 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
| 567 | { |
| 568 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
| 569 | pjsip_tx_data *tdata; |
| 570 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 571 | status = pjsip_auth_clt_reinit_req( &pubc->auth_sess, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 572 | rdata, |
| 573 | tsx->last_tx, |
| 574 | &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame] | 575 | if (status != PJ_SUCCESS) { |
| 576 | call_callback(pubc, status, tsx->status_code, |
| 577 | &rdata->msg_info.msg->line.status.reason, |
| 578 | rdata, -1); |
| 579 | } else { |
| 580 | status = pjsip_publishc_send(pubc, tdata); |
| 581 | } |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 582 | |
| 583 | } else { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 584 | pjsip_rx_data *rdata; |
| 585 | pj_int32_t expiration = 0xFFFF; |
| 586 | |
| 587 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 588 | pjsip_msg *msg; |
| 589 | pjsip_expires_hdr *expires; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 590 | pjsip_generic_string_hdr *etag_hdr; |
| 591 | const pj_str_t STR_ETAG = { "SIP-ETag", 8 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 592 | |
| 593 | rdata = event->body.tsx_state.src.rdata; |
| 594 | msg = rdata->msg_info.msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 595 | |
| 596 | /* Save ETag value */ |
| 597 | etag_hdr = (pjsip_generic_string_hdr*) |
| 598 | pjsip_msg_find_hdr_by_name(msg, &STR_ETAG, NULL); |
| 599 | if (etag_hdr) { |
| 600 | pj_strdup(pubc->pool, &pubc->etag, &etag_hdr->hvalue); |
| 601 | } else { |
| 602 | pubc->etag.slen = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 605 | /* Update expires value */ |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 606 | expires = (pjsip_expires_hdr*) |
| 607 | pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 608 | |
Benny Prijono | 53984d1 | 2009-04-28 22:19:49 +0000 | [diff] [blame] | 609 | if (pubc->auto_refresh && expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 610 | expiration = expires->ivalue; |
| 611 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 612 | if (pubc->auto_refresh && expiration!=0 && expiration!=0xFFFF) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 613 | pj_time_val delay = { 0, 0}; |
| 614 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 615 | /* Cancel existing timer, if any */ |
| 616 | if (pubc->timer.id != 0) { |
| 617 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 618 | pubc->timer.id = 0; |
| 619 | } |
| 620 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 621 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 622 | if (pubc->expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED && |
| 623 | delay.sec > (pj_int32_t)pubc->expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 624 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 625 | delay.sec = pubc->expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 626 | } |
| 627 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 628 | delay.sec = DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 629 | pubc->timer.cb = &pubc_refresh_timer_cb; |
| 630 | pubc->timer.id = REFRESH_TIMER; |
| 631 | pubc->timer.user_data = pubc; |
| 632 | pjsip_endpt_schedule_timer( pubc->endpt, &pubc->timer, &delay); |
| 633 | pj_gettimeofday(&pubc->last_refresh); |
| 634 | pubc->next_refresh = pubc->last_refresh; |
| 635 | pubc->next_refresh.sec += delay.sec; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | } else { |
| 639 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 640 | event->body.tsx_state.src.rdata : NULL; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /* Call callback. */ |
| 645 | if (expiration == 0xFFFF) expiration = -1; |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 646 | |
| 647 | /* Temporarily increment pending_tsx to prevent callback from |
| 648 | * destroying pubc. |
| 649 | */ |
| 650 | ++pubc->pending_tsx; |
| 651 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 652 | call_callback(pubc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 653 | (rdata ? &rdata->msg_info.msg->line.status.reason |
| 654 | : pjsip_get_status_text(tsx->status_code)), |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 655 | rdata, expiration); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 656 | |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 657 | --pubc->pending_tsx; |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 658 | |
| 659 | /* If we have pending request(s), send them now */ |
| 660 | pj_mutex_lock(pubc->mutex); |
| 661 | while (!pj_list_empty(&pubc->pending_reqs)) { |
| 662 | pjsip_tx_data *tdata = pubc->pending_reqs.next; |
| 663 | pj_list_erase(tdata); |
Benny Prijono | c7b3c3b | 2010-02-24 11:22:21 +0000 | [diff] [blame] | 664 | |
| 665 | /* Add SIP-If-Match if we have etag and the request doesn't have |
| 666 | * one (http://trac.pjsip.org/repos/ticket/996) |
| 667 | */ |
| 668 | if (pubc->etag.slen) { |
| 669 | const pj_str_t STR_HNAME = { "SIP-If-Match", 12 }; |
| 670 | pjsip_generic_string_hdr *sim_hdr; |
| 671 | |
| 672 | sim_hdr = (pjsip_generic_string_hdr*) |
| 673 | pjsip_msg_find_hdr_by_name(tdata->msg, &STR_HNAME, NULL); |
| 674 | if (!sim_hdr) { |
| 675 | /* Create the header */ |
| 676 | sim_hdr = pjsip_generic_string_hdr_create(tdata->pool, |
| 677 | &STR_HNAME, |
| 678 | &pubc->etag); |
| 679 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)sim_hdr); |
| 680 | |
| 681 | } else { |
| 682 | /* Update */ |
| 683 | if (pj_strcmp(&pubc->etag, &sim_hdr->hvalue)) |
| 684 | pj_strdup(tdata->pool, &sim_hdr->hvalue, &pubc->etag); |
| 685 | } |
| 686 | } |
| 687 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 688 | status = pjsip_publishc_send(pubc, tdata); |
| 689 | if (status == PJ_EPENDING) { |
| 690 | pj_assert(!"Not expected"); |
| 691 | pj_list_erase(tdata); |
| 692 | pjsip_tx_data_dec_ref(tdata); |
| 693 | } else if (status == PJ_SUCCESS) { |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | pj_mutex_unlock(pubc->mutex); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 700 | /* Delete the record if user destroy pubc during the callback. */ |
| 701 | if (pubc->_delete_flag && pubc->pending_tsx==0) { |
| 702 | pjsip_publishc_destroy(pubc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 706 | |
| 707 | PJ_DEF(pj_status_t) pjsip_publishc_send(pjsip_publishc *pubc, |
| 708 | pjsip_tx_data *tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 709 | { |
| 710 | pj_status_t status; |
| 711 | pjsip_cseq_hdr *cseq_hdr; |
| 712 | pj_uint32_t cseq; |
| 713 | |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 714 | PJ_ASSERT_RETURN(pubc && tdata, PJ_EINVAL); |
| 715 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 716 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 717 | pj_mutex_lock(pubc->mutex); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 718 | if (pubc->pending_tsx) { |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 719 | if (pubc->opt.queue_request) { |
| 720 | pj_list_push_back(&pubc->pending_reqs, tdata); |
| 721 | pj_mutex_unlock(pubc->mutex); |
| 722 | PJ_LOG(4,(THIS_FILE, "Request is queued, pubc has another " |
| 723 | "transaction pending")); |
| 724 | return PJ_EPENDING; |
| 725 | } else { |
| 726 | pjsip_tx_data_dec_ref(tdata); |
| 727 | pj_mutex_unlock(pubc->mutex); |
| 728 | PJ_LOG(4,(THIS_FILE, "Unable to send request, pubc has another " |
| 729 | "transaction pending")); |
| 730 | return PJ_EBUSY; |
| 731 | } |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 732 | } |
Benny Prijono | fe50c9e | 2009-10-12 07:44:14 +0000 | [diff] [blame] | 733 | pj_mutex_unlock(pubc->mutex); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 734 | |
| 735 | /* Invalidate message buffer. */ |
| 736 | pjsip_tx_data_invalidate_msg(tdata); |
| 737 | |
| 738 | /* Increment CSeq */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 739 | cseq = ++pubc->cseq_hdr->cseq; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 740 | cseq_hdr = (pjsip_cseq_hdr*) |
| 741 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 742 | cseq_hdr->cseq = cseq; |
| 743 | |
| 744 | /* Increment pending transaction first, since transaction callback |
| 745 | * may be called even before send_request() returns! |
| 746 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 747 | ++pubc->pending_tsx; |
| 748 | status = pjsip_endpt_send_request(pubc->endpt, tdata, -1, pubc, |
| 749 | &tsx_callback); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 750 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 751 | // no need to decrement, callback has been called and it should |
| 752 | // already decremented pending_tsx. Decrementing this here may |
| 753 | // cause accessing freed memory location. |
| 754 | //--pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 755 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 756 | } |
| 757 | |
| 758 | return status; |
| 759 | } |
| 760 | |