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 | { |
| 101 | return pjsip_endpt_add_capability( endpt, NULL, PJSIP_H_ALLOW, NULL, |
| 102 | 1, &pjsip_publish_method.name); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | PJ_DEF(pj_status_t) pjsip_publishc_create( pjsip_endpoint *endpt, |
| 107 | unsigned options, |
| 108 | void *token, |
| 109 | pjsip_publishc_cb *cb, |
| 110 | pjsip_publishc **p_pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 111 | { |
| 112 | pj_pool_t *pool; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 113 | pjsip_publishc *pubc; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 114 | pj_status_t status; |
| 115 | |
| 116 | /* Verify arguments. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 117 | PJ_ASSERT_RETURN(endpt && cb && p_pubc, PJ_EINVAL); |
| 118 | PJ_ASSERT_RETURN(options == 0, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 119 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 120 | PJ_UNUSED_ARG(options); |
| 121 | |
| 122 | pool = pjsip_endpt_create_pool(endpt, "pubc%p", 1024, 1024); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 123 | PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM); |
| 124 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 125 | pubc = PJ_POOL_ZALLOC_T(pool, pjsip_publishc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 126 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 127 | pubc->pool = pool; |
| 128 | pubc->endpt = endpt; |
| 129 | pubc->token = token; |
| 130 | pubc->cb = cb; |
| 131 | pubc->expires = PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 132 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 133 | status = pjsip_auth_clt_init(&pubc->auth_sess, endpt, pubc->pool, 0); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 134 | if (status != PJ_SUCCESS) |
| 135 | return status; |
| 136 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 137 | pj_list_init(&pubc->route_set); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 138 | |
| 139 | /* Done */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 140 | *p_pubc = pubc; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 141 | return PJ_SUCCESS; |
| 142 | } |
| 143 | |
| 144 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 145 | PJ_DEF(pj_status_t) pjsip_publishc_destroy(pjsip_publishc *pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 146 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 147 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 148 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 149 | if (pubc->pending_tsx) { |
| 150 | pubc->_delete_flag = 1; |
| 151 | pubc->cb = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 152 | } else { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 153 | pjsip_endpt_release_pool(pubc->endpt, pubc->pool); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return PJ_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 160 | PJ_DEF(pj_pool_t*) pjsip_publishc_get_pool(pjsip_publishc *pubc) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 161 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 162 | return pubc->pool; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 165 | static void set_expires( pjsip_publishc *pubc, pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 166 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 167 | if (expires != pubc->expires) { |
| 168 | pubc->expires_hdr = pjsip_expires_hdr_create(pubc->pool, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 169 | } else { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 170 | pubc->expires_hdr = NULL; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 175 | PJ_DEF(pj_status_t) pjsip_publishc_init(pjsip_publishc *pubc, |
| 176 | const pj_str_t *event, |
| 177 | const pj_str_t *target_uri, |
| 178 | const pj_str_t *from_uri, |
| 179 | const pj_str_t *to_uri, |
| 180 | pj_uint32_t expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 181 | { |
| 182 | pj_str_t tmp; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 183 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 184 | PJ_ASSERT_RETURN(pubc && event && target_uri && from_uri && to_uri && |
| 185 | expires, PJ_EINVAL); |
| 186 | |
| 187 | /* Copy event type */ |
| 188 | pj_strdup_with_null(pubc->pool, &pubc->event, event); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 189 | |
| 190 | /* Copy server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 191 | pj_strdup_with_null(pubc->pool, &pubc->str_target_uri, target_uri); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 192 | |
| 193 | /* Set server URL. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 194 | tmp = pubc->str_target_uri; |
| 195 | pubc->target_uri = pjsip_parse_uri( pubc->pool, tmp.ptr, tmp.slen, 0); |
| 196 | if (pubc->target_uri == NULL) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 197 | return PJSIP_EINVALIDURI; |
| 198 | } |
| 199 | |
| 200 | /* Set "From" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 201 | pj_strdup_with_null(pubc->pool, &pubc->from_uri, from_uri); |
| 202 | tmp = pubc->from_uri; |
| 203 | pubc->from_hdr = pjsip_from_hdr_create(pubc->pool); |
| 204 | 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] | 205 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 206 | if (!pubc->from_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 207 | return PJSIP_EINVALIDURI; |
| 208 | } |
| 209 | |
| 210 | /* Set "To" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 211 | pj_strdup_with_null(pubc->pool, &tmp, to_uri); |
| 212 | pubc->to_hdr = pjsip_to_hdr_create(pubc->pool); |
| 213 | 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] | 214 | PJSIP_PARSE_URI_AS_NAMEADDR); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 215 | if (!pubc->to_hdr->uri) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 216 | return PJSIP_EINVALIDURI; |
| 217 | } |
| 218 | |
| 219 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 220 | /* Set "Expires" header, if required. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 221 | set_expires( pubc, expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 222 | |
| 223 | /* Set "Call-ID" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 224 | pubc->cid_hdr = pjsip_cid_hdr_create(pubc->pool); |
| 225 | pj_create_unique_string(pubc->pool, &pubc->cid_hdr->id); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 226 | |
| 227 | /* Set "CSeq" header. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 228 | pubc->cseq_hdr = pjsip_cseq_hdr_create(pubc->pool); |
| 229 | pubc->cseq_hdr->cseq = pj_rand() % 0xFFFF; |
| 230 | pjsip_method_set( &pubc->cseq_hdr->method, PJSIP_REGISTER_METHOD); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 231 | |
| 232 | /* Done. */ |
| 233 | return PJ_SUCCESS; |
| 234 | } |
| 235 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 236 | PJ_DEF(pj_status_t) pjsip_publishc_set_credentials( pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 237 | int count, |
| 238 | const pjsip_cred_info cred[] ) |
| 239 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 240 | PJ_ASSERT_RETURN(pubc && count && cred, PJ_EINVAL); |
| 241 | return pjsip_auth_clt_set_credentials(&pubc->auth_sess, count, cred); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 244 | 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] | 245 | const pjsip_route_hdr *route_set) |
| 246 | { |
| 247 | const pjsip_route_hdr *chdr; |
| 248 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 249 | PJ_ASSERT_RETURN(pubc && route_set, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 250 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 251 | pj_list_init(&pubc->route_set); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 252 | |
| 253 | chdr = route_set->next; |
| 254 | while (chdr != route_set) { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 255 | 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] | 256 | chdr = chdr->next; |
| 257 | } |
| 258 | |
| 259 | return PJ_SUCCESS; |
| 260 | } |
| 261 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 262 | static pj_status_t create_request(pjsip_publishc *pubc, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 263 | pjsip_tx_data **p_tdata) |
| 264 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 265 | const pj_str_t STR_EVENT = { "Event", 5 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 266 | pj_status_t status; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 267 | pjsip_generic_string_hdr *hdr; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 268 | pjsip_tx_data *tdata; |
| 269 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 270 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 271 | |
| 272 | /* Create the request. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 273 | status = pjsip_endpt_create_request_from_hdr( pubc->endpt, |
| 274 | &pjsip_publish_method, |
| 275 | pubc->target_uri, |
| 276 | pubc->from_hdr, |
| 277 | pubc->to_hdr, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 278 | NULL, |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 279 | pubc->cid_hdr, |
| 280 | pubc->cseq_hdr->cseq, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 281 | NULL, |
| 282 | &tdata); |
| 283 | if (status != PJ_SUCCESS) |
| 284 | return status; |
| 285 | |
| 286 | /* Add cached authorization headers. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 287 | pjsip_auth_clt_init_req( &pubc->auth_sess, tdata ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 288 | |
| 289 | /* Add Route headers from route set, ideally after Via header */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 290 | if (!pj_list_empty(&pubc->route_set)) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 291 | pjsip_hdr *route_pos; |
| 292 | const pjsip_route_hdr *route; |
| 293 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 294 | route_pos = (pjsip_hdr*) |
| 295 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 296 | if (!route_pos) |
| 297 | route_pos = &tdata->msg->hdr; |
| 298 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 299 | route = pubc->route_set.next; |
| 300 | while (route != &pubc->route_set) { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 301 | pjsip_hdr *new_hdr = (pjsip_hdr*) |
| 302 | pjsip_hdr_shallow_clone(tdata->pool, route); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 303 | pj_list_insert_after(route_pos, new_hdr); |
| 304 | route_pos = new_hdr; |
| 305 | route = route->next; |
| 306 | } |
| 307 | } |
| 308 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 309 | /* Add Event header */ |
| 310 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_EVENT, |
| 311 | &pubc->event); |
| 312 | if (hdr) |
| 313 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 314 | |
| 315 | |
| 316 | /* Add SIP-If-Match if we have etag */ |
| 317 | if (pubc->etag.slen) { |
| 318 | const pj_str_t STR_HNAME = { "SIP-If-Match", 12 }; |
| 319 | |
| 320 | hdr = pjsip_generic_string_hdr_create(tdata->pool, &STR_HNAME, |
| 321 | &pubc->etag); |
| 322 | if (hdr) |
| 323 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr); |
| 324 | } |
| 325 | |
| 326 | |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 327 | /* Done. */ |
| 328 | *p_tdata = tdata; |
| 329 | return PJ_SUCCESS; |
| 330 | } |
| 331 | |
| 332 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 333 | PJ_DEF(pj_status_t) pjsip_publishc_publish(pjsip_publishc *pubc, |
| 334 | pj_bool_t auto_refresh, |
| 335 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 336 | { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 337 | pj_status_t status; |
| 338 | pjsip_tx_data *tdata; |
| 339 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 340 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 341 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 342 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 343 | if (status != PJ_SUCCESS) |
| 344 | return status; |
| 345 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 346 | /* Add Expires header */ |
| 347 | if (pubc->expires_hdr) { |
| 348 | pjsip_hdr *dup; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 349 | |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 350 | dup = (pjsip_hdr*) |
| 351 | pjsip_hdr_shallow_clone(tdata->pool, pubc->expires_hdr); |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 352 | if (dup) |
| 353 | pjsip_msg_add_hdr(tdata->msg, dup); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 356 | /* Cancel existing timer */ |
| 357 | if (pubc->timer.id != 0) { |
| 358 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 359 | pubc->timer.id = 0; |
| 360 | } |
| 361 | |
| 362 | pubc->auto_refresh = auto_refresh; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 363 | |
| 364 | /* Done */ |
| 365 | *p_tdata = tdata; |
| 366 | return PJ_SUCCESS; |
| 367 | } |
| 368 | |
| 369 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 370 | PJ_DEF(pj_status_t) pjsip_publishc_unpublish(pjsip_publishc *pubc, |
| 371 | pjsip_tx_data **p_tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 372 | { |
| 373 | pjsip_tx_data *tdata; |
| 374 | pjsip_msg *msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 375 | pjsip_expires_hdr *expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 376 | pj_status_t status; |
| 377 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 378 | PJ_ASSERT_RETURN(pubc && p_tdata, PJ_EINVAL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 379 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 380 | if (pubc->timer.id != 0) { |
| 381 | pjsip_endpt_cancel_timer(pubc->endpt, &pubc->timer); |
| 382 | pubc->timer.id = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 385 | status = create_request(pubc, &tdata); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 386 | if (status != PJ_SUCCESS) |
| 387 | return status; |
| 388 | |
| 389 | msg = tdata->msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 390 | |
| 391 | /* Add Expires:0 header */ |
| 392 | expires = pjsip_expires_hdr_create(tdata->pool, 0); |
| 393 | pjsip_msg_add_hdr( msg, (pjsip_hdr*)expires); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 394 | |
| 395 | *p_tdata = tdata; |
| 396 | return PJ_SUCCESS; |
| 397 | } |
| 398 | |
| 399 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 400 | PJ_DEF(pj_status_t) pjsip_publishc_update_expires( pjsip_publishc *pubc, |
| 401 | pj_uint32_t expires ) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 402 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 403 | PJ_ASSERT_RETURN(pubc, PJ_EINVAL); |
| 404 | set_expires( pubc, expires ); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 405 | return PJ_SUCCESS; |
| 406 | } |
| 407 | |
| 408 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 409 | static void call_callback(pjsip_publishc *pubc, pj_status_t status, |
| 410 | int st_code, const pj_str_t *reason, |
| 411 | pjsip_rx_data *rdata, pj_int32_t expiration) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 412 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 413 | struct pjsip_publishc_cbparam cbparam; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 414 | |
| 415 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 416 | cbparam.pubc = pubc; |
| 417 | cbparam.token = pubc->token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 418 | cbparam.status = status; |
| 419 | cbparam.code = st_code; |
| 420 | cbparam.reason = *reason; |
| 421 | cbparam.rdata = rdata; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 422 | cbparam.expiration = expiration; |
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 | (*pubc->cb)(&cbparam); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 427 | static void pubc_refresh_timer_cb( pj_timer_heap_t *timer_heap, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 428 | struct pj_timer_entry *entry) |
| 429 | { |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 430 | pjsip_publishc *pubc = (pjsip_publishc*) entry->user_data; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 431 | pjsip_tx_data *tdata; |
| 432 | pj_status_t status; |
| 433 | |
| 434 | PJ_UNUSED_ARG(timer_heap); |
| 435 | |
| 436 | entry->id = 0; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 437 | status = pjsip_publishc_publish(pubc, 1, &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame^] | 438 | if (status != PJ_SUCCESS) { |
| 439 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 440 | pj_str_t reason = pj_strerror(status, errmsg, sizeof(errmsg)); |
| 441 | call_callback(pubc, status, 400, &reason, NULL, -1); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | status = pjsip_publishc_send(pubc, tdata); |
| 446 | /* No need to call callback as it should have been called */ |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | static void tsx_callback(void *token, pjsip_event *event) |
| 450 | { |
| 451 | pj_status_t status; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 452 | pjsip_publishc *pubc = (pjsip_publishc*) token; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 453 | pjsip_transaction *tsx = event->body.tsx_state.tsx; |
| 454 | |
| 455 | /* Decrement pending transaction counter. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 456 | pj_assert(pubc->pending_tsx > 0); |
| 457 | --pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 458 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 459 | /* If publication data has been deleted by user then remove publication |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 460 | * data from transaction's callback, and don't call callback. |
| 461 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 462 | if (pubc->_delete_flag) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 463 | |
| 464 | /* Nothing to do */ |
| 465 | ; |
| 466 | |
| 467 | } else if (tsx->status_code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED || |
| 468 | tsx->status_code == PJSIP_SC_UNAUTHORIZED) |
| 469 | { |
| 470 | pjsip_rx_data *rdata = event->body.tsx_state.src.rdata; |
| 471 | pjsip_tx_data *tdata; |
| 472 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 473 | status = pjsip_auth_clt_reinit_req( &pubc->auth_sess, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 474 | rdata, |
| 475 | tsx->last_tx, |
| 476 | &tdata); |
Benny Prijono | 0e9f762 | 2008-07-01 19:11:55 +0000 | [diff] [blame^] | 477 | if (status != PJ_SUCCESS) { |
| 478 | call_callback(pubc, status, tsx->status_code, |
| 479 | &rdata->msg_info.msg->line.status.reason, |
| 480 | rdata, -1); |
| 481 | } else { |
| 482 | status = pjsip_publishc_send(pubc, tdata); |
| 483 | } |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 484 | |
| 485 | } else { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 486 | pjsip_rx_data *rdata; |
| 487 | pj_int32_t expiration = 0xFFFF; |
| 488 | |
| 489 | if (tsx->status_code/100 == 2) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 490 | pjsip_msg *msg; |
| 491 | pjsip_expires_hdr *expires; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 492 | pjsip_generic_string_hdr *etag_hdr; |
| 493 | const pj_str_t STR_ETAG = { "SIP-ETag", 8 }; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 494 | |
| 495 | rdata = event->body.tsx_state.src.rdata; |
| 496 | msg = rdata->msg_info.msg; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 497 | |
| 498 | /* Save ETag value */ |
| 499 | etag_hdr = (pjsip_generic_string_hdr*) |
| 500 | pjsip_msg_find_hdr_by_name(msg, &STR_ETAG, NULL); |
| 501 | if (etag_hdr) { |
| 502 | pj_strdup(pubc->pool, &pubc->etag, &etag_hdr->hvalue); |
| 503 | } else { |
| 504 | pubc->etag.slen = 0; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 507 | /* Update expires value */ |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 508 | expires = (pjsip_expires_hdr*) |
| 509 | pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 510 | |
| 511 | if (expires) |
| 512 | expiration = expires->ivalue; |
| 513 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 514 | if (pubc->auto_refresh && expiration!=0 && expiration!=0xFFFF) { |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 515 | pj_time_val delay = { 0, 0}; |
| 516 | |
| 517 | delay.sec = expiration - DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 518 | if (pubc->expires != PJSIP_PUBC_EXPIRATION_NOT_SPECIFIED && |
| 519 | delay.sec > (pj_int32_t)pubc->expires) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 520 | { |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 521 | delay.sec = pubc->expires; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 522 | } |
| 523 | if (delay.sec < DELAY_BEFORE_REFRESH) |
| 524 | delay.sec = DELAY_BEFORE_REFRESH; |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 525 | pubc->timer.cb = &pubc_refresh_timer_cb; |
| 526 | pubc->timer.id = REFRESH_TIMER; |
| 527 | pubc->timer.user_data = pubc; |
| 528 | pjsip_endpt_schedule_timer( pubc->endpt, &pubc->timer, &delay); |
| 529 | pj_gettimeofday(&pubc->last_refresh); |
| 530 | pubc->next_refresh = pubc->last_refresh; |
| 531 | pubc->next_refresh.sec += delay.sec; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | } else { |
| 535 | rdata = (event->body.tsx_state.type==PJSIP_EVENT_RX_MSG) ? |
| 536 | event->body.tsx_state.src.rdata : NULL; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* Call callback. */ |
| 541 | if (expiration == 0xFFFF) expiration = -1; |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 542 | |
| 543 | /* Temporarily increment pending_tsx to prevent callback from |
| 544 | * destroying pubc. |
| 545 | */ |
| 546 | ++pubc->pending_tsx; |
| 547 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 548 | call_callback(pubc, PJ_SUCCESS, tsx->status_code, |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 549 | (rdata ? &rdata->msg_info.msg->line.status.reason |
| 550 | : pjsip_get_status_text(tsx->status_code)), |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 551 | rdata, expiration); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 552 | |
Benny Prijono | da1e063 | 2007-05-23 14:12:35 +0000 | [diff] [blame] | 553 | --pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 556 | /* Delete the record if user destroy pubc during the callback. */ |
| 557 | if (pubc->_delete_flag && pubc->pending_tsx==0) { |
| 558 | pjsip_publishc_destroy(pubc); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 562 | |
| 563 | PJ_DEF(pj_status_t) pjsip_publishc_send(pjsip_publishc *pubc, |
| 564 | pjsip_tx_data *tdata) |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 565 | { |
| 566 | pj_status_t status; |
| 567 | pjsip_cseq_hdr *cseq_hdr; |
| 568 | pj_uint32_t cseq; |
| 569 | |
| 570 | /* Make sure we don't have pending transaction. */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 571 | if (pubc->pending_tsx) { |
| 572 | PJ_LOG(4,(THIS_FILE, "Unable to send request, pubc has another " |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 573 | "transaction pending")); |
| 574 | pjsip_tx_data_dec_ref( tdata ); |
| 575 | return PJSIP_EBUSY; |
| 576 | } |
| 577 | |
| 578 | /* Invalidate message buffer. */ |
| 579 | pjsip_tx_data_invalidate_msg(tdata); |
| 580 | |
| 581 | /* Increment CSeq */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 582 | cseq = ++pubc->cseq_hdr->cseq; |
Benny Prijono | 9d4469d | 2007-05-02 05:14:29 +0000 | [diff] [blame] | 583 | cseq_hdr = (pjsip_cseq_hdr*) |
| 584 | pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 585 | cseq_hdr->cseq = cseq; |
| 586 | |
| 587 | /* Increment pending transaction first, since transaction callback |
| 588 | * may be called even before send_request() returns! |
| 589 | */ |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 590 | ++pubc->pending_tsx; |
| 591 | status = pjsip_endpt_send_request(pubc->endpt, tdata, -1, pubc, |
| 592 | &tsx_callback); |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 593 | if (status!=PJ_SUCCESS) { |
Benny Prijono | 2943815 | 2007-06-28 02:47:32 +0000 | [diff] [blame] | 594 | // no need to decrement, callback has been called and it should |
| 595 | // already decremented pending_tsx. Decrementing this here may |
| 596 | // cause accessing freed memory location. |
| 597 | //--pubc->pending_tsx; |
Benny Prijono | 21b9ad9 | 2006-08-15 13:11:22 +0000 | [diff] [blame] | 598 | PJ_LOG(4,(THIS_FILE, "Error sending request, status=%d", status)); |
| 599 | } |
| 600 | |
| 601 | return status; |
| 602 | } |
| 603 | |