| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjsip-ua/sip_replaces.h> |
| 20 |
#include <pjsip-ua/sip_inv.h> |
| 21 |
#include <pjsip/print_util.h> |
| 22 |
#include <pjsip/sip_endpoint.h> |
| 23 |
#include <pjsip/sip_errno.h> |
| 24 |
#include <pjsip/sip_parser.h> |
| 25 |
#include <pjsip/sip_transport.h> |
| 26 |
#include <pjsip/sip_ua_layer.h> |
| 27 |
#include <pjsip/sip_util.h> |
| 28 |
#include <pj/assert.h> |
| 29 |
#include <pj/log.h> |
| 30 |
#include <pj/pool.h> |
| 31 |
#include <pj/string.h> |
| 32 |
|
| 33 |
#define THIS_FILE "sip_replaces.c" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
static int replaces_hdr_print( pjsip_replaces_hdr *hdr, |
| 40 |
char *buf, pj_size_t size); |
| 41 |
static pjsip_replaces_hdr* replaces_hdr_clone( pj_pool_t *pool, |
| 42 |
const pjsip_replaces_hdr *hdr); |
| 43 |
static pjsip_replaces_hdr* replaces_hdr_shallow_clone( pj_pool_t *pool, |
| 44 |
const pjsip_replaces_hdr*); |
| 45 |
|
| 46 |
static pjsip_hdr_vptr replaces_hdr_vptr = |
| 47 |
{ |
| 48 |
(pjsip_hdr_clone_fptr) &replaces_hdr_clone, |
| 49 |
(pjsip_hdr_clone_fptr) &replaces_hdr_shallow_clone, |
| 50 |
(pjsip_hdr_print_fptr) &replaces_hdr_print, |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
static pjsip_endpoint *the_endpt; |
| 55 |
static pj_bool_t is_initialized; |
| 56 |
|
| 57 |
PJ_DEF(pjsip_replaces_hdr*) pjsip_replaces_hdr_create(pj_pool_t *pool) |
| 58 |
{ |
| 59 |
pjsip_replaces_hdr *hdr = PJ_POOL_ZALLOC_T(pool, pjsip_replaces_hdr); |
| 60 |
hdr->type = PJSIP_H_OTHER; |
| 61 |
hdr->name.ptr = "Replaces"; |
| 62 |
hdr->name.slen = 8; |
| 63 |
hdr->vptr = &replaces_hdr_vptr; |
| 64 |
pj_list_init(hdr); |
| 65 |
pj_list_init(&hdr->other_param); |
| 66 |
return hdr; |
| 67 |
} |
| 68 |
|
| 69 |
static int replaces_hdr_print( pjsip_replaces_hdr *hdr, |
| 70 |
char *buf, pj_size_t size) |
| 71 |
{ |
| 72 |
char *p = buf; |
| 73 |
char *endbuf = buf+size; |
| 74 |
pj_ssize_t printed; |
| 75 |
const pjsip_parser_const_t *pc = pjsip_parser_const(); |
| 76 |
|
| 77 |
copy_advance(p, hdr->name); |
| 78 |
*p++ = ':'; |
| 79 |
*p++ = ' '; |
| 80 |
|
| 81 |
copy_advance(p, hdr->call_id); |
| 82 |
copy_advance_pair(p, ";to-tag=", 8, hdr->to_tag); |
| 83 |
copy_advance_pair(p, ";from-tag=", 10, hdr->from_tag); |
| 84 |
|
| 85 |
if (hdr->early_only) { |
| 86 |
const pj_str_t str_early_only = { ";early-only", 11 }; |
| 87 |
copy_advance(p, str_early_only); |
| 88 |
} |
| 89 |
|
| 90 |
printed = pjsip_param_print_on(&hdr->other_param, p, endbuf-p, |
| 91 |
&pc->pjsip_TOKEN_SPEC, |
| 92 |
&pc->pjsip_TOKEN_SPEC, ';'); |
| 93 |
if (printed < 0) |
| 94 |
return (int)printed; |
| 95 |
|
| 96 |
p += printed; |
| 97 |
return (int)(p - buf); |
| 98 |
} |
| 99 |
|
| 100 |
static pjsip_replaces_hdr* replaces_hdr_clone( pj_pool_t *pool, |
| 101 |
const pjsip_replaces_hdr *rhs) |
| 102 |
{ |
| 103 |
pjsip_replaces_hdr *hdr = pjsip_replaces_hdr_create(pool); |
| 104 |
pj_strdup(pool, &hdr->call_id, &rhs->call_id); |
| 105 |
pj_strdup(pool, &hdr->to_tag, &rhs->to_tag); |
| 106 |
pj_strdup(pool, &hdr->from_tag, &rhs->from_tag); |
| 107 |
hdr->early_only = rhs->early_only; |
| 108 |
pjsip_param_clone(pool, &hdr->other_param, &rhs->other_param); |
| 109 |
return hdr; |
| 110 |
} |
| 111 |
|
| 112 |
static pjsip_replaces_hdr* |
| 113 |
replaces_hdr_shallow_clone( pj_pool_t *pool, |
| 114 |
const pjsip_replaces_hdr *rhs ) |
| 115 |
{ |
| 116 |
pjsip_replaces_hdr *hdr = PJ_POOL_ALLOC_T(pool, pjsip_replaces_hdr); |
| 117 |
pj_memcpy(hdr, rhs, sizeof(*hdr)); |
| 118 |
pjsip_param_shallow_clone(pool, &hdr->other_param, &rhs->other_param); |
| 119 |
return hdr; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static pjsip_hdr *parse_hdr_replaces(pjsip_parse_ctx *ctx) |
| 127 |
{ |
| 128 |
pjsip_replaces_hdr *hdr = pjsip_replaces_hdr_create(ctx->pool); |
| 129 |
const pj_str_t to_tag = { "to-tag", 6 }; |
| 130 |
const pj_str_t from_tag = { "from-tag", 8 }; |
| 131 |
const pj_str_t early_only_tag = { "early-only", 10 }; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
pj_scan_get_until_ch(ctx->scanner, ';', &hdr->call_id); |
| 138 |
|
| 139 |
while (*ctx->scanner->curptr == ';') { |
| 140 |
pj_str_t pname, pvalue; |
| 141 |
|
| 142 |
pj_scan_get_char(ctx->scanner); |
| 143 |
pjsip_parse_param_imp(ctx->scanner, ctx->pool, &pname, &pvalue, 0); |
| 144 |
|
| 145 |
if (pj_stricmp(&pname, &to_tag)==0) { |
| 146 |
hdr->to_tag = pvalue; |
| 147 |
} else if (pj_stricmp(&pname, &from_tag)==0) { |
| 148 |
hdr->from_tag = pvalue; |
| 149 |
} else if (pj_stricmp(&pname, &early_only_tag)==0) { |
| 150 |
hdr->early_only = PJ_TRUE; |
| 151 |
} else { |
| 152 |
pjsip_param *param = PJ_POOL_ALLOC_T(ctx->pool, pjsip_param); |
| 153 |
param->name = pname; |
| 154 |
param->value = pvalue; |
| 155 |
pj_list_push_back(&hdr->other_param, param); |
| 156 |
} |
| 157 |
} |
| 158 |
pjsip_parse_end_hdr_imp( ctx->scanner ); |
| 159 |
return (pjsip_hdr*)hdr; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static void pjsip_replaces_deinit_module(pjsip_endpoint *endpt) |
| 165 |
{ |
| 166 |
PJ_TODO(provide_initialized_flag_for_each_endpoint); |
| 167 |
PJ_UNUSED_ARG(endpt); |
| 168 |
is_initialized = PJ_FALSE; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
PJ_DEF(pj_status_t) pjsip_replaces_init_module(pjsip_endpoint *endpt) |
| 175 |
{ |
| 176 |
pj_status_t status; |
| 177 |
const pj_str_t STR_REPLACES = { "replaces", 8 }; |
| 178 |
|
| 179 |
the_endpt = endpt; |
| 180 |
|
| 181 |
if (is_initialized) |
| 182 |
return PJ_SUCCESS; |
| 183 |
|
| 184 |
|
| 185 |
status = pjsip_register_hdr_parser( "Replaces", NULL, |
| 186 |
&parse_hdr_replaces); |
| 187 |
if (status != PJ_SUCCESS) |
| 188 |
return status; |
| 189 |
|
| 190 |
|
| 191 |
status = pjsip_endpt_add_capability(endpt, NULL, PJSIP_H_SUPPORTED, NULL, |
| 192 |
1, &STR_REPLACES); |
| 193 |
|
| 194 |
|
| 195 |
if (pjsip_endpt_atexit(endpt, &pjsip_replaces_deinit_module) != PJ_SUCCESS) |
| 196 |
{ |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
pj_assert(!"Failed to register Replaces deinit."); |
| 202 |
PJ_LOG(1, (THIS_FILE, "Failed to register Replaces deinit.")); |
| 203 |
} |
| 204 |
|
| 205 |
is_initialized = PJ_TRUE; |
| 206 |
return PJ_SUCCESS; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
PJ_DEF(pj_status_t) pjsip_replaces_verify_request( pjsip_rx_data *rdata, |
| 214 |
pjsip_dialog **p_dlg, |
| 215 |
pj_bool_t lock_dlg, |
| 216 |
pjsip_tx_data **p_tdata) |
| 217 |
{ |
| 218 |
const pj_str_t STR_REPLACES = { "Replaces", 8 }; |
| 219 |
pjsip_replaces_hdr *rep_hdr; |
| 220 |
int code = 200; |
| 221 |
const char *warn_text = NULL; |
| 222 |
pjsip_hdr res_hdr_list; |
| 223 |
pjsip_dialog *dlg = NULL; |
| 224 |
pjsip_inv_session *inv; |
| 225 |
pj_status_t status = PJ_SUCCESS; |
| 226 |
|
| 227 |
PJ_ASSERT_RETURN(rdata && p_dlg, PJ_EINVAL); |
| 228 |
|
| 229 |
|
| 230 |
PJ_ASSERT_RETURN(the_endpt != NULL, PJ_EINVALIDOP); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
*p_dlg = NULL; |
| 235 |
if (p_tdata) *p_tdata = NULL; |
| 236 |
|
| 237 |
pj_list_init(&res_hdr_list); |
| 238 |
|
| 239 |
|
| 240 |
rep_hdr = (pjsip_replaces_hdr*) |
| 241 |
pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_REPLACES, |
| 242 |
NULL); |
| 243 |
if (!rep_hdr) { |
| 244 |
|
| 245 |
return PJ_SUCCESS; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &STR_REPLACES, |
| 253 |
rep_hdr->next)) { |
| 254 |
code = PJSIP_SC_BAD_REQUEST; |
| 255 |
warn_text = "Found multiple Replaces headers"; |
| 256 |
goto on_return; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
dlg = pjsip_ua_find_dialog(&rep_hdr->call_id, &rep_hdr->to_tag, |
| 263 |
&rep_hdr->from_tag, PJ_TRUE); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
if (dlg == NULL) { |
| 269 |
code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 270 |
warn_text = "No dialog found for Replaces request"; |
| 271 |
goto on_return; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
inv = pjsip_dlg_get_inv_session(dlg); |
| 276 |
|
| 277 |
|
| 278 |
if (inv == NULL) { |
| 279 |
code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 280 |
warn_text = "No INVITE session found for Replaces request"; |
| 281 |
goto on_return; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if (inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 288 |
code = PJSIP_SC_DECLINE; |
| 289 |
warn_text = "INVITE session already terminated"; |
| 290 |
goto on_return; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
if (rep_hdr->early_only && inv->state >= PJSIP_INV_STATE_CONNECTING) { |
| 298 |
code = PJSIP_SC_BUSY_HERE; |
| 299 |
warn_text = "INVITE session already established"; |
| 300 |
goto on_return; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
if (inv->state <= PJSIP_INV_STATE_EARLY && inv->role != PJSIP_ROLE_UAC) |
| 308 |
{ |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if (inv->state != PJSIP_INV_STATE_EARLY || |
| 313 |
pjsip_cfg()->endpt.accept_replace_in_early_state == PJ_FALSE) |
| 314 |
{ |
| 315 |
code = PJSIP_SC_CALL_TSX_DOES_NOT_EXIST; |
| 316 |
warn_text = "Found early INVITE session but not initiated by " |
| 317 |
"this UA"; |
| 318 |
goto on_return; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
*p_dlg = dlg; |
| 327 |
status = PJ_SUCCESS; |
| 328 |
code = 200; |
| 329 |
|
| 330 |
on_return: |
| 331 |
|
| 332 |
|
| 333 |
if (code != 200) { |
| 334 |
|
| 335 |
if (dlg) |
| 336 |
pjsip_dlg_dec_lock(dlg); |
| 337 |
|
| 338 |
|
| 339 |
if (p_tdata) { |
| 340 |
pjsip_tx_data *tdata; |
| 341 |
const pjsip_hdr *h; |
| 342 |
|
| 343 |
status = pjsip_endpt_create_response(the_endpt, rdata, code, |
| 344 |
NULL, &tdata); |
| 345 |
|
| 346 |
if (status != PJ_SUCCESS) |
| 347 |
return status; |
| 348 |
|
| 349 |
|
| 350 |
h = res_hdr_list.next; |
| 351 |
while (h != &res_hdr_list) { |
| 352 |
pjsip_hdr *cloned; |
| 353 |
|
| 354 |
cloned = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, h); |
| 355 |
PJ_ASSERT_RETURN(cloned, PJ_ENOMEM); |
| 356 |
|
| 357 |
pjsip_msg_add_hdr(tdata->msg, cloned); |
| 358 |
|
| 359 |
h = h->next; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
if (warn_text) { |
| 364 |
pjsip_warning_hdr *warn_hdr; |
| 365 |
pj_str_t warn_value = pj_str((char*)warn_text); |
| 366 |
|
| 367 |
warn_hdr=pjsip_warning_hdr_create(tdata->pool, 399, |
| 368 |
pjsip_endpt_name(the_endpt), |
| 369 |
&warn_value); |
| 370 |
pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)warn_hdr); |
| 371 |
} |
| 372 |
|
| 373 |
*p_tdata = tdata; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
if (status == PJ_SUCCESS) |
| 380 |
status = PJSIP_ERRNO_FROM_SIP_STATUS(code); |
| 381 |
|
| 382 |
} else { |
| 383 |
|
| 384 |
if (!lock_dlg) |
| 385 |
pjsip_dlg_dec_lock(dlg); |
| 386 |
} |
| 387 |
|
| 388 |
return status; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|