Monday, 28 November 2011

Integrating Lync 2010 and Asterisk

Lync 2010 is a great product and can stand on its own to deliver a good PABX system for the education or business sectors. Where Lync falls down is flexibility, tying in 3rd party scripts, connectivity with products like Skype and even voice based menus. Fortunately we can easily route calls between Asterisk and Lync to produce a much more flexible outcome.

I am a massive fan of Asterisk and have used it numerous times in the past for both my home PABX and several businesses I previously supported. Asterisk is not the easiest product for the end user to connect to, a strength that Lync defiantly has.

In my environment I am using an Asterisk box with advanced scripting based dial plans to handle the incoming PSTN to SIP traffic.



The Integration Process

Obviously you need a working Lync and Asterisk installation before you can begin the process. I'm not going into the Asterisk/Lync server setup process in this tutorial but AsteriskNOW is a great place to start if you want an easy to install Asterisk product. I recommend installing the command line variant and not using the limiting AsteriskGUI, it's harder to learn but also gives you many more possibilities.

1. First we need to configure the SIP trunk in Asterisk's sip.conf. Open /etc/asterisk/sip.conf in your favourite Linux editor and add the following text to the end of the file.

[lynctrunk]
type=friend
port=5068
host=192.168.1.10
dtmfmode=rfc2833
context=fromlync
qualify=yes
transport=tcp,udp

The host= variable needs to reflect your Lync front end, for example my front end IP address is 192.168.1.10. Then save and close the file.

2. Next we are going to add an entry to our /etc/asterisk/extensions.conf file to give us an extension we can call from Lync as a test. Open /etc/asterisk/extensions.conf and add the following text to the end of the file.

[fromlync]
;echo test
exten=>9999,1,Answer()
exten=>9999,2,Playback(en/demo-echotest)
exten=>9999,n,Echo
exten=>9999,n,Playback(en/demo-echodone)
exten=>9999,n(eof),Hangup()

Save and close the file. This 9999 extensions will allow us to do a simple echo test, the [fromlync] header is important as this is the context that our sip.conf entry refers to in step 1.

3. Issue the reload command from the Linux command line, this will force Asterisk to load the above changes.
asterisk -rx "reload"

4. Now we can move onto the Lync side, jump on your Lync server and fire up the the topology builder. "Download the latest toplogy from existing deployment" and then jump straight to the "PSTN Gateway" section.

5. As far as Lync is concerned, Asterisk is a PSTN gateway, so right click PSTN Gateways and click "New IP/PSTN Gateway..."

6. On the "Define New IP/PSTN Gateway" screen we need to specify the settings for our Asterisk box. You will need to enter the IP Address, set 5060 as the port and select TCP as the SIP transport protocol as per the image below.

7. To publish the topology changes we have just made, right click on the Lync Server 2010 menu option and select "Publish Toplogy...". After a few seconds these topology changes should be complete, but it can take some time for them to propagate to the Lync server.

8. Now our SIP trunk is in place, Lync needs a dial-plan and associated routes so it knows what numbers it should route to our new Asterisk trunk. We will start by adding a dial plan entry, open the Lync Control Panel and click on the "Voice Routing" option, then select "Dial Plan"

9. Click on the "Global" entry, then select "edit" and "show details".

10. Under the "Associated Normalization Rules" select "New" to add a new rule.

11. You can call the rule anything you want, for the other settings:
"Starting digits" field put "9"
"Length:" field select "Exactly" and "4"
"Digits to remove:" field select "0"
"Digits to add:" field remove the "+" to leave the field blank
You can copy my settings as per the example photo below, then click "OK"

12. You now need to commit the changes. Click "Commit" and then select "Commit all", on the following page click "Commit" again.

13. Now we need to define a "Voice Policy" to forward any 9XXX dialled numbers in Lync to Asterisk. To do this select "Voice Routing" from the left hand side menu and then select "Voice Policy".

14. Again we need to select the "Global" rule, click "edit" and "show details".

15. Under the "Associated PSTN Usages" select "New" to add a new rule.

16. Give your rule a name and then click "New" to add a new route.

17. On the "New Route" screen, give your route a name and then and the in the "Build a Pattern to Match" section add 9 into the box and select "add". This is an extremely board match, but you can tighten it up later it you want.

18. Then move down to the "Associated gateways" box and add the PSTN gateway we earlier added to our topology builder. Click "OK" in the top left hand corner to accept the route. Then "OK" again on the "New PSTN Usage Record" screen to accept. Finally click "OK" on the Edit Voice Policy screen to accept your new route entry.

19. Again we must commit the changes by selecting "Commit" and clicking "Commit all", then click "Commit" on the following screen.

20. The last piece of the configuration puzzle is to go to the "Voice Routing" options tab from the left hand menu, select "Route" from the top menu. and commit these changes. To commit the changes select "Commit" and click "Commit all", then click "Commit" on the following screen.



Now for the fun part

The dreaded configuration is out of the way, now lets test it out. If things don't work immediately please wait 15 minutes and try again, Lync can take some time to propagate changes.

Before we attempt the call it is a good idea to open an asterisk console by SSH'ing into the Asterisk box and typing asterisk -cr on the command line. This will allow you to want the traffic coming in from Lync to Asterisk and how the dial plans are accessed in real time.

1. Open your Lync client and dial 9999

2. If you opened the Asterisk console you will see the call passed from Lync to Asterisk and the dial plan take effect. You should hear the echo test begin though your Lync connected head set or phone.

I have blanked out some of the sensitive information but here is an example of what you should see on the Asterisk console (click to enlarge).

Congratulations! You now have a Asterisk/Lync monster that you can manipulate however you want. Of course if you want calls to go in the other direction you can use a dial plan entry such as Dial(SIP/1000@lynctrunk,35,tr) in your Asterisk extensions.conf to call SIP extension 1000 on your Lync box.



Some troubleshooting tips
  • First troubleshooting step is to wait 15-30 minutes. It may take some time for the topology changes to propogate.
  • Sign out and then back into Lync.
  • Do a hard restart of Asterisk with the /etc/init.d/asterisk restart command. This will force a reload of the configuration.
  • Re-read step 7 and ensure you did publish the topology changes, then wait 15 minutes.
  • If you have any firewall enabled on your Lync or Asterisk box take it down until you get things working. If you require a firewall after all is working, then you need to enable the following port exclusions.
Port 5060 TCP/UDP should be open on your Asterisk box.
Ports 10000-20000 should be open for RTP traffic on the Asterisk box.
Port 5068 TCP/UDP should be open on your Lync box.  

4 comments:

  1. Hi,

    Hope you're well. Thanks for your post. I'm having a bit of a problem, was hoping you could help out or point me in the right direction.

    I can make outbound calls from Lync to the outside world via my FreePBX box, and I can call between FreePBX and Lync. How can I setup incoming calls on FreePBX to be sent to Lync?

    Any help will be appriciated

    ReplyDelete
  2. Hi there Limbada,

    Unfortunately I havn't used FreePBX for a long time and I havn't integrated them both,

    Similarly to how I have setup a s,1, dial plan for incoming calls to be passed from Asterisk to Lync you will need to do the same with FreePBX,

    Maybe you can make an extension in FreePBX that when rung calls Lync, then pass incoming calls to that extension.

    Sorry I can't offer you instructions for this one

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete