Using SIP Phones like home phones

For years now, I’ve been using Asterisk for our home phone line (as well as one for our rental property business). One persistent issue has been getting SIP phones to act like regular non-PBX extension phones. You know — where two people in different parts of the house can use two phones on the same call.

Asterisk has shared line appearance (SLA) features, but this is designed to make it work like an old key system PBX. Not really the way we want it to work. If John is on a phone call and Jane picks up another extension, Jane can’t join the call — she just gets a new outbound line. Most of this is by design. For one thing, in most non-home settings you really don’t want just anyone to be able to join your call. If you want to add someone, you use the conference feature to dial their phone and add them. In a home setup, though, it’s a hassle and it’s complicated — since you don’t necessarily know which extension a person is using, especially if you’re using cordless phones. Asterisk is really not developed for home use; for some pretty obvious reasons, it’s mostly used by businesses. It is, after all, a PBX.

The solution I’ve used thus far is a regular analog cordless phone system (Panasonic, Vtech, etc.) and an ATA adapter like the Grandstream HT801. That device connects your analog phones to Asterisk. This approach is good, but it’s not a perfect solution. You can’t do things like use your cordless phones to make more than one simultaneous call (unless you have a multiline cordless phone and an HT802). These are also typically consumer grade phones, with limited displays and battery life. The quality of cordless phone systems has most definitely not improved over the past few years.

I want to try out some of the new Wifi SIP phones for a number of reasons, but I want to do it without losing the basic advantages of the consumer type cordless phones. What I finally came up with is a decent solution that combines the best of both worlds. Now if John is on a call and Jane picks up the phone, she can place a new call without interrupting John. She can also, however, join John’s call if she wants.

In my Asterisk dial plan, I added a new extension to my internal context. Dialing *11 will join your call to an existing call if one is in progress. I do this with the BridgeAdd() application and the CHANNELS() function, like this:

exten = _*11,1,NoOp(Join call)
 same = n,If(${CHANNELS(trunk)})
 same = n,BridgeAdd(${CHANNELS(trunk)})
 same = n,EndIf()
 same = n,Hangup()

CHANNELS(trunk) works because any channel that is created for an external call has that pattern in the channel ID. In my pjsip.conf file, I have my trunk connections configured with that string in the name – so it’s an easy target for the regex parameter passed to CHANNELS().

I’ve got a Wifi SIP phone on the way that has some programmable softkeys. I suspect I’ll find a good use for one of those to make operating them as simple as a Panasonic DECT phone.

Rolling your own dynamic DNS

First let me acknowledge that there are many ways to accomplish this. An easy solution for me would have been to simply use dyndns.com, no-ip.com, or one of the other commercial DDNS services supported by my router. For various reasons, I decided not to use one of those. Actually I did use duckdns.org for a while, but there were occasional issues that I got tired of dealing with.

I’m currently using Porkbun for DNS. They’re cheap, reliable, and have a decent user interface. They, like many other DNS services, also provide an API to make changes programmatically, without needing to log into their web site and make manual changes.

In my case, I have a shell script that runs as a cron job every 5 minutes. It checks my router for the WAN address and compares it to the last recorded address. If the two are not the same, it emails me and runs a Python script to update DNS.

I realize that some of this is pretty specific to my setup. Still, it might be a useful starting point. I found the Python script to update Porkbun DNS on their web site. The command to check the WAN IP address at the router may work for yours, or you may need to take a different approach.

#!/bin/bash

# Read the old IP address from a file.  The EOL will go into a variable we don't use.  This is necessary.
read OldIP b < /home/dale/myipaddress.txt
# Get our curent IP address from the router.
OUTFILE=~/myipstatus.txt
MyIP=`ssh -o StrictHostKeyChecking=no [username]@[router.ip] "ifconfig eth0 \
| grep inet | sed -e 's/.*addr:\([^ ]*\) .*/\1/'"`

while [[ $MyIP == "" ||  $MyIP == "192.168."*  ]] ; do
 sleep 10
 MyIP=`ssh -o StrictHostKeyChecking=no [username]@[router.ip] "ifconfig eth0 \
 | grep inet | sed -e 's/.*addr:\([^ ]*\) .*/\1/'"`
done

if [  "$OldIP" != "$MyIP" ] ; then
  echo "`date`" > $OUTFILE
  echo "Found new IP $MyIP, which is different from our previous $OldIP!" >> $OUTFILE
  echo "Updating Porkbun DNS entries..." >> $OUTFILE
  python3 ~/porkbun/porkbun-ddns.py ~/porkbun/config.json <mydomain.com> <hostname> >> $OUTFILE
  mail -s "IP address change detected" <myemail@domain.com>  < $OUTFILE
  echo $MyIP > ~/myipaddress.txt
else
 echo -n "." >> $OUTFILE
fi

In practice, this can result in a 5-10 minute lag between the time your IP address changes and the time your DNS is updated. If your ISP changes your IP address frequently, it may be too long. In my case, our ISP only changes our IP on rare occasions — typically less than once a year.

Again, there are other approaches, but most will not update DNS entries in your own domain. You can get around this to a certain extent by using CNAME entries, but this was the best way that I found to update my own domain’s DNS.

Running Web Servers on Residential Internet

Once upon a time, residential home Internet connections — cable modem and DSL being the choices at the time — were unfiltered and un-firewalled. This had good and bad aspects to it. You may or may not have been firewalled off from your neighbors. I remember a guy who worked for me demonstrating on the TV news one night how he could see every Windows PC in his neighborhood, and send print jobs to random peoples’ printers if he wanted to. Even after they wised up to that little bit of “openness”, it was still possible to run your own services — mail, web, and so on.

Since then ISPs have come a long way. Residential cable, DSL, and fiber connections, often topping out at 1 GB or even higher, are tightly restricted. Your ISP really wants to support only web prosing and gaming, and most certainly do not want any services running on their network. No web servers, no email (in or outbound). Anything inbound on ports 80, 25, and often 443 are blocked, as is outbound port 25.

So, you’ve got your own little web server you run for your own blog (like this one)… or one you run for a nonprofit, club, whatever. You’ve got your own domain and want to run your own email. The solution is usually some combination of a hosted VM, Google, what have you. But it can get a little expensive, and of course you’re dependent on others for critical bits and pieces of your infrastructure. I can’t take all of that pain away, but I can maybe help to reduce it somewhat.

So let’s look at the issues you may face, and how to solve them. I’ll detail each solution in subsequent blog posts, with solutions that may work for you as they have for me.

  • Your IP address is dynamic, and you need reliable DNS. This can be fixed using a script to detect when your IP address changes, and update your DNS accordingly. It’s not perfect in that there will be a delay before the IP address change is detected and updated, but if your IP only changes occasionally it’s “good enough”. Of course there are dynamic DNS (DDNS) solutions that will do this as well, if you don’t mind paying for them. I’m a cheap bastard and I like a challenge, so I rolled my own.
  • Your ISP blocks connections on port 25 (SMTP). This is pretty much going to require an external mail relay. I have yet to find a way to get the rest of the world to use any port other than 25 for SMTP connections… it really is too bad there’s not a DNS based way around this, like a SRV record (see RFC 6186). Until that happens, I use a small external hosted VM relaying mail on a different port. It could actually be a lot simpler, but I prefer to keep our actual email on a server here, at my house.
  • Your ISP blocks incoming traffic on web ports 80 and 443. Easy. Nginx is your answer, what was the question?

The fun part is sizing this stuff. If you’re used to working in a corporate environment like I have been for the past (mumble) years, you’re thinking, “OK, a 4 CPU 16 GB machine for a mail server, than another one for the proxy… that might be OK… ” Nah. You might be shocked at just how little power it takes to do this stuff. After all, we’re just passing packets around. The TLS encryption is the most heavyweight thing being done, I think. If I had a solid place to hang a Raspberry Pi where it would have a static IP and no filtering of privileged ports, it wouldn’t break a sweat — though I’ve had too many of them just stop working to trust them for this kind of stuff, really.

Using Nomorobo to block calls in Asterisk

Nomorobo is a fantastic service. It’s not perfect; plenty of illegal phone spammers are using throwaway numbers and/or illegally spoofing caller ID numbers to make calls that appear to be from random numbers — usually in your own area code. Short of using a strict whitelist, I don’t see a real way to get rid of those. Using Nomorobo, though, will dramatically cut down on the number of junk calls you will receive.

There’s a little problem, though… while many phone providers offer the service (we’ve been using Ooma), they don’t appear to offer the service to individuals or small businesses who run their own phones.

I ran my own Asterisk PBX for several years, supporting our home phones as well as a separate line I used for work, and even a toll-free number for my side business. Life was good for quite a while, but eventually it got to be quite a hassle trying to keep up with all the junk calls. Then my VOIP carrier changed their pricing to make them much less attractive from a cost standpoint. Eventually we switched to Ooma. They’ve been good, but not without issues. The Telo Air occasionally loses communication with the mothership, and if you don’t see the red light you won’t know that your phones aren’t working. The cost has gone up, now running over $20 per month for the Ooma Premier, which includes what I consider to be some pretty basic features — like call blocking, for example.

Now we have some family members who need a home phone, but I just can’t bear to see them get roped into paying really stupid monthly costs for a simple phone line. That, and our Ooma service is getting more expensive and (it seems) less reliable by the year. Time to switch back. But how can I keep Nomorobo? It would be a tough sell to do without that!

Well, Twilio to the rescue! They offer a Nomorobo lookup API that costs a tiny amount per lookup — $.003, or 0.3 cents per incoming call lookup. Conversely, that’s 333 lookups per dollar. Not bad, I’ll gladly pay that to avoid taking telemarketing or scam robocalls. Now, if only we could get Nomorobo to list all of the numbers used by political “push polls”, recorded messages, and other political campaign silliness!

Twilio’s call rates are not outrageously high either, and their monthly costs for DIDs (phone numbers) are pretty reasonable. The only thing I’ll fault them on is too much hassle to set up CNAM for your outbound calls, so unless you go through that process everything shows up as the number only with no CID name. Flowroute is MUCH better for this, so I route most of my outbound calls through them.

So — how to get Asterisk to do the lookup? After several hours of playing around with this, I found that it’s pretty easy to do. While it wouldn’t be terribly helpful (or smart) for me to post my entire dialplan here, I’ll include enough to get you going. I put this very near the top of the context I use for incoming calls from PSTN trunks. There’s no sense in burning CPU cycles on a call if you’re just going to drop it anyway.

First, you’ll need a Twilio account. They’re even nice enough to give you some credit on your account if you’re new, and it’s enough for quite a bit of learning and development work. I funded my account so I can use them for international calls — they’re ridiculously cheap for most destinations. They’re also a good solution if you want to get DIDs in countries outside the US.

Once you have a Twilio account established, use your account SID and auth token to set CURLOPT() with your username and password. This will be used in the next line to make the curl call to the API:

same = n,Set(CURLOPT(userpwd)=username:password)

Now, make the call to Twilio’s API to get the spam score. The result is a block of JSON that gets saved as TWILIO_RESULT:

same = n,Set(TWILIO_RESULT=${CURL("https://lookups.twilio.com/v1/PhoneNumbers/${CALLERID(num)}?AddOns=nomorobo_spamscore")})

Since we’ve got a block of JSON, we’ll need to extract the one wee bit we need. Fortunately Asterisk has a solution for that as well, so we don’t need to resort to anything drastic like a shell command:

same = n,Set(SPAMSCORE=${JSON_DECODE(TWILIO_RESULT,add_ons.results.nomorobo_spamscore.result.score)})

Now we use that result to drop the call if it’s spam. A simple Hangup(2) tells the caller that their call was rejected:

same = n,GotoIf($[ ${SPAMSCORE} = 1]?dropcall)

Later in the dialplan, after we’ve done the whole “call the user, drop to voicemail if they don’t answer, yadda yadda yadda” we have this:

same = n(dropcall),Hangup(21)

The Hangup(21) tells that their call was rejected. There are other, even more creative codes to use… like these (list courtesy of voip-info.org):

  • 1 – Unallocated number
  • 22 – Number changed
  • 27 – Destination out of order
  • 38 – Network out of order

Letsencrypt, Duckdns, and Cox

Like some other ISPs, Cox blocks all incoming access to port 80 on residential connections. They also use DHCP to assign dynamic IP addresses, which can can and do change occasionally — especially when you reboot your router. That’s fine in most cases, but can be a real pain in the ass if you run any local services that you need to access from outside the home. For example, if you run your own email and want to use IMAP, you’re likely going to need an SSL certificate. You need a way to have your DNS update to point to your new IP when it changes.

One way to do all of this without paying subscription fees is with Duckdns and Letsencrypt. Duckdns is a free DNS service with an easy to use API that can be updated by a script when your IP address changes. Letsencrypt is a free SSL certificate CA; I can’t say enough good things about Letsencrypt and encourage you to support them with a donation as I have.

So. First we can use cron to run a command that updates our duckdns IP address every ten minutes or so.

echo url="https://www.duckdns.org/update?domains={my_domain}&token={my_token}&ip=" | curl -k -o ~/duck.log -K -

Simple, right? Now we have a hostname that always points to our own home IP address – or at least always does within ten minutes of an IP address change, which is probably good enough for most purposes.

Now for the SSL certificate. Letsencrypt will happily issue free a 90 day SSL cert for your domain. Normally, one runs a script from cron that renews the certificate if the cert is expiring in less than 30 days. IF you can expose port 80 to the web, even temporarily, then life is good — just run ‘certbot renew‘ once a day, or even once a week, and everything happens for you in the background. If, however, your ISP filters port 80… well, there’s the pain-in-the-ass part. The certbot script renew script will only work if you have port 80 open to the web. I haven’t found a way to get Letsencrypt’s server to use any other port to reach your web server, so forwarding a non-blocked port (8880, for example) to your local server’s port 80 does you no good.

All is not lost; it just means a bit more work. Letsencrypt will also issue certificates using DNS challenges for authentication, placing specific TXT records to prove that you have control of the domain or subdomain in question. The process looks like this:

certbot certonly --manual --preferred-challenges dns -d example.com-d -d example-com.duckdns.org

The certbot script will tell you to create TXT records in DNS for your domain, and will wait for you to do so before proceeding. You can use your DNS provider’s web or API interface to add or change the TXT record accordingly. Duckdns now supports TXT records in addition to A records, and updating yours is simple:

curl 'https://www.duckdns.org/update?domains={my_domain}&token={my_token}&txt={my_txt}&verbose=true'

Once you’ve verified that the TXT records are there using, say, ‘dig _acme_challenge.{my_domain}.duckdns.org TXT‘ — simply hit ENTER to let the script finish. You should end up with a renewed SSL cert.

My previous ISP didn’t block port 80, so I never had to do any work at all for this. I ran the ‘certbot renew’ command from cron once a day, and it automatically updated the certs for me. Now that port 80 is no longer an option, I will need to manually renew the certificate every 90 days. I’ll actually do it at around 75 days, because Letsencrypt helpfully sends out emails to let you know when your certificate is within 15 days of its expiration.

A year’s worth of updates

Time flies when you’re ignoring a blog, right? I’ll catch up.

  • The Mercedes is gone. After everything I’d fixed on it, when the transmission decided it didn’t want to work reliably any more — screw it, I was done. It was an awesome car to drive, but not so much fun to own. I replaced it with a much newer 2018 BMW 540i Xdrive, which has been wonderful.
  • Still flying occasionally, but nowhere near as much as I should or want to.
  • Nothing’s happened with the Mustang, other than getting the engine put back together.
  • We’ve picked up a couple more rental houses; that enterprise is going pretty well overall.
  • We switched from Visible to T-Mobile. Visible had great service when we signed up; it slowly degraded to barely usable. TMO has been better, but not great.
  • I just dumped CenturyLink. Our CenturyLink fiber service has been down since Wednesday morning (it’s Friday now). It took me three hours to get through to a human there, on the phone, who told me they could have someone out Saturday morning. Absolutely appalling service. We were up and running on Cox within an hour of leaving the house to go pick up their equipment.
  • Now I remember why I didn’t like Cox’s equipment… zero flexibility, no control over your own local network at all. You can’t even set your own DNS, so my Pi-Hole is not functional. I’ve got new equipment coming this afternoon. New cable modem, router, and mesh wifi.
  • I left my long time employer (a bank) a little over a year ago and now work for another bank.

Fixing a front heat issue

The symptom: No (or intermittent) heat in the front seats of a 2007 W221 S600.

  • The vents blow cool to cold air most of the time.
  • Changing the temperature zone setting has no effect.
  • Turning the climate control system OFF for a minute or so, then back ON will result in warm air blowing briefly, cooling off rapidly — as if there is hot water in the heat exchanger, but no flow of hot water from the engine.
  • XENTRY/DAS diagnostics show all temperature sensors working.

Fixes tried:

  • Replaced AC water valve. The old one was in need of replacement; the top water hose neck snapped off during removal and the rack & pinion gear was jammed with some debris, so it likely wasn’t working. The valve is a non-Mercedes sourced part, unbranded, likely Chinese sourced like everything else. $40, quality appears to be quite good and the fit was perfect.
  • Tested valve operation with XENTRY/DAS. Using the actuations top open and close the valve result in hot air (CLOSED) and cool air (OPEN), so the valve is mechanically and electrically operational.
  • Used diagnostics to teach-in and normalize all actuators, flaps, and air control potentiometers. This had no effect on the symptoms.
  • I wanted to reverse the wires to the valve at the plug. My hypothesis was that the motor was simply installed backward, resulting in the valve running opposite the direction commanded by the HVAC system. Since it’s an open loop system, the front SAM would have no idea that this was happening. Unfortunately, I had no convenient place to swap the wires. I ended up determining that it was indeed wired backwards internally. A new part from a Mercedes dealer (thanks, Husker Auto Group!) fixed it.

Results: Fully working heat!!

Moved.

I’ve moved the blog to a new web service… one of the AWS virtual server offerings. So far, so good… and dirt cheap.

A couple months of Mercedes ownership

I’ve been adjusting to “semi sort of exotic” V12 ownership.  Early May I bought a Mercedes S600 with under 48K miles on the odometer.  If you’re unfamiliar, it’s an exceptionally opulent luxury sedan with a 510 HP, twin-turbocharged 5.5 liter V12 and an active hydraulic suspension system, among other things. It hasn’t been trouble-free, but I don’t think anything new has broken since I bought it — it just had problems that weren’t obvious when I looked at it.  If I’d have had a proper dealer PPI done, I would have screwed the price down a few more thousand — but the logistics of doing that in a distant city are difficult to say the least.  Next time (and there will be a next time), I’ll do it differently.  

The real adjustment is in how these things are serviced.  Got a problem?  Unless it’s something mechanical that’s obviously broken, you’re going to absolutely need the Mercedes dealer level software (at the very least), on a dedicated laptop, and the hardware to get it to talk to the car.  Period.  Or, you take it to a dealer that charges a $160 “diagnostic fee” per symptom.  Or, you find an independent shop and hope they’re as good as they claim.  The mechanical systems are very complex.  The electronics are far, far more complex.  Just an example: You turn the thumbwheel on the dash air vent to control airflow.  It’s not a mechanical control.  It’s a potentiometer, which is read by a control unit that sits on the CAN bus, and talks to numerous other control units, and a decision is made how much to move the electrically actuated damper behind that vent.  Oh, the potentiometer went bad and can’t be read?  No A/C for you, pal.

I’ll be about $3K deep in repairs, parts, and vehicle-specific tools by the time I’m done, maybe a little less.  The good news is, half of that is the one thing that the dealer HAS to do — the rest I can do myself with parts sourced from Fleabay or a couple of dealers that sell factory original parts at a deep discount.  By the weekend I’ll be equipped to do anything the dealer can do diagnostic-wise, which will pay for itself quickly.  

On the plus side…  the thing is over-built, and the level of engineering and the build quality is fantastic.  Even at 13 years old, this car has features most new cars don’t.  You can cruise all day long in ridiculous comfort (the massaging seats help), and if the mood ever strikes you to see, for example, how long it takes to go from 40 to 130 MPH…  it will happily and very quickly do it, without drama, and you’re nowhere near the top end.  This model is limited to 157 MPH, and it will easily do it.  It’s not going to be as cheap to own and operate as my F150, for example, but once it’s fully sorted out I don’t think it will be punitively bad, either.  You don’t own a car like this (or a Ferrari, or a McLaren, or a Bentley, or whatever) because it’s cheap.  

Props to Hulu.

We’ve caught up on all the Hulu shows we watch, no new ones for a couple of months. There is really nothing we need from them until the new season is available. Went to cancel my subscription, with the intent of re-starting in April or May. Hey — they let you “pause” your subscription for up to 12 weeks. Sweet!! Exactly what I needed to do. Thanks for anticipating our needs, guys.