blob: de3b53399ae52af2d03122580a60929d18664c39 [file] [log] [blame]
Benny Prijonofff245c2007-04-02 11:44:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijonofff245c2007-04-02 11:44:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#define THIS_FILE "stateless_proxy.c"
21
22/* Common proxy functions */
23#define STATEFUL 0
24#include "proxy.h"
25
26
27/* Callback to be called to handle incoming requests. */
28static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
29
30/* Callback to be called to handle incoming response. */
31static pj_bool_t on_rx_response( pjsip_rx_data *rdata );
32
33
34static pj_status_t init_stateless_proxy(void)
35{
36 static pjsip_module mod_stateless_proxy =
37 {
38 NULL, NULL, /* prev, next. */
39 { "mod-stateless-proxy", 19 }, /* Name. */
40 -1, /* Id */
41 PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, /* Priority */
42 NULL, /* load() */
43 NULL, /* start() */
44 NULL, /* stop() */
45 NULL, /* unload() */
46 &on_rx_request, /* on_rx_request() */
47 &on_rx_response, /* on_rx_response() */
48 NULL, /* on_tx_request. */
49 NULL, /* on_tx_response() */
50 NULL, /* on_tsx_state() */
51 };
52
53 pj_status_t status;
54
55 /* Register our module to receive incoming requests. */
56 status = pjsip_endpt_register_module( global.endpt, &mod_stateless_proxy);
57 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
58
59 return PJ_SUCCESS;
60}
61
62
63/* Callback to be called to handle incoming requests. */
64static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
65{
66 pjsip_tx_data *tdata;
67 pj_status_t status;
68
69
70 /* Verify incoming request */
71 status = proxy_verify_request(rdata);
72 if (status != PJ_SUCCESS) {
73 app_perror("RX invalid request", status);
74 return PJ_TRUE;
75 }
76
77 /*
78 * Request looks sane, next clone the request to create transmit data.
79 */
80 status = pjsip_endpt_create_request_fwd(global.endpt, rdata, NULL,
81 NULL, 0, &tdata);
82 if (status != PJ_SUCCESS) {
83 pjsip_endpt_respond_stateless(global.endpt, rdata,
84 PJSIP_SC_INTERNAL_SERVER_ERROR, NULL,
85 NULL, NULL);
86 return PJ_TRUE;
87 }
88
89
90 /* Process routing */
91 status = proxy_process_routing(tdata);
92 if (status != PJ_SUCCESS) {
93 app_perror("Error processing route", status);
94 return PJ_TRUE;
95 }
96
97 /* Calculate target */
98 status = proxy_calculate_target(rdata, tdata);
99 if (status != PJ_SUCCESS) {
100 app_perror("Error calculating target", status);
101 return PJ_TRUE;
102 }
103
104 /* Target is set, forward the request */
105 status = pjsip_endpt_send_request_stateless(global.endpt, tdata,
106 NULL, NULL);
107 if (status != PJ_SUCCESS) {
108 app_perror("Error forwarding request", status);
109 return PJ_TRUE;
110 }
111
112 return PJ_TRUE;
113}
114
115
116/* Callback to be called to handle incoming response. */
117static pj_bool_t on_rx_response( pjsip_rx_data *rdata )
118{
119 pjsip_tx_data *tdata;
120 pjsip_response_addr res_addr;
121 pjsip_via_hdr *hvia;
122 pj_status_t status;
123
124 /* Create response to be forwarded upstream (Via will be stripped here) */
125 status = pjsip_endpt_create_response_fwd(global.endpt, rdata, 0, &tdata);
126 if (status != PJ_SUCCESS) {
127 app_perror("Error creating response", status);
128 return PJ_TRUE;
129 }
130
131 /* Get topmost Via header */
132 hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL);
133 if (hvia == NULL) {
134 /* Invalid response! Just drop it */
135 pjsip_tx_data_dec_ref(tdata);
136 return PJ_TRUE;
137 }
138
139 /* Calculate the address to forward the response */
140 pj_bzero(&res_addr, sizeof(res_addr));
141 res_addr.dst_host.type = PJSIP_TRANSPORT_UDP;
142 res_addr.dst_host.flag = pjsip_transport_get_flag_from_type(PJSIP_TRANSPORT_UDP);
143
144 /* Destination address is Via's received param */
145 res_addr.dst_host.addr.host = hvia->recvd_param;
146 if (res_addr.dst_host.addr.host.slen == 0) {
147 /* Someone has messed up our Via header! */
148 res_addr.dst_host.addr.host = hvia->sent_by.host;
149 }
150
151 /* Destination port is the rpot */
152 if (hvia->rport_param != 0 && hvia->rport_param != -1)
153 res_addr.dst_host.addr.port = hvia->rport_param;
154
155 if (res_addr.dst_host.addr.port == 0) {
156 /* Ugh, original sender didn't put rport!
157 * At best, can only send the response to the port in Via.
158 */
159 res_addr.dst_host.addr.port = hvia->sent_by.port;
160 }
161
162 /* Forward response */
163 status = pjsip_endpt_send_response(global.endpt, &res_addr, tdata,
164 NULL, NULL);
165 if (status != PJ_SUCCESS) {
166 app_perror("Error forwarding response", status);
167 return PJ_TRUE;
168 }
169
170 return PJ_TRUE;
171}
172
173
174/*
175 * main()
176 */
177int main(int argc, char *argv[])
178{
179 pj_status_t status;
180
181 global.port = 5060;
182 pj_log_set_level(4);
183
184 status = init_options(argc, argv);
185 if (status != PJ_SUCCESS)
186 return 1;
187
188 status = init_stack();
189 if (status != PJ_SUCCESS) {
190 app_perror("Error initializing stack", status);
191 return 1;
192 }
193
194 status = init_proxy();
195 if (status != PJ_SUCCESS) {
196 app_perror("Error initializing proxy", status);
197 return 1;
198 }
199
200 status = init_stateless_proxy();
201 if (status != PJ_SUCCESS) {
202 app_perror("Error initializing stateless proxy", status);
203 return 1;
204 }
205
206#if PJ_HAS_THREADS
207 status = pj_thread_create(global.pool, "sproxy", &worker_thread,
208 NULL, 0, 0, &global.thread);
209 if (status != PJ_SUCCESS) {
210 app_perror("Error creating thread", status);
211 return 1;
212 }
213
214 while (!global.quit_flag) {
215 char line[10];
216
217 puts("\n"
218 "Menu:\n"
219 " q quit\n"
220 " d dump status\n"
221 " dd dump detailed status\n"
222 "");
223
Benny Prijono32d267b2009-01-01 22:08:21 +0000224 if (fgets(line, sizeof(line), stdin) == NULL) {
225 puts("EOF while reading stdin, will quit now..");
226 global.quit_flag = PJ_TRUE;
227 break;
228 }
Benny Prijonofff245c2007-04-02 11:44:47 +0000229
230 if (line[0] == 'q') {
231 global.quit_flag = PJ_TRUE;
232 } else if (line[0] == 'd') {
233 pj_bool_t detail = (line[1] == 'd');
234 pjsip_endpt_dump(global.endpt, detail);
235#if STATEFUL
236 pjsip_tsx_layer_dump(detail);
237#endif
238 }
239 }
240
241 pj_thread_join(global.thread);
242
243#else
244 puts("\nPress Ctrl-C to quit\n");
245 for (;;) {
246 pj_time_val delay = {0, 0};
247 pjsip_endpt_handle_events(global.endpt, &delay);
248 }
249#endif
250
251 destroy_stack();
252
253 return 0;
254}
255