[globals]
RADIUS_Server=Master.ip.adress
RADIUS_Secret=password
RADIUS_Auth_Port=1812
RAIUS_Acct_Port=1813
Acct_Update_Timeout=60 
NAS_IP_Address=your.ip.adress
II=0
max_duration=7200

[general]
static=yes
autofallthrough=yes

[forbidden]
exten => _X.,1,NoOp(-- No context for host ${SIPCHANINFO(recvip)} --)
exten => _X.,n,Hangup(21)

[auth-ip]
exten => _X.,1,NoOp(-- Inbound Authentication for cusromer IP --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(CDR(accountcode)=${SIPCHANINFO(recvip)})
exten => _X.,n,Set(MYCALLID=${SIPCALLID})
exten => _X.,n,Set(CDR(CustomerType)=IpAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=Account&Password=cisco&H323_ID=${H323_ID}&CustomerType=IpAuth
exten => _X.,n,Goto(routing,${EXTEN},1)

[auth-inbound]
exten => _X.,1,NoOp(-- Inbound Authentication --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(CDR(accountcode)=${SIPCHANINFO(recvip)})
exten => _X.,n,Set(MYCALLID=${SIPCALLID})
exten => _X.,n,Set(CDR(CustomerType)=PeerAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=Account&Password=cisco&H323_ID=${H323_ID}&CustomerType=PeerAuth
exten => _X.,n,Goto(routing,${EXTEN},1)

[sip-auth]
exten => _X.,1,NoOp(-- SIP Authentication --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(SIP_Authorization=${SIP_HEADER(Proxy-Authorization)}) 
exten => _X.,n,Set(CDR(CustomerType)=SipAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=SIP&IfFailed=DoNotHangup&H323_ID=${H323_ID}&CustomerType=SipAuth
exten => _X.,n,Set(CDR(accountcode)=${SIP_Username})
exten => _X.,n,Set(CDR(disconnect-reason)=${h323-return-code})
exten => _X.,n,GotoIf($[ ${h323-return-code} = 1 | ${h323-return-code} = 2 ]?Invalid-Account,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 4 | ${h323-return-code} = 5 | ${h323-return-code} = 6 | ${h323-return-code} = 7 ]?blocked,${EXTEN},1)
exten => _X.,n,Goto(routing,${EXTEN},1)

[h323-id]
exten => _X.,1,NoOp(-- H323 ID --)
exten => _X.,n,Set(ID=${SIP_HEADER(h323-conf-id)})
exten => _X.,n,ExecIf($[ "${ID}" = "" ],Set,ID=${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)})
exten => _X.,n,Set(H323_ID=${ID}) 
exten => _X.,n,Set(H323_ID_HEX=${MATH(${CUT(ID,,1)}+0,hex)} ${MATH(${CUT(ID,,2)}+0,hex)} ${MATH(${CUT(ID,,3)}+0,hex)} ${MATH(${CUT(ID,,4)}+0,hex)})
exten => _X.,n,SIPAddHeader(h323-conf-id: ${H323_ID})
exten => _X.,n,Set(CDR(h323id)=${H323_ID_HEX})
exten => _X.,n,Return()

[routing]
exten => _X.,1,Set(II=1)
exten => _X.,n(loop),NoOp()
exten => _X.,n,Set(KK=0)
exten => _X.,n,Set(SUM_ROUTE=${XSUM_${II}})
exten => _X.,n(loop1),NoOp()
exten => _X.,n,Set(TARGET_ROUTE=${XROUTE_${II}_${KK}})
exten => _X.,n,Set(TARGET_EXPIRE=${XEXP_${II}_${KK}})
exten => _X.,n,Set(TARGET_CLI=${XCLI_${II}_${KK}})
exten => _X.,n,Set(CALLERID(all)=${TARGET_CLI} <${TARGET_CLI}>)
exten => _X.,n,Set(MYDNID=${CUT(TARGET_ROUTE,@,1)})
exten => _X.,n,Set(VDNID=${CUT(MYDNID,/,2)})
exten => _X.,n,Ringing
exten => _X.,n,Dial(${IF($[ ${CUT(TARGET_ROUTE,@,2)} = ${NAS_IP_Address}]?${MYDNID}:${TARGET_ROUTE})},${TARGET_EXPIRE})
exten => _X.,n,Set(KK=$[${KK}+1])
exten => _X.,n,GotoIf($[$["${DIALSTATUS}" = "BUSY"] | $["${DIALSTATUS}" = "NOANSWER"]]?end1)
exten => _X.,n,GotoIf($[${KK} < ${SUM_ROUTE} ]?loop1:end1)
exten => _X.,n(end1),NoOp()
exten => _X.,n,Set(II=$[${II}+1])
exten => _X.,n,GotoIf($[${II} < ${XTOT} ]?loop:end)
exten => _X.,n(end),NoOp()
exten => _X.,n,Hangup(17)

;TODO check other return codes
[blocked]
exten => _X.,1,Playback(FR-invalid-Account)
exten => _X.,n,Hangup()

[Invalid-Account]
exten => _X.,1,Playback(FR-login-or-pwd-incorrect)
exten => _X.,n,Hangup()
