Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/stateful_proxy.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19 #define THIS_FILE "stateful_proxy.c"
20
21 /* Common proxy functions */
22 #define STATEFUL 1
23 #include "proxy.h"
24
25
26 /*
27 * mod_stateful_proxy is the module to receive SIP request and
28 * response message that is outside any transaction context.
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, /* prev, next. */
36 { "mod-stateful-proxy", 18 }, /* Name. */
37 -1, /* Id */
38 PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, /* Priority */
39 NULL, /* load() */
40 NULL, /* start() */
41 NULL, /* stop() */
42 NULL, /* unload() */
43 &proxy_on_rx_request, /* on_rx_request() */
44 &proxy_on_rx_response, /* on_rx_response() */
45 NULL, /* on_tx_request. */
46 NULL, /* on_tx_response() */
47 NULL, /* on_tsx_state() */
48 };
49
50
51 /*
52 * mod_tu (tu=Transaction User) is the module to receive notification
53 * from transaction when the transaction state has changed.
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, /* prev, next. */
60 { "mod-transaction-user", 20 }, /* Name. */
61 -1, /* Id */
62 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
63 NULL, /* load() */
64 NULL, /* start() */
65 NULL, /* stop() */
66 NULL, /* unload() */
67 NULL, /* on_rx_request() */
68 NULL, /* on_rx_response() */
69 NULL, /* on_tx_request. */
70 NULL, /* on_tx_response() */
71 &tu_on_tsx_state, /* on_tsx_state() */
72 };
73
74
75 /* This is the data that is attached to the UAC transaction */
76 struct uac_data
77 {
78 pjsip_transaction *uas_tsx;
79 pj_timer_entry timer;
80 };
81
82
83 /* This is the data that is attached to the UAS transaction */
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 /* Callback to be called to handle new incoming requests. */
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 /* Verify incoming request */
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 * Request looks sane, next clone the request to create transmit data.
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 /* Process routing */
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 /* Calculate target */
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 /* Everything is set to forward the request. */
151
152 /* If this is an ACK request, forward statelessly.
153 * This happens if the proxy records route and this ACK
154 * is sent for 2xx response. An ACK that is sent for non-2xx
155 * final response will be absorbed by transaction layer, and
156 * it will not be received by on_rx_request() callback.
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 /* Create UAC transaction for forwarding the request.
170 * Set our module as the transaction user to receive further
171 * events from this transaction.
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 /* Create UAS transaction to handle incoming request */
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 /* Feed the request to the UAS transaction to drive it's state
194 * out of NULL state.
195 */
196 pjsip_tsx_recv_msg(uas_tsx, rdata);
197
198 /* Attach a data to the UAC transaction, to be used to find the
199 * UAS transaction when we receive response in the UAC side.
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 /* Attach data to the UAS transaction, to find the UAC transaction
207 * when cancelling INVITE request.
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 /* Everything is setup, forward the request */
215 status = pjsip_tsx_send_msg(uac_tsx, tdata);
216 if (status != PJ_SUCCESS) {
217 pjsip_tx_data *err_res;
218
219 /* Fail to send request, for some reason */
220
221 /* Destroy transmit data */
222 pjsip_tx_data_dec_ref(tdata);
223
224 /* I think UAC transaction should have been destroyed when
225 * it fails to send request, so no need to destroy it.
226 pjsip_tsx_terminate(uac_tsx, PJSIP_SC_INTERNAL_SERVER_ERROR);
227 */
228
229 /* Send 500/Internal Server Error to UAS transaction */
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 /* Send 100/Trying if this is an INVITE */
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 /* This is CANCEL request */
248 pjsip_transaction *invite_uas;
249 struct uas_data *uas_data2;
250 pj_str_t key;
251
252 /* Find the UAS INVITE transaction */
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 /* Invite transaction not found, respond CANCEL with 481 */
258 pjsip_endpt_respond_stateless(global.endpt, rdata, 481, NULL,
259 NULL, NULL);
260 return PJ_TRUE;
261 }
262
263 /* Respond 200 OK to CANCEL */
264 pjsip_endpt_respond(global.endpt, NULL, rdata, 200, NULL, NULL,
265 NULL, NULL);
266
267 /* Send CANCEL to cancel the UAC transaction.
268 * The UAS INVITE transaction will get final response when
269 * we receive final response from the UAC INVITE transaction.
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 /* Unlock UAS tsx because it is locked in find_tsx() */
293 pj_grp_lock_release(invite_uas->grp_lock);
294 }
295
296 return PJ_TRUE;
297 }
298
299
300 /* Callback to be called to handle incoming response outside
301 * any transactions. This happens for example when 2xx/OK
302 * for INVITE is received and transaction will be destroyed
303 * immediately, so we need to forward the subsequent 2xx/OK
304 * retransmission statelessly.
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 /* Create response to be forwarded upstream (Via will be stripped here) */
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 /* Get topmost Via header */
321 hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
322 if (hvia == NULL) {
323 /* Invalid response! Just drop it */
324 pjsip_tx_data_dec_ref(tdata);
325 return PJ_TRUE;
326 }
327
328 /* Calculate the address to forward the response */
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 /* Destination address is Via's received param */
335 res_addr.dst_host.addr.host = hvia->recvd_param;
336 if (res_addr.dst_host.addr.host.slen == 0) {
337 /* Someone has messed up our Via header! */
338 res_addr.dst_host.addr.host = hvia->sent_by.host;
339 }
340
341 /* Destination port is the rport */
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 /* Ugh, original sender didn't put rport!
347 * At best, can only send the response to the port in Via.
348 */
349 res_addr.dst_host.addr.port = hvia->sent_by.port;
350 }
351
352 /* Forward response */
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 /* Callback to be called to handle transaction state changed. */
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 /* Get the data that we attached to the UAC transaction previously */
387 uac_data = (struct uac_data*) tsx->mod_data[mod_tu.id];
388
389
390 /* Handle incoming response */
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 /* Do not forward 100 response for INVITE (we already responded
401 * INVITE with 100)
402 */
403 if (tsx->method.id == PJSIP_INVITE_METHOD &&
404 rdata->msg_info.msg->line.status.code == 100)
405 {
406 return;
407 }
408
409 /* Create response to be forwarded upstream
410 * (Via will be stripped here)
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 /* Get topmost Via header of the new response */
420 hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA,
421 NULL);
422 if (hvia == NULL) {
423 /* Invalid response! Just drop it */
424 pjsip_tx_data_dec_ref(tdata);
425 return;
426 }
427
428 /* Calculate the address to forward the response */
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 /* Destination address is Via's received param */
435 res_addr.dst_host.addr.host = hvia->recvd_param;
436 if (res_addr.dst_host.addr.host.slen == 0) {
437 /* Someone has messed up our Via header! */
438 res_addr.dst_host.addr.host = hvia->sent_by.host;
439 }
440
441 /* Destination port is the rport */
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 /* Ugh, original sender didn't put rport!
447 * At best, can only send the response to the port in Via.
448 */
449 res_addr.dst_host.addr.port = hvia->sent_by.port;
450 }
451
452 /* Forward response with the UAS transaction */
453 pjsip_tsx_send_msg(uac_data->uas_tsx, tdata);
454
455 }
456
457 /* If UAC transaction is terminated, terminate the UAS as well.
458 * This could happen because of:
459 * - timeout on the UAC side
460 * - receipt of 2xx response to INVITE
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 /* Send 408/Timeout if this is an INVITE transaction, since
476 * we must have sent provisional response before. For non
477 * INVITE transaction, just destroy it.
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 /* For non-INVITE, just destroy the UAS transaction */
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 * main()
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