test: switch from deprecated /var/run to /run
[sip-router] / test / unit / 7.cfg
1 ########################################################################
2 # This configuration is autogenerated by sip:wizard
3 # (http://www.sipwise.com/wizard) on Fri Nov 09 17:06:52 +0100 2007
4 # for Kamailio 1.2
5 #
6 # Copyright (C) 2007 Sipwise (support@sipwise.com)
7 ########################################################################
8
9 ########################################################################
10 # By obtaining, using, and/or copying this configuration and/or its
11 # associated documentation, you agree that you have read, understood,
12 # and will comply with the Terms of Usage provided at
13 # http://www.sipwise.com/news/?page_id=6 as well as the following
14 # additions:
15 #
16 # Permission to use, copy, modify, and distribute this configuration and
17 # its associated documentation for any purpose and without fee is hereby
18 # granted, provided that the above copyright notice appears in all
19 # copies, and that both that copyright notice and this permission notice
20 # appear in supporting documentation, and that the name of Sipwise or
21 # the author will not be used in advertising or publicity pertaining to
22 # distribution of the configuration without specific, written prior
23 # permission.
24 ########################################################################
25
26 ########################################################################
27 # Before using this configuration, read the following prerequisites in
28 # order to gain the designated functionallity:
29 #
30 # base:
31 #    You have to insert all locally served domains (i.e. 
32 #    "kamailioctl domain add your.domain.com").
33 #    
34 # usr-preferences:
35 #    This feature relies on UUID-based provisioning. Thus, you have 
36 #    to add the uuid-column to the subscriber table ("alter table 
37 #    subscriber add column uuid varchar(64);") and populate it with a 
38 #    UUID (unique user identifier) per user, for example an 
39 #    auto-incremented id.
40 #    
41 # offnet-incoming-sip:
42 #    You have to populate the "trusted"-table with rules for 
43 #    allowed peering hosts (i.e. src_ip="1.2.3.4", proto="udp", 
44 #    from_pattern="^sip:.*@domain.of.peering.host$", tag="1234"). If the 
45 #    feature "usr-preferences" is selected, the tag-value is used as 
46 #    caller-uuid for such calls.
47 #    
48 # offnet-pstn:
49 #    You have to add a routing entry for lcr (i.e. "kamailioctl  lcr 
50 #    addroute '' '' 1 1"). Additionally, you have to add your gateways 
51 #    (i.e. "kamailioctl lcr addgw my-test-gw 1.2.3.4 5060 sip udp 1").
52 #    
53 # ring-timeout:
54 #    You have to provision the ring-timeout (AVP ringtimeout as 
55 #    type 1) for each user in the usr_preferences table (i.e. 
56 #    uuid='1234', username='', domain='', attribute='ringtimeout', 
57 #    type=1, value='60'). If no timeout is provisioned, the default 
58 #    timeout will be used.
59 #    
60 # cfu:
61 #    You have to provision the call-forward-unconditional as full 
62 #    SIP URI (AVP cfu as type 0) for each user in the usr_preferences 
63 #    table (i.e. uuid='1234', username='', domain='', attribute='cfu', 
64 #    type=0, value='sip:foo@otherdomain.com').  Forwards to another user 
65 #    in the same domain or to other domains are possible.
66 #    
67 # cfc:
68 #    You have to provision the call-forward-conditional as full SIP 
69 #    URI (AVP cfc as type 0) for each user in the usr_preferences table 
70 #    (i.e. uuid='1234', username='', domain='', attribute='cfc', type=0, 
71 #    value='sip:foo@otherdomain.com').  Forwards to another user in the 
72 #    same domain or to other domains are possible.
73 #    
74 # user-aliases:
75 #    You have to add aliases for your users (i.e. "kamailioctl alias 
76 #    add 01234567 sip:bob@yourdomain.com" for usrloc-based aliases or 
77 #    make entries into "dbaliases" table for db-based aliases)
78 #    
79 # cli:
80 #    You have to provision the CLI as full SIP URI (AVP cli as type 
81 #    0) for each user in the usr_preferences table (i.e. uuid='1234', 
82 #    username='', domain='', attribute='cli', type=0, 
83 #    value='sip:01234567@yourdomain.com').
84 #    
85 # clir:
86 #    You have to provision '1' to enable CLIR and '0' to disable it 
87 #    (AVP clir as type 1) for each user in the usr_preferences table 
88 #    (i.e. uuid='1234', username='', domain='', attribute='clir', 
89 #    type=1, value='1').
90 #    
91 # acc-db:
92 #    You have to add the columns "src_leg" and "dst_leg" to your 
93 #    acc-table ("alter table acc add column src_leg varchar(128); alter 
94 #    table acc add column dst_leg varchar(128);").
95 #    
96 ########################################################################
97
98 ########################################################################
99 # Configuration 'sip:wizard - Fri Nov 09 17:06:52 +0100 2007'
100 ########################################################################
101
102 listen = udp:127.0.0.1:5060
103 children = 8
104 debug = 2
105 fork = yes
106 disable_tcp = no
107 log_facility = LOG_DAEMON
108 log_stderror = no
109 tcp_children = 4
110 mhomed = no
111 server_signature = yes
112 reply_to_via = no
113 sip_warning = no
114 check_via = no
115 dns = no
116 rev_dns = no
117 disable_core_dump = no
118 dns_try_ipv6 = yes
119 dns_use_search_list = yes
120
121 loadmodule "pv/pv.so"
122 loadmodule "corex/corex.so"
123 loadmodule "kex/kex.so"
124
125 loadmodule "usrloc/usrloc.so"
126 modparam("usrloc", "user_column", "username")
127 modparam("usrloc", "domain_column", "domain")
128 modparam("usrloc", "contact_column", "contact")
129 modparam("usrloc", "expires_column", "expires")
130 modparam("usrloc", "q_column", "q")
131 modparam("usrloc", "callid_column", "callid")
132 modparam("usrloc", "cseq_column", "cseq")
133 modparam("usrloc", "methods_column", "methods")
134 modparam("usrloc", "flags_column", "flags")
135 modparam("usrloc", "user_agent_column", "user_agent")
136 modparam("usrloc", "received_column", "received")
137 modparam("usrloc", "socket_column", "socket")
138 modparam("usrloc", "use_domain", 0)
139 modparam("usrloc", "desc_time_order", 0)
140 modparam("usrloc", "timer_interval", 60)
141 modparam("usrloc", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
142 modparam("usrloc", "db_mode", 1)
143 modparam("usrloc", "matching_mode", 0)
144 modparam("usrloc", "cseq_delay", 20)
145 modparam("usrloc", "nat_bflag", 6)
146
147 loadmodule "maxfwd/maxfwd.so"
148 modparam("maxfwd", "max_limit", 256)
149
150 loadmodule "rr/rr.so"
151 modparam("rr", "enable_full_lr", 0)
152 modparam("rr", "append_fromtag", 1)
153 modparam("rr", "enable_double_rr", 1)
154 modparam("rr", "add_username", 0)
155
156 loadmodule "tm/tm.so"
157 modparam("tm", "fr_timer", 30)
158 modparam("tm", "fr_inv_timer", 120)
159 modparam("tm", "wt_timer", 5)
160 modparam("tm", "delete_timer", 2)
161 modparam("tm", "ruri_matching", 1)
162 modparam("tm", "via1_matching", 1)
163 modparam("tm", "unix_tx_timeout", 2)
164 modparam("tm", "restart_fr_on_each_reply", 1)
165 modparam("tm", "pass_provisional_replies", 0)
166 modparam("tm", "fr_inv_timer_avp", "$avp(s:callee_fr_inv_timer)")
167
168 loadmodule "xlog/xlog.so"
169 modparam("xlog", "buf_size", 4096)
170 modparam("xlog", "force_color", 0)
171
172 loadmodule "domain/domain.so"
173 modparam("domain", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
174 modparam("domain", "domain_table", "domain")
175 modparam("domain", "domain_col", "domain")
176
177 loadmodule "nathelper/nathelper.so"
178 modparam("nathelper", "natping_interval", 0)
179 modparam("nathelper", "ping_nated_only", 1)
180 modparam("nathelper", "sipping_method", "OPTIONS")
181 modparam("nathelper", "received_avp", "$avp(i:801)")
182
183 loadmodule "rtpproxy/rtpproxy.so"
184 modparam("rtpproxy", "rtpproxy_sock", "unix:/run/rtpproxy.sock")
185 modparam("rtpproxy", "rtpproxy_disable_tout", 60)
186 modparam("rtpproxy", "rtpproxy_tout", 1)
187 modparam("rtpproxy", "rtpproxy_retr", 5)
188
189 loadmodule "textops/textops.so"
190
191 loadmodule "siputils/siputils.so"
192
193 loadmodule "registrar/registrar.so"
194 modparam("registrar", "default_expires", 3600)
195 modparam("registrar", "min_expires", 60)
196 modparam("registrar", "max_expires", 0)
197 modparam("registrar", "default_q", 0)
198 modparam("registrar", "append_branches", 1)
199 modparam("registrar", "case_sensitive", 0)
200 modparam("registrar", "received_param", "received")
201 modparam("registrar", "max_contacts", 0)
202 modparam("registrar", "retry_after", 0)
203 modparam("registrar", "method_filtering", 0)
204 modparam("registrar", "path_mode", 2)
205 modparam("registrar", "path_use_received", 0)
206 modparam("registrar", "received_avp", "$avp(i:801)")
207
208 loadmodule "sl/sl.so"
209 modparam("sl", "bind_tm", 1)
210
211 loadmodule "db_mysql/db_mysql.so"
212 modparam("db_mysql", "ping_interval", 300)
213 modparam("db_mysql", "auto_reconnect", 1)
214
215 loadmodule "auth/auth.so"
216 modparam("auth", "nonce_expire", 300)
217
218 loadmodule "auth_db/auth_db.so"
219 modparam("auth_db", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
220 modparam("auth_db", "user_column", "username")
221 modparam("auth_db", "domain_column", "domain")
222 modparam("auth_db", "password_column", "password")
223 modparam("auth_db", "password_column_2", "ha1b")
224 modparam("auth_db", "calculate_ha1", 1)
225 modparam("auth_db", "use_domain", 0)
226 modparam("auth_db", "load_credentials", "$avp(s:caller_uuid)=uuid")
227
228 loadmodule "uri_db/uri_db.so"
229 modparam("uri_db", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
230 modparam("uri_db", "db_table", "subscriber")
231 modparam("uri_db", "use_uri_table", 0)
232 modparam("uri_db", "use_domain", 0)
233
234 loadmodule "avpops/avpops.so"
235 modparam("avpops", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
236 modparam("avpops", "avp_table", "usr_preferences")
237 modparam("avpops", "use_domain", 0)
238 modparam("avpops", "uuid_column", "uuid")
239 modparam("avpops", "username_column", "username")
240 modparam("avpops", "domain_column", "domain")
241 modparam("avpops", "attribute_column", "attribute")
242 modparam("avpops", "value_column", "value")
243 modparam("avpops", "type_column", "type")
244
245 loadmodule "enum/enum.so"
246 modparam("enum", "domain_suffix", "e164.org.")
247
248 loadmodule "permissions/permissions.so"
249 modparam("permissions", "default_allow_file", "permissions.allow")
250 modparam("permissions", "default_deny_file", "permissions.deny")
251 modparam("permissions", "check_all_branches", 1)
252 modparam("permissions", "allow_suffix", ".allow")
253 modparam("permissions", "deny_suffix", ".deny")
254 modparam("permissions", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
255 modparam("permissions", "db_mode", 1)
256 modparam("permissions", "trusted_table", "trusted")
257 modparam("permissions", "source_col", "src_ip")
258 modparam("permissions", "proto_col", "proto")
259 modparam("permissions", "from_col", "from_pattern")
260 modparam("permissions", "tag_col", "tag")
261 modparam("permissions", "peer_tag_avp", "$avp(s:peer_uuid)")
262
263 loadmodule "lcr/lcr.so"
264 modparam("lcr", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
265 modparam("lcr", "gw_uri_avp", "$avp(709)")
266 modparam("lcr", "ruri_user_avp", "$avp(500)")
267 modparam("lcr", "tag_avp", "$avp(lcr_tag)")
268
269 loadmodule "uac_redirect/uac_redirect.so"
270 modparam("uac_redirect", "default_filter", "accept")
271 modparam("uac_redirect", "acc_function", "acc_log_request")
272 modparam("uac_redirect", "acc_db_table", "acc")
273
274 loadmodule "alias_db/alias_db.so"
275 modparam("alias_db", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
276 modparam("alias_db", "user_column", "username")
277 modparam("alias_db", "domain_column", "domain")
278 modparam("alias_db", "alias_user_column", "alias_username")
279 modparam("alias_db", "alias_domain_column", "alias_domain")
280 modparam("alias_db", "use_domain", 0)
281
282 loadmodule "uac/uac.so"
283 modparam("uac", "rr_from_store_param", "vsf")
284 modparam("uac", "rr_to_store_param", "vsf")
285 modparam("uac", "restore_mode", "auto")
286 modparam("uac", "restore_passwd", "s1p:Wiz4rd!")
287
288 loadmodule "acc/acc.so"
289 modparam("acc", "early_media", 0)
290 modparam("acc", "failed_transaction_flag", 24)
291 modparam("acc", "report_ack", 0)
292 modparam("acc", "report_cancels", 0)
293 modparam("acc", "log_flag", 0)
294 modparam("acc", "log_missed_flag", 0)
295 modparam("acc", "log_level", 2)
296 modparam("acc", "db_flag", 25)
297 modparam("acc", "db_missed_flag", 0)
298 modparam("acc", "db_table_acc", "acc")
299 modparam("acc", "db_table_missed_calls", "missed_calls")
300 modparam("acc", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")
301 modparam("acc", "acc_method_column", "method")
302 modparam("acc", "acc_callid_column", "callid")
303 modparam("acc", "acc_time_column", "time")
304 modparam("acc", "acc_from_tag_column", "from_tag")
305 modparam("acc", "detect_direction", 1)
306 modparam("acc", "acc_sip_code_column", "sip_code")
307 modparam("acc", "acc_sip_reason_column", "sip_reason")
308 modparam("acc", "multi_leg_info", "src_leg=$avp(i:901);dst_leg=$avp(i:902)")
309
310 ########################################################################
311 # Request route 'main'
312 ########################################################################
313 request_route {
314         xlog("L_INFO", "New request - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
315         force_rport();
316         if (!mf_process_maxfwd_header("10"))
317         {
318                 
319                 xlog("L_INFO", "Too many hops - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
320                 sl_send_reply("483", "Too Many Hops");
321                 exit;
322         }
323         if(!is_method("REGISTER"))
324         {
325                 if(nat_uac_test("19"))
326                 {
327                         record_route(";nat=yes");
328                 }
329                 else
330                 {
331                         record_route();
332                 }
333         }
334         if(is_method("CANCEL") || is_method("BYE"))
335         {
336                 rtpproxy_destroy();
337         }
338         if(loose_route())
339         {
340                 if(!has_totag())
341                 {
342                         
343                         xlog("L_INFO", "Initial loose-routing rejected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
344                         sl_send_reply("403", "Initial Loose-Routing Rejected");
345                         exit;
346                 }
347                 if(nat_uac_test("19") || search("^Route:.*;nat=yes"))
348                 {
349                         fix_nated_contact();
350                         setbflag(6);
351                 }
352                 if(is_method("BYE"))
353                 {
354                         setflag(24); # account failed transactions
355                         setflag(25); # account successful transactions
356                 }
357                 # mark as loose-routed for acc
358                 setflag(26);
359                 
360                 route(12);
361         }
362         if(is_method("REGISTER"))
363         {
364                 route(11);
365         }
366         setflag(24); # account failed transactions
367         setflag(25); # account successful transactions
368         if(is_method("INVITE"))
369         {
370                 route(13);
371         }
372         if(is_method("CANCEL") || is_method("ACK"))
373         {
374                 route(19);
375         }
376         
377         route(20);
378 }
379
380 ########################################################################
381 # Request route 'clear-usr-preferences-caller'
382 ########################################################################
383 route[1]
384 {
385         xlog("L_INFO", "Clear caller preferences - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
386         avp_delete("$avp(s:caller_cli)/g");
387         avp_delete("$avp(s:clir)/g");
388         
389 }
390
391 ########################################################################
392 # Request route 'clear-usr-preferences-callee'
393 ########################################################################
394 route[2]
395 {
396         xlog("L_INFO", "Clear callee preferences - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
397         avp_delete("$avp(s:callee_fr_inv_timer)/g");
398         avp_delete("$avp(s:cfu)/g");
399         avp_delete("$avp(s:cfc)/g");
400         
401 }
402
403 ########################################################################
404 # Request route 'usr-preferences-caller'
405 ########################################################################
406 route[3]
407 {
408         route(1);
409         xlog("L_INFO", "Load caller preferences for uuid '$avp(s:caller_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
410         # load caller avps
411         avp_db_load("$avp(s:caller_uuid)", "*");
412         avp_copy("$avp(s:cli)", "$avp(s:caller_cli)/d");
413         if(is_avp_set("$avp(s:clir)/n") && avp_check("$avp(s:clir)", "eq/i:1"))
414         {
415                 # mark for anonymization
416                 setflag(28);
417         }
418         
419 }
420
421 ########################################################################
422 # Request route 'usr-preferences-callee'
423 ########################################################################
424 route[4]
425 {
426         xlog("L_INFO", "Load callee preferences for uuid '$avp(s:callee_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
427         # load callee avps
428         avp_db_load("$avp(s:callee_uuid)", "*");
429         if(is_avp_set("$avp(s:cfu)/s"))
430         {
431                 
432                 xlog("L_INFO", "Call-forward-unconditional to '$avp(s:cfu)' found - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
433                 route(8);
434                 avp_delete("$avp(s:caller_uuid)/g");
435                 avp_copy("$avp(s:callee_uuid)", "$avp(s:caller_uuid)/d");
436                 avp_pushto("$ru", "$avp(s:cfu)");
437                 
438                 route(3);
439                 route(14);
440                 exit;
441         }
442         if(is_avp_set("$avp(s:ringtimeout)/n"))
443         {
444                 
445                 xlog("L_INFO", "Setting ring timeout to $avp(s:ringtimeout) secs - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
446                 avp_copy("$avp(s:ringtimeout)", "$avp(s:callee_fr_inv_timer)/d");
447         }
448         
449 }
450
451 ########################################################################
452 # Request route 'acc-caller'
453 ########################################################################
454 route[5]
455 {
456         xlog("L_INFO", "Setting acc source-leg for uuid '$avp(s:caller_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
457         avp_printf("$avp(i:901)", "$avp(s:caller_uuid)|$avp(s:acc_caller_user)|$avp(s:acc_caller_domain)|$avp(s:acc_state)");
458         
459 }
460
461 ########################################################################
462 # Request route 'acc-callee'
463 ########################################################################
464 route[6]
465 {
466         xlog("L_INFO", "Setting acc destination-leg for uuid '$avp(s:callee_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
467         avp_printf("$avp(i:902)", "$avp(s:callee_uuid)|$avp(s:acc_callee_user)|$avp(s:acc_callee_domain)");
468         
469 }
470
471 ########################################################################
472 # Request route 'acc-failure'
473 ########################################################################
474 route[7]
475 {
476         xlog("L_INFO", "Accounting failed request for uuid '$avp(s:caller_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
477         route(5);
478         route(6);
479         resetflag(24);
480         acc_db_request("404", "acc");
481         
482 }
483
484 ########################################################################
485 # Request route 'cfu-acc'
486 ########################################################################
487 route[8]
488 {
489         $avp(s:acc_callee_user) = $rU;
490         $avp(s:acc_callee_domain) = $rd;
491         
492         route(5);
493         route(6);
494         avp_delete("$avp(s:acc_caller_user)");
495         avp_delete("$avp(s:acc_caller_domain)");
496         avp_copy("$avp(s:acc_callee_user)", "$avp(s:acc_caller_user)");
497         avp_copy("$avp(s:acc_callee_domain)", "$avp(s:acc_caller_domain)");
498         avp_delete("$avp(s:acc_state)/g");
499         $avp(s:acc_state) = "cfu";
500         
501 }
502
503 ########################################################################
504 # Request route 'clir'
505 ########################################################################
506 route[9]
507 {
508         if(isflagset(28) && !isflagset(27))
509         {
510                 setflag(27);
511                 
512                 xlog("L_INFO", "Anonymize caller - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
513                 uac_replace_from("Anonymous","sip:anonymous@anonymous.invalid");
514                 if(is_present_hf("Privacy"))
515                 {
516                         remove_hf("Privacy");
517                 }
518                 append_hf("Privacy: id\r\n");
519         }
520         
521 }
522
523 ########################################################################
524 # Request route 'stop-media-proxy'
525 ########################################################################
526 route[10]
527 {
528         if(isflagset(22))
529         {
530                 rtpproxy_destroy();
531         }
532         
533 }
534
535 ########################################################################
536 # Request route 'base-route-register'
537 ########################################################################
538 route[11]
539 {
540         sl_send_reply("100", "Trying");
541         if(!www_authorize("$td", "subscriber")) 
542         {
543                 
544                 xlog("L_INFO", "Register authentication failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
545                 www_challenge("$td", "0");
546                 exit;
547         }
548         if(!check_to()) 
549         {
550                 
551                 xlog("L_INFO", "Spoofed To-URI detected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
552                 sl_send_reply("403", "Spoofed To-URI Detected");
553                 exit;
554         }
555         consume_credentials();
556         if(!search("^Contact:[ ]*\*") && nat_uac_test("19")) 
557         {
558                 fix_nated_register();
559                 setbflag(6);
560         }
561         if(!save("location")) 
562         {
563                 
564                 xlog("L_ERR", "Saving contact failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
565                 sl_reply_error();
566                 exit;
567         }
568         
569         xlog("L_INFO", "Registration successful - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
570         exit;
571         
572 }
573
574 ########################################################################
575 # Request route 'base-outbound'
576 ########################################################################
577 route[12]
578 {
579         if(is_present_hf("P-Asserted-Identity"))
580         {
581                 remove_hf("P-Asserted-Identity");
582         }
583         if(is_present_hf("Remote-Party-ID"))
584         {
585                 remove_hf("Remote-Party-ID");
586         }
587         if(is_avp_set("$avp(s:caller_cli)/s"))
588         {
589                 if(!isflagset(28))
590                 {
591                         
592                         xlog("L_INFO", "Set caller CLI '$avp(s:caller_cli)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
593                         append_hf("P-Asserted-Identity: <$avp(s:caller_cli)>\r\n");
594                 }
595         }
596         
597         route(9);
598         if(isbflagset(6))
599         {
600                 if(!isflagset(22) && !search("^Content-Length:[ ]*0"))
601                 {
602                         setflag(22);
603                         rtpproxy_manage();
604                 }
605                 
606                 t_on_reply("2");
607         }
608         else
609         {
610                 
611                 t_on_reply("1");
612         }
613         if(!isflagset(21))
614         {
615                 
616                 t_on_failure("2");
617                 if(!isflagset(26))
618                 {
619                         $avp(s:acc_callee_user) = $rU;
620                         $avp(s:acc_callee_domain) = $rd;
621                         
622                         route(5);
623                         route(6);
624                 }
625         }
626         if(isflagset(29))
627         {
628                 append_branch();
629         }
630         if(is_present_hf("Proxy-Authorization"))
631         {
632                 consume_credentials();
633         }
634         
635         xlog("L_INFO", "Request leaving server, D-URI='$du' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
636         # no 100 (we already sent it) and no DNS blacklisting
637         if(!t_relay_to("0x05"))
638         {
639                 sl_reply_error();
640                 if(is_method("INVITE") && isbflagset(6))
641                 {
642                         rtpproxy_destroy();
643                 }
644         }
645         exit;
646         
647 }
648
649 ########################################################################
650 # Request route 'base-route-invite'
651 ########################################################################
652 route[13]
653 {
654         sl_send_reply("100", "Trying");
655         if(from_gw(1))
656         {
657                 $avp(s:caller_uuid) = "0";
658                 
659                 xlog("L_INFO", "Call from PSTN' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
660                 setflag(23);
661         }
662         else
663         {
664                 if(allow_trusted())
665                 {
666                         if(is_avp_set("$avp(s:peer_uuid)/s"))
667                         {
668                                 # use tag-column from trusted-table as uuid for this caller
669                                 avp_copy("$avp(s:peer_uuid)", "$avp(s:caller_uuid)/d");
670                         }
671                         else
672                         {
673                                 # if no uuid is set, use "0" as default uuid
674                                 $avp(s:caller_uuid) = "0";
675                         }
676                         
677                         xlog("L_INFO", "Call from trusted peer with uuid '$avp(s:caller_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
678                         if(!is_domain_local("$rd"))
679                         {
680                                 
681                                 xlog("L_INFO", "Rejecting peering attempt with non-local request domain - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
682                                 sl_send_reply("403", "Relaying Denied");
683                                 exit;
684                         }
685                         setflag(23);
686                 }
687                 else
688                 {
689                         if(!proxy_authorize("$fd", "subscriber")) 
690                         {
691                                 
692                                 xlog("L_INFO", "Proxy authentication failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
693                                 proxy_challenge("$fd", "0");
694                                 exit;
695                         }
696                         if(!check_from()) 
697                         {
698                                 
699                                 xlog("L_INFO", "Spoofed From-URI detected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
700                                 sl_send_reply("403", "Spoofed From-URI Detected");
701                                 exit;
702                         }
703                 }
704         }
705         $avp(s:acc_caller_user) = $fU;
706         $avp(s:acc_caller_domain) = $fd;
707         $avp(s:acc_state) = "call";
708         
709         route(3);
710         if(nat_uac_test("19")) 
711         {
712                 fix_nated_contact();
713                 setbflag(6);
714         }
715         
716         route(14);
717 }
718
719 ########################################################################
720 # Request route 'invite-find-callee'
721 ########################################################################
722 route[14]
723 {
724         if(alias_db_lookup("dbaliases"))
725         {
726                 
727                 xlog("L_INFO", "Callee was aliased - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
728         }
729         
730         route(2);
731         if(!is_domain_local("$rd"))
732         {
733                 setflag(20);
734                 $avp(s:callee_uuid) = "0";
735                 
736                 route(16);
737         }
738         avp_delete("$avp(s:callee_uuid)");
739         avp_db_query("select uuid from subscriber where username = '$rU'", "$avp(s:callee_uuid)");
740         if(is_avp_set("$avp(s:callee_uuid)/s"))
741         {
742                 
743                 xlog("L_INFO", "Callee is local, uuid='$avp(s:callee_uuid)' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
744                 route(15);
745         }
746         else
747         {
748                 $avp(s:callee_uuid) = "0";
749                 
750                 xlog("L_INFO", "Callee is not local - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
751                 route(16);
752         }
753         exit;
754         
755 }
756
757 ########################################################################
758 # Request route 'invite-to-internal'
759 ########################################################################
760 route[15]
761 {
762         route(4);
763         if(!lookup("location")) 
764         {
765                 
766                 xlog("L_INFO", "Local user offline - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
767                 $avp(s:acc_callee_user) = $rU;
768                 $avp(s:acc_callee_domain) = $rd;
769                 
770                 route(7);
771                 sl_send_reply("404", "User Offline");
772         }
773         else
774         {
775                 
776                 xlog("L_INFO", "Local user online - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
777                 route(12);
778         }
779         exit;
780         
781 }
782
783 ########################################################################
784 # Request route 'invite-to-external'
785 ########################################################################
786 route[16]
787 {
788         if(isflagset(20))
789         {
790                 
791                 xlog("L_INFO", "Call to foreign domain - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
792                 route(12);
793                 exit;
794         }
795         
796         route(18);
797         if(!isflagset(23))
798         {
799                 # don't allow calls relaying from PSTN to PSTN, if not explicitely forwarded
800                 if(uri =~ "^sip:[0-9]+@")
801                 {
802                         # only route numeric users to PSTN
803                         if(!load_gws(1))
804                         {
805                                 
806                                 xlog("L_ERR", "Error loading PSTN gateways - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
807                                 sl_send_reply("503", "PSTN Termination Currently Unavailable");
808                                 exit;
809                         }
810                         if(!next_gw())
811                         {
812                                 
813                                 xlog("L_ERR", "No PSTN gateways available - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
814                                 sl_send_reply("503", "PSTN Termination Currently Unavailable");
815                                 exit;
816                         }
817                         setflag(21);
818                         
819                         t_on_failure("1");
820                         route(12);
821                 }
822         }
823         
824         xlog("L_INFO", "Call to unknown user - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
825         route(7);
826         sl_send_reply("404", "User Not Found");
827         exit;
828         
829 }
830
831 ########################################################################
832 # Request route 'normalize-e164'
833 ########################################################################
834 route[17]
835 {
836         # European numbering plans look like this:
837         #   CC  = country code (i.e. 43 for Austria)
838         #   NDC = national destination code (i.e. 1 for Vienna)
839         #   SN  = subscriber number (i.e. 4001234)
840         #  
841         #       CC + NDC + SN, i.e. 4314001234
842         #
843         # Within the same CC+NDC, it can be omitted, so if
844         # +4314001234 wants to call +4315002345, one can dial
845         # just 5002345.
846         #
847         # Within the same CC, CC can be ommitted and a "0" is prefixed
848         # to NDC, so if +4314001234 wants to call +4326003456, 
849         # one can dial 026003456.
850         #
851         # For international calls, either "00" or + is prefixed, like
852         # +49123456780 or 0049123456789.
853         #
854         avp_delete("$avp(s:orig_callee_user)/g");
855         $avp(s:orig_callee_user) = $rU;
856         if(uri =~ "^sip:(\+[1-9])?[0-9]+@")
857         {
858                 # looks like a PSTN number
859                 if(uri =~ "^sip:0[1-9][0-9]+@")
860                 {
861                         # we have format 0+NDC+SN
862                         strip(1);
863                         prefix("+49");
864                 }
865                 else if(uri =~ "^sip:00[1-9]+@")
866                 {
867                         # we have format 00 + CC + NDC + SN
868                         strip(2);
869                         prefix("+");
870                 }
871                 else if(!uri =~ "^sip:\+[1-9][0-9]+@")
872                 {
873                         # unknown format, maybe NDC wasn't added before?
874                         
875                         xlog("L_INFO", "Not normalized callee '$avp(s:orig_callee_user)' to E.164 format - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
876                         return(-1);
877                 }
878                 # else we have "+" + CC + NDC + SN
879                 
880                 xlog("L_INFO", "Normalized callee '$avp(s:orig_callee_user)' to E.164 format '$rU' - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
881                 return(1);
882         }
883         else
884         {
885                 
886                 xlog("L_INFO", "Not normalized callee '$avp(s:orig_callee_user)' to E.164 format - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
887                 return(-1);
888         }
889         
890 }
891
892 ########################################################################
893 # Request route 'lookup-enum'
894 ########################################################################
895 route[18]
896 {
897         route(17);
898         if($rc == 1)
899         {
900                 if(enum_query("e164.org."))
901                 {
902                         # TODO: do GW fallback (load gws, set failure-route)?
903                         
904                         xlog("L_INFO", "ENUM query succeeded - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
905                         setdsturi("sip:bogus.localhost:5060");
906                         
907                         route(12);
908                         exit;
909                 }
910                 else
911                 {
912                         
913                         xlog("L_INFO", "ENUM query failed - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
914                         # ENUM query failed, revert $rU
915                         avp_pushto("$ru/username", "$avp(s:orig_callee_user)");
916                 }
917         }
918         
919 }
920
921 ########################################################################
922 # Request route 'base-route-local'
923 ########################################################################
924 route[19]
925 {
926         t_on_reply("1");
927         if(t_check_trans())
928         {
929                 
930                 xlog("L_INFO", "Request leaving server - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
931                 if(!t_relay())
932                 {
933                         sl_reply_error();
934                 }
935         }
936         else
937         {
938                 
939                 xlog("L_INFO", "Dropping mis-routed request - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
940         }
941         exit;
942         
943 }
944
945 ########################################################################
946 # Request route 'base-route-generic'
947 ########################################################################
948 route[20]
949 {
950         xlog("L_INFO", "Method not supported - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
951         sl_send_reply("501", "Method Not Supported Here");
952         exit;
953         
954 }
955
956 ########################################################################
957 # Request route 'base-filter-failover'
958 ########################################################################
959 route[21]
960 {
961         if(!t_check_status("408|500|503"))
962         {
963                 
964                 xlog("L_INFO", "No failover routing needed for this response code - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
965                 route(10);
966                 exit;
967         }
968         
969 }
970
971 ########################################################################
972 # Reply route 'base-standard-reply'
973 ########################################################################
974 onreply_route[1]
975 {
976         xlog("L_INFO", "Reply - S=$rs D=$rr F=$fu T=$tu IP=$si ID=$ci\n");
977         exit;
978         
979 }
980
981 ########################################################################
982 # Reply route 'base-nat-reply'
983 ########################################################################
984 onreply_route[2]
985 {
986         xlog("L_INFO", "NAT-Reply - S=$rs D=$rr F=$fu T=$tu IP=$si ID=$ci\n");
987         if(nat_uac_test("1"))
988         {
989                 fix_nated_contact();
990         }
991         if(isbflagset(6) && status=~"(180)|(183)|2[0-9][0-9]") 
992         {
993                 if(!search("^Content-Length:[ ]*0"))
994                 {
995                         rtpproxy_manage();
996                 }
997         }
998         exit;
999         
1000 }
1001
1002 ########################################################################
1003 # Failure route 'pstn-failover'
1004 ########################################################################
1005 failure_route[1]
1006 {
1007         xlog("L_INFO", "Failure route for PSTN entered - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1008         route(21);
1009         if(!next_gw())
1010         {
1011                 
1012                 xlog("L_ERR", "Failed to select next PSTN gateway - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1013                 route(10);
1014                 exit;
1015         }
1016         
1017         t_on_failure("1");
1018         route(12);
1019 }
1020
1021 ########################################################################
1022 # Failure route 'base-standard-failure'
1023 ########################################################################
1024 failure_route[2]
1025 {
1026         if(t_check_status("422|481|487"))
1027         {
1028                 
1029                 xlog("L_INFO", "Final reply - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1030                 route(10);
1031                 exit;
1032         }
1033         if(t_check_status("301|302"))
1034         {
1035                 avp_delete("$avp(s:acc_caller_user)/g");
1036                 avp_delete("$avp(s:acc_caller_domain)/g");
1037                 avp_delete("$avp(s:acc_state)/g");
1038                 avp_copy("$avp(s:acc_callee_user)", "$avp(s:acc_caller_user)");
1039                 avp_copy("$avp(s:acc_callee_domain)", "$avp(s:acc_caller_domain)");
1040                 $avp(s:acc_state) = "cfc";
1041                 setflag(29);
1042                 if(!get_redirects("1:1"))
1043                 {
1044                         
1045                         xlog("L_ERROR", "Failed to fetch contact '$ct' from 301/302 - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1046                         acc_db_request("480", "acc");
1047                         
1048                         route(10);
1049                         t_reply("480", "Temporarily Unavailable");
1050                         exit;
1051                 }
1052                 # get last URI from destination-set and set it as R-URI
1053                 avp_delete("$avp(s:tmp)/g");
1054                 $avp(s:tmp) = $ds;      
1055                 avp_subst("$avp(s:tmp)", "/.*(sip:.+@[^:;>]+).*$/\1/");
1056                 avp_pushto("$ru", "$avp(s:tmp)");
1057                 setflag(29);
1058                 append_branch();
1059                 
1060                 t_on_branch("1");
1061                 xlog("L_INFO", "Redirect from UAC intercepted - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1062                 route(14);
1063                 exit;
1064         }
1065         if($avp(s:cfc) != $null)
1066         {
1067                 avp_delete("$avp(s:acc_caller_user)/g");
1068                 avp_delete("$avp(s:acc_caller_domain)/g");
1069                 avp_delete("$avp(s:acc_state)/g");
1070                 avp_copy("$avp(s:acc_callee_user)", "$avp(s:acc_caller_user)");
1071                 avp_copy("$avp(s:acc_callee_domain)", "$avp(s:acc_caller_domain)");
1072                 $avp(s:acc_state) = "cfc";
1073                 avp_pushto("$ru", "$avp(s:cfc)");
1074                 setflag(29);
1075                 append_branch();
1076                 
1077                 t_on_branch("1");
1078                 xlog("L_INFO", "CFC detected - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1079                 route(14);
1080         }
1081         
1082         route(21);
1083         route(10);
1084 }
1085
1086 ########################################################################
1087 # Branch route 'cfc-drop-local'
1088 ########################################################################
1089 branch_route[1]
1090 {
1091         if(is_domain_local("$rd"))
1092         {
1093                 
1094                 xlog("L_INFO", "Dropping local branch - M=$rm RURI=$ru F=$fu T=$tu IP=$si ID=$ci\n");
1095                 drop();
1096         }
1097         
1098 }
1099