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