pjsip - sip 設定及realtime設定

最後更新: 2015-07-20

介紹

Asterisk 12 and later versions contain two SIP stacks

pjproject (Asterisk 12 dynamically links to pjproject.)

open source multimedia communication library written in C language implementing standard based protocols such as SIP, SDP, RTP, STUN, TURN, and ICE.

It combines signaling protocol (SIP) with rich multimedia framework and NAT traversal functionality into high level API

# 測試平台: Ubuntu 14

一個 SIP 有以下元素

- Section Names
- ENDPOINT (auth, aor and transport)
- TRANSPORT
- AUTH
- AOR
- CONTACT - acts as an alias for a SIP URIs and holds information about an inbound registrations
- REGISTRATION - outbound registration
- IDENTIFY - determines what endpoint an incoming packet is from
- ACL
 

Object

 

ENDPOINT

    Many ENDPOINTs can be associated with many AORs
    Zero to many ENDPOINTs can be associated with zero to one AUTHs
    Zero to many ENDPOINTs can be associated with at least one TRANSPORT
    Zero to one ENDPOINTs can be associated with an IDENTIFY

AOR

    Many ENDPOINTs can be associated with many AORs
    Many AORs can be associated with many CONTACTs

    AOR objects (Address of Record)
    
    tell Asterisk where an endpoint can be contacted.
    - mailboxes for MWI
    - expiration
    - qualify

CONTACT

    The contact config object effectively acts as an alias for a SIP URIs
    and holds information about an inbound registrations.

    Contacts are created automatically upon registration to an AOR,
    or can be created manually by using the "contact=" config option

Compile

* Asterisk will not use the embedded third party libraries within pjproject.

options

--enable-shared
--prefix
-with-external-srtp

./configure --prefix=/usr --enable-shared --disable-sound --disable-resample --disable-video --disable-opencore-amr CFLAGS='-O2 -DNDEBUG'

make dep
make
make install
ldconfig

ldconfig -p | grep libpj

# Asterisk

./configure

make menuselect

Resource Modules category -> res_pjsip modules

 

Checking

module show like res_pjsip

 


設定一個 transport (Section Names, TRANSPORT)

[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0

設定 endpoints

[509]
type=endpoint
context=internal
transport=transport-udp
disallow=all
allow=ulaw
aors=509
auth=auth-509

設定 aor

[509]
type=aor
max_contacts=1

設定 auth

[auth-509]
type=auth
auth_type=userpass
password=1234
username=509

Connect 了的 peer 才有 contacts

pjsip show contacts

 

 


Fail and re-try

 

When a temporary failure occurs, Asterisk may re-attempt registering if a retry_interval is configured in the outbound registration.

retry_interval=60
max_retries=10

The forbidden_retry_interval can be set such that if Asterisk receives a "403 Forbidden" response from a registrar,

Asterisk can wait the number of seconds indicated and re-attempt registration.

forbidden_retry_interval = 300

 


安裝 Alembic

Alembic is a full database migration tool, with support for upgrading the schemas of existing databases,

versioning of schemas, creation of new tables and databases, and a whole lot more. 

- python-dev
- python-pip
- python-mysqldb

Install 必須的 Package

apt-get install unixodbc unixodbc-dev libmyodbc python-dev python-pip python-mysqldb

pip install alembic

resource modules

 - res_config_odbc
 - res_odbc

建立 Schema

cd contrib/ast-db-manage/
cp config.ini.sample config.ini
config.ini
sqlalchemy.url = mysql://root:password@localhost/asterisk

# setup the database tables

alembic -c config.ini upgrade head

Configuring ODBC

# First, we'll tell ODBC how to connect to MySQL

/etc/odbcinst.ini

[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/x86_64-linux-gnu/odbc/libmyodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libodbcmyS.so
UsageCount = 2

# tell ODBC which MySQL database to use

/etc/odbc.ini

[asterisk]
Driver = MySQL
Description = MySQL connection to ‘asterisk’ database
Server = localhost
Port = 3306
Database = asterisk
UserName = root
Password = password
Socket = /var/run/mysqld/mysqld.sock

# configure Asterisk's ODBC resource

/etc/asterisk/res_odbc.conf

; context name
[asterisk]
enabled => yes

; 對應 /etc/odbc.ini
dsn => asterisk

; Default 係 " context name / blank"
username => root
password => password

; Build a connection at startup
pre-connect => yes

; execute to ensure that our connection is still alive
sanitysql => select 1

; 對應 mysql 的 "wait_timeout=60"
idlecheck => 3600

; we use a single connection for all queries
share_connections => yes

; attempt to connect before considering the connection dead
connect_timeout => 3

; When a connection fails, how long (in seconds) should we cache that information
negative_connection_cache => 300

Checking

odbc show

ODBC DSN Settings
-----------------

  Name:   asterisk
  DSN:    asterisk
    Last connection attempt: 1970-01-01 08:00:00
  Pooled: No
  Connected: Yes

 


Aserisk 設定

 

/etc/asterisk/modules.conf

preload => res_odbc.so
preload => res_config_odbc.so
noload => chan_sip.so

PJSIP stack uses a new data abstraction layer in Asterisk called sorcery

/etc/asterisk/sorcery.conf

[res_pjsip]
endpoint=realtime,ps_endpoints
auth=realtime,ps_auths
aor=realtime,ps_aors
domain_alias=realtime,ps_domain_aliases
contact=realtime,ps_contacts
 
[res_pjsip_endpoint_identifier_ip]
identify=realtime,ps_endpoint_id_ips

; {object_type} = {sorcery_wizard_name},{wizard_arguments}

; Wizards
;
; Wizards are the persistence mechanism for objects. They are loaded as Asterisk modules and register
; themselves with the sorcery core. All implementation specific details of how objects are persisted is isolated
; within wizards.

Configuring sorcery for realtime and non-realtime data sources

sorcery.conf

[res_pjsip]
endpoint=config,pjsip.conf,criteria=type=endpoint
endpoint=realtime,ps_endpoints

auth=config,pjsip.conf,criteria=type=auth
auth=realtime,ps_auths

aor=config,pjsip.conf,criteria=type=aor
aor=realtime,ps_aors

* You can swap the order to control which data source is read first.

Tell Asterisk to use a "database backend" with the object types

# and not just the flat pjsip.conf file.

extconfig.conf

[settings]
ps_endpoints => odbc,asterisk
ps_auths => odbc,asterisk
ps_aors => odbc,asterisk
ps_domain_aliases => odbc,asterisk
ps_endpoint_id_ips => odbc,asterisk
ps_contacts => odbc,asterisk

PJSIP configuration

/etc/asterisk/pjsip.conf
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0

Endpoint Population

# mysql -u root -p -D asterisk;
insert into ps_aors (id, max_contacts) values (501, 1);
insert into ps_aors (id, max_contacts) values (502, 1);
insert into ps_auths (id, auth_type, password, username) values (501, 'userpass', 1234, 501);
insert into ps_auths (id, auth_type, password, username) values (502, 'userpass', 1234, 502);
insert into ps_endpoints (id, transport, aors, auth, context, disallow, allow, direct_media) 
 values (501, 'transport-udp', '501', '501', 'testing', 'all', 'g722', 'no');
insert into ps_endpoints (id, transport, aors, auth, context, disallow, allow, direct_media) 
 values (502, 'transport-udp', '502', '502', 'testing', 'all', 'g722', 'no');
quit;

Checking

pjsip show endpoints

 Endpoint:  501                                                  Unavailable   0 of inf
     InAuth:  501/501
        Aor:  501                                                1
  Transport:  transport-udp             udp      0      0  0.0.0.0:5060

A Little Dialplan

/etc/asterisk/extensions.conf

[testing]
exten => _5XX,1,NoOp()
same => n,Dial(PJSIP/${EXTEN})

 


Command

  • pjsip list ???s

 

# 查看有什麼 login

pjsip show auths

<AuthId/UserName>

Auth:  auth-509/509

# 查看 login 的資料

pjsip show auth auth-509

ParameterName  : ParameterValue
 ===============================
 auth_type      : userpass
 md5_cred       :
 nonce_lifetime : 32
 password       : 1234
 realm          :
 username       : 509

# endpoints

# 有 auth 唔代表有 endpoints

pjsip show endpoints

State

- Not in use
- Unavailable

# transports

pjsip show transports

# identifies

pjsip show identifies

# Manually register 與 unregister

pjsip list registrations

<Registration/ServerURI..............................>  <Auth..........>  <Status.......>

output

mytrunk/sip:x.y.z                       mytrunk       Registered

pjsip send unregister <registration name>

pjsip send register <registration name>

 


一架電話機的設定

;==============TRANSPORTS
[transport-udp]
type=transport
protocol=udp
bind=0.0.0.0

;============== endpoints
[509]
type=endpoint
context=internal
transport=transport-udp
disallow=all
allow=ulaw,gsm
aors=509
auth=auth-509

[509]
type=aor
max_contacts=1

[auth-509]
type=auth
auth_type=userpass
password=1234
username=509

 

 


TRUNK Setup

 

[mytrunk]
type=registration
transport=transport-udp-nat
outbound_auth=auth_mytrunk
server_uri=sip:y.z
client_uri=sip:[email protected]
retry_interval=60
expiration=600

[mytrunk_endpoint]
type=endpoint
transport=transport-udp
context=from-external
disallow=all
allow=ulaw,alaw,gsm
outbound_auth=mytrunk_auth
aors=aor_mytrunk
direct_media=no

[aor_mytrunk]
type=aor
max_contacts=1
contact=sip:y.z:5060

[auth_mytrunk]
type=auth
auth_type=userpass
username=xxxxxxxx
password=????????

[mytrunk_identify]
type=identify
endpoint=mytrunk_endpoint
match=202.130.146.120

 


Dialing from dialplan

 

; look at the AOR of the same name as the endpoint and start dialing the first contact associated.
exten => _6XXX,1,Dial(PJSIP/${EXTEN})

; To dial all the contacts associated with the endpoint
exten => _6XXX,1,Dial(${PJSIP_DIAL_CONTACTS(${EXTEN})})

; dial with an explicit SIP URI
exten => _9NXXNXXXXXX,1,Dial(PJSIP/mytrunk/sip:${EXTEN:1}@203.0.113.1:5060)

exten => _9NXXNXXXXXX,1,Dial(PJSIP/${EXTEN:1}@mytrunk)

ACL

 

independent of endpoints and operates on all inbound SIP communication

 - permit and deny
 - contactpermit and contactdeny (Contact header addresses in incoming REGISTER requests)

acl.conf

; List of IP ACL section names in acl.conf
acl

; List of Contact ACL section names in acl.conf
contact_acl

 


NAT

 

; we won't apply any NAT-related settings
local_net=

; the media address in the SDP will be rewritten to
external_media_address=

; like the "external_media_address" setting
external_media_address=

; Determines whether media may flow directly between endpoints
; "direct_media=no" ensures Asterisk stays in the media path
direct_media=no

Configure

[transport-udp-nat]
type=transport
protocol=udp
bind=0.0.0.0
local_net=192.168.88.0/24
local_net=127.0.0.1/32
external_media_address=x.x.x.x
external_signaling_address=x.x.x.x

[mytrunk]
type=registration
transport=transport-udp-nat
outbound_auth=auth_mytrunk
server_uri=sip:y.z
client_uri=sip:[email protected]
retry_interval=60

[mytrunk_endpoint]
type=endpoint
transport=transport-udp
context=from-external
disallow=all
allow=ulaw,alaw,gsm
outbound_auth=mytrunk_auth
aors=mytrunk_aor
direct_media=no

 


DOC

 

https://wiki.asterisk.org/wiki/display/AST/Asterisk+12+Configuration_res_pjsip

https://wiki.asterisk.org/wiki/display/AST/PJSIP+Configuration+Sections+and+Relationships

https://wiki.asterisk.org/wiki/display/AST/Asterisk+12+Configuration_res_pjsip

 

 

 

 

 

 

Creative Commons license icon Creative Commons license icon