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