| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define THIS_FILE "stateful_proxy.c" |
| 20 |
|
| 21 |
|
| 22 |
#define STATEFUL 1 |
| 23 |
#include "proxy.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
static pj_bool_t proxy_on_rx_request(pjsip_rx_data *rdata ); |
| 31 |
static pj_bool_t proxy_on_rx_response(pjsip_rx_data *rdata ); |
| 32 |
|
| 33 |
static pjsip_module mod_stateful_proxy = |
| 34 |
{ |
| 35 |
NULL, NULL, |
| 36 |
{ "mod-stateful-proxy", 18 }, |
| 37 |
-1, |
| 38 |
PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, |
| 39 |
NULL, |
| 40 |
NULL, |
| 41 |
NULL, |
| 42 |
NULL, |
| 43 |
&proxy_on_rx_request, |
| 44 |
&proxy_on_rx_response, |
| 45 |
NULL, |
| 46 |
NULL, |
| 47 |
NULL, |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static void tu_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event); |
| 56 |
|
| 57 |
static pjsip_module mod_tu = |
| 58 |
{ |
| 59 |
NULL, NULL, |
| 60 |
{ "mod-transaction-user", 20 }, |
| 61 |
-1, |
| 62 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 63 |
NULL, |
| 64 |
NULL, |
| 65 |
NULL, |
| 66 |
NULL, |
| 67 |
NULL, |
| 68 |
NULL, |
| 69 |
NULL, |
| 70 |
NULL, |
| 71 |
&tu_on_tsx_state, |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct uac_data |
| 77 |
{ |
| 78 |
pjsip_transaction *uas_tsx; |
| 79 |
pj_timer_entry timer; |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
struct uas_data |
| 85 |
{ |
| 86 |
pjsip_transaction *uac_tsx; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static pj_status_t init_stateful_proxy(void) |
| 92 |
{ |
| 93 |
pj_status_t status; |
| 94 |
|
| 95 |
status = pjsip_endpt_register_module( global.endpt, &mod_stateful_proxy); |
| 96 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 97 |
|
| 98 |
status = pjsip_endpt_register_module( global.endpt, &mod_tu); |
| 99 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 100 |
|
| 101 |
return PJ_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static pj_bool_t proxy_on_rx_request( pjsip_rx_data *rdata ) |
| 107 |
{ |
| 108 |
pjsip_transaction *uas_tsx, *uac_tsx; |
| 109 |
struct uac_data *uac_data; |
| 110 |
struct uas_data *uas_data; |
| 111 |
pjsip_tx_data *tdata; |
| 112 |
pj_status_t status; |
| 113 |
|
| 114 |
if (rdata->msg_info.msg->line.req.method.id != PJSIP_CANCEL_METHOD) { |
| 115 |
|
| 116 |
|
| 117 |
status = proxy_verify_request(rdata); |
| 118 |
if (status != PJ_SUCCESS) { |
| 119 |
app_perror("RX invalid request", status); |
| 120 |
return PJ_TRUE; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
status = pjsip_endpt_create_request_fwd(global.endpt, rdata, NULL, |
| 127 |
NULL, 0, &tdata); |
| 128 |
if (status != PJ_SUCCESS) { |
| 129 |
pjsip_endpt_respond_stateless(global.endpt, rdata, |
| 130 |
PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 131 |
NULL, NULL, NULL); |
| 132 |
return PJ_TRUE; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
status = proxy_process_routing(tdata); |
| 138 |
if (status != PJ_SUCCESS) { |
| 139 |
app_perror("Error processing route", status); |
| 140 |
return PJ_TRUE; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
status = proxy_calculate_target(rdata, tdata); |
| 145 |
if (status != PJ_SUCCESS) { |
| 146 |
app_perror("Error calculating target", status); |
| 147 |
return PJ_TRUE; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
if (tdata->msg->line.req.method.id == PJSIP_ACK_METHOD) { |
| 159 |
status = pjsip_endpt_send_request_stateless(global.endpt, tdata, |
| 160 |
NULL, NULL); |
| 161 |
if (status != PJ_SUCCESS) { |
| 162 |
app_perror("Error forwarding request", status); |
| 163 |
return PJ_TRUE; |
| 164 |
} |
| 165 |
|
| 166 |
return PJ_TRUE; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
status = pjsip_tsx_create_uac(&mod_tu, tdata, &uac_tsx); |
| 174 |
if (status != PJ_SUCCESS) { |
| 175 |
pjsip_tx_data_dec_ref(tdata); |
| 176 |
pjsip_endpt_respond_stateless(global.endpt, rdata, |
| 177 |
PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 178 |
NULL, NULL, NULL); |
| 179 |
return PJ_TRUE; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
status = pjsip_tsx_create_uas(&mod_tu, rdata, &uas_tsx); |
| 184 |
if (status != PJ_SUCCESS) { |
| 185 |
pjsip_tx_data_dec_ref(tdata); |
| 186 |
pjsip_endpt_respond_stateless(global.endpt, rdata, |
| 187 |
PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 188 |
NULL, NULL, NULL); |
| 189 |
pjsip_tsx_terminate(uac_tsx, PJSIP_SC_INTERNAL_SERVER_ERROR); |
| 190 |
return PJ_TRUE; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
pjsip_tsx_recv_msg(uas_tsx, rdata); |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
uac_data = (struct uac_data*) |
| 202 |
pj_pool_alloc(uac_tsx->pool, sizeof(struct uac_data)); |
| 203 |
uac_data->uas_tsx = uas_tsx; |
| 204 |
uac_tsx->mod_data[mod_tu.id] = (void*)uac_data; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
uas_data = (struct uas_data*) |
| 210 |
pj_pool_alloc(uas_tsx->pool, sizeof(struct uas_data)); |
| 211 |
uas_data->uac_tsx = uac_tsx; |
| 212 |
uas_tsx->mod_data[mod_tu.id] = (void*)uas_data; |
| 213 |
|
| 214 |
|
| 215 |
status = pjsip_tsx_send_msg(uac_tsx, tdata); |
| 216 |
if (status != PJ_SUCCESS) { |
| 217 |
pjsip_tx_data *err_res; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pjsip_tx_data_dec_ref(tdata); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
pjsip_endpt_create_response(global.endpt, rdata, |
| 231 |
500, NULL, &err_res); |
| 232 |
pjsip_tsx_send_msg(uas_tsx, err_res); |
| 233 |
|
| 234 |
return PJ_TRUE; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
| 239 |
pjsip_tx_data *res100; |
| 240 |
|
| 241 |
pjsip_endpt_create_response(global.endpt, rdata, 100, NULL, |
| 242 |
&res100); |
| 243 |
pjsip_tsx_send_msg(uas_tsx, res100); |
| 244 |
} |
| 245 |
|
| 246 |
} else { |
| 247 |
|
| 248 |
pjsip_transaction *invite_uas; |
| 249 |
struct uas_data *uas_data2; |
| 250 |
pj_str_t key; |
| 251 |
|
| 252 |
|
| 253 |
pjsip_tsx_create_key(rdata->tp_info.pool, &key, PJSIP_UAS_ROLE, |
| 254 |
pjsip_get_invite_method(), rdata); |
| 255 |
invite_uas = pjsip_tsx_layer_find_tsx(&key, PJ_TRUE); |
| 256 |
if (!invite_uas) { |
| 257 |
|
| 258 |
pjsip_endpt_respond_stateless(global.endpt, rdata, 481, NULL, |
| 259 |
NULL, NULL); |
| 260 |
return PJ_TRUE; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
pjsip_endpt_respond(global.endpt, NULL, rdata, 200, NULL, NULL, |
| 265 |
NULL, NULL); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
uas_data2 = (struct uas_data*) invite_uas->mod_data[mod_tu.id]; |
| 272 |
if (uas_data2->uac_tsx && uas_data2->uac_tsx->status_code < 200) { |
| 273 |
pjsip_tx_data *cancel; |
| 274 |
pj_status_t cancel_status; |
| 275 |
|
| 276 |
pj_grp_lock_acquire(uas_data2->uac_tsx->grp_lock); |
| 277 |
|
| 278 |
cancel_status = pjsip_endpt_create_cancel(global.endpt, uas_data2->uac_tsx->last_tx, |
| 279 |
&cancel); |
| 280 |
if (cancel_status == PJ_SUCCESS) { |
| 281 |
status = pjsip_endpt_send_request(global.endpt, cancel, -1, NULL, NULL); |
| 282 |
if (status != PJ_SUCCESS) { |
| 283 |
PJ_LOG(4,(THIS_FILE, "Failed to send CANCEL request (%d)", status)); |
| 284 |
} |
| 285 |
} else { |
| 286 |
PJ_LOG(4,(THIS_FILE, "Failed to create CANCEL request (%d)", cancel_status)); |
| 287 |
} |
| 288 |
|
| 289 |
pj_grp_lock_release(uas_data2->uac_tsx->grp_lock); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
pj_grp_lock_release(invite_uas->grp_lock); |
| 294 |
} |
| 295 |
|
| 296 |
return PJ_TRUE; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
static pj_bool_t proxy_on_rx_response( pjsip_rx_data *rdata ) |
| 307 |
{ |
| 308 |
pjsip_tx_data *tdata; |
| 309 |
pjsip_response_addr res_addr; |
| 310 |
pjsip_via_hdr *hvia; |
| 311 |
pj_status_t status; |
| 312 |
|
| 313 |
|
| 314 |
status = pjsip_endpt_create_response_fwd(global.endpt, rdata, 0, &tdata); |
| 315 |
if (status != PJ_SUCCESS) { |
| 316 |
app_perror("Error creating response", status); |
| 317 |
return PJ_TRUE; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 322 |
if (hvia == NULL) { |
| 323 |
|
| 324 |
pjsip_tx_data_dec_ref(tdata); |
| 325 |
return PJ_TRUE; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
pj_bzero(&res_addr, sizeof(res_addr)); |
| 330 |
res_addr.dst_host.type = PJSIP_TRANSPORT_UDP; |
| 331 |
res_addr.dst_host.flag = |
| 332 |
pjsip_transport_get_flag_from_type(PJSIP_TRANSPORT_UDP); |
| 333 |
|
| 334 |
|
| 335 |
res_addr.dst_host.addr.host = hvia->recvd_param; |
| 336 |
if (res_addr.dst_host.addr.host.slen == 0) { |
| 337 |
|
| 338 |
res_addr.dst_host.addr.host = hvia->sent_by.host; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
if (hvia->rport_param != 0 && hvia->rport_param != -1) |
| 343 |
res_addr.dst_host.addr.port = hvia->rport_param; |
| 344 |
|
| 345 |
if (res_addr.dst_host.addr.port == 0) { |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
res_addr.dst_host.addr.port = hvia->sent_by.port; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
status = pjsip_endpt_send_response(global.endpt, &res_addr, tdata, |
| 354 |
NULL, NULL); |
| 355 |
if (status != PJ_SUCCESS) { |
| 356 |
pjsip_tx_data_dec_ref(tdata); |
| 357 |
app_perror("Error forwarding response", status); |
| 358 |
return PJ_TRUE; |
| 359 |
} |
| 360 |
|
| 361 |
return PJ_TRUE; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
static void tu_on_tsx_state(pjsip_transaction *tsx, pjsip_event *event) |
| 367 |
{ |
| 368 |
struct uac_data *uac_data; |
| 369 |
pj_status_t status; |
| 370 |
|
| 371 |
if (tsx->role == PJSIP_ROLE_UAS) { |
| 372 |
if (tsx->state == PJSIP_TSX_STATE_TERMINATED) { |
| 373 |
struct uas_data *uas_data; |
| 374 |
|
| 375 |
uas_data = (struct uas_data*) tsx->mod_data[mod_tu.id]; |
| 376 |
if (uas_data->uac_tsx) { |
| 377 |
uac_data = (struct uac_data*) |
| 378 |
uas_data->uac_tsx->mod_data[mod_tu.id]; |
| 379 |
uac_data->uas_tsx = NULL; |
| 380 |
} |
| 381 |
|
| 382 |
} |
| 383 |
return; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
uac_data = (struct uac_data*) tsx->mod_data[mod_tu.id]; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if (event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 392 |
|
| 393 |
pjsip_rx_data *rdata; |
| 394 |
pjsip_response_addr res_addr; |
| 395 |
pjsip_via_hdr *hvia; |
| 396 |
pjsip_tx_data *tdata; |
| 397 |
|
| 398 |
rdata = event->body.tsx_state.src.rdata; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
if (tsx->method.id == PJSIP_INVITE_METHOD && |
| 404 |
rdata->msg_info.msg->line.status.code == 100) |
| 405 |
{ |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
status = pjsip_endpt_create_response_fwd(global.endpt, rdata, 0, |
| 413 |
&tdata); |
| 414 |
if (status != PJ_SUCCESS) { |
| 415 |
app_perror("Error creating response", status); |
| 416 |
return; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, |
| 421 |
NULL); |
| 422 |
if (hvia == NULL) { |
| 423 |
|
| 424 |
pjsip_tx_data_dec_ref(tdata); |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
pj_bzero(&res_addr, sizeof(res_addr)); |
| 430 |
res_addr.dst_host.type = PJSIP_TRANSPORT_UDP; |
| 431 |
res_addr.dst_host.flag = |
| 432 |
pjsip_transport_get_flag_from_type(PJSIP_TRANSPORT_UDP); |
| 433 |
|
| 434 |
|
| 435 |
res_addr.dst_host.addr.host = hvia->recvd_param; |
| 436 |
if (res_addr.dst_host.addr.host.slen == 0) { |
| 437 |
|
| 438 |
res_addr.dst_host.addr.host = hvia->sent_by.host; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
if (hvia->rport_param != 0 && hvia->rport_param != -1) |
| 443 |
res_addr.dst_host.addr.port = hvia->rport_param; |
| 444 |
|
| 445 |
if (res_addr.dst_host.addr.port == 0) { |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
res_addr.dst_host.addr.port = hvia->sent_by.port; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
pjsip_tsx_send_msg(uac_data->uas_tsx, tdata); |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
if (tsx->state == PJSIP_TSX_STATE_TERMINATED && uac_data && |
| 463 |
uac_data->uas_tsx) |
| 464 |
{ |
| 465 |
|
| 466 |
pjsip_transaction *uas_tsx; |
| 467 |
struct uas_data *uas_data; |
| 468 |
|
| 469 |
uas_tsx = uac_data->uas_tsx; |
| 470 |
uas_data = (struct uas_data*) uas_tsx->mod_data[mod_tu.id]; |
| 471 |
uas_data->uac_tsx = NULL; |
| 472 |
|
| 473 |
if (event->body.tsx_state.type == PJSIP_EVENT_TIMER) { |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if (tsx->method.id == PJSIP_INVITE_METHOD) { |
| 480 |
|
| 481 |
pjsip_tx_data *tdata = uas_tsx->last_tx; |
| 482 |
|
| 483 |
tdata->msg->line.status.code = PJSIP_SC_REQUEST_TIMEOUT; |
| 484 |
tdata->msg->line.status.reason = pj_str("Request timed out"); |
| 485 |
tdata->msg->body = NULL; |
| 486 |
|
| 487 |
pjsip_tx_data_add_ref(tdata); |
| 488 |
pjsip_tx_data_invalidate_msg(tdata); |
| 489 |
|
| 490 |
pjsip_tsx_send_msg(uas_tsx, tdata); |
| 491 |
|
| 492 |
} else { |
| 493 |
|
| 494 |
pjsip_tsx_terminate(uas_tsx, PJSIP_SC_REQUEST_TIMEOUT); |
| 495 |
} |
| 496 |
|
| 497 |
} else if (event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) { |
| 498 |
|
| 499 |
if (uas_tsx->state < PJSIP_TSX_STATE_TERMINATED) { |
| 500 |
pjsip_msg *msg; |
| 501 |
int code; |
| 502 |
|
| 503 |
msg = event->body.tsx_state.src.rdata->msg_info.msg; |
| 504 |
code = msg->line.status.code; |
| 505 |
|
| 506 |
uac_data->uas_tsx = NULL; |
| 507 |
pjsip_tsx_terminate(uas_tsx, code); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
int main(int argc, char *argv[]) |
| 518 |
{ |
| 519 |
pj_status_t status; |
| 520 |
|
| 521 |
global.port = 5060; |
| 522 |
global.record_route = 0; |
| 523 |
|
| 524 |
pj_log_set_level(4); |
| 525 |
|
| 526 |
status = init_options(argc, argv); |
| 527 |
if (status != PJ_SUCCESS) |
| 528 |
return 1; |
| 529 |
|
| 530 |
status = init_stack(); |
| 531 |
if (status != PJ_SUCCESS) { |
| 532 |
app_perror("Error initializing stack", status); |
| 533 |
return 1; |
| 534 |
} |
| 535 |
|
| 536 |
status = init_proxy(); |
| 537 |
if (status != PJ_SUCCESS) { |
| 538 |
app_perror("Error initializing proxy", status); |
| 539 |
return 1; |
| 540 |
} |
| 541 |
|
| 542 |
status = init_stateful_proxy(); |
| 543 |
if (status != PJ_SUCCESS) { |
| 544 |
app_perror("Error initializing stateful proxy", status); |
| 545 |
return 1; |
| 546 |
} |
| 547 |
|
| 548 |
#if PJ_HAS_THREADS |
| 549 |
status = pj_thread_create(global.pool, "sproxy", &worker_thread, |
| 550 |
NULL, 0, 0, &global.thread); |
| 551 |
if (status != PJ_SUCCESS) { |
| 552 |
app_perror("Error creating thread", status); |
| 553 |
return 1; |
| 554 |
} |
| 555 |
|
| 556 |
while (!global.quit_flag) { |
| 557 |
char line[10]; |
| 558 |
|
| 559 |
puts("\n" |
| 560 |
"Menu:\n" |
| 561 |
" q quit\n" |
| 562 |
" d dump status\n" |
| 563 |
" dd dump detailed status\n" |
| 564 |
""); |
| 565 |
|
| 566 |
if (fgets(line, sizeof(line), stdin) == NULL) { |
| 567 |
puts("EOF while reading stdin, will quit now.."); |
| 568 |
global.quit_flag = PJ_TRUE; |
| 569 |
break; |
| 570 |
} |
| 571 |
|
| 572 |
if (line[0] == 'q') { |
| 573 |
global.quit_flag = PJ_TRUE; |
| 574 |
} else if (line[0] == 'd') { |
| 575 |
pj_bool_t detail = (line[1] == 'd'); |
| 576 |
pjsip_endpt_dump(global.endpt, detail); |
| 577 |
pjsip_tsx_layer_dump(detail); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
pj_thread_join(global.thread); |
| 582 |
|
| 583 |
#else |
| 584 |
puts("\nPress Ctrl-C to quit\n"); |
| 585 |
for (;;) { |
| 586 |
pj_time_val delay = {0, 0}; |
| 587 |
pjsip_endpt_handle_events(global.endpt, &delay); |
| 588 |
} |
| 589 |
#endif |
| 590 |
|
| 591 |
destroy_stack(); |
| 592 |
|
| 593 |
return 0; |
| 594 |
} |
| 595 |
|