Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +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 | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 19 | #include <pjsip-simple/publish.h> |
| 20 | #include <pjsip/sip_auth.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 21 | #include <pjsip/sip_endpoint.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 22 | #include <pjsip/sip_errno.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 23 | #include <pjsip/sip_event.h> |
| 24 | #include <pjsip/sip_msg.h> |
| 25 | #include <pjsip/sip_transaction.h> |
| 26 | #include <pjsip/sip_uri.h> |
| 27 | #include <pjsip/sip_util.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 28 | #include <pj/assert.h> |
| 29 | #include <pj/guid.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 30 | #include <pj/log.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 31 | #include <pj/os.h> |
| 32 | #include <pj/pool.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 33 | #include <pj/rand.h> |
| 34 | #include <pj/string.h> |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 35 | #include <pj/timer.h> |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | #define REFRESH_TIMER 1 |
| 39 | #define DELAY_BEFORE_REFRESH 5 |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 40 | #define THIS_FILE "publishc.c" |
| 41 | |
| 42 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 43 | /* Let's define this enum, so that it'll trigger compilation error |
| 44 | * when somebody define the same enum in sip_msg.h |
| 45 | */ |
| 46 | enum |
| 47 | { |
| 48 | PJSIP_PUBLISH_METHOD = PJSIP_OTHER_METHOD, |
| 49 | }; |
| 50 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 51 | const pjsip_method pjsip_publish_method = |
| 52 | { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 53 | (pjsip_method_e)PJSIP_PUBLISH_METHOD, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 54 | { "PUBLISH", 7 } |
| 55 | }; |
| 56 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 59 | * SIP client publication structure. |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 60 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 61 | struct pjsip_publishc |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 62 | { |
| 63 | pj_pool_t *pool; |
| 64 | pjsip_endpoint *endpt; |
| 65 | pj_bool_t _delete_flag; |
| 66 | int pending_tsx; |
| 67 | |
| 68 | void *token; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 69 | pjsip_publishc_cb *cb; |
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 | pj_str_t event; |
| 72 | pj_str_t str_target_uri; |
| 73 | pjsip_uri *target_uri; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 74 | pjsip_cid_hdr *cid_hdr; |
| 75 | pjsip_cseq_hdr *cseq_hdr; |
| 76 | pj_str_t from_uri; |
| 77 | pjsip_from_hdr *from_hdr; |
| 78 | pjsip_to_hdr *to_hdr; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 79 | pj_str_t etag; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 80 | pjsip_expires_hdr *expires_hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 81 | pj_uint32_t expires; |
| 82 | pjsip_route_hdr route_set; |
| 83 | |
| 84 | /* Authorization sessions. */ |
| 85 | pjsip_auth_clt_sess auth_sess; |
| 86 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 87 | /* Auto refresh publication. */ |
| 88 | pj_bool_t auto_refresh; |
| 89 | pj_time_val last_refresh; |
| 90 | pj_time_val next_refresh; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 91 | pj_timer_entry timer; |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 96 | /* |
| 97 | * Initialize client publication module. |
| 98 | */ |
| 99 | PJ_DEF(pj_status_t) pjsip_publishc_init_module(pjsip_endpoint *endpt) |
| 100 | { |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 101 | /* Note: |
| 102 | Commented out the capability registration below, since it's |
| 103 | wrong to include PUBLISH in Allow header of INVITE requests/ |
| 104 | responses. |
| 105 | |
| 106 | 13.2.1 Creating the Initial INVITE |
| 107 | An Allow header field (Section 20.5) SHOULD be present in the |
| 108 | INVITE. It indicates what methods can be invoked within a dialog |
| 109 | |
| 110 | 20.5 Allow |
| 111 | The Allow header field lists the set of methods supported by the |
| 112 | UA generating the message. |
| 113 | |
| 114 | While the semantic of Allow header in non-dialog requests is unclear, |
| 115 | it's probably best not to include PUBLISH in Allow header for now |
| 116 | until we can find out how to customize the inclusion of methods in |
| 117 | Allow header for in-dialog vs out-dialog requests. |
| 118 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 119 | return pjsip_endpt_add_capability( endpt, NULL, PJSIP_H_ALLOW, NULL, |
| 120 | 1, &pjsip_publish_method.name); |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 121 | */ |
Benny Prijono | 10d8dbd | 2008-07-13 13:12:36 +0000 | [diff] [blame] | 122 | PJ_UNUSED_ARG(endpt); |
Benny Prijono | 7741c8a | 2008-07-07 19:57:16 +0000 | [diff] [blame] | 123 | return PJ_SUCCESS; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | PJ_DEF(pj_status_t) pjsip_publishc_create( pjsip_endpoint *endpt, |
| 128 | unsigned options, |
| 129 | void *token, |
| 130 | pjsip_publishc_cb *cb, |
| 131 | pjsip_publishc **p_pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 132 | { |
| 133 | pj_pool_t *pool; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 134 | pjsip_publishc *pubc; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 135 | pj_status_t status; |
| 136 | |
| 137 | /* Verify arguments. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 138 | PJ_ASSERT_RETURN(endpt && cb && p_pubc, PJ_EINVAL); |
| 139 | PJ_ASSERT_RETURN(options == 0, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 140 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 141 | PJ_UNUSED_ARG(options); |
| 142 | |
| 143 | pool = pjsip_endpt_create_pool(endpt, "pubc%p", 1024, 1024); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 144 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 145 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 146 | pubc = PJ_POOL_ZALLOC_T(pool, pjsip_publishc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 147 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 148 | pubc->pool = pool; |
| 149 | pubc->endpt = endpt; |
| 150 | pubc->token = token; |
| 151 | pubc->cb = cb; |
| 152 | pubc->expires = PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 153 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 154 | status = pjsip_auth_clt_init(&pubc->auth_sess, endpt, pubc->pool, 0); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 155 | if (status != PJ_SUCCESS) |
| 156 | return status; |
| 157 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 158 | pj_list_init(&pubc->route_set); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 159 | |
| 160 | /* Done */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 161 | *p_pubc = pubc; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 162 | return PJ_SUCCESS; |
| 163 | } |
| 164 | |
| 165 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 166 | PJ_DEF(pj_status_t) pjsip_publishc_destroy(pjsip_publishc *pubc) |
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 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 169 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 170 | if (pubc->pending_tsx) { |
| 171 | pubc->_delete_flag = 1; |
| 172 | pubc->cb = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 173 | } else { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 174 | pjsip_endpt_release_pool(pubc->endpt, pubc->pool); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | return PJ_SUCCESS; |
| 178 | } |
| 179 | |
| 180 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 181 | PJ_DEF(pj_pool_t*) pjsip_publishc_get_pool(pjsip_publishc *pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 182 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 183 | return pubc->pool; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 186 | static void set_expires( pjsip_publishc *pubc, pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 187 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 188 | if (expires != pubc->expires) { |
| 189 | pubc->expires_hdr = pjsip_expires_hdr_create(pubc->pool, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 190 | } else { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 191 | pubc->expires_hdr = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 196 | PJ_DEF(pj_status_t) pjsip_publishc_init(pjsip_publishc *pubc, |
| 197 | const pj_str_t *event, |
| 198 | const pj_str_t *target_uri, |
| 199 | const pj_str_t *from_uri, |
| 200 | const pj_str_t *to_uri, |
| 201 | pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 202 | { |
| 203 | pj_str_t tmp; |
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 && event && target_uri && from_uri && to_uri && |
| 206 | expires, PJ_EINVAL); |
| 207 | |
| 208 | /* Copy event type */ |
| 209 | pj_strdup_with_null(pubc->pool, &pubc->event, event); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 210 | |
| 211 | /* Copy server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 212 | pj_strdup_with_null(pubc->pool, &pubc->str_target_uri, target_uri); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 213 | |
| 214 | /* Set server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 215 | tmp = pubc->str_target_uri; |
| 216 | pubc->target_uri = pjsip_parse_uri( pubc->pool, tmp.ptr, tmp.slen, 0); |
| 217 | if (pubc->target_uri == NULL) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 218 | return PJSIP_EINVALIDURI; |
| 219 | } |
| 220 | |
| 221 | /* Set "From" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 222 | pj_strdup_with_null(pubc->pool, &pubc->from_uri, from_uri); |
| 223 | tmp = pubc->from_uri; |
| 224 | pubc->from_hdr = pjsip_from_hdr_create(pubc->pool); |
| 225 | 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] | 226 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 227 | if (!pubc->from_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 228 | return PJSIP_EINVALIDURI; |
| 229 | } |
| 230 | |
| 231 | /* Set "To" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 232 | pj_strdup_with_null(pubc->pool, &tmp, to_uri); |
| 233 | pubc->to_hdr = pjsip_to_hdr_create(pubc->pool); |
| 234 | 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] | 235 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 236 | if (!pubc->to_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 237 | return PJSIP_EINVALIDURI; |
| 238 | } |
| 239 | |
| 240 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 241 | /* Set "Expires" header, if required. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 242 | set_expires( pubc, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 243 | |
| 244 | /* Set "Call-ID" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 245 | pubc->cid_hdr = pjsip_cid_hdr_create(pubc->pool); |
| 246 | pj_create_unique_string(pubc->pool, &pubc->cid_hdr->id); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 247 | |
| 248 | /* Set "CSeq" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 249 | pubc->cseq_hdr = pjsip_cseq_hdr_create(pubc->pool); |
| 250 | pubc->cseq_hdr->cseq = pj_rand() % 0xFFFF; |
| 251 | pjsip_method_set( &pubc->cseq_hdr->method, PJSIP_REGISTER_METHOD); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 252 | |
| 253 | /* Done. */ |
| 254 | return PJ_SUCCESS; |
| 255 | } |
| 256 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 257 | PJ_DEF(pj_status_t) pjsip_publishc_set_credentials( pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 258 | int count, |
| 259 | const pjsip_cred_info cred[] ) |
| 260 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 261 | PJ_ASSERT_RETURN(pubc && count && cred, PJ_EINVAL); |
| 262 | return pjsip_auth_clt_set_credentials(&pubc->auth_sess, count, cred); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 265 | 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] | 266 | const pjsip_route_hdr *route_set) |
| 267 | { |
| 268 | const pjsip_route_hdr *chdr; |
| 269 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 270 | PJ_ASSERT_RETURN(pubc && route_set, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 271 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 272 | pj_list_init(&pubc->route_set); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 273 | |
| 274 | chdr = route_set->next; |
| 275 | while (chdr != route_set) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 276 | 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] | 277 | chdr = chdr->next; |
| 278 | } |
| 279 | |
| 280 | return PJ_SUCCESS; |
| 281 | } |
| 282 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 283 | static pj_status_t create_request(pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 284 | pjsip_tx_data **p_tdata) |
| 285 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 286 | const pj_str_t STR_EVENT = { "Event", 5 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 287 | pj_status_t status; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 288 | pjsip_generic_string_hdr *hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 289 | pjsip_tx_data *tdata; |
| 290 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 291 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 292 | |
| 293 | /* Create the request. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 294 | status = pjsip_endpt_create_request_from_hdr( pubc->endpt, |
| 295 | &pjsip_publish_method, |
| 296 | pubc->target_uri, |
| 297 | pubc->from_hdr, |
| 298 | pubc->to_hdr, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 299 | NULL, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 300 | pubc->cid_hdr, |
| 301 | pubc->cseq_hdr->cseq, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 302 | NULL, |
| 303 | &tdata); |
| 304 | if (status != PJ_SUCCESS) |
| 305 | return status; |
| 306 | |
| 307 | /* Add cached authorization headers. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 308 | pjsip_auth_clt_init_req( &pubc->auth_sess, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 309 | |
| 310 | /* Add Route headers from route set, ideally after Via header */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 311 | if (!pj_list_empty(&pubc->route_set)) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 312 | pjsip_hdr *route_pos; |
| 313 | const pjsip_route_hdr *route; |
| 314 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 315 | route_pos = (pjsip_hdr*) |
| 316 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 317 | if (!route_pos) |
| 318 | route_pos = &tdata->msg->hdr; |
| 319 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 320 | route = pubc->route_set.next; |
| 321 | while (route != &pubc->route_set) { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 322 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 323 | pjsip_hdr_shallow_clone(tdata->pool, route); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 324 | pj_list_insert_after(route_pos, new_hdr); |
| 325 | route_pos = new_hdr; |
| 326 | route = route->next; |
| 327 | } |
| 328 | } |
| 329 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 330 | /* Add Event header */ |
| 331 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_EVENT, |
| 332 | &pubc->event); |
| 333 | if (hdr) |
| 334 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 335 | |
| 336 | |
| 337 | /* Add SIP-If-Match if we have etag */ |
| 338 | if (pubc->etag.slen) { |
| 339 | const pj_str_t STR_HNAME = { "SIP-If-Match", 12 }; |
| 340 | |
| 341 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_HNAME, |
| 342 | &pubc->etag); |
| 343 | if (hdr) |
| 344 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 345 | } |
| 346 | |
| 347 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 348 | /* Done. */ |
| 349 | *p_tdata = tdata; |
| 350 | return PJ_SUCCESS; |
| 351 | } |
| 352 | |
| 353 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 354 | PJ_DEF(pj_status_t) pjsip_publishc_publish(pjsip_publishc *pubc, |
| 355 | pj_bool_t auto_refresh, |
| 356 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 357 | { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 358 | pj_status_t status; |
| 359 | pjsip_tx_data *tdata; |
| 360 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 361 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 362 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 363 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 364 | if (status != PJ_SUCCESS) |
| 365 | return status; |
| 366 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 367 | /* Add Expires header */ |
| 368 | if (pubc->expires_hdr) { |
| 369 | pjsip_hdr *dup; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 370 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 371 | dup = (pjsip_hdr*) |
| 372 | pjsip_hdr_shallow_clone(tdata->pool, pubc->expires_hdr); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 373 | if (dup) |
| 374 | pjsip_msg_add_hdr(tdata->msg, dup); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 377 | /* Cancel existing timer */ |
| 378 | if (pubc->timer.id != 0) { |
| 379 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 380 | pubc->timer.id = 0; |
| 381 | } |
| 382 | |
| 383 | pubc->auto_refresh = auto_refresh; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 384 | |
| 385 | /* Done */ |
| 386 | *p_tdata = tdata; |
| 387 | return PJ_SUCCESS; |
| 388 | } |
| 389 | |
| 390 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 391 | PJ_DEF(pj_status_t) pjsip_publishc_unpublish(pjsip_publishc *pubc, |
| 392 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 393 | { |
| 394 | pjsip_tx_data *tdata; |
| 395 | pjsip_msg *msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 396 | pjsip_expires_hdr *expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 397 | pj_status_t status; |
| 398 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 399 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 400 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 401 | if (pubc->timer.id != 0) { |
| 402 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 403 | pubc->timer.id = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 406 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 407 | if (status != PJ_SUCCESS) |
| 408 | return status; |
| 409 | |
| 410 | msg = tdata->msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 411 | |
| 412 | /* Add Expires:0 header */ |
| 413 | expires = pjsip_expires_hdr_create(tdata->pool, 0); |
| 414 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 415 | |
| 416 | *p_tdata = tdata; |
| 417 | return PJ_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 421 | PJ_DEF(pj_status_t) pjsip_publishc_update_expires( pjsip_publishc *pubc, |
| 422 | pj_uint32_t expires ) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 423 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 424 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
| 425 | set_expires( pubc, expires ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 426 | return PJ_SUCCESS; |
| 427 | } |
| 428 | |
| 429 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 430 | static void call_callback(pjsip_publishc *pubc, pj_status_t status, |
| 431 | int st_code, const pj_str_t *reason, |
| 432 | pjsip_rx_data *rdata, pj_int32_t expiration) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 433 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 434 | struct pjsip_publishc_cbparam cbparam; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 435 | |
| 436 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 437 | cbparam.pubc = pubc; |
| 438 | cbparam.token = pubc->token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 439 | cbparam.status = status; |
| 440 | cbparam.code = st_code; |
| 441 | cbparam.reason = *reason; |
| 442 | cbparam.rdata = rdata; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 443 | cbparam.expiration = expiration; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 444 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 445 | (*pubc->cb)(&cbparam); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 448 | static void pubc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 449 | struct pj_timer_entry *entry) |
| 450 | { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 451 | pjsip_publishc *pubc = (pjsip_publishc*) entry->user_data; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 452 | pjsip_tx_data *tdata; |
| 453 | pj_status_t status; |
| 454 | |
| 455 | PJ_UNUSED_ARG(timer_heap); |
| 456 | |
| 457 | entry->id = 0; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 458 | status = pjsip_publishc_publish(pubc, 1, &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame] | 459 | if (status != PJ_SUCCESS) { |
| 460 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 461 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
| 462 | call_callback(pubc, status, 400, &reason, NULL, -1); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | status = pjsip_publishc_send(pubc, tdata); |
| 467 | /* No need to call callback as it should have been called */ |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | static void tsx_callback(void *token, pjsip_event *event) |
| 471 | { |
| 472 | pj_status_t status; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 473 | pjsip_publishc *pubc = (pjsip_publishc*) token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 474 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
| 475 | |
| 476 | /* Decrement pending transaction counter. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 477 | pj_assert(pubc->pending_tsx > 0); |
| 478 | --pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 479 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 480 | /* If publication data has been deleted by user then remove publication |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 481 | * data from transaction's callback, and don't call callback. |
| 482 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 483 | if (pubc->_delete_flag) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 484 | |
| 485 | /* Nothing to do */ |
| 486 | ; |
| 487 | |
| 488 | } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 489 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
| 490 | { |
| 491 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
| 492 | pjsip_tx_data *tdata; |
| 493 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 494 | status = pjsip_auth_clt_reinit_req( &pubc->auth_sess, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 495 | rdata, |
| 496 | tsx->last_tx, |
| 497 | &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame] | 498 | if (status != PJ_SUCCESS) { |
| 499 | call_callback(pubc, status, tsx->status_code, |
| 500 | &rdata->msg_info.msg->line.status.reason, |
| 501 | rdata, -1); |
| 502 | } else { |
| 503 | status = pjsip_publishc_send(pubc, tdata); |
| 504 | } |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 505 | |
| 506 | } else { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 507 | pjsip_rx_data *rdata; |
| 508 | pj_int32_t expiration = 0xFFFF; |
| 509 | |
| 510 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 511 | pjsip_msg *msg; |
| 512 | pjsip_expires_hdr *expires; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 513 | pjsip_generic_string_hdr *etag_hdr; |
| 514 | const pj_str_t STR_ETAG = { "SIP-ETag", 8 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 515 | |
| 516 | rdata = event->body.tsx_state.src.rdata; |
| 517 | msg = rdata->msg_info.msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 518 | |
| 519 | /* Save ETag value */ |
| 520 | etag_hdr = (pjsip_generic_string_hdr*) |
| 521 | pjsip_msg_find_hdr_by_name(msg, &STR_ETAG, NULL); |
| 522 | if (etag_hdr) { |
| 523 | pj_strdup(pubc->pool, &pubc->etag, &etag_hdr->hvalue); |
| 524 | } else { |
| 525 | pubc->etag.slen = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 528 | /* Update expires value */ |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 529 | expires = (pjsip_expires_hdr*) |
| 530 | pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 531 | |
| 532 | if (expires) |
| 533 | expiration = expires->ivalue; |
| 534 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 535 | if (pubc->auto_refresh && expiration!=0 && expiration!=0xFFFF) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 536 | pj_time_val delay = { 0, 0}; |
| 537 | |
| 538 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 539 | if (pubc->expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED && |
| 540 | delay.sec > (pj_int32_t)pubc->expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 541 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 542 | delay.sec = pubc->expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 543 | } |
| 544 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 545 | delay.sec = DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 546 | pubc->timer.cb = &pubc_refresh_timer_cb; |
| 547 | pubc->timer.id = REFRESH_TIMER; |
| 548 | pubc->timer.user_data = pubc; |
| 549 | pjsip_endpt_schedule_timer( pubc->endpt, &pubc->timer, &delay); |
| 550 | pj_gettimeofday(&pubc->last_refresh); |
| 551 | pubc->next_refresh = pubc->last_refresh; |
| 552 | pubc->next_refresh.sec += delay.sec; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | } else { |
| 556 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 557 | event->body.tsx_state.src.rdata : NULL; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | /* Call callback. */ |
| 562 | if (expiration == 0xFFFF) expiration = -1; |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 563 | |
| 564 | /* Temporarily increment pending_tsx to prevent callback from |
| 565 | * destroying pubc. |
| 566 | */ |
| 567 | ++pubc->pending_tsx; |
| 568 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 569 | call_callback(pubc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 570 | (rdata ? &rdata->msg_info.msg->line.status.reason |
| 571 | : pjsip_get_status_text(tsx->status_code)), |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 572 | rdata, expiration); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 573 | |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 574 | --pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 577 | /* Delete the record if user destroy pubc during the callback. */ |
| 578 | if (pubc->_delete_flag && pubc->pending_tsx==0) { |
| 579 | pjsip_publishc_destroy(pubc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 583 | |
| 584 | PJ_DEF(pj_status_t) pjsip_publishc_send(pjsip_publishc *pubc, |
| 585 | pjsip_tx_data *tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 586 | { |
| 587 | pj_status_t status; |
| 588 | pjsip_cseq_hdr *cseq_hdr; |
| 589 | pj_uint32_t cseq; |
| 590 | |
| 591 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 592 | if (pubc->pending_tsx) { |
| 593 | PJ_LOG(4,(THIS_FILE, "Unable to send request, pubc has another " |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 594 | "transaction pending")); |
| 595 | pjsip_tx_data_dec_ref( tdata ); |
| 596 | return PJSIP_EBUSY; |
| 597 | } |
| 598 | |
| 599 | /* Invalidate message buffer. */ |
| 600 | pjsip_tx_data_invalidate_msg(tdata); |
| 601 | |
| 602 | /* Increment CSeq */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 603 | cseq = ++pubc->cseq_hdr->cseq; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 604 | cseq_hdr = (pjsip_cseq_hdr*) |
| 605 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 606 | cseq_hdr->cseq = cseq; |
| 607 | |
| 608 | /* Increment pending transaction first, since transaction callback |
| 609 | * may be called even before send_request() returns! |
| 610 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 611 | ++pubc->pending_tsx; |
| 612 | status = pjsip_endpt_send_request(pubc->endpt, tdata, -1, pubc, |
| 613 | &tsx_callback); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 614 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 615 | // no need to decrement, callback has been called and it should |
| 616 | // already decremented pending_tsx. Decrementing this here may |
| 617 | // cause accessing freed memory location. |
| 618 | //--pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 619 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 620 | } |
| 621 | |
| 622 | return status; |
| 623 | } |
| 624 | |