Hi,
This is an example, one of the many scripts I've
written over the last several months. Many of these
parts have been copied from the
iptel.org example, which is
very good for learning.
It will route to REGISTERed phones and to PSTN numbers.
If you forward your PSTN gateway to this server, it
will route incoming PSTN calls as well. This
script does authentication with the user agents. It
also does accounting, call INVITE and BYE to syslog.
---greg
loadmodule "/usr/local/lib/ser/modules/sl.so"
loadmodule "/usr/local/lib/ser/modules/tm.so"
loadmodule "/usr/local/lib/ser/modules/acc.so"
loadmodule "/usr/local/lib/ser/modules/rr.so"
loadmodule "/usr/local/lib/ser/modules/maxfwd.so"
loadmodule "/usr/local/lib/ser/modules/usrloc.so"
loadmodule "/usr/local/lib/ser/modules/registrar.so"
loadmodule "/usr/local/lib/ser/modules/exec.so"
loadmodule "/usr/local/lib/ser/modules/mysql.so"
loadmodule "/usr/local/lib/ser/modules/auth.so"
loadmodule "/usr/local/lib/ser/modules/textops.so"
modparam("usrloc","db_mode",2)
modparam("usrloc","db_url",
"sql://ser:xxxxxx@localhost/ser")
modparam("acc", "log_level", 1)
modparam("acc", "acc_flag", 1 )
modparam("auth","db_url", "sql://ser:xxxxxx@localhost/ser")
#
# this is the main routing block for
augustvoice.net
# this block can handle any routing request, local or foreign
#
route
{
#
# check to see if the message has been around too long
# probably means that it is looping
#
if (!mf_process_maxfwd_header("10"))
{
log("LOG: Too many hops\n");
sl_send_reply("483","Too Many Hops");
break;
};
#
# make sure the length of the message isn't too long!
#
if (len_gt( max_len ))
{
sl_send_reply("513", "Wow -- Message too large");
break;
};
#
# rewrite the From stuff
#
rewriteFromRoute();
if (method=="REGISTER")
{
route(1);
break;
};
if(method=="INVITE" | method=="BYE")
{
setflag(2);
};
# now check if it's about PSTN destinations through our gateway;
if (uri=~"sip:[0-9]{10}@.*")
{
route(3);
break;
};
# now check if it's about LD PSTN destinations through our
gateway;
if (uri=~"sip:1[0-9]{10}@.*")
{
route(3);
break;
};
# check for native destinations
if (lookup("location"))
{
log(1,"found location");
};
if(isflagset(2))
{
log(1,"TRACE: addRecordRoute()");
addRecordRoute();
};
# turn on local delivery accounting
if(method=="INVITE" | method=="BYE")
{
log(1,"TRACE: turn on accounting for INVITE or BYE");
setflag(1);
};
append_hf("P-hint: USRLOC\r\n");
if(!t_relay())
{
sl_reply_error();
break;
};
}
#
# route[1], REGISTER block
# REGISTER messages destined for our realm are forwarded here.
# after a successful registration a customer can receive calls.
#
route[1]
{
if(!www_authorize("augustvoice.net", "subscriber"))
{
www_challenge("augustvoice.net", "0");
break;
};
# if(!check_to())
# {
# log("LOG: To Cheating attempt\n");
# sl_send_reply("403","That is ugly -- use To=id next
time");
# break;
# };
log("here is a register");
if(!save("location"))
{
sl_reply_error();
};
break;
}
#
# route[2], offnet requests
#
route[2]
{
log(1,"TRACE:In route[2]");
if(!(src_ip==PSTN.GATEWAY.IP.ADDRESS) &
!(proxy_authorize("augustvoice.net","subscriber")))
{
log(1, "source ip is PSTN.GATEWAY.IP.ADDRESS");
if(method=="ACK")
{
log("LOG: failed outbound authentication for
ACK\n");
}
else if(method=="CANCEL")
{
log("LOG: failed outbound authentication for
CANCEL\n");
}
else
{
proxy_challenge("augustvoice.net","0");
break;
};
}
else
{
log(1, "NOT from IP address PSTN.GATEWAY.IP.ADDRESS");
};
}
# route[3], logic for calls to the PSTN
route[3]
{
log(1,"TRACE:In route[3]");
if(!(src_ip==PSTN.GATEWAY.IP.ADDRESS | method=="ACK" |
method=="CANCEL" | method=="BYE"))
{
if(!www_authorize("augustvoice.net","subscriber"))
{
www_challenge("augustvoice.net","0");
break;
}
# let's check from=id ... avoids accounting confusion
else if (method=="INVITE" & !check_from())
{
log("LOG: From Cheating attempt\n");
sl_send_reply("403",
"That is ugly -- use From=id next time
(gw)");
break;
};
};
# we passed all authorization checks for PSTN -- move on!
# tag this transaction for accounting
setflag(1);
# requests to gateway must be record-routed because the GW
accepts
# only reqeusts coming from our proxy
if (isflagset(2) || method=="INVITE" || method=="BYE")
{
addRecordRoute();
};
# if you have passed through all the checks, let your call go to
GW!
rewritehostport("PSTN.GATEWAY.IP.ADDRESS:5060");
append_hf("P-hint: GATEWAY\r\n");
if (!t_relay()) {
sl_reply_error();
break;
};
}
-----------------cut---------------
-----Original Message-----
From: serusers-admin(a)iptel.org [mailto:serusers-admin@lists.iptel.org] On
Behalf Of Phillip Haynes
Sent: Thursday, January 30, 2003 2:57 PM
To: serusers(a)lists.iptel.org
Subject: [Serusers] Routing to the PSTN
Hi,
I have set up the SER and its working. The only problem is I cant get
the routing logic to simultaneously route to either the User Loc or the
PSTN. Its only doing one of the other.
I am new to SIP, can u show me a sample script that will do both at the
same time?
Best regards,
Phillip