--- kamailio.cfg.orig	2014-02-19 17:22:25.040166479 -0500
+++ kamailio.cfg	2014-02-27 17:02:26.599259984 -0500
@@ -1,4 +1,12 @@
 #!KAMAILIO
+ 
+#!define WITH_ODBC
+#!define WITH_AUTH
+#!define WITH_USRLOCDB
+#!define WITH_ASTERISK
+#!define WITH_PRESENCE
+#!define WITH_NAT
+ 
 #
 # Kamailio (OpenSER) SIP Server v4.1 - default configuration script
 #     - web: http://www.kamailio.org
@@ -105,6 +113,14 @@
 ####### Defined Values #########
 
 # *** Value defines - IDs used later in config
+#!ifdef WITH_ODBC
+#!ifndef DBURL
+#!define DBURL "unixodbc:///kamailio-connector"
+#!endif
+#!ifdef WITH_ASTERISK
+#!define DBASTURL "unixodbc:///elxpbx-connector"
+#!endif
+#!endif
 #!ifdef WITH_MYSQL
 # - database URL - used to connect to database server by modules such
 #       as: auth_db, acc, usrloc, a.s.o.
@@ -203,6 +219,16 @@
 voicemail.srv_port = "5060" desc "VoiceMail Port"
 #!endif
 
+ 
+#!ifdef WITH_ASTERISK
+asterisk.bindip = "127.0.0.1" desc "Asterisk IP Address"
+asterisk.bindport = "5080" desc "Asterisk Port"
+kamailio.bindip = "127.0.0.1" desc "Kamailio IP Address"
+kamailio.bindport = "5060" desc "Kamailio Port"
+#!endif
+
+mhomed = 1
+ 
 ####### Modules Section ########
 
 # set paths to location of modules (to sources or installation folders)
@@ -216,6 +242,10 @@
 loadmodule "db_mysql.so"
 #!endif
 
+#!ifdef WITH_ODBC
+loadmodule "db_unixodbc.so"
+#!endif
+
 loadmodule "mi_fifo.so"
 loadmodule "kex.so"
 loadmodule "corex.so"
@@ -283,6 +313,10 @@
 loadmodule "debugger.so"
 #!endif
 
+#!ifdef WITH_ASTERISK
+loadmodule "uac.so"
+#!endif
+ 
 # ----------------- setting module-specific parameters ---------------
 
 
@@ -303,8 +337,11 @@
 # add value to ;lr param to cope with most of the UAs
 modparam("rr", "enable_full_lr", 1)
 # do not append from tag to the RR (no need for this script)
+#!ifdef WITH_ASTERISK
+modparam("rr", "append_fromtag", 1)
+#!else
 modparam("rr", "append_fromtag", 0)
-
+#!endif
 
 # ----- registrar params -----
 modparam("registrar", "method_filtering", 1)
@@ -356,11 +393,19 @@
 
 # ----- auth_db params -----
 #!ifdef WITH_AUTH
-modparam("auth_db", "db_url", DBURL)
 modparam("auth_db", "calculate_ha1", yes)
-modparam("auth_db", "password_column", "password")
 modparam("auth_db", "load_credentials", "")
+ 
+#!ifdef WITH_ASTERISK
+modparam("auth_db", "user_column", "name")
+modparam("auth_db", "password_column", "sippasswd")
+modparam("auth_db", "db_url", DBASTURL)
+modparam("auth_db", "version_table", 0)
+#!else
+modparam("auth_db", "db_url", DBURL)
+modparam("auth_db", "password_column", "password")
 modparam("auth_db", "use_domain", MULTIDOMAIN)
+#!endif
 
 # ----- permissions params -----
 #!ifdef WITH_IPAUTH
@@ -405,7 +450,10 @@
 
 #!ifdef WITH_NAT
 # ----- rtpproxy params -----
-modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
+#modparam("rtpproxy", "rtpproxy_sock", "udp:127.0.0.1:7722")
+modparam("rtpproxy", "rtpproxy_sock", "1 == udp:127.0.0.1:7722")
+modparam("rtpproxy", "rtpproxy_sock", "2 == udp:127.0.0.1:7723")
+modparam("rtpproxy", "rtpproxy_sock", "3 == udp:127.0.0.1:7724")
 
 # ----- nathelper params -----
 modparam("nathelper", "natping_interval", 30)
@@ -629,6 +677,10 @@
 		if (!save("location"))
 			sl_reply_error();
 
+#!ifdef WITH_ASTERISK
+		route(REGFWD);
+#!endif
+
 		exit;
 	}
 }
@@ -649,6 +701,16 @@
 		route(SIPOUT);
 #!endif
 
+#!ifdef WITH_ASTERISK
+	if(is_method("INVITE") && (!route(FROMASTERISK))) {
+		# if new call from out there - send to Asterisk
+		# - non-INVITE request are routed directly by Kamailio
+		# - traffic from Asterisk is routed also directy by Kamailio
+		route(TOASTERISK);
+		exit;
+	}
+#!endif
+ 
 	$avp(oexten) = $rU;
 	if (!lookup("location")) {
 		$var(rc) = $rc;
@@ -718,6 +780,12 @@
 route[AUTH] {
 #!ifdef WITH_AUTH
 
+#!ifdef WITH_ASTERISK
+	# do not auth traffic from Asterisk - trusted!
+	if(route(FROMASTERISK))
+		return;
+#!endif
+ 
 #!ifdef WITH_IPAUTH
 	if((!is_method("REGISTER")) && allow_source_address())
 	{
@@ -729,7 +797,11 @@
 	if (is_method("REGISTER") || from_uri==myself)
 	{
 		# authenticate requests
+#!ifdef WITH_ASTERISK
+		if (!auth_check("$fd", "sip", "1")) {
+#!else
 		if (!auth_check("$fd", "subscriber", "1")) {
+#!endif
 			auth_challenge("$fd", "0");
 			exit;
 		}
@@ -779,7 +851,19 @@
 	if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB)))
 		return;
 
-	rtpproxy_manage("co");
+	#rtpproxy_manage("co");
+	if ($Ri=="10.1.0.3" || $(hdr(Record-Route)[1]) =~ "10.1.0.3") {
+		set_rtp_proxy_set("1");
+		rtpproxy_manage("co", "10.1.0.3");
+	} else if ($Ri=="192.168.5.18" || $(hdr(Record-Route)[1]) =~ "192.168.5.18") {
+		set_rtp_proxy_set("2");
+		rtpproxy_manage("co", "192.168.5.18");
+	} else if ($Ri=="10.0.0.2" || $(hdr(Record-Route)[1]) =~ "10.0.0.2") {
+		set_rtp_proxy_set("3");
+		rtpproxy_manage("co", "10.0.0.2");
+	} else {
+		xlog("L_ALERT", "ALERT: no matching set: Ri=$Ri $(hdr(Record-Route)[0])  $(hdr(Record-Route)[1])\n");
+	}
 
 	if (is_request()) {
 		if (!has_totag()) {
@@ -939,3 +1023,42 @@
 	}
 #!endif
 }
+ 
+#!ifdef WITH_ASTERISK
+# Test if coming from Asterisk
+route[FROMASTERISK] {
+	if($si==$sel(cfg_get.asterisk.bindip)
+			&& $sp==$sel(cfg_get.asterisk.bindport))
+		return 1;
+	return -1;
+}
+ 
+# Send to Asterisk
+route[TOASTERISK] {
+	$du = "sip:" + $sel(cfg_get.asterisk.bindip) + ":"
+			+ $sel(cfg_get.asterisk.bindport);
+	route(RELAY);
+	exit;
+}
+ 
+# Forward REGISTER to Asterisk
+route[REGFWD] {
+	if(!is_method("REGISTER"))
+	{
+		return;
+	}
+	$var(rip) = $sel(cfg_get.asterisk.bindip);
+	$uac_req(method)="REGISTER";
+	$uac_req(ruri)="sip:" + $var(rip) + ":" + $sel(cfg_get.asterisk.bindport);
+	$uac_req(furi)="sip:" + $au + "@" + $var(rip) + ":5060";
+	$uac_req(turi)="sip:" + $au + "@" + $var(rip) + ":5060";
+	$uac_req(hdrs)="Contact: <sip:" + $au + "@"
+				+ $sel(cfg_get.kamailio.bindip)
+				+ ":" + $sel(cfg_get.kamailio.bindport) + ">\r\n";
+	if($sel(contact.expires) != $null)
+		$uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $sel(contact.expires) + "\r\n";
+	else
+		$uac_req(hdrs)= $uac_req(hdrs) + "Expires: " + $hdr(Expires) + "\r\n";
+	uac_req_send();
+}
+#!endif
