4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of ser, a free SIP server.
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
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:
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.
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
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)
48 #include "parser/msg_parser.h"
54 #include "udp_server.h"
56 #include "tcp_server.h"
59 #include "compiler_opt.h"
63 struct socket_info* get_send_socket(struct sip_msg* msg,
64 union sockaddr_union* su, int proto);
65 struct socket_info* get_out_socket(union sockaddr_union* to, int proto);
66 int check_self(str* host, unsigned short port, unsigned short proto);
67 int check_self_port(unsigned short port, unsigned short proto);
68 int forward_request( struct sip_msg* msg, str* dst, unsigned short port,
69 struct dest_info* send_info);
70 int update_sock_struct_from_via( union sockaddr_union* to,
72 struct via_body* via );
74 /* use src_ip, port=src_port if rport, via port if via port, 5060 otherwise */
75 #define update_sock_struct_from_ip( to, msg ) \
76 init_su((to), &(msg)->rcv.src_ip, \
77 (((msg)->via1->rport)||((msg)->msg_flags&FL_FORCE_RPORT))? \
78 (msg)->rcv.src_port: \
79 ((msg)->via1->port)?(msg)->via1->port: SIP_PORT )
81 int forward_reply( struct sip_msg* msg);
86 * dst = struct dest_info containing:
87 * send_sock = 0 if not known (e.g. for udp in some cases), non-0 otherwise;
88 * if 0 or mcast a new send_sock will be automatically choosen
90 * to = destination (sockaddr_union)
91 * id = only used on tcp, it will force sending on connection "id" if id!=0
92 * and the connection exists, else it will send to "to"
93 * (useful for sending replies on the same connection as the request
94 * that generated them; use 0 if you don't want this)
96 * returns: 0 if ok, -1 on error*/
97 static inline int msg_send(struct dest_info* dst, char* buf, int len)
99 struct dest_info new_dst;
101 if (likely(dst->proto==PROTO_UDP)){
102 if (unlikely((dst->send_sock==0) ||
103 (dst->send_sock->flags & SI_IS_MCAST))){
105 new_dst.send_sock=get_send_socket(0, &dst->to, dst->proto);
106 if (unlikely(new_dst.send_sock==0)){
107 LOG(L_ERR, "msg_send: ERROR: no sending socket found\n");
112 if (unlikely(udp_send(dst, buf, len)==-1)){
114 LOG(L_ERR, "msg_send: ERROR: udp_send failed\n");
119 else if (dst->proto==PROTO_TCP){
120 if (unlikely(tcp_disable)){
122 LOG(L_WARN, "msg_send: WARNING: attempt to send on tcp and tcp"
123 " support is disabled\n");
126 if (unlikely(tcp_send(dst, 0, buf, len)<0)){
128 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
134 else if (dst->proto==PROTO_TLS){
135 if (unlikely(tls_disable)){
137 LOG(L_WARN, "msg_send: WARNING: attempt to send on tls and tls"
138 " support is disabled\n");
141 if (unlikely(tcp_send(dst, 0, buf, len)<0)){
143 LOG(L_ERR, "msg_send: ERROR: tcp_send failed\n");
151 LOG(L_CRIT, "BUG: msg_send: unknown proto %d\n", dst->proto);