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