Monday, March 2, 2015

Call Routing with the Siemens n300ip

Call Routing with the Siemens n300ip

We've had one of these DECT IP phone boxes for quite a while. In fact we've had two, but the first one had a 10Meg half duplex interface which caused a lot of grief, which caused us to get the current one.
Why did we get a DECT VOIP phone? Because I was trying to save money of course! A small amount, granted. The idea was that we would drop our phone line and just rely on cable internet with VOIP for our house phone and mobiles to cover any outages. This worked well for 3 years. The calls were cheaper and the quality better, plus we had features on the VOIP line we would have had to pay for with a land line:
  • Voicemail to email
  • Incoming CLID
  • Itemised billing
Plus we could use our number even when we were away from home by logging in with MicroSIP.
Sadly our cable provider kept hiking up the price in increments, and eventually an offer came along that offered more bandwidth with a land line for less money, and with the added bonus of free outbound weekend calls. (I am aware that Virgin did free weekend calls, but the additional cost for the land line was more than we spend on calls).
How to integrate these free calls into the current network? Well the n300ip has dial plans, they are basic but could be configured to route calls through either VOIP or the land line based on the number dialled. However the one thing it doesn't have is any logic related to dates and times. If BT's calls were cheaper than SIPGATE we might forgo the CLID and the itemised e-billing and the network voicemail, but during the week they aren't. 

Timed call routing on the cheap

The solution is a nifty script I popped together in python, the script uses libCurl, runs on Linux or Windows with suitable addition of libCurl and need python 3.3 (I leave it as an exercise for the more able to convert it so that it runs on all Python version.

The script runs Saturday morning when our server wakes from its slumber and again on Monday morning.
The call plan fields were captured by using wireshark and then edited using VIM, the script has to do session cookie handling for the password cookie, because I couldn't get libCurl (or Curl for that matter) to persist session cookies.
The functionality of the script is login, upload the call plan and then logout. The parameter is to toggle between enable.txt and disable.txt (the two call plans, enable puts UK landlines beginning 01 and 02 through the fixed line and disable.txt undos this by disabling those call plans). The script is very simple, if you change the call plans it will overwrite them.

Python Code

#!/usr/bin/env python3
import pycurl
from io import StringIO
    from io import BytesIO
except ImportError:
    from StringIO import StringIO as BytesIO

import email

import argparse

def enable_disable(string):
     valid_values = ['enable','disable']
     value = str(string)
     if not value in valid_values:
        msg = "action is either enable or disable, you typed " % string
        raise argparse.ArgumentTypeError(msg)
     return value

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('action', choices=['enable','disable'] , nargs = 1,
                   help='either enable or disable, set the dialplan in enable.txt or disable.txt')

args = parser.parse_args()

def CallPlanFileName(x):
        return {
        "enable" : 'enable.txt' ,
        "disable" : 'disable.txt' }[x]

filename = CallPlanFileName(args.action[0])

#print (args.action[0])

## Callback function invoked when body data is ready
def stdout(buf):
    # Print body data to stdout
    import sys
    # Returning None implies that all bytes were written

## Callback function invoked when header data is ready
def stderr(buf):
    # Print header data to stderr
    import sys
    # Returning None implies that all bytes were written


c.setopt(c.POSTFIELDS, 'language=1&password=XXXX')
c.setopt(c.WRITEFUNCTION, body.write)
c.setopt(pycurl.HEADERFUNCTION, header.write)
c.setopt(c.COOKIEFILE, '')  #Make Curl Cookie aware

with open('header.txt','wb'as h:

with open('body.txt','wb'as b:

request_line, headers_alone = ((header.getvalue()).decode('ISO-8859-1')).split('\r\n',1)
mhead = email.message_from_string(headers_alone)

print (mhead.as_string(True))
cookies = mhead['Set-Cookie']
print (cookies)
c.setopt(c.COOKIE, cookies)

with open(filename,'r'as f:
    c.setopt(c.POSTFIELDS, data)

c.setopt(c.URL, 'http://192.168.XX.XX/logout.html')


Call plans






My server isn't on all the time, it shuts down in the evening and doesn't start up again until everyone is up and about. Initially I was puzzled why my script wasn't having any effect at the weekends. Of course I set the time of the cron job to be midnight Friday and midnight Sunday, both at times my server was asleep. Here's the solution I use:
#                    Test if it is Saturday
@reboot              test $(date +\%u) -eq 6 && ( cd $HOME/bin; ./BT_CallPlan enable ) >> ~/BT.log
#                    Test if it is Monday
@reboot              test $(date +\%u) -eq 1 && ( cd $HOME/bin; ./BT_CallPlan disable ) >> ~/BT.log

Other details:

Our cable provider was Virgin Media, we replaced this with BT Infinity 1 
  • Virgin (30M down 2.5M up) - usually 20M down 1M up
  • BT Infinity 1 (38M down 9.5M up) - so far 34M down and 9M up (much better for telecommuting)
Our VOIP provider is SIPGATE
I sold the Home Hub 5 (£35) and bought an Huawei FTTC terminal adapter/router (which I put custom firmware on).
We use a Buffalo Airstation running OpenWRT (with Asterisk running on it for VOIP), this also provides IPV6 via Hurricane Electric, 2.5 and 5 GHz wireless N and remote access. 
When we needed analogues phone lines I used an old BT voyager ADSL router (ADSL disabled) as an ATA (logged into Asterisk as an extension), of course we now have a real analogue line :) .
Virgin Media super Hub (very poor piece of kit), was set to Modem mode due to it's appalling WIFI drop out rates, and general lack of configurable options.