Asterisk basic set up
Tags: beginners, asterisk, voip, videocall
Published: 2021-04-23 04:16:17

## Weekly sharing

Hi everyone, I am Ekim, a fresh Bootcamp graduate and an IT helper (I don't dare to call myself a programmer yet). Every Friday, I will share some of the work that I've done over the last week in a bid to get feedbacks from you guys and record my journey to become a programmer. 


### Introduction

Over the last week, I've been familiarizing myself with Asterisk, the open-source communications toolkit, which powers IP PBX systems, VoIP gateways, and conference servers. It is such a bitter start for a coding newbie like me, in which I struggled a lot in the installation process and making a simple phone call under the same network. This article aims at sharing a laconic work flow of the asterisk set-up and how a video call is made. 



##### Before getting started ...

I'm using WSL Ubuntu to do the Asterisk set-up. 

And before getting straight into the code, I would like to share several links that helped me a lot when I started from zero. 

- A simplified Chinese installation guide of Asterisk 17 (Thanks for Google Translate)
  - https://zh.codepre.com/how-to-12071.html
- Call making via SIP Softphone
  - https://www.youtube.com/watch?v=rtHFdhCm434&t=432s&ab_channel=RocketSystems
- Basic PBX set-up
  - https://wiki.asterisk.org/wiki/display/AST/Basic+PBX+Functionality
- Asterisk firewall rules
  - https://www.voip-info.org/asterisk-firewall-rules/



### Get started! 

To start with, as always,  

``` bash
sudo su		# enter the superuser mode 
apt update
apt upgrade
apt autoclean
```



### How to install Asterisk ?

Installing Asterisk 18

```bash 
cd /var/local/	# Go to /var/local as the download location
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-18-current.tar.gz	# Download Asterisk 18
tar zxf asterisk-18-current.tar.gz		# Unzip it
apt install flex bison subversion		# Install flex, bison, and subversion
contrib/scripts/get_mp3_source.sh		# Download the mp3 decoder save it in ./asterisk 18.xxx/contrib/scripts
contrib/scripts/install_prereq install	# Ensure all dependencies are set up
./configure		# Set up the asterisk based on the above
make menuconfig	# Choose the modules you need
make -j 3 		# Allocate cpu cores to run the installation
install		# Install all the things we've just set up
```



### Softphone recommendation

I use MicroSIP and Linphone to make calls on Windows.  



### Connecting softphone to WSL Ubuntu

#####  Asterisk Firewall Rules

- Config of Linux IPTables firewall 	(https://www.voip-info.org/asterisk-firewall-rules/)

  ```bash
  # SIP on UDP port 5060. Other SIP servers may need TCP port 5060 as well
  iptables -A INPUT -p udp -m udp --dport 5060 -j ACCEPT
  
   # IAX2- the IAX protocol
  iptables -A INPUT -p udp -m udp --dport 4569 -j ACCEPT
  
   # IAX - most have switched to IAX v2, or ought to
  iptables -A INPUT -p udp -m udp --dport 5036 -j ACCEPT
  
   # RTP - the media stream
   # (related to the port range in /etc/asterisk/rtp.conf) 
  iptables -A INPUT -p udp -m udp --dport 10000:20000 -j ACCEPT
   
  
   # MGCP - if you use media gateway control protocol in your configuration
  iptables -A INPUT -p udp -m udp --dport 2727 -j ACCEPT
  ```

  

### Basic calling set-up

- Going to set our configs.

  ``` bash
  cd /etc/asterisk
  nano pjsip.conf
  ```

- The below is the pjsip.conf for basic calling (Account creation). 

  ``` conf
  ;================================ TRANSPORTS ==
  ; Our primary transport definition for UDP communication behind NAT.
  [transport-udp-nat]
  type = transport
  protocol = udp
  bind = 0.0.0.0
  ;================================ CONFIG FOR SIP ITSP ==
  
  [calling](!)				      ; template
  type=endpoint				      ; specify the below are the configurations related to an endpoint (a device)
  context=interaction			  ; *** it refers to the context set in the dialplan (extensions.conf) ***
  allow = !all, ulaw, alaw	; do not allow all codecs and only allow audio codecs - ulaw and alaw
  direct_media=no				    ; do not allow two devices directly talking to each other
  trust_id_outbound=yes
  rtp_symmetric=yes
  force_rport=yes
  rewrite_contact=yes
  device_state_busy_at=1
  dtmf_mode=rfc4733
  
  [auth-userpass](!)		    ; template 
  type = auth				        ; specify the below are the configurations related to authentication
  auth_type = userpass	    ; specify the type of the authentication is based on username and password
  
  [aor-single-reg](!)		    ; template
  type = aor
  max_contacts = 1		      ; specify the maximum sip addresses allocated. Here, that means each sip address could only connect to one device.
  
  [7000](calling)			      ; endpoint 7000 inherits the settings in calling template
  auth=7000				          ; authentication = 7000
  aors=7000				          ; address of record = 7000
  callerid = 7000 <7000>	  ; caller's id = 7000
  
  [7000](auth-userpass)	    ; account creation for endpoint 7000
  password = 7000
  username = 7000
  
  [7000](aor-single-reg)
  
  [7100](calling)
  auth=7100
  aors=7100
  callerid = 7100 <7100>
  
  [7100](auth-userpass)
  password = 7100
  username = 7100
  
  [7100](aor-single-reg)
  ```
  
  
  
- The below are the configurations of the extensions.conf (Dialplan)

  ```conf
  [globals]
  INTERNAL_DIAL_OPT=,30
  
  [interaction]		                  ; refers to the `context=interaction` in calling template in pjsip.conf
  exten = 7000,1,Answer()		        ; the first argument 7000 refers to the number you dial on your softphone. 
  same = n,Dial(PJSIP/7000,60)	    ; PJSIP/7000 refers to the endpoint 7000 , 60 means waiting for 60 seconds
  same = n,Playback(vm-nobodyavail)
  same = n,Voicemail(7000@main)
  same = n,Hangup()
  
  exten = 7100,1,Answer()
  same = n,Dial(PJSIP/7100,60)
  same = n,Playback(vm-nobodyavail)
  same = n,Voicemail(7000@main)
  same = n,Hangup()
  
  ; same means following the same extension
  ; n means the next action. 
  ```

  

- After setting the pjsip.conf and extensions.conf, you need to reload the pjsip and dialplan. 

  ```bash
  asterisk -r	# go to the CLI of Asterisk (command line interface)
  ```

  ```bash
  core reload		# reload pjsip and modules
  
  dialplan reload		# reload dialplan
  
  pjsip show endpoints # showing the endpoints you have
  ```

  

- The below is how you set up on the MicroSIP softphone

  ![microsip-set-up](https://drive.google.com/uc?export=view&id=1IyRFrFOInZC7gDlomfAgw1222aV4o4rF)

  

  - Do the same on another softphone for the 7100 endpoint. 

  - Then call 7100 on 7000 endpoint / 7000 on 7100 endpoint to each other. 

    

- Why I can call certain numbers on my device and can ring another device ? 

  - https://wiki.asterisk.org/wiki/display/AST/Creating+Dialplan+Extensions
  - This is because we've done the set-up in the extensions.conf, in which we've set a number, for example, 6001, in the illustration below, for the dial action. 

  ```conf
                  [from-internal]
                  exten=>6001,1,Dial(SIP/demo-alice,20)
                  exten=>6002,1,Dial(SIP/demo-bob,20)
  ```

  

### Video calling

- To achieve video calling, you only need to add the video codec in the pjsip.conf to make it work. And there's no other differences from the basic calling settings. 

  ``` conf
  [calling](!)
              type=endpoint
              context=interaction
              allow = !all, ulaw, alaw, vp8, h263, h263p		; video codec vp8, h263 and h263p are added 
              direct_media=no
              trust_id_outbound=yes
              rtp_symmetric=yes
              force_rport=yes
              rewrite_contact=yes
              device_state_busy_at=1
              dtmf_mode=rfc4733
  ```

- After changing the pjsip.conf, remember to reload the pjsip like the above again. 



### Conclusion

- By now, I hope you could achieve video calling in your own network. In the meantime, stay healthy and stay tuned for more content !!!