Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/stateless_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 "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 pjsip_tx_data_dec_ref(tdata);
166 app_perror("Error forwarding response", status);
167 return PJ_TRUE;
168 }
169
170 return PJ_TRUE;
171 }
172
173
174 /*
175 * main()
176 */
177 int 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
224 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 }
229
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