How to configure a backup SIP trunk

Hi all,

My SIP trunk provider offers me 2 SIP accounts on 2 different servers. My question is how to change sip.conf to have a failover ot the second SIP server when the first one is out. I can register to both servers at the same time, but I can only use one context in sip.conf to deal with calls.

Thanks!

BC

You do the fallback in the dialplan, not in sip.conf, and you should be thinking of using chan_pjsip, as chan_sip will be completely removed in about 2 version’s time.

here is my code for having multiple trunks
you just need to setup an accounts for each of your providers servers

alternative look into if the provider are using SRV record, assuming that the username/password are identical on the different servers
as that is supported with PJSIP

same => Set(__NoFailoverCause=1,16,17,18,19,20,21,22,28,127)
                                ; 1     UNALLOCATED                                                     
                                ; 16    NORMAL_CLEARING
                                ; 17    USER_BUSY
                                ; 18    NO_USER_RESPONSE
                                ; 19    NO_ANSWER
                                ; 20    SUBSCRIBER_ABSENT
                                ; 21    CALL_REJECTED
                                ; 22    NUMBER_CHANGED
                                ; 28    INVALID_NUMBER_FORMAT
                                ; 127   INTERWORKING
same => n,Set(Trunk=Connection1,Connection2,Connection3)
same => n,Goto(Trunk,${EXTEN},1)

[Trunk] ;loadbalacing and failover
exten => _[+0-9*#s]!,1,Set(Num=${FIELDQTY(Trunk,\,)})
same => n,Set(CHANNEL(hangup_handler_push)=Hangup_Handler,${FILTER(+1234567890#*,${EXTEN})},1)          ; enable that we show hangup reason in the log
same => n,ExecIf($[${EXISTS(${PJSIP_HEADER(read,Privacy)})} & "${PJSIP_HEADER(read,Privacy)}" != "none"]?Set(_Number_pres="prohib")     ; Privacy: id;user;header
same => n,Set(Count=0)
same => n,Set(Current=${RAND(1,${Num})})                                                                ; select a random place to start (load balacing)
same => n,While($[${INC(Count)} <= ${Num}])
same => n,Set(Current=$[${MATH(${Current}%${Num},i)}+1])                                                ; fancy modulus stuff to loop the list of hosts
same => n,ExecIf($["${DEVICE_STATE(PJSIP/${CUT(Trunk,\,,${Current})})}"="UNAVAILABLE"]?ContinueWhile()) ; check where the device in responding to option
same => n,ExecIf($["${DEVICE_STATE(PJSIP/${CUT(Trunk,\,,${Current})})}"="INVALID"]?ContinueWhile())     ; check that the device exists
same => n,Dial(PJSIP/${EXTEN}@${CUT(Trunk,\,,${Current})},${DialTimeout},iS(${MaxCallTime})b(Dial_Handler^${EXTEN}^1))
same => n,ExecIf($["${NoFailoverCause}"!="${LISTFILTER(NoFailoverCause,",",${HANGUPCAUSE})}"]?ExitWhile())      ; check if we should failover or not
same => n,EndWhile()
same => n,ExecIF($["${HANGUPCAUSE}"!="0"]?Hangup(${HANGUPCAUSE}):Hangup(38))                            ; 38    NETWORK_OUT_OF_ORDER

Thanks David, I will look into that. I’m much more familiar with chan_sip but it’s clear that it will be replaced as it had a noload line in modules.conf

BC

Thanks Mark!

Looks quite complicated, will have to study that code a bit,

BC

if you only want failover

[Trunk] ;failover
exten => _[+0-9*#s]!,1,While(${SET(Current=${SHIFT(Trunk)})})
same => n,ExecIf($["${DEVICE_STATE(PJSIP/${Current})}"="UNAVAILABLE"]?ContinueWhile()) ; check where the device in responding to option
same => n,ExecIf($["${DEVICE_STATE(PJSIP/${Current})}"="INVALID"]?ContinueWhile())     ; check that the device exists
same => n,Dial(PJSIP/${EXTEN}@${Current},${DialTimeout},iS(${MaxCallTime})b(Dial_Handler^${EXTEN}^1))
same => n,ExecIf($["${NoFailoverCause}"!="${LISTFILTER(NoFailoverCause,",",${HANGUPCAUSE})}"]?ExitWhile())      ; check if we should failover or not
same => n,EndWhile()
same => n,ExecIF($["${HANGUPCAUSE}"!="0"]?Hangup(${HANGUPCAUSE}):Hangup(38))                            ; 38    NETWORK_OUT_OF_ORDER

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.