cfg_db: moved from modules_s/ to modules/
[sip-router] / modules / iptrtpproxy / examples / ser.cfg
1 # $Id$
2 #
3 # Simple application level gateway config script.
4 #
5 #
6
7 # ------------------ module loading ----------------------------------
8
9 loadmodule "./modules/mysql/mysql.so"
10
11 loadmodule "./modules/iptrtpproxy/iptrtpproxy.so"
12 loadmodule "./modules/maxfwd/maxfwd.so"
13 loadmodule "./modules/rr/rr.so"
14 loadmodule "./modules/tm/tm.so"
15 loadmodule "./modules/sl/sl.so"
16 loadmodule "./modules/avp_db/avp_db.so"
17 loadmodule "./modules/usrloc/usrloc.so"
18 loadmodule "./modules/nathelper/nathelper.so"
19 loadmodule "./modules/registrar/registrar.so"
20
21 flags
22         FLAG_NAT_REG,
23         FLAG_NAT;
24
25
26 avpflags
27         rtpproxy_dlg,
28         dialog_cookie;
29
30 # ----------------- setting module-specific parameters ---------------
31
32 modparam("avp_db|usrloc", "db_url", "mysql://ser:heslo@127.0.0.1/ser");
33
34 modparam("usrloc", "db_mode", 1)
35
36 modparam("rr", "cookie_secret", "MyRRAVPcookiesecret");
37
38 modparam("nathelper", "rtpproxy_disable", 1);
39
40 modparam("iptrtpproxy", "config", "/etc/iptrtpproxy.cfg");
41 modparam("iptrtpproxy", "switchboard", "name=*;learning-timeout-a=10;learning-timeout-b=10;ringing-timeout=90");
42
43 modparam("registrar", "save_nat_flag", "FLAG_NAT_REG");
44 modparam("registrar", "load_nat_flag", "FLAG_NAT_REG");
45
46 modparam("avp_db", "attr_group", "id=rtp_dlg,flag=rtpproxy_dlg,table=rtpproxy_attrs");
47
48 # ------------------ main fun below ----------------------------------
49
50 route {
51
52         if (!mf_process_maxfwd_header("10")) {
53                 sl_send_reply("483", "Too Many Hops");
54                 break;
55         };
56         if (msg:len > max_len) {
57                 sl_send_reply("513", "Message too big");
58                 break;
59         };
60 #       if (method=="INVITE") {
61 #               if (!(uri == myself)) {
62 #                       sl_send_reply("403", "Call cannot be served here");
63 #                       break;
64 #               }
65 #       };
66
67         if (!t_newtran()) {
68                 sl_send_reply("500", "Internal error newtran");
69                 break;
70         };
71         # Do strict routing if pre-loaded route headers present
72         if (loose_route()) {
73                 if ($dialog_id) {  # AVP loaded from cookie
74                         if (method == "INVITE" || method == "UPDATE") {   # TODO: check if there is a SDP
75                                 # re-INVITEs
76                                 if (@route[0].ftag == @from.tag) {
77                                         $gate_a_to_b = 1;
78                                         if ($init_dialog)
79                                                 $tmp = 3;
80                                         else
81                                                 $tmp = 1;
82                                 }
83                                 else {
84                                         $gate_a_to_b = 0;
85                                         if ($init_dialog)
86                                                 $tmp = 2;
87                                         else
88                                                 $tmp = 0;
89                                 }
90                                 resetflag("FLAG_NAT");
91                                 # test if caller is behind NAT 
92                                 if (nat_uac_test("18")) {
93                                         setflag("FLAG_NAT");
94                                 }
95                                 # is callee behind NAT? we know it from registration
96                                 if (lookup("location")) {
97                                         if (isflagset("FLAG_NAT_REG"))
98                                                 setflag("FLAG_NAT");
99                                 }
100                                 else {
101                                         sl_reply("404", "Unknown user in To");
102                                         break;
103                                 }
104
105                                 if ($sess_ids) {  # pending non-confirmed sessions?
106                                         iptrtpproxy_delete("$sess_ids");
107                                         $sess_ids = "";
108                                 }
109                                 if (isflagset("FLAG_NAT")) {                                    
110                                         if (!iptrtpproxy_alloc("$tmp", "my")) {
111                                                 sl_reply("500", "RTP proxy error");
112                                                 break;
113                                         }
114                                         $sess_ids = @iptrtpproxy.session_ids;
115                                         t_on_reply("invite_response");
116                                         t_on_failure("invite_failure");
117                                 }
118                         }
119                         else if (method == "BYE") {   # CANCEL ?
120                                 t_on_reply("bye_response");
121                         }
122                 }
123                 t_relay();
124                 break;
125         };
126
127         if (method=="REGISTER") {
128
129                 if (nat_uac_test("18")) {
130                         setflag("FLAG_NAT_REG");
131                 }
132
133                 if (!save_contacts("location")) {
134                         sl_reply("400", "Invalid REGISTER Request");
135                         break;
136                 }
137         
138                 break;
139         }
140
141         if (method=="INVITE" && @to.tag=="") {
142                 $dialog_id = @sys.unique;
143                 $init_dialog = 1;
144
145                 # test if caller is behind NAT 
146                 if (nat_uac_test("18")) {
147                         setflag("FLAG_NAT");
148                 }
149                 
150                 # is callee behind NAT? we know it from registration
151                 if (lookup("location")) {
152                         if (isflagset("FLAG_NAT_REG"))
153                                 setflag("FLAG_NAT");
154                 }
155                 else {
156                         sl_reply("404", "Unknown user in To");
157                         break;
158                 }
159                 # test if call comming at gate a or b by ip, port etc.
160                 $gate_a_to_b = 1;  # TODO: we do not consider two gates now, a->b & ringing-timeout
161
162                 if (isflagset("FLAG_NAT")) {
163                         if (iptrtpproxy_alloc("3", "my")) {
164                                 $sess_ids = @iptrtpproxy.session_ids;
165                                 t_on_reply("invite_response");
166                                 t_on_failure("invite_failure");
167                         }
168                 }
169
170                 setavpflag("$f.dialog_id", "dialog_cookie");
171                 record_route();  # it will add ftag to record-route, we can check direction 
172         }
173
174         if (!t_relay()) {
175                 sl_reply_error();
176         }
177
178 }
179
180 onreply_route["invite_response"] {
181         if (status=~"18[0-9]" || status=~"2[0-9][0-9]") {
182                 if ($gate_a_to_b == "1") {
183                         if ($init_dialog && status=~"18[0-9]")
184                                 $tmp = 2;  # ringing-timeout
185                         else
186                                 $tmp = 0;
187                 }
188                 else {
189                         if ($init_dialog && status=~"18[0-9]")
190                                 $tmp = 3;  # ringing-timeout
191                         else
192                                 $tmp = 1;
193                 }
194                 if (iptrtpproxy_update("$tmp", "$sess_ids")) {
195                         if (status=~"2[0-9][0-9]") {
196                                 if ($init_dialog) {
197                                         iptrtpproxy_adjust_timeout("$gate_a_to_b", "$sess_ids");  # decrease ringing-timeout
198                                 }
199                                 # delete old session, e.g. in case of re-INVITE
200                                 load_extra_attrs("rtp_dlg", "$dialog_id");
201                                 iptrtpproxy_delete("$dlg_sess_ids");
202                                 # save session ids
203                                 $dlg_sess_ids = @iptrtpproxy.session_ids;
204                                 setavpflag("$f.dlg_sess_ids", "rtpproxy_dlg");
205                                 save_extra_attrs("rtp_dlg", "$dialog_id");
206                         }
207                 }
208         }
209 }
210
211 failure_route["invite_failure"] {
212         if ($sess_ids) {
213                 iptrtpproxy_delete("$sess_ids");
214                 $sess_ids = ""; 
215         }
216 }
217
218 onreply_route["bye_response"] {
219         if (status=~"2[0-9][0-9]") {
220                 load_extra_attrs("rtp_dlg", "$dialog_id");
221                 iptrtpproxy_delete("$dlg_sess_ids");
222                 remove_extra_attrs("rtp_dlg", "$dialog_id");
223         }
224 }
225