689a86d60f169c44dda4629266940333e54153bf
[sip-router] / forward.c
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2001-2003 Fhg Fokus
5  *
6  * This file is part of ser, a free SIP server.
7  *
8  * ser is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version
12  *
13  * For a license to use the ser software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * ser is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License 
24  * along with this program; if not, write to the Free Software 
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28
29
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39 #include "forward.h"
40 #include "hash_func.h"
41 #include "config.h"
42 #include "parser/msg_parser.h"
43 #include "route.h"
44 #include "dprint.h"
45 #include "udp_server.h"
46 #include "globals.h"
47 #include "data_lump.h"
48 #include "ut.h"
49 #include "mem/mem.h"
50 #include "msg_translator.h"
51 #include "sr_module.h"
52 #include "stats.h"
53 #include "ip_addr.h"
54 #include "resolve.h"
55 #include "name_alias.h"
56
57 #ifdef DEBUG_DMALLOC
58 #include <dmalloc.h>
59 #endif
60
61
62
63 /* returns a socket_info pointer to the sending socket or 0 on error
64  * params: destination socket_union pointer
65  */
66 struct socket_info* get_send_socket(union sockaddr_union* to)
67 {
68         struct socket_info* send_sock;
69         
70         send_sock=0;
71         /* check if we need to change the socket (different address families -
72          * eg: ipv4 -> ipv6 or ipv6 -> ipv4) */
73         if ((bind_address==0) ||(to->s.sa_family!=bind_address->address.af)){
74                 switch(to->s.sa_family){
75                         case AF_INET:   send_sock=sendipv4;
76                                                         break;
77 #ifdef USE_IPV6
78                         case AF_INET6:  send_sock=sendipv6;
79                                                         break;
80 #endif
81                         default:                LOG(L_ERR, "get_send_socket: BUG: don't know how"
82                                                                         " to forward to af %d\n", to->s.sa_family);
83                 }
84         }else send_sock=bind_address;
85         return send_sock;
86 }
87
88
89
90 /* checks if the host:port is one of the address we listen on;
91  * if port==0, the  port number is ignored
92  * returns 1 if true, 0 if false, -1 on error
93 */
94 int check_self(str* host, unsigned short port)
95 {
96         int r;
97         
98         for (r=0; r<sock_no; r++){
99                 DBG("check_self - checking if host==us: %d==%d && "
100                                 " [%.*s] == [%.*s]\n", 
101                                         host->len,
102                                         sock_info[r].name.len,
103                                         host->len, host->s,
104                                         sock_info[r].name.len, sock_info[r].name.s
105                         );
106                 if  ((port)&&(sock_info[r].port_no!=port)) continue;
107                 if ( (host->len==sock_info[r].name.len) && 
108         #ifdef USE_IPV6
109                         (strncasecmp(host->s, sock_info[r].name.s,
110                                                                  sock_info[r].name.len)==0) /*slower*/
111         #else
112                         (memcmp(host->s, sock_info[r].name.s, 
113                                                                 sock_info[r].name.len)==0)
114         #endif
115                         )
116                         break;
117         /* check if host == ip address */
118                 if (    (!sock_info[r].is_ip) &&
119                                 (host->len==sock_info[r].address_str.len) && 
120         #ifdef USE_IPV6
121                         (strncasecmp(host->s, sock_info[r].address_str.s,
122                                                                  sock_info[r].address_str.len)==0) /*slower*/
123         #else
124                         (memcmp(host->s, sock_info[r].address_str.s, 
125                                                                 sock_info[r].address_str.len)==0)
126         #endif
127                         )
128                         break;
129         }
130         if (r==sock_no){
131                 /* try to look into the aliases*/
132                 if (grep_aliases(host->s, host->len, port)==0){
133                         DBG("check_self: host != me\n");
134                         return 0;
135                 }
136         }
137         return 1;
138 }
139
140
141
142 int forward_request( struct sip_msg* msg, struct proxy_l * p)
143 {
144         unsigned int len;
145         char* buf;
146         union sockaddr_union* to;
147         struct socket_info* send_sock;
148         char md5[MD5_LEN];
149         
150         to=0;
151         buf=0;
152         
153         to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
154         if (to==0){
155                 ser_error=E_OUT_OF_MEM;
156                 LOG(L_ERR, "ERROR: forward_request: out of memory\n");
157                 goto error;
158         }
159         
160         
161         /* if error try next ip address if possible */
162         if (p->ok==0){
163                 if (p->host.h_addr_list[p->addr_idx+1])
164                         p->addr_idx++;
165                 else p->addr_idx=0;
166                 p->ok=1;
167         }
168         
169         hostent2su(to, &p->host, p->addr_idx, 
170                                 (p->port)?htons(p->port):htons(SIP_PORT));
171         p->tx++;
172         p->tx_bytes+=len;
173         
174
175         send_sock=get_send_socket(to);
176         if (send_sock==0){
177                 LOG(L_ERR, "forward_req: ERROR: cannot forward to af %d "
178                                 "no coresponding listening socket\n", to->s.sa_family);
179                 ser_error=E_NO_SOCKET;
180                 goto error1;
181         }
182
183         /* calculate branch for outbound request;  if syn_branch is turned off,
184            calculate is from transaction key, i.e., as an md5 of From/To/CallID/
185            CSeq exactly the same way as TM does; good for reboot -- than messages
186            belonging to transaction lost due to reboot will still be forwarded
187            with the same branch parameter and will be match-able downstream
188
189        if it is turned on, we don't care about reboot; we simply put a simple
190            value in there; better for performance
191         */
192
193         if (syn_branch ) {
194                 *msg->add_to_branch_s='0';
195                 msg->add_to_branch_len=1;
196         } else {
197                 if (!char_msg_val( msg, md5 ))  { /* parses transaction key */
198                         LOG(L_ERR, "ERROR: forward_request: char_msg_val failed\n");
199                         goto error1;
200                 }
201                 msg->hash_index=hash( msg->callid->body, get_cseq(msg)->number);
202                 if (!branch_builder( msg->hash_index, 0, md5, 0 /* 0-th branch */,
203                                         msg->add_to_branch_s, &msg->add_to_branch_len )) {
204                         LOG(L_ERR, "ERROR: forward_request: branch_builder failed\n");
205                         goto error1;
206                 }
207         }
208
209         buf = build_req_buf_from_sip_req( msg, &len, send_sock);
210         if (!buf){
211                 LOG(L_ERR, "ERROR: forward_request: building failed\n");
212                 goto error1;
213         }
214          /* send it! */
215         DBG("Sending:\n%s.\n", buf);
216         DBG("orig. len=%d, new_len=%d\n", msg->len, len );
217         
218         if (udp_send(send_sock, buf, len,  to)==-1){
219                         ser_error=E_SEND;
220                         p->errors++;
221                         p->ok=0;
222                         STATS_TX_DROPS;
223                         goto error1;
224         }
225         /* sent requests stats */
226         else STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
227         
228         pkg_free(buf);
229         free(to);
230         /* received_buf & line_buf will be freed in receive_msg by free_lump_list*/
231         return 0;
232
233 error1:
234         free(to);
235 error:
236         if (buf) pkg_free(buf);
237         return -1;
238 }
239
240
241 int update_sock_struct_from_ip( union sockaddr_union* to,
242         struct sip_msg *msg )
243 {
244
245         init_su(to, &msg->src_ip, 
246                 (msg->via1->port)?htons(msg->via1->port): htons(SIP_PORT) );
247         return 1;
248 }
249
250 int update_sock_struct_from_via( union sockaddr_union* to,
251                                                                  struct via_body* via )
252 {
253         struct hostent* he;
254         char *host_copy;
255         str* name;
256         unsigned short port;
257
258
259         if (via->received){
260                 DBG("update_sock_struct_from_via: using 'received'\n");
261                 name=&(via->received->value);
262                 /* making sure that we won't do SRV lookup on "received"
263                  * (possible if no DNS_IP_HACK is used)*/
264                 port=via->port?via->port:SIP_PORT; 
265         }else{
266                 DBG("update_sock_struct_from_via: using via host\n");
267                 name=&(via->host);
268                 port=via->port;
269         }
270         /* we do now a malloc/memcpy because gethostbyname loves \0-terminated 
271            strings; -jiri 
272            but only if host is not null terminated
273            (host.s[len] will always be ok for a via)
274             BTW: when is via->host.s non null terminated? tm copy? - andrei 
275             Yes -- it happened on generating a 408 by TM; -jiri
276         */
277         if (name->s[name->len]){
278                 host_copy=pkg_malloc( name->len+1 );
279                 if (!host_copy) {
280                         LOG(L_NOTICE, "ERROR: update_sock_struct_from_via:"
281                                                         " not enough memory\n");
282                         return -1;
283                 }
284                 memcpy(host_copy, name->s, name->len );
285                 host_copy[name->len]=0;
286                 DBG("update_sock_struct_from_via: trying SRV lookup\n");
287                 he=sip_resolvehost(host_copy, &port);
288                 
289                 pkg_free( host_copy );
290         }else{
291                 DBG("update_sock_struct_from_via: trying SRV lookup\n");
292                 he=sip_resolvehost(name->s, &port);
293         }
294         
295         if (he==0){
296                 LOG(L_NOTICE, "ERROR:forward_reply:resolve_host(%s) failure\n",
297                                 name->s);
298                 return -1;
299         }
300         hostent2su(to, he, 0, htons(port));
301         return 1;
302 }
303
304
305 /* removes first via & sends msg to the second */
306 int forward_reply(struct sip_msg* msg)
307 {
308         char* new_buf;
309         union sockaddr_union* to;
310         struct socket_info* send_sock;
311         unsigned int new_len;
312         struct sr_module *mod;
313         
314         to=0;
315         new_buf=0;
316         /*check if first via host = us */
317         if (check_via){
318                 if (check_self(&msg->via1->host,
319                                         msg->via1->port?msg->via1->port:SIP_PORT)!=1){
320                         LOG(L_NOTICE, "ERROR: forward_reply: host in first via!=me :"
321                                         " %.*s:%d\n", msg->via1->host.len, msg->via1->host.s,
322                                                                         msg->via1->port);
323                         /* send error msg back? */
324                         goto error;
325                 }
326         }
327         /* quick hack, slower for mutliple modules*/
328         for (mod=modules;mod;mod=mod->next){
329                 if ((mod->exports) && (mod->exports->response_f)){
330                         DBG("forward_reply: found module %s, passing reply to it\n",
331                                         mod->exports->name);
332                         if (mod->exports->response_f(msg)==0) goto skip;
333                 }
334         }
335
336         /* we have to forward the reply stateless, so we need second via -bogdan*/
337         if (parse_headers( msg, HDR_VIA2, 0 )==-1 
338                 || (msg->via2==0) || (msg->via2->error!=PARSE_OK))
339         {
340                 /* no second via => error */
341                 LOG(L_ERR, "ERROR: forward_msg: no 2nd via found in reply\n");
342                 goto error;
343         }
344
345         to=(union sockaddr_union*)malloc(sizeof(union sockaddr_union));
346         if (to==0){
347                 LOG(L_ERR, "ERROR: forward_reply: out of memory\n");
348                 goto error;
349         }
350
351         new_buf = build_res_buf_from_sip_res( msg, &new_len);
352         if (!new_buf){
353                 LOG(L_ERR, "ERROR: forward_reply: building failed\n");
354                 goto error;
355         }
356
357         if (update_sock_struct_from_via( to, msg->via2 )==-1) goto error;
358         send_sock=get_send_socket(to);
359         if (send_sock==0){
360                 LOG(L_ERR, "forward_reply: ERROR: no sending socket found\n");
361                 goto error;
362         }
363
364         if (udp_send(send_sock, new_buf,new_len,  to)==-1)
365         {
366                 STATS_TX_DROPS;
367                 goto error;
368         } else {
369 #ifdef STATS
370                 int j = msg->first_line.u.reply.statuscode/100;
371                 STATS_TX_RESPONSE(  j );
372 #endif
373         }
374
375         DBG(" reply forwarded to %s:%d\n", msg->via2->host.s,
376                 (unsigned short) msg->via2->port);
377
378         pkg_free(new_buf);
379         free(to);
380 skip:
381         return 0;
382 error:
383         if (new_buf) pkg_free(new_buf);
384         if (to) free(to);
385         return -1;
386 }