core: Changed WS from being a flag on a TCP/TLS connection to a protocol in its own...
[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_server.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
109 int is_check_self_func_list_set(void);
110
111
112 /* params:
113  * dst = struct dest_info containing:
114  *    send_sock = 0 if not known (e.g. for udp in some cases), non-0 otherwise;
115  *                if 0 or mcast a new send_sock will be automatically choosen
116  *    proto = TCP|UDP
117  *    to = destination (sockaddr_union)
118  *    id = only used on tcp, it will force sending on connection "id" if id!=0 
119  *         and the connection exists, else it will send to "to" 
120  *        (useful for sending replies on  the same connection as the request
121  *         that generated them; use 0 if you don't want this)
122  * buf, len = buffer
123  * returns: 0 if ok, -1 on error*/
124
125 static inline int msg_send(struct dest_info* dst, char* buf, int len)
126 {
127         struct dest_info new_dst;
128         str outb;
129 #ifdef USE_TCP 
130         int port;
131         struct ip_addr ip;
132         union sockaddr_union* from = NULL;
133         union sockaddr_union local_addr;
134         struct tcp_connection *con = NULL;
135         struct ws_event_info wsev;
136 #endif
137         
138         outb.s = buf;
139         outb.len = len;
140         sr_event_exec(SREV_NET_DATA_OUT, (void*)&outb);
141
142 #ifdef USE_TCP
143         if (unlikely((dst->proto == PROTO_WS
144 #ifdef USE_TLS
145                 || dst->proto == PROTO_WSS
146 #endif
147         ) && sr_event_enabled(SREV_TCP_WS_FRAME_OUT))) {
148                 if (unlikely(dst->send_flags.f & SND_F_FORCE_SOCKET
149                                 && dst->send_sock)) {
150                         local_addr = dst->send_sock->su;
151                         su_setport(&local_addr, 0); /* any local port will do */
152                         from = &local_addr;
153                 }
154
155                 port = su_getport(&dst->to);
156                 if (likely(port)) {
157                         su2ip_addr(&ip, &dst->to);
158                         con = tcpconn_get(dst->id, &ip, port, from, 0);
159                 }
160                 else if (likely(dst->id))
161                         con = tcpconn_get(dst->id, 0, 0, 0, 0);
162                 else {
163                         LM_CRIT("BUG: msg_send called with null_id & to\n");
164                         return -1;
165                 }
166
167                 memset(&wsev, 0, sizeof(ws_event_info_t));
168                 wsev.type = SREV_TCP_WS_FRAME_OUT;
169                 wsev.buf = outb.s;
170                 wsev.len = outb.len;
171                 wsev.id = con->id;
172                 return sr_event_exec(SREV_TCP_WS_FRAME_OUT, (void *) &wsev);
173         }
174 #endif
175
176         if (likely(dst->proto==PROTO_UDP)){
177                 if (unlikely((dst->send_sock==0) || 
178                                         (dst->send_sock->flags & SI_IS_MCAST))){
179                         new_dst=*dst;
180                         new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
181                         if (unlikely(new_dst.send_sock==0)){
182                                 LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
183                                 goto error;
184                         }
185                         dst=&new_dst;
186                 }
187                 if (unlikely(udp_send(dst, outb.s, outb.len)==-1)){
188                         STATS_TX_DROPS;
189                         LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
190                         goto error;
191                 }
192         }
193 #ifdef USE_TCP
194         else if (dst->proto==PROTO_TCP){
195                 if (unlikely(tcp_disable)){
196                         STATS_TX_DROPS;
197                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tcp and tcp"
198                                         " support is disabled\n");
199                         goto error;
200                 }else{
201                         if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
202                                                 dst->send_sock)) {
203                                 local_addr=dst->send_sock->su;
204                                 su_setport(&local_addr, 0); /* any local port will do */
205                                 from=&local_addr;
206                         }
207                         if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
208                                 STATS_TX_DROPS;
209                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
210                                 goto error;
211                         }
212                 }
213         }
214 #ifdef USE_TLS
215         else if (dst->proto==PROTO_TLS){
216                 if (unlikely(tls_disable)){
217                         STATS_TX_DROPS;
218                         LOG(L_WARN, "msg_send: WARNING: attempt to send on tls and tls"
219                                         " support is disabled\n");
220                         goto error;
221                 }else{
222                         if (unlikely((dst->send_flags.f & SND_F_FORCE_SOCKET) &&
223                                                 dst->send_sock)) {
224                                 local_addr=dst->send_sock->su;
225                                 su_setport(&local_addr, 0); /* any local port will do */
226                                 from=&local_addr;
227                         }
228                         if (unlikely(tcp_send(dst, from, outb.s, outb.len)<0)){
229                                 STATS_TX_DROPS;
230                                 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
231                                 goto error;
232                         }
233                 }
234         }
235 #endif /* USE_TLS */
236 #endif /* USE_TCP */
237 #ifdef USE_SCTP
238         else if (dst->proto==PROTO_SCTP){
239                 if (unlikely(sctp_disable)){
240                         STATS_TX_DROPS;
241                         LOG(L_WARN, "msg_send: WARNING: attempt to send on sctp and sctp"
242                                         " support is disabled\n");
243                         goto error;
244                 }else{
245                         if (unlikely(dst->send_sock==0)){
246                                 new_dst=*dst;
247                                 new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
248                                 if (unlikely(new_dst.send_sock==0)){
249                                         LOG(L_ERR, "msg_send: ERROR: no sending SCTP socket found\n");
250                                         goto error;
251                                 }
252                                 dst=&new_dst;
253                         }
254                         if (unlikely(sctp_msg_send(dst, outb.s, outb.len)<0)){
255                                 STATS_TX_DROPS;
256                                 LOG(L_ERR, "msg_send: ERROR: sctp_msg_send failed\n");
257                                 goto error;
258                         }
259                 }
260         }
261 #endif /* USE_SCTP */
262         else{
263                         LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", dst->proto);
264                         goto error;
265         }
266         if(outb.s != buf)
267                 pkg_free(outb.s);
268         return 0;
269 error:
270         if(outb.s != buf)
271                 pkg_free(outb.s);
272         return -1;
273 }
274
275 #endif