00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <string.h>
00057 #include <stdio.h>
00058 #include <stdlib.h>
00059 #include <errno.h>
00060 #include <sys/types.h>
00061 #include <sys/socket.h>
00062 #include <netdb.h>
00063 #include <netinet/in.h>
00064 #include <arpa/inet.h>
00065
00066 #include "forward.h"
00067 #include "parser/msg_parser.h"
00068 #include "dprint.h"
00069 #include "ut.h"
00070 #include "mem/mem.h"
00071 #include "msg_translator.h"
00072 #include "sr_module.h"
00073 #include "ip_addr.h"
00074 #include "resolve.h"
00075 #include "name_alias.h"
00076 #include "socket_info.h"
00077 #include "core_stats.h"
00078 #include "blacklists.h"
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 struct socket_info* get_out_socket(union sockaddr_union* to, int proto)
00091 {
00092 int temp_sock;
00093 socklen_t len;
00094 union sockaddr_union from;
00095 struct socket_info* si;
00096 struct ip_addr ip;
00097
00098 if (proto!=PROTO_UDP) {
00099 LM_CRIT("can only be called for UDP\n");
00100 return 0;
00101 }
00102
00103 temp_sock=socket(to->s.sa_family, SOCK_DGRAM, 0 );
00104 if (temp_sock==-1) {
00105 LM_ERR("socket() failed: %s\n", strerror(errno));
00106 return 0;
00107 }
00108 if (connect(temp_sock, &to->s, sockaddru_len(*to))==-1) {
00109 LM_ERR("connect failed: %s\n", strerror(errno));
00110 goto error;
00111 }
00112 len=sizeof(from);
00113 if (getsockname(temp_sock, &from.s, &len)==-1) {
00114 LM_ERR("getsockname failed: %s\n", strerror(errno));
00115 goto error;
00116 }
00117 su2ip_addr(&ip, &from);
00118 si=find_si(&ip, 0, proto);
00119 if (si==0) goto error;
00120 close(temp_sock);
00121 LM_DBG("socket determined: %p\n", si );
00122 return si;
00123 error:
00124 LM_ERR("no socket found\n");
00125 close(temp_sock);
00126 return 0;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 struct socket_info* get_send_socket(struct sip_msg *msg,
00139 union sockaddr_union* to, int proto)
00140 {
00141 struct socket_info* send_sock;
00142
00143
00144 if (msg && msg->force_send_socket){
00145 if (msg->force_send_socket->proto!=proto){
00146 LM_DBG("force_send_socket of different proto (%d)!\n", proto);
00147 msg->force_send_socket=find_si(&(msg->force_send_socket->address),
00148 msg->force_send_socket->port_no,
00149 proto);
00150 }
00151 if (msg->force_send_socket && (msg->force_send_socket->socket!=-1))
00152 return msg->force_send_socket;
00153 else{
00154 if (msg->force_send_socket && msg->force_send_socket->socket==-1)
00155 LM_WARN("not listening on the requested socket, no fork mode?\n");
00156 else
00157 LM_WARN("protocol/port mismatch\n");
00158 }
00159 };
00160
00161 if (mhomed && proto==PROTO_UDP){
00162 send_sock=get_out_socket(to, proto);
00163 if ((send_sock==0) || (send_sock->socket!=-1))
00164 return send_sock;
00165 else if (send_sock->socket==-1){
00166 LM_WARN("not listening on the requested socket, no fork mode?\n");
00167
00168 }
00169 }
00170
00171 send_sock=0;
00172
00173
00174 switch(proto){
00175 #ifdef USE_TCP
00176 case PROTO_TCP:
00177
00178
00179
00180 switch(to->s.sa_family){
00181
00182 case AF_INET: send_sock=sendipv4_tcp;
00183 break;
00184 #ifdef USE_IPV6
00185 case AF_INET6: send_sock=sendipv6_tcp;
00186 break;
00187 #endif
00188 default: LM_ERR("don't know how to forward to af %d\n",
00189 to->s.sa_family);
00190 }
00191 break;
00192 #endif
00193 #ifdef USE_TLS
00194 case PROTO_TLS:
00195 switch(to->s.sa_family){
00196
00197 case AF_INET: send_sock=sendipv4_tls;
00198 break;
00199 #ifdef USE_IPV6
00200 case AF_INET6: send_sock=sendipv6_tls;
00201 break;
00202 #endif
00203 default: LM_ERR("don't know how"
00204 " to forward to af %d\n", to->s.sa_family);
00205 }
00206 break;
00207 #endif
00208 #ifdef USE_SCTP
00209 case PROTO_SCTP:
00210 switch(to->s.sa_family){
00211 case AF_INET: send_sock=sendipv4_sctp;
00212 break;
00213 #ifdef USE_IPV6
00214 case AF_INET6: send_sock=sendipv6_sctp;
00215 break;
00216 #endif
00217 default: LM_ERR("don't know how to forward to af %d\n",
00218 to->s.sa_family);
00219 }
00220 break;
00221 #endif
00222 case PROTO_UDP:
00223 if ((bind_address==0)||(to->s.sa_family!=bind_address->address.af)||
00224 (bind_address->proto!=PROTO_UDP)){
00225 switch(to->s.sa_family){
00226 case AF_INET: send_sock=sendipv4;
00227 break;
00228 #ifdef USE_IPV6
00229 case AF_INET6: send_sock=sendipv6;
00230 break;
00231 #endif
00232 default: LM_ERR("don't know how to forward to af %d\n",
00233 to->s.sa_family);
00234 }
00235 }else send_sock=bind_address;
00236 break;
00237 default:
00238 LM_CRIT("unknown proto %d\n", proto);
00239 }
00240 return send_sock;
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 int check_self(str* host, unsigned short port, unsigned short proto)
00254 {
00255 if (grep_sock_info(host, port, proto)) goto found;
00256
00257 if (grep_aliases(host->s, host->len, port, proto)==0){
00258 LM_DBG("host != me\n");
00259 return 0;
00260 }
00261 found:
00262 LM_DBG("host == me\n");
00263 return 1;
00264 }
00265
00266
00267
00268 static inline str* get_sl_branch(struct sip_msg* msg)
00269 {
00270 static str default_branch = str_init("0");
00271 struct hdr_field *h_via;
00272 struct via_body *b_via;
00273 str *branch;
00274 int via_parsed;
00275
00276 via_parsed = 0;
00277 branch = 0;
00278
00279
00280 for( h_via=msg->h_via1 ; h_via ; h_via=h_via->sibling ) {
00281
00282 b_via = (struct via_body*)h_via->parsed;
00283 for( ; b_via ; b_via=b_via->next ) {
00284
00285 if (b_via->branch==0 || b_via->branch->value.s==0
00286 || b_via->branch->value.len==0 )
00287 continue;
00288 branch = &b_via->branch->value;
00289
00290 if (branch->len <= (int)MCOOKIE_LEN
00291 || memcmp( branch->s, MCOOKIE, MCOOKIE_LEN)!=0 )
00292 continue;
00293
00294 goto found;
00295 }
00296
00297 if (!via_parsed) {
00298 if ( parse_headers(msg,HDR_EOH_F,0)<0 ) {
00299 LM_ERR("failed to parse all hdrs\n");
00300 return 0;
00301 }
00302 via_parsed = 1;
00303 }
00304 }
00305
00306
00307 found:
00308 if (branch) {
00309 if (branch->len>(int)MAX_BRANCH_PARAM_LEN)
00310 branch->len = MAX_BRANCH_PARAM_LEN;
00311 } else {
00312
00313 branch = &default_branch;
00314 }
00315 return branch;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 int forward_request( struct sip_msg* msg, struct proxy_l * p)
00329 {
00330 union sockaddr_union to;
00331 unsigned int len;
00332 char* buf;
00333 struct socket_info* send_sock;
00334 struct socket_info* last_sock;
00335 str *branch;
00336
00337 buf=0;
00338
00339
00340
00341
00342
00343
00344 if ( msg->add_to_branch_len==0 ) {
00345 branch = get_sl_branch(msg);
00346 if (branch==0) {
00347 LM_ERR("unable to compute branch\n");
00348 goto error;
00349 }
00350 msg->add_to_branch_len = branch->len;
00351 memcpy( msg->add_to_branch_s, branch->s, branch->len);
00352 }
00353
00354 hostent2su( &to, &p->host, p->addr_idx, (p->port)?p->port:SIP_PORT);
00355 last_sock = 0;
00356
00357 do {
00358 send_sock=get_send_socket( msg, &to, p->proto);
00359 if (send_sock==0){
00360 LM_ERR("cannot forward to af %d, proto %d no corresponding"
00361 "listening socket\n", to.s.sa_family, p->proto);
00362 ser_error=E_NO_SOCKET;
00363 continue;
00364 }
00365
00366 if ( last_sock!=send_sock ) {
00367
00368 if (buf)
00369 pkg_free(buf);
00370
00371 buf = build_req_buf_from_sip_req(msg, &len, send_sock, p->proto, 0);
00372 if (!buf){
00373 LM_ERR("building req buf failed\n");
00374 goto error;
00375 }
00376
00377 last_sock = send_sock;
00378 }
00379
00380 if (check_blacklists( p->proto, &to, buf, len)) {
00381 LM_DBG("blocked by blacklists\n");
00382 ser_error=E_IP_BLOCKED;
00383 continue;
00384 }
00385
00386
00387 LM_DBG("sending:\n%.*s.\n", (int)len, buf);
00388 LM_DBG("orig. len=%d, new_len=%d, proto=%d\n",
00389 msg->len, len, p->proto );
00390
00391 if (msg_send(send_sock, p->proto, &to, 0, buf, len)<0){
00392 ser_error=E_SEND;
00393 continue;
00394 }
00395
00396 ser_error = 0;
00397 break;
00398
00399 }while( get_next_su( p, &to, (ser_error==E_IP_BLOCKED)?0:1)==0 );
00400
00401 if (ser_error) {
00402 update_stat( drp_reqs, 1);
00403 goto error;
00404 }
00405
00406
00407 update_stat( fwd_reqs, 1);
00408
00409 pkg_free(buf);
00410
00411 return 0;
00412
00413 error:
00414 if (buf) pkg_free(buf);
00415 return -1;
00416 }
00417
00418
00419
00420 int update_sock_struct_from_via( union sockaddr_union* to,
00421 struct sip_msg* msg,
00422 struct via_body* via )
00423 {
00424 struct hostent* he;
00425 str* name;
00426 int err;
00427 unsigned short port;
00428
00429 port=0;
00430 if(via==msg->via1){
00431
00432
00433
00434 if ((msg->msg_flags&FL_FORCE_RPORT)||(via->rport))
00435 port=msg->rcv.src_port;
00436 else port=via->port;
00437 if(via->maddr)
00438 name= &(via->maddr->value);
00439 else
00440 name=&(via->host);
00441
00442 }else{
00443
00444 if (via->rport && via->rport->value.s){
00445 LM_DBG("using 'rport'\n");
00446 port=str2s(via->rport->value.s, via->rport->value.len, &err);
00447 if (err){
00448 LM_NOTICE("bad rport value(%.*s)\n",
00449 via->rport->value.len,via->rport->value.s);
00450 port=0;
00451 }
00452 }
00453
00454 if (via->maddr){
00455 name= &(via->maddr->value);
00456 if (port==0) port=via->port?via->port:SIP_PORT;
00457 } else if (via->received){
00458 LM_DBG("using 'received'\n");
00459 name=&(via->received->value);
00460
00461 if (port==0) port=via->port?via->port:SIP_PORT;
00462 }else{
00463 LM_DBG("using via host\n");
00464 name=&(via->host);
00465 if (port==0) port=via->port;
00466 }
00467 }
00468 LM_DBG("trying SRV lookup\n");
00469 he=sip_resolvehost(name, &port, &via->proto, 0, 0);
00470
00471 if (he==0){
00472 LM_NOTICE("resolve_host(%.*s) failure\n", name->len, name->s);
00473 return -1;
00474 }
00475
00476 hostent2su( to, he, 0, port);
00477 return 1;
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 int forward_reply(struct sip_msg* msg)
00489 {
00490 char* new_buf;
00491 union sockaddr_union* to;
00492 unsigned int new_len;
00493 struct sr_module *mod;
00494 int proto;
00495 int id;
00496 struct socket_info *send_sock;
00497 #ifdef USE_TCP
00498 char* s;
00499 int len;
00500 #endif
00501
00502 to=0;
00503 id=0;
00504 new_buf=0;
00505
00506 if (check_via){
00507 if (check_self(&msg->via1->host,
00508 msg->via1->port?msg->via1->port:SIP_PORT,
00509 msg->via1->proto)!=1){
00510 LM_ERR("host in first via!=me : %.*s:%d\n",
00511 msg->via1->host.len, msg->via1->host.s, msg->via1->port);
00512
00513 goto error;
00514 }
00515 }
00516
00517 for (mod=modules;mod;mod=mod->next){
00518 if ((mod->exports) && (mod->exports->response_f)){
00519 LM_DBG("found module %s, passing reply to it\n",
00520 mod->exports->name);
00521 if (mod->exports->response_f(msg)==0) goto skip;
00522 }
00523 }
00524
00525
00526 if (parse_headers( msg, HDR_VIA2_F, 0 )==-1
00527 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
00528 {
00529
00530 LM_ERR("no 2nd via found in reply\n");
00531 goto error;
00532 }
00533
00534 to=(union sockaddr_union*)pkg_malloc(sizeof(union sockaddr_union));
00535 if (to==0){
00536 LM_ERR("out of pkg memory\n");
00537 goto error;
00538 }
00539
00540 new_buf = build_res_buf_from_sip_res( msg, &new_len);
00541 if (!new_buf){
00542 LM_ERR("failed to build rpl from req failed\n");
00543 goto error;
00544 }
00545
00546 proto=msg->via2->proto;
00547 if (update_sock_struct_from_via( to, msg, msg->via2 )==-1) goto error;
00548
00549 #ifdef USE_TCP
00550 if (proto==PROTO_TCP
00551 #ifdef USE_TLS
00552 || proto==PROTO_TLS
00553 #endif
00554 ){
00555
00556 if (msg->via1->i&&msg->via1->i->value.s){
00557 s=msg->via1->i->value.s;
00558 len=msg->via1->i->value.len;
00559 id=reverse_hex2int(s, len);
00560 }
00561 }
00562 #endif
00563
00564 send_sock = get_send_socket(msg, to, proto);
00565
00566 if (msg_send(send_sock, proto, to, id, new_buf, new_len)<0) {
00567 update_stat( drp_rpls, 1);
00568 goto error0;
00569 }
00570 update_stat( fwd_rpls, 1);
00571
00572
00573
00574
00575
00576
00577 LM_DBG("reply forwarded to %.*s:%d\n", msg->via2->host.len,
00578 msg->via2->host.s, (unsigned short) msg->via2->port);
00579
00580 pkg_free(new_buf);
00581 pkg_free(to);
00582 skip:
00583 return 0;
00584 error:
00585 update_stat( err_rpls, 1);
00586 error0:
00587 if (new_buf) pkg_free(new_buf);
00588 if (to) pkg_free(to);
00589 return -1;
00590 }