- core sctp support
[sip-router] / forward.h
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  * History:
29  * -------
30  *  2001-??-?? created by andrei
31  *  ????-??-?? lots of changes by a lot of people
32  *  2003-02-11 added inline msg_send (andrei)
33  *  2003-04-07 changed all ports to host byte order (andrei)
34  *  2003-04-12  FORCE_RPORT_T added (andrei)
35  *  2003-04-15  added tcp_disable support (andrei)
36  *  2006-04-12  reduced msg_send() parameter list: it uses now a struct 
37  *               dest_info param. (andrei)
38  *  2007-10-08  msg_send() will ignore a mcast send_sock and choose another
39  *               one by itself (andrei)
40  */
41
42
43
44 #ifndef forward_h
45 #define forward_h
46
47 #include "globals.h"
48 #include "parser/msg_parser.h"
49 #include "route.h"
50 #include "proxy.h"
51 #include "ip_addr.h"
52
53 #include "stats.h"
54 #include "udp_server.h"
55 #ifdef USE_TCP
56 #include "tcp_server.h"
57 #endif
58 #ifdef USE_SCTP
59 #include "sctp_server.h"
60 #endif
61
62 #include "compiler_opt.h"
63
64
65
66 struct socket_info* get_send_socket(struct sip_msg* msg,
67                                                                         union sockaddr_union* su, int proto);
68 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
69 int check_self(str* host, unsigned short port, unsigned short proto);
70 int check_self_port(unsigned short port, unsigned short proto);
71 int forward_request( struct sip_msg* msg, str* dst,  unsigned short port,
72                                                 struct dest_info* send_info);
73 int update_sock_struct_from_via( union sockaddr_union* to,
74                                                                  struct sip_msg* msg,
75                                                                  struct via_body* via );
76
77 /* use src_ip, port=src_port if rport, via port if via port, 5060 otherwise */
78 #define update_sock_struct_from_ip(  to, msg ) \
79         init_su((to), &(msg)->rcv.src_ip, \
80                         (((msg)->via1->rport)||((msg)->msg_flags&FL_FORCE_RPORT))? \
81                                                         (msg)->rcv.src_port: \
82                                                         ((msg)->via1->port)?(msg)->via1->port: SIP_PORT )
83
84 int forward_reply( struct sip_msg* msg);
85
86
87
88 /* params:
89  * dst = struct dest_info containing:
90  *    send_sock = 0 if not known (e.g. for udp in some cases), non-0 otherwise;
91  *                if 0 or mcast a new send_sock will be automatically choosen
92  *    proto = TCP|UDP
93  *    to = destination (sockaddr_union)
94  *    id = only used on tcp, it will force sending on connection "id" if id!=0 
95  *         and the connection exists, else it will send to "to" 
96  *        (useful for sending replies on  the same connection as the request
97  *         that generated them; use 0 if you don't want this)
98  * buf, len = buffer
99  * returns: 0 if ok, -1 on error*/
100 static inline int msg_send(struct dest_info* dst, char* buf, int len)
101 {
102         struct dest_info new_dst;
103         
104         if (likely(dst->proto==PROTO_UDP)){
105                 if (unlikely((dst->send_sock==0) || 
106                                         (dst->send_sock->flags & SI_IS_MCAST))){
107                         new_dst=*dst;
108                         new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
109                         if (unlikely(new_dst.send_sock==0)){
110                                 LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
111                                 goto error;
112                         }
113                         dst=&new_dst;
114                 }
115                 if (unlikely(udp_send(dst, buf, len)==-1)){
116                         STATS_TX_DROPS;
117                         LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
118                         goto error;
119                 }
120         }
121 #ifdef USE_TCP
122         else if (dst->proto==PROTO_TCP){
123                 if (unlikely(tcp_disable)){
124                         STATS_TX_DROPS;
125                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tcp and tcp"
126                                         " support is disabled\n");
127                         goto error;
128                 }else{
129                         if (unlikely(tcp_send(dst, 0, buf, len)<0)){
130                                 STATS_TX_DROPS;
131                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
132                                 goto error;
133                         }
134                 }
135         }
136 #ifdef USE_TLS
137         else if (dst->proto==PROTO_TLS){
138                 if (unlikely(tls_disable)){
139                         STATS_TX_DROPS;
140                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tls and tls"
141                                         " support is disabled\n");
142                         goto error;
143                 }else{
144                         if (unlikely(tcp_send(dst, 0, buf, len)<0)){
145                                 STATS_TX_DROPS;
146                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
147                                 goto error;
148                         }
149                 }
150         }
151 #endif /* USE_TLS */
152 #endif /* USE_TCP */
153 #ifdef USE_SCTP
154         else if (dst->proto==PROTO_SCTP){
155                 if (unlikely(sctp_disable)){
156                         STATS_TX_DROPS;
157                         LOG(L_WARN, "msg_send: WARNING: attempt to send on sctp and sctp"
158                                         " support is disabled\n");
159                         goto error;
160                 }else{
161                         if (unlikely(sctp_msg_send(dst, buf, len)<0)){
162                                 STATS_TX_DROPS;
163                                 LOG(L_ERR, "msg_send: ERROR: sctp_msg_send failed\n");
164                                 goto error;
165                         }
166                 }
167         }
168 #endif /* USE_SCTP */
169         else{
170                         LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", dst->proto);
171                         goto error;
172         }
173         return 0;
174 error:
175         return -1;
176 }
177
178 #endif