| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define THIS_FILE "stateless_proxy.c" |
| 20 |
|
| 21 |
|
| 22 |
#define STATEFUL 0 |
| 23 |
#include "proxy.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 28 |
|
| 29 |
|
| 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, |
| 38 |
{ "mod-stateless-proxy", 19 }, |
| 39 |
-1, |
| 40 |
PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, |
| 41 |
NULL, |
| 42 |
NULL, |
| 43 |
NULL, |
| 44 |
NULL, |
| 45 |
&on_rx_request, |
| 46 |
&on_rx_response, |
| 47 |
NULL, |
| 48 |
NULL, |
| 49 |
NULL, |
| 50 |
}; |
| 51 |
|
| 52 |
pj_status_t status; |
| 53 |
|
| 54 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 131 |
hvia = (pjsip_via_hdr*) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL); |
| 132 |
if (hvia == NULL) { |
| 133 |
|
| 134 |
pjsip_tx_data_dec_ref(tdata); |
| 135 |
return PJ_TRUE; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 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 |
|
| 144 |
res_addr.dst_host.addr.host = hvia->recvd_param; |
| 145 |
if (res_addr.dst_host.addr.host.slen == 0) { |
| 146 |
|
| 147 |
res_addr.dst_host.addr.host = hvia->sent_by.host; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 158 |
res_addr.dst_host.addr.port = hvia->sent_by.port; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 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 |
|
| 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 |
|