4680a8e2b6e189005ed6380721ffc4f8cc66a064
[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 #include "tcp_conn.h"
58 #endif
59 #ifdef USE_SCTP
60 #include "sctp_core.h"
61 #endif
62
63 #include "compiler_opt.h"
64 #include "events.h"
65
66
67 enum ss_mismatch {
68         SS_MISMATCH_OK=0,
69         SS_MISMATCH_PROTO, /* proto mismatch, but found same addr:port */
70         SS_MISMATCH_ADDR,  /* proto and addr:port mismatch */
71         SS_MISMATCH_AF,    /* af mismatch */
72         SS_MISMATCH_MCAST  /* mcast forced send socket */
73 };
74
75 struct socket_info* get_send_socket2(struct socket_info* force_send_socket,
76                                                                         union sockaddr_union* su, int proto,
77                                                                         enum ss_mismatch* mismatch);
78
79
80 inline static struct socket_info* get_send_socket(struct sip_msg* msg,
81                                                                         union sockaddr_union* su, int proto)
82 {
83         return get_send_socket2(msg?msg->force_send_socket:0, su, proto, 0);
84 }
85
86
87 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
88 typedef int (*check_self_f)(str* host, unsigned short port,
89                 unsigned short proto);
90 int register_check_self_func(check_self_f f);
91 int check_self(str* host, unsigned short port, unsigned short proto);
92 int check_self_port(unsigned short port, unsigned short proto);
93 int forward_request( struct sip_msg* msg, str* dst,  unsigned short port,
94                                                 struct dest_info* send_info);
95 int update_sock_struct_from_via( union sockaddr_union* to,
96                                                                  struct sip_msg* msg,
97                                                                  struct via_body* via );
98
99 /* use src_ip, port=src_port if rport, via port if via port, 5060 otherwise */
100 #define update_sock_struct_from_ip(  to, msg ) \
101         init_su((to), &(msg)->rcv.src_ip, \
102                         (((msg)->via1->rport)|| \
103                          (((msg)->msg_flags|global_req_flags)&FL_FORCE_RPORT))? \
104                                                         (msg)->rcv.src_port: \
105                                                         ((msg)->via1->port)?(msg)->via1->port: SIP_PORT )
106
107 int forward_reply( struct sip_msg* msg);
108 int forward_reply_nocb( struct sip_msg* msg);
109
110 int is_check_self_func_list_set(void);
111
112
113 /* params:
114  * dst = struct dest_info containing:
115  *    send_sock = 0 if not known (e.g. for udp in some cases), non-0 otherwise;
116  *                if 0 or mcast a new send_sock will be automatically choosen
117  *    proto = TCP|UDP
118  *    to = destination (sockaddr_union)
119  *    id = only used on tcp, it will force sending on connection "id" if id!=0 
120  *         and the connection exists, else it will send to "to" 
121  *        (useful for sending replies on  the same connection as the request
122  *         that generated them; use 0 if you don't want this)
123  * buf, len = buffer
124  * returns: 0 if ok, -1 on error*/
125
126 static inline int msg_send(struct dest_info* dst, char* buf, int len)
127 {
128         struct dest_info new_dst;
129         str outb;
130 #ifdef USE_TCP 
131         int port;
132         struct ip_addr ip;
133         union sockaddr_union* from = NULL;
134         union sockaddr_union local_addr;
135         struct tcp_connection *con = NULL;
136         struct ws_event_info wsev;
137         int ret;
138 #endif
139         
140         outb.s = buf;
141         outb.len = len;
142         sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
143
144 #ifdef USE_TCP
145         if (unlikely((dst->proto == PROTO_WS
146 #ifdef USE_TLS
147                 || dst->proto == PROTO_WSS
148 #endif
149         ) && sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
150                 if (unlikely(dst->send_flags.f & SND_F_FORCE_SOCKET
151                                 && dst->send_sock)) {
152                         local_addr = dst->send_sock->su;
153                         su_setport(&local_addr, 0); /* any local port will do */
154                         from = &local_addr;
155                 }
156
157                 port = su_getport(&dst->to);
158                 if (likely(port)) {
159                         su2ip_addr(&ip, &dst->to);
160                         con = tcpconn_get(dst->id, &ip, port, from, 0);
161                 }
162                 else if (likely(dst->id))
163                         con = tcpconn_get(dst->id, 0, 0, 0, 0);
164                 else {
165                         LM_CRIT("BUG: msg_send called with null_id & to\n");
166                         goto error;
167                 }
168
169                 if (con == NULL)
170                 {
171                         LM_WARN("TCP/TLS connection for WebSocket could not be found\n");
172                         goto error;
173                 }
174
175                 memset(&wsev, 0, sizeof(ws_event_info_t));
176                 wsev.type = SREV_TCP_WS_FRAME_OUT;
177                 wsev.buf = outb.s;
178                 wsev.len = outb.len;
179                 wsev.id = con->id;
180                 ret = sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
181                 tcpconn_put(con);
182                 return ret;
183         }
184 #endif
185
186         if (likely(dst->proto==PROTO_UDP)){
187                 if (unlikely((dst->send_sock==0) || 
188                                         (dst->send_sock->flags & SI_IS_MCAST))){
189                         new_dst=*dst;
190                         new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
191                         if (unlikely(new_dst.send_sock==0)){
192                                 LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
193                                 goto error;
194                         }
195                         dst=&new_dst;
196                 }
197                 if (unlikely(udp_send(dst, outb.s, outb.len)==-1)){
198                         STATS_TX_DROPS;
199                         LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
200                         goto error;
201                 }
202         }
203 #ifdef USE_TCP
204         else if (dst->proto==PROTO_TCP){
205                 if (unlikely(tcp_disable)){
206                         STATS_TX_DROPS;
207                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tcp and tcp"
208                                         " support is disabled\n");
209                         goto error;
210                 }else{
211                         if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
212                                                 dst->send_sock)) {
213                                 local_addr=dst->send_sock->su;
214                                 su_setport(&local_addr, 0); /* any local port will do */
215                                 from=&local_addr;
216                         }
217                         if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
218                                 STATS_TX_DROPS;
219                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
220                                 goto error;
221                         }
222                 }
223         }
224 #ifdef USE_TLS
225         else if (dst->proto==PROTO_TLS){
226                 if (unlikely(tls_disable)){
227                         STATS_TX_DROPS;
228                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tls and tls"
229                                         " support is disabled\n");
230                         goto error;
231                 }else{
232                         if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
233                                                 dst->send_sock)) {
234                                 local_addr=dst->send_sock->su;
235                                 su_setport(&local_addr, 0); /* any local port will do */
236                                 from=&local_addr;
237                         }
238                         if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
239                                 STATS_TX_DROPS;
240                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
241                                 goto error;
242                         }
243                 }
244         }
245 #endif /* USE_TLS */
246 #endif /* USE_TCP */
247 #ifdef USE_SCTP
248         else if (dst->proto==PROTO_SCTP){
249                 if (unlikely(sctp_disable)){
250                         STATS_TX_DROPS;
251                         LOG(L_WARN, "msg_send: WARNING: attempt to send on sctp and sctp"
252                                         " support is disabled\n");
253                         goto error;
254                 }else{
255                         if (unlikely(dst->send_sock==0)){
256                                 new_dst=*dst;
257                                 new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
258                                 if (unlikely(new_dst.send_sock==0)){
259                                         LOG(L_ERR, "msg_send: ERROR: no sending SCTP socket found\n");
260                                         goto error;
261                                 }
262                                 dst=&new_dst;
263                         }
264                         if (unlikely(sctp_core_msg_send(dst, outb.s, outb.len)<0)){
265                                 STATS_TX_DROPS;
266                                 LOG(L_ERR, "msg_send: ERROR: sctp_msg_send failed\n");
267                                 goto error;
268                         }
269                 }
270         }
271 #endif /* USE_SCTP */
272         else{
273                         LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", dst->proto);
274                         goto error;
275         }
276         if(outb.s != buf)
277                 pkg_free(outb.s);
278         return 0;
279 error:
280         if(outb.s != buf)
281                 pkg_free(outb.s);
282         return -1;
283 }
284
285 #endif