Friday, June 6, 2008

Route Calls from Openser to Asterisk

Last time we talked about on how to install and set up the OpenSER and to make it work. Now we're going to discuss on how are we going to integrate OpenSER with the Powerful Asterisk PBX. By the way, why do we need to integrate it anyway? OpenSER is just use as a SIP Proxy, SIP Server and can only communicate using SIP protocol and has no way to access such as PSTN lines. Another thing to mention is that it doesn't have that rich features just like what a PBX could offer. This is the point where Asterisk comes into the scene.

Now let's prepare things first for the Asterisk.

1. Create the SIP trunk where the OpenSER will connect to the Asterisk Server. We can do this in 2 ways.
Either we register the Asterisk server as a SIP UA to OpenSER or we will statically declare a SIP trunk to the Asterisk Server. So we will choose the latter.

Edit sip.conf using your favorite text editor (for me it's vi)

# vi /etc/asterisk/sip.conf

[openser]
type=friend
context=incoming
host=192.168.2.235 ; replace this with the IP address of your SIP server (OpenSER)
fromdomain=192.168.2.235 ; same thing here
insecure=port,invite

# vi /etc/asterisk/extensions.conf

[incoming]

exten => 205,1,Dial(SIP/205,60) ; note that the UA 205 is registered on the Asterisk server not on the OpenSER
exten => 205,n,Hangup

Offcourse you have to reload asterisk so the changes would take effect then

2. Alright now it's time to tweak our OpenSER.

# vi /usr/local/etc/openser/openser.cfg

;note that we're using the default configuration of openser ( we'll discuss on our future tutorial on how to make use of the sipwise wizard )

;look for this line

if (is_method("INVITE")) {
setflag(1); # do accouting

}

;make it like this one

if (is_method("INVITE")) {
setflag(1); # do accouting

if (uri=~"sip:205@192.168.2.235")
{
route(2);
}

}


Hey what does this stuff mean??

Let me explain everytime a SIP UA initiate a call it sends an INVITE message sending also the uri being requested in our case it is "sip:205@192.168.2.235". Again change the IP here with the IP of your SIP server. Here I've used static value '205' but you can actually make use of pattern matching here say for instance all numbers beginning with 1 followed by 1 or 2 we can represent this by "sip:1[1-2]@192.168.235"

What's the next stuff?

route(2) ====> this one refer to a routing block which contains lines of rules

so we have to add at the end of the file the following to define our route(2). Here's how?

route[2] {

rewritehostport("192.168.2.2:5060"); # change the IP here with the IP of your Asterisk Server
t_relay();
exit;
}

3. Offcourse you have to reload or restart OpenSER

4. Now you're done and let us test the things that we have done.

register a SIP UA to Asterisk Server with username 205
register a SIP UA to OpenSER with username of what you like (Please refer to my previous tutorial on how to add new UA subscriber)

Using your phone registered to OpenSER dial 205.....

It should now ring the local extension registered on your Asterisk Server. :-)

5. Same principle I guess to use if you want to route your PSTN calls from OpenSER to your Asterisk PBX which is capable to access PSTN network.

Okey that's all for this tutorial. I just hope that you find it useful.

CHeers!!!!!! Take Care and God Bless!!!!

3 comments:

rickygm said...

this example is asterisk and openser running in the same pc?

I cannot make calls of openser asterisk pstn, he sends me the code 407


U +0.000579 192.168.10.1:5060 -> 192.168.10.29:5060
SIP/2.0 407 Proxy Authentication Required
Via: SIP/2.0/UDP 192.168.10.29:5060;branch=z9hG4bKce1376860743d4c3
From: sip:119@192.168.10.1;tag=a5a05ec70f06c023
To: sip:2650221@192.168.10.1;tag=d7e793d797690be5ce5420203095040f.0263
Call-ID: 9405de116791b097@192.168.10.29
CSeq: 10039 INVITE
Proxy-Authenticate: Digest realm="192.168.10.1", nonce="48895fa94380d49a59f622e081cea9f613deb37f"
Server: OpenSER (1.3.2-notls (i386/linux))
Content-Length: 0


God Bless you too

tabing said...

Hi do you have a tutorial on how to integrate voicemail to openser? I want Openser registered users to have voicemail feature....

Unknown said...

In my though, your article presents the way how to integrate Asterisk and OpenSER in which asterisk server and OpenSER server belong to the same subnet.
Now, I assume that asterisk server has IP address 172.28.20.10 and OpenSER server has IP address 10.10.10.10, the problem is how to asterisk and openser can contact to each other ?
What should we implement in extension.conf file, sip.conf file and openser.conf file, etc. In my model, asterisk plays as media gateway and OpenSER plays as SIP proxy.
Can you attach the configuration files to your reply and teach me the way how to integrate Asterisk and OpenSER in this case.
Thank you very much