1 /*
2  * $Id: SipCtrlInterface.h 1048 2008-07-15 18:48:07Z sayer $
3  *
4  * Copyright (C) 2007 Raphael Coeffic
5  *
6  * This file is part of SEMS, a free SIP media server.
7  *
8  * SEMS 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. This program is released under
12  * the GPL with the additional exemption that compiling, linking,
13  * and/or using OpenSSL is allowed.
14  *
15  * For a license to use the SEMS software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * SEMS is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29 #ifndef _SipCtrlInterface_h_
30 #define _SipCtrlInterface_h_
31 
32 #include "sip/sip_ua.h"
33 #include "AmThread.h"
34 
35 #include <string>
36 #include <list>
37 using std::string;
38 using std::list;
39 
40 class AmSipRequest;
41 class AmSipReply;
42 
43 struct sip_msg;
44 struct sip_header;
45 class trans_ticket;
46 
47 
48 class udp_trsp_socket;
49 class udp_trsp;
50 
51 class tcp_server_socket;
52 class tcp_trsp;
53 
54 class _SipCtrlInterface:
55     public sip_ua
56 {
57     bool sip_msg2am_request(const sip_msg *msg, const trans_ticket& tt, AmSipRequest &request);
58     bool sip_msg2am_reply(sip_msg *msg, AmSipReply &reply);
59 
60     void prepare_routes_uac(const list<sip_header*>& routes, string& route_field);
61     void prepare_routes_uas(const list<sip_header*>& routes, string& route_field);
62 
63     friend class udp_trsp;
64 
65     AmCondition<bool> stopped;
66 
67     unsigned short    nr_udp_sockets;
68     udp_trsp_socket** udp_sockets;
69 
70     unsigned short    nr_udp_servers;
71     udp_trsp**        udp_servers;
72 
73     unsigned short    nr_tcp_sockets;
74     tcp_server_socket** tcp_sockets;
75 
76     unsigned short    nr_tcp_servers;
77     tcp_trsp**        tcp_servers;
78 
79     int alloc_udp_structs();
80     int init_udp_servers(int if_num);
81 
82     int alloc_tcp_structs();
83     int init_tcp_servers(int if_num);
84 
85 public:
86 
87     static string outbound_host;
88     static unsigned int outbound_port;
89     static bool log_parsed_messages;
90     static int udp_rcvbuf;
91 
92     _SipCtrlInterface();
~_SipCtrlInterface()93     ~_SipCtrlInterface(){}
94 
95     int load();
96 
97     int run();
98     void stop();
99     void cleanup();
100 
101     /**
102      * Sends a SIP request.
103      *
104      * @param req The request to send. If the request creates a transaction,
105      *            its ticket is written into req.tt.
106      */
107     static int send(AmSipRequest &req, const string& dialog_id,
108 		    const string& next_hop = "", int outbound_interface = -1,
109 		    unsigned int flags = 0, msg_logger* logger = NULL);
110 
111     /**
112      * Sends a SIP reply.
113      *
114      * @param rep The reply to be sent. 'rep.tt' should be set to transaction
115      *            ticket included in the SIP request.
116      */
117     static int send(const AmSipReply &rep, const string& dialog_id,
118 		    msg_logger* logger = NULL);
119 
120     /**
121      * CANCELs an INVITE transaction.
122      *
123      * @param tt transaction ticket of the request to cancel.
124      */
125     static int cancel(trans_ticket* tt, const string& dialog_id,
126 		      unsigned int inv_cseq, const string& hdrs);
127 
128     /**
129      * From sip_ua
130      */
131     void handle_sip_request(const trans_ticket& tt, sip_msg* msg);
132     void handle_sip_reply(const string& dialog_id, sip_msg* msg);
133     void handle_reply_timeout(AmSipTimeoutEvent::EvType evt,
134         sip_trans *tr, trans_bucket *buk=0);
135 };
136 
137 typedef singleton<_SipCtrlInterface> SipCtrlInterface;
138 
139 #endif
140 
141 /** EMACS **
142  * Local variables:
143  * mode: c++
144  * c-basic-offset: 4
145  * End:
146  */
147