Two SIP phones
The smallest PBX example is a two SIP phone setup. We create one with the extension 2000 and the other one with the extension 2001. Each phone should be able to call the other one by dialing the other's extension.
The first SIP account is 2000 with the password 1234. The second SIP account is 2001 with the password 1234. Please set up your SIP phones with these accounts.
Asterisk configuration
Asterisk stores its SIP account information in the configuration file /etc/asterisk/sip.conf
. Please replace the default sip.conf
with a new one that contains the following code:
[general] port=5060 bindaddr=0.0.0.0 [2000] type=friend secret=1234 context=default host=dynamic [2001] type=friend secret=1234 context=default host=dynamic
The dialplan is stored in /etc/asterisk/extensions.conf
. Please replace it with this code:
[default] exten => _200[1-2],1,Dial(SIP/${EXTEN})
As we bring up Asterisk (asterisk -c
) and set the verbose level to 3
(core set verbose 3
) we can see how the phones register:
*CLI> -- Registered SIP '2000' at 10.0.0.21:2048 -- Registered SIP '2001' at 10.0.0.20:3072 -- Unregistered SIP '2001' -- Registered SIP '2001' at 10.0.0.20:3072
Now we can make a phone call from one phone to the other. During such a call the command sip show channels
will display some basic information about the current call:
*CLI> sip show channels Peer User/ANR Call ID Format Hold Last Message Expiry Peer 10.0.0.21 2000 150b1e3879a2bff (ulaw) No Tx: ACK 2000 10.0.0.20 2001 ea88263cebdd-la (ulaw) No Tx: ACK 2001 2 active SIP dialogs
Now that we've established a call on Asterisk, let's do the equivalent on a FreeSWITCH. If necessary shut down Asterisk with the core stop now
command.
FreeSWITCH configuration
FreeSWITCH doesn't have a fixed file and directory structure. The structure you see in /usr/local/freeswitch/conf
is just an example. You could put everything into one XML file or separate it into numerous XML files all named as you like. The sample configuration already contains a couple of example SIP accounts and demo extensions. However, we want to recreate the preceding Asterisk example.
Create the file /usr/local/freeswitch/conf/directory/default/2000.xml
with the following content for your first SIP account:
<include> <user id="2000"> <params> <param name="password" value="1234"/> </params> <variables> <variable name="user_context" value="default"/> </variables> </user> </include>
Then create a second file, /usr/local/freeswitch/conf/directory/default/2001.xml
, with the configuration for the second SIP account:
<include> <user id="2001"> <params> <param name="password" value="1234"/> </params> <variables> <variable name="user_context" value="default"/> </variables> </user> </include>
Finally, let's create a new Dialplan file for our two new SIP accounts. Create the new file, /usr/local/freeswitch/conf/dialplan/default/01_New.xml
, with this Dialplan content:
<?xml version="1.0" encoding="utf-8"?> <include> <context name="default"> <extension name="Local_Extension"> <condition field="destination_number" expression="^(200[1-2])$"> <action application="export" data="dialed_extension=$1"/> <action application="bridge" data="user/${dialed_extension}@${domain_name}"/> </condition> </extension> </context> </include>
After you have saved these three files, start FreeSWITCH:
/usr/local/freeswitch/bin/freeswitch –nc
Wait a few moments for the FreeSWITCH process to start, then connect to it with fs_cli
:
/usr/local/freeswitch/bin/fs_cli
Make sure that your two SIP phones are on and have attempted to register. (You will need to restart them or perform a re-registration if they were recently connected to your Asterisk server.) To see the status of your registrations, issue this command:
sofia status profile internal reg
The output will be like this:
freeswitch@internal> sofia status profile internal reg Registrations: Call-ID: a270263caa23-uocan9j61z5y User: 2000@127.0.0.1 Contact: "2000" <sip:2000@10.0.0.20:3072;line=0tqusdnm> Agent: snom821/8.4.35 Status: Registered(UDP)(unknown) EXP(2013-01-13 06:46:31) EXPSECS(3529) Host: debian IP: 10.0.0.20 Port: 3072 Auth-User: 2000 Auth-Realm: 10.0.0.10 MWI-Account: 2000@127.0.0.1 Call-ID: 3c26708e4d57-yzfzr61f7x4l User: 2001@127.0.0.1 Contact: "2001" <sip:2001@10.0.0.21:2048;line=9r6kyu0i> Agent: snom360/8.4.35 Status: Registered(UDP)(unknown) EXP(2013-01-13 06:46:45) EXPSECS(3543) Host: debian IP: 10.0.0.21 Port: 2048 Auth-User: 2001 Auth-Realm: 10.0.0.10 MWI-Account: 2001@127.0.0.1 Total items returned: 2 freeswitch@internal>
Now you can make a call from 2000 to 2001 and vice versa.
You can analyze the used channels during the call with show channels
. Make a call between the two phones, and then issue the command show channels
. You will see output like this:
freeswitch@internal> show channels uuid,direction,created,created_epoch,name,state,cid_name,cid_num,ip_addr,dest,application,application_data,dialplan,context,read_codec,read_rate,read_bit_rate,write_codec,write_rate,write_bit_rate,secure,hostname,presence_id,presence_data,callstate,callee_name,callee_num,callee_direction,call_uuid,sent_callee_name,sent_callee_num af6dc664-5cb3-11e2-ae64-41a8c0d6e735,inbound,2013-01-12 13:29:18,1357993758,sofia/internal/2000@10.0.0.10,CS_EXECUTE,2000,2000,10.0.0.20,2001,bridge,user/2001@127.0.0.1,XML,default,PCMU,8000,64000,PCMU,8000,64000,,debian,2000@10.0.0.10,,ACTIVE,Outbound Call,2001,SEND,af6dc664-5cb3-11e2-ae64-41a8c0d6e735,Outbound Call,2001 af861bd8-5cb3-11e2-ae6d-41a8c0d6e735,outbound,2013-01-12 13:29:18,1357993758,sofia/internal/sip:2001@10.0.0.21:2048,CS_EXCHANGE_MEDIA,2000,2000,10.0.0.20,2001,,,XML,default,PCMU,8000,64000,PCMU,8000,64000,,debian,2001@127.0.0.1,,ACTIVE,Outbound Call,2001,SEND,af6dc664-5cb3-11e2-ae64-41a8c0d6e735,2000,2000 2 total. freeswitch@internal>
You have set up SIP users on both Asterisk and FreeSWITCH.
Tip
If you edit a FreeSWITCH XML configuration file while FreeSWITCH is running, be sure to execute the command reloadxml
. Alternatively, you can press the F6 key.
Analysis
FreeSWITCH uses XML and Asterisk uses traditional "ini" files. XML has the big advantage that it can easily be checked for syntax errors. Asterisk is somewhat loose in this area and in some cases does not give enough feedback to the system administrator. On several occasions we had an Asterisk Dialplan that looked fine and which worked most times but in some edge cases it didn't. Many times this was because of syntax errors in the Dialplan that weren't found by Asterisk itself. So having a strict XML configuration is a good thing, but it does take some time to get used to it. A good XML editor might be helpful in this transition phase.
Tip
A text editor with syntax highlighting is invaluable when reviewing or editing FreeSWITCH configuration files.
The syntax of the SIP account definitions is quite different between these two pieces of software. The Dialplan is also very different. We defined the SIP accounts with a default context (literally "default") for Asterisk and FreeSWITCH. Each software searches for a default
context in its configurations. Asterisk uses the configuration file extensions.conf
for that and a customized syntax. Within the [default]
context it searches for a matching extension. The regular expression _200[1-2]
matches the dialed number and starts the Dial application, which initiates a call with the SIP protocol to ${EXTEN}
, which is a variable that was set automatically by Asterisk and contains the dialed number.
FreeSWITCH searches for the default
context too (because the SIP accounts were defined within this context). It runs through all defined extensions within this context until it finds one that has a matching condition
field. Conditions can be many things such as a time or as in this example a destination_number
that is matched with the regular expression ^(200[1-2])$
. A condition
itself contains code that is fired up when the condition is true. In our example it is this code:
<action application="export" data="dialed_extension=$1"/> <action application="bridge" data="user/${dialed_extension}@${domain_name}"/>
We could write it in one line like this:
<action application="bridge" data="user/$1@${domain_name}"/>
But setting a dialed_extension
variable is often very handy. It might remind the Asterisk veteran of ${EXTEN}
. Also, we can see that FreeSWITCH's bridge
is the equivalent of Asterisk's Dial
application. In FreeSWITCH we see a reference to @${domain_name}
in the bridge
argument. The channel variable ${domain_name}
is set elsewhere in the example configuration.
More information is presented in Chapter 5, Understanding the XML Dialplan, and in Chapter 8, Advanced Dialplan Concepts.