123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- /*
- * $Id$
- *
- * Copyright (C) 2001-2003 Fhg Fokus
- *
- * This file is part of ser, a free SIP server.
- *
- * ser is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * For a license to use the ser software under conditions
- * other than those described here, or to purchase support for this
- * software, please contact iptel.org by e-mail at the following addresses:
- * [email protected]
- *
- * ser is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include "forward.h"
- #include "hash_func.h"
- #include "config.h"
- #include "parser/msg_parser.h"
- #include "route.h"
- #include "dprint.h"
- #include "udp_server.h"
- #include "globals.h"
- #include "data_lump.h"
- #include "ut.h"
- #include "mem/mem.h"
- #include "msg_translator.h"
- #include "sr_module.h"
- #include "stats.h"
- #include "ip_addr.h"
- #include "resolve.h"
- #include "name_alias.h"
- #ifdef USE_TCP
- #include "tcp_server.h"
- #endif
- #ifdef DEBUG_DMALLOC
- #include <dmalloc.h>
- #endif
- /* returns a socket_info pointer to the sending socket or 0 on error
- * params: destination socket_union pointer
- */
- struct socket_info* get_send_socket(union sockaddr_union* to, int proto)
- {
- struct socket_info* send_sock;
-
- send_sock=0;
- /* check if we need to change the socket (different address families -
- * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
- #ifdef USE_TCP
- if (proto==PROTO_TCP){
- /* on tcp just use the "main address", we don't really now the
- * sending address (we can find it out, but we'll find also to see
- * if we listen on it, and if yes on which port -> too complicated*/
- switch(to->s.sa_family){
- case AF_INET: send_sock=sendipv4_tcp;
- break;
- #ifdef USE_IPV6
- case AF_INET6: send_sock=sendipv6_tcp;
- break;
- #endif
- default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
- " to forward to af %d\n", to->s.sa_family);
- }
- }else
- #endif
- if ((bind_address==0) ||(to->s.sa_family!=bind_address->address.af)){
- switch(to->s.sa_family){
- case AF_INET: send_sock=sendipv4;
- break;
- #ifdef USE_IPV6
- case AF_INET6: send_sock=sendipv6;
- break;
- #endif
- default: LOG(L_ERR, "get_send_socket: BUG: don't know how"
- " to forward to af %d\n", to->s.sa_family);
- }
- }else send_sock=bind_address;
- return send_sock;
- }
- /* checks if the host:port is one of the address we listen on;
- * if port==0, the port number is ignored
- * returns 1 if true, 0 if false, -1 on error
- */
- int check_self(str* host, unsigned short port)
- {
- int r;
-
- for (r=0; r<sock_no; r++){
- DBG("check_self - checking if host==us: %d==%d && "
- " [%.*s] == [%.*s]\n",
- host->len,
- sock_info[r].name.len,
- host->len, host->s,
- sock_info[r].name.len, sock_info[r].name.s
- );
- if ((port)&&(sock_info[r].port_no!=port)) continue;
- if ( (host->len==sock_info[r].name.len) &&
- #ifdef USE_IPV6
- (strncasecmp(host->s, sock_info[r].name.s,
- sock_info[r].name.len)==0) /*slower*/
- #else
- (memcmp(host->s, sock_info[r].name.s,
- sock_info[r].name.len)==0)
- #endif
- )
- break;
- /* check if host == ip address */
- if ( (!sock_info[r].is_ip) &&
- (host->len==sock_info[r].address_str.len) &&
- #ifdef USE_IPV6
- (strncasecmp(host->s, sock_info[r].address_str.s,
- sock_info[r].address_str.len)==0) /*slower*/
- #else
- (memcmp(host->s, sock_info[r].address_str.s,
- sock_info[r].address_str.len)==0)
- #endif
- )
- break;
- }
- if (r==sock_no){
- /* try to look into the aliases*/
- if (grep_aliases(host->s, host->len, port)==0){
- DBG("check_self: host != me\n");
- return 0;
- }
- }
- return 1;
- }
- int forward_request( struct sip_msg* msg, struct proxy_l * p, int proto)
- {
- unsigned int len;
- char* buf;
- union sockaddr_union* to;
- struct socket_info* send_sock;
- char md5[MD5_LEN];
- int id; /* used as branch for tcp! */
-
- to=0;
- buf=0;
- id=0;
-
- to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
- if (to==0){
- ser_error=E_OUT_OF_MEM;
- LOG(L_ERR, "ERROR: forward_request: out of memory\n");
- goto error;
- }
-
-
- /* if error try next ip address if possible */
- if (p->ok==0){
- if (p->host.h_addr_list[p->addr_idx+1])
- p->addr_idx++;
- else p->addr_idx=0;
- p->ok=1;
- }
-
- hostent2su(to, &p->host, p->addr_idx,
- (p->port)?htons(p->port):htons(SIP_PORT));
- p->tx++;
- p->tx_bytes+=len;
-
- send_sock=get_send_socket(to, proto);
- if (send_sock==0){
- LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
- "no coresponding listening socket\n", to->s.sa_family);
- ser_error=E_NO_SOCKET;
- goto error1;
- }
- /* calculate branch for outbound request; if syn_branch is turned off,
- calculate is from transaction key, i.e., as an md5 of From/To/CallID/
- CSeq exactly the same way as TM does; good for reboot -- than messages
- belonging to transaction lost due to reboot will still be forwarded
- with the same branch parameter and will be match-able downstream
- if it is turned on, we don't care about reboot; we simply put a simple
- value in there; better for performance
- */
- #ifdef USE_TCP
- if (msg->rcv.proto==PROTO_TCP) id=msg->rcv.proto_reserved1;
- #endif
- if (syn_branch ) {
- *msg->add_to_branch_s='0';
- msg->add_to_branch_len=1;
- } else {
- if (!char_msg_val( msg, md5 )) { /* parses transaction key */
- LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
- goto error1;
- }
- msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
- if (!branch_builder( msg->hash_index, 0, md5, id /* 0-th branch */,
- msg->add_to_branch_s, &msg->add_to_branch_len )) {
- LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
- goto error1;
- }
- }
- buf = build_req_buf_from_sip_req( msg, &len, send_sock, proto);
- if (!buf){
- LOG(L_ERR, "ERROR: forward_request: building failed\n");
- goto error1;
- }
- /* send it! */
- DBG("Sending:\n%s.\n", buf);
- DBG("orig. len=%d, new_len=%d, proto=%d\n", msg->len, len, proto );
-
-
- if (proto==PROTO_UDP){
- if (udp_send(send_sock, buf, len, to)==-1){
- ser_error=E_SEND;
- p->errors++;
- p->ok=0;
- STATS_TX_DROPS;
- goto error1;
- }
- }
- #ifdef USE_TCP
- else if (proto==PROTO_TCP){
- if (tcp_send(buf, len, to, 0)==-1){
- ser_error=E_SEND;
- p->errors++;
- p->ok=0;
- STATS_TX_DROPS;
- goto error1;
- }
- }
- #endif
- else{
- LOG(L_CRIT, "BUG: forward_request: unknown proto %d\n", proto);
- ser_error=E_SEND;
- STATS_TX_DROPS;
- goto error1;
- }
- /* sent requests stats */
- STATS_TX_REQUEST( msg->first_line.u.request.method_value );
-
- pkg_free(buf);
- free(to);
- /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
- return 0;
- error1:
- free(to);
- error:
- if (buf) pkg_free(buf);
- return -1;
- }
- int update_sock_struct_from_via( union sockaddr_union* to,
- struct via_body* via )
- {
- struct hostent* he;
- str* name;
- unsigned short port;
- if (via->received){
- DBG("update_sock_struct_from_via: using 'received'\n");
- name=&(via->received->value);
- /* making sure that we won't do SRV lookup on "received"
- * (possible if no DNS_IP_HACK is used)*/
- port=via->port?via->port:SIP_PORT;
- }else{
- DBG("update_sock_struct_from_via: using via host\n");
- name=&(via->host);
- port=via->port;
- }
- /* we do now a malloc/memcpy because gethostbyname loves \0-terminated
- strings; -jiri
- but only if host is not null terminated
- (host.s[len] will always be ok for a via)
- BTW: when is via->host.s non null terminated? tm copy? - andrei
- Yes -- it happened on generating a 408 by TM; -jiri
- sip_resolvehost now accepts str -janakj
- */
- DBG("update_sock_struct_from_via: trying SRV lookup\n");
- he=sip_resolvehost(name, &port);
-
- if (he==0){
- LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%.*s) failure\n",
- name->len, name->s);
- return -1;
- }
- hostent2su(to, he, 0, htons(port));
- return 1;
- }
- /* removes first via & sends msg to the second */
- int forward_reply(struct sip_msg* msg)
- {
- char* new_buf;
- union sockaddr_union* to;
- struct socket_info* send_sock;
- unsigned int new_len;
- struct sr_module *mod;
- int proto;
- #ifdef USE_TCP
- char* s;
- char* p;
- int len;
- int id;
- #endif
-
- to=0;
- new_buf=0;
- /*check if first via host = us */
- if (check_via){
- if (check_self(&msg->via1->host,
- msg->via1->port?msg->via1->port:SIP_PORT)!=1){
- LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
- " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
- msg->via1->port);
- /* send error msg back? */
- goto error;
- }
- }
- /* quick hack, slower for mutliple modules*/
- for (mod=modules;mod;mod=mod->next){
- if ((mod->exports) && (mod->exports->response_f)){
- DBG("forward_reply: found module %s, passing reply to it\n",
- mod->exports->name);
- if (mod->exports->response_f(msg)==0) goto skip;
- }
- }
- /* we have to forward the reply stateless, so we need second via -bogdan*/
- if (parse_headers( msg, HDR_VIA2, 0 )==-1
- || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
- {
- /* no second via => error */
- LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
- goto error;
- }
- to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
- if (to==0){
- LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
- goto error;
- }
- new_buf = build_res_buf_from_sip_res( msg, &new_len);
- if (!new_buf){
- LOG(L_ERR, "ERROR: forward_reply: building failed\n");
- goto error;
- }
- if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
- send_sock=get_send_socket(to, msg->rcv.proto);
- if (send_sock==0){
- LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
- goto error;
- }
- proto=msg->via2->proto;
- if (proto==PROTO_UDP){
- if (udp_send(send_sock, new_buf,new_len, to)==-1)
- {
- STATS_TX_DROPS;
- goto error;
- }
- }
- #ifdef USE_TCP
- else if (proto==PROTO_TCP){
- id=0;
- /* find id in branch if it exists */
- if ((msg->via1->branch)&&(msg->via1->branch->value.len>MCOOKIE_LEN) &&
- (memcmp(msg->via1->branch->value.s, MCOOKIE, MCOOKIE_LEN)==0)){
- DBG("forward_reply: found branch\n");
- s=msg->via1->branch->value.s+MCOOKIE_LEN;
- len=msg->via1->branch->value.len-MCOOKIE_LEN;
- for (p=s; p<s+len && *p!=BRANCH_SEPARATOR; p++);
- p++;
- for(;p<s+len && *p!=BRANCH_SEPARATOR; p++);
- p++;
- if (p<s+len){
- /* we found the second BRANCH_SEPARATOR, p points after it */
- len-=(int)(p-s);
- id=reverse_hex2int(p, len);
- DBG("forward_reply: id= %x\n", id);
- }else{
- DBG("forward_reply: no id in branch\n");
- }
- }
-
- if (tcp_send(new_buf, new_len, to, id)==-1)
- {
- STATS_TX_DROPS;
- goto error;
- }
- }
- #endif
- else{
- LOG(L_CRIT, "BUG: forward_reply: unknown proto %d\n", proto);
- goto error;
- }
- #ifdef STATS
- STATS_TX_RESPONSE( (msg->first_line.u.reply.statuscode/100) );
- #endif
- DBG(" reply forwarded to %s:%d\n", msg->via2->host.s,
- (unsigned short) msg->via2->port);
- pkg_free(new_buf);
- free(to);
- skip:
- return 0;
- error:
- if (new_buf) pkg_free(new_buf);
- if (to) free(to);
- return -1;
- }
|