stateless_proxy.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  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 "stateless_proxy.c"
  20. /* Common proxy functions */
  21. #define STATEFUL 0
  22. #include "proxy.h"
  23. /* Callback to be called to handle incoming requests. */
  24. static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
  25. /* Callback to be called to handle incoming response. */
  26. static pj_bool_t on_rx_response( pjsip_rx_data *rdata );
  27. static pj_status_t init_stateless_proxy(void)
  28. {
  29. static pjsip_module mod_stateless_proxy =
  30. {
  31. NULL, NULL, /* prev, next. */
  32. { "mod-stateless-proxy", 19 }, /* Name. */
  33. -1, /* Id */
  34. PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, /* Priority */
  35. NULL, /* load() */
  36. NULL, /* start() */
  37. NULL, /* stop() */
  38. NULL, /* unload() */
  39. &on_rx_request, /* on_rx_request() */
  40. &on_rx_response, /* on_rx_response() */
  41. NULL, /* on_tx_request. */
  42. NULL, /* on_tx_response() */
  43. NULL, /* on_tsx_state() */
  44. };
  45. pj_status_t status;
  46. /* Register our module to receive incoming requests. */
  47. status = pjsip_endpt_register_module( global.endpt, &mod_stateless_proxy);
  48. PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
  49. return PJ_SUCCESS;
  50. }
  51. /* Callback to be called to handle incoming requests. */
  52. static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
  53. {
  54. pjsip_tx_data *tdata;
  55. pj_status_t status;
  56. /* Verify incoming request */
  57. status = proxy_verify_request(rdata);
  58. if (status != PJ_SUCCESS) {
  59. app_perror("RX invalid request", status);
  60. return PJ_TRUE;
  61. }
  62. /*
  63. * Request looks sane, next clone the request to create transmit data.
  64. */
  65. status = pjsip_endpt_create_request_fwd(global.endpt, rdata, NULL,
  66. NULL, 0, &tdata);
  67. if (status != PJ_SUCCESS) {
  68. pjsip_endpt_respond_stateless(global.endpt, rdata,
  69. PJSIP_SC_INTERNAL_SERVER_ERROR, NULL,
  70. NULL, NULL);
  71. return PJ_TRUE;
  72. }
  73. /* Process routing */
  74. status = proxy_process_routing(tdata);
  75. if (status != PJ_SUCCESS) {
  76. app_perror("Error processing route", status);
  77. return PJ_TRUE;
  78. }
  79. /* Calculate target */
  80. status = proxy_calculate_target(rdata, tdata);
  81. if (status != PJ_SUCCESS) {
  82. app_perror("Error calculating target", status);
  83. return PJ_TRUE;
  84. }
  85. /* Target is set, forward the request */
  86. status = pjsip_endpt_send_request_stateless(global.endpt, tdata,
  87. NULL, NULL);
  88. if (status != PJ_SUCCESS) {
  89. app_perror("Error forwarding request", status);
  90. return PJ_TRUE;
  91. }
  92. return PJ_TRUE;
  93. }
  94. /* Callback to be called to handle incoming response. */
  95. static pj_bool_t on_rx_response( pjsip_rx_data *rdata )
  96. {
  97. pjsip_tx_data *tdata;
  98. pjsip_response_addr res_addr;
  99. pjsip_via_hdr *hvia;
  100. pj_status_t status;
  101. /* Create response to be forwarded upstream (Via will be stripped here) */
  102. status = pjsip_endpt_create_response_fwd(global.endpt, rdata, 0, &tdata);
  103. if (status != PJ_SUCCESS) {
  104. app_perror("Error creating response", status);
  105. return PJ_TRUE;
  106. }
  107. /* Get topmost Via header */
  108. hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
  109. if (hvia == NULL) {
  110. /* Invalid response! Just drop it */
  111. pjsip_tx_data_dec_ref(tdata);
  112. return PJ_TRUE;
  113. }
  114. /* Calculate the address to forward the response */
  115. pj_bzero(&res_addr, sizeof(res_addr));
  116. res_addr.dst_host.type = PJSIP_TRANSPORT_UDP;
  117. res_addr.dst_host.flag = pjsip_transport_get_flag_from_type(PJSIP_TRANSPORT_UDP);
  118. /* Destination address is Via's received param */
  119. res_addr.dst_host.addr.host = hvia->recvd_param;
  120. if (res_addr.dst_host.addr.host.slen == 0) {
  121. /* Someone has messed up our Via header! */
  122. res_addr.dst_host.addr.host = hvia->sent_by.host;
  123. }
  124. /* Destination port is the rpot */
  125. if (hvia->rport_param != 0 && hvia->rport_param != -1)
  126. res_addr.dst_host.addr.port = hvia->rport_param;
  127. if (res_addr.dst_host.addr.port == 0) {
  128. /* Ugh, original sender didn't put rport!
  129. * At best, can only send the response to the port in Via.
  130. */
  131. res_addr.dst_host.addr.port = hvia->sent_by.port;
  132. }
  133. /* Forward response */
  134. status = pjsip_endpt_send_response(global.endpt, &res_addr, tdata,
  135. NULL, NULL);
  136. if (status != PJ_SUCCESS) {
  137. pjsip_tx_data_dec_ref(tdata);
  138. app_perror("Error forwarding response", status);
  139. return PJ_TRUE;
  140. }
  141. return PJ_TRUE;
  142. }
  143. /*
  144. * main()
  145. */
  146. int main(int argc, char *argv[])
  147. {
  148. pj_status_t status;
  149. global.port = 5060;
  150. pj_log_set_level(4);
  151. status = init_options(argc, argv);
  152. if (status != PJ_SUCCESS)
  153. return 1;
  154. status = init_stack();
  155. if (status != PJ_SUCCESS) {
  156. app_perror("Error initializing stack", status);
  157. return 1;
  158. }
  159. status = init_proxy();
  160. if (status != PJ_SUCCESS) {
  161. app_perror("Error initializing proxy", status);
  162. return 1;
  163. }
  164. status = init_stateless_proxy();
  165. if (status != PJ_SUCCESS) {
  166. app_perror("Error initializing stateless proxy", status);
  167. return 1;
  168. }
  169. #if PJ_HAS_THREADS
  170. status = pj_thread_create(global.pool, "sproxy", &worker_thread,
  171. NULL, 0, 0, &global.thread);
  172. if (status != PJ_SUCCESS) {
  173. app_perror("Error creating thread", status);
  174. return 1;
  175. }
  176. while (!global.quit_flag) {
  177. char line[10];
  178. puts("\n"
  179. "Menu:\n"
  180. " q quit\n"
  181. " d dump status\n"
  182. " dd dump detailed status\n"
  183. "");
  184. if (fgets(line, sizeof(line), stdin) == NULL) {
  185. puts("EOF while reading stdin, will quit now..");
  186. global.quit_flag = PJ_TRUE;
  187. break;
  188. }
  189. if (line[0] == 'q') {
  190. global.quit_flag = PJ_TRUE;
  191. } else if (line[0] == 'd') {
  192. pj_bool_t detail = (line[1] == 'd');
  193. pjsip_endpt_dump(global.endpt, detail);
  194. #if STATEFUL
  195. pjsip_tsx_layer_dump(detail);
  196. #endif
  197. }
  198. }
  199. pj_thread_join(global.thread);
  200. #else
  201. puts("\nPress Ctrl-C to quit\n");
  202. for (;;) {
  203. pj_time_val delay = {0, 0};
  204. pjsip_endpt_handle_events(global.endpt, &delay);
  205. }
  206. #endif
  207. destroy_stack();
  208. return 0;
  209. }