Fax for two with Asterisk and HylaFAX

This has resumed working for me – see below.

The one big obstacle you would face if you went for an Asterisk based domestic telephone system would be if you ever made use of faxes. You could connect a fax machine in parallel with the PBX and have it respond to distinctive rings (CallSign if you're on BT), but then you have the issue of contention – the PBX cannot tell when there is a fax going on.

Fortunately, for no additional money over the cost of the FXO card, you can arrange the sending of faxes. Receiving is also possible but I have found it unreliable. For a small fee, there are even services that can provide you with fax numbers, possibly as part of a range some of which can be used for voice. Suppose you have three spare numbers – you could arrange for one to be a fax for each of you and one for both. It is even possible to configure HylaFAX and iaxmodem to allow your own fax number to be on outbound faxes.

Assuming that you have a system running Asterisk, an account providing fax-to-email numbers and an FXO card to give you access to your domestic telephone line through Asterisk, it is fairly easy.

You will need to be root on the system for all of this unless otherwise stated.

I use Debian (currently squeeze, which is stable) and installed Asterisk, HylaFAX and iaxmodem from it.

Remember, this assumes your Asterisk system works already – if not, sort that first. Also, I have found that, with a BT line, you need to make a gain adjustment. In /etc/asterisk/dahdi-channels.conf, add the following lines before the channel => line for your FXO card:

rxgain=4
txgain=3.5

Write this down somewhere, as running dahdi_genconf will cause it to be lost. As you will soon be restarting Asterisk anyway, there is no need to restart it just yet.

After installing iaxmodem from Debian, do invoke-rc.d iaxmodem stop, then edit /etc/init.d/iaxmodem and add the line “exit 0” immediately below the first line. This setup requires that iaxmodem is started elsewhere.

Let us assume that you will call your three iaxmodems ttyIAX0, ttyIAX1 and ttyIAX2, and that ttyIAX0 will be the one with the shared number. This one will also be configured to receive calls from Asterisk just in case someone faxes your domestic telephone line.

You will need to create files of these names in /etc/iaxmodem. ttyIAX0 should look like this:

device  /dev/ttyIAX0
owner   uucp:uucp
mode    660
port    4570
refresh 60
server  127.0.0.1
peername        iaxmodem0
secret  set-a-password-here
codec   alaw

ttyIAX1:

device  /dev/ttyIAX1
owner   uucp:uucp
mode    660
port    4571
refresh 0
server  127.0.0.1
peername        iaxmodem1
secret  set-a-password-here
codec   alaw

ttyIAX2:

device  /dev/ttyIAX2
owner   uucp:uucp
mode    660
port    4572
refresh 0
server  127.0.0.1
peername        iaxmodem2
secret  set-a-password-here
codec   alaw

Also, you need to add the following lines to /etc/inittab:

IA00:23:respawn:/usr/bin/iaxmodem ttyIAX0
IA01:23:respawn:/usr/bin/iaxmodem ttyIAX1
IA02:23:respawn:/usr/bin/iaxmodem ttyIAX2
mo00:23:respawn:/usr/sbin/faxgetty ttyIAX0
mo01:23:respawn:/usr/sbin/faxgetty ttyIAX1
mo02:23:respawn:/usr/sbin/faxgetty ttyIAX2

Note: If you installed iaxmodem by compiling and installing it yourself, it will be in /usr/local/bin/iaxmodem, not /usr/bin/iaxmodem. Then, do:

init q

This will start the modems. Now run faxsetup, and press Enter on the first few questions. The first one you need to answer is:

Serial port that modem is connected to [ttyS0]? ttyIAX0

Then the following ones:

Country code [1]? 44 (this assumes that you are in the UK)
Area code []? 20 (this assumes that your phone line is in London – never mind where your fax-to-email numbers are)
Phone number of fax modem [+1.999.555.1212]? +44 20 7946 0908 (this should be your real joint fax-to-email number) (note 1)
Local identification string (for TSI/CIG)? [“NothingSetup”]? 020 7946 0908 (use the national format here)
Long distance dialing prefix [1]? 0 (assumes UK)
International dialing prefix [011]? 00 (assumes UK)

Accept the defaults on the remaining questions, unless you know you need to change them. When shown the settings, double-check and accept the “yes” answer if they are OK. Also, accept defaults for class (1) and Are these OK (yes) after the modem probing. Accept also “yes” on “Do you want to run faxaddmodem to configure another modem?”, but this time, do ttyIAX1, and make the numbers those of the first user, and again, ttyIAX2, and the numbers of the second user. Finally, answer no when asked about configuring another modem, and accept yes to start faxmodem.

Next, we have to configure Asterisk to deal with this lot. First, it is vital that fax detection on the DAHDI channels is enabled. Make sure the following setting is in /etc/asterisk/chan_dahdi.conf:

faxdetect=both

Also, the modems need accounts in iax.conf, like these:

[iaxmodem0]
type=friend
secret=the-password-you-set-here
port=4570
host=dynamic
context=fax-out
disallow=all
allow=alaw

[iaxmodem1]
type=user
secret=the-password-you-set-here
port=4571
host=dynamic
context=fax-out
disallow=all
allow=alaw

[iaxmodem2]
type=user
secret=the-password-you-set-here
port=4572
host=dynamic
context=fax-out
disallow=all
allow=alaw

You also need the following context in /etc/asterisk/extensions.conf:

[fax-out]
exten => _X.,1,Dial(DAHDI/4/${EXTEN}) (assuming your FXO port is DAHDI/4)

... and also this line in the default context which receives calls from the FXO:

exten => fax,1,Dial(IAX2/iaxmodem0)

One last thing you will want to do is set the destination of faxes that are received this way – even if it doesn't work reliably, you will want this in case it works. In /var/spool/hylafax/etc/FaxDispatch:

SENDTO=both-fax@domain (enter this as you need to and configure your mail system as appropriate)
FILETYPE=pdf (or tiff or ps if you wish)

Also, add the IP addresses of all the systems from which you will send faxes to /etc/hylafax/hosts.hfaxd. Finally, restart all the necessary programs. From the Asterisk CLI, do “core restart when convenient”, and then from the shell, /etc/init.d/hylafax restart. Go back to the Asterisk CLI (asterisk -r) and do iax2 show peers to check that iaxmodem0 is listed. If it is, all is well.

If you are installing client software other than just the client side of HylaFAX on any system, choose one that allows you to set the modem, and set it to the user's own modem. You can choose the modem as part of choosing the host in sendfax – read its man page to find out more.

All this will allow a queue of faxes – they will be sent only when the line is clear.

This works for me – I make no promises it will work for you but there is no harm in trying it.

Note 1: Observers will note that the fax number I gave for the modem above is actually a drama number – not a real telephone number.