Documenting Problems That Were Difficult To Find The Answer To

Capturing Incoming SIP Address With Asterisk

When a call comes into your Asterisk server via a SIP trunk or just over SIP it will usually have ${CALLERID(num)} set to the incoming number if that call originated from the plain old telephony system (POTS).

However how do you capture caller ID from a SIP address such as

Well, in this case ${CALLERID(num)} will contain the text john.smith – but getting the domain name is trickier.

In actual fact the domain will be found in the ${CHANNEL} variable looking something like SIP/ Now you can use the ${CUT(CHANNEL,-,2)} in a vain hope to trim off that hexadecimal number at the end – but this will fail miserably if there is a dash in the domain name (a perfectly legal thing to do).

Instead we need to define a macro that will trim off everything up to the right-most dash. The following will do this and store the result in ${trimresult}:

exten => s,1,NoOp()
  same => n,Verbose(parameter is <${ARG1}>)
  same => n(loop),NoOp()
  same => n,GotoIf($["${ARG1}" = ""]?end)
  same => n,GotoIf($[${LEN(${ARG1})} < 2]?end)
  same => n,GotoIf($["${ARG1:-1}" = "-"]?founddash)
  same => n,Set(ARG1=${ARG1:0:$[${LEN(${ARG1})}-1]})
  same => n,Goto(loop)
  same => n(founddash),Set(ARG1=${ARG1:0:$[${LEN(${ARG1})}-1]})
  same => n(end),Set(trimresult=${ARG1})

Now we can define a macro that filters our incoming caller ID to prevent any nasty characters from having side effects when creating files or calling system binaries (result is in ${FILTERED_ID}):

exten => s,1,NoOp()
  same => n,GotoIf($["${CUT(CHANNEL,/,1)}" != "SIP"]?notsip)
  same => n,Macro(trim-to-dash,${CHANNEL})
  same => n,Set(sipdomainin=${CUT(trimresult,/,2)})
  same => n,Set(FILTERED_ID=${FILTER(0-9a-zA-Z@._ +,${CALLERID(num)}@${sipdomainin})})
  same => n,Goto(end)
  same => n(notsip),NoOp()
  same => n,GotoIf($["${CALLERID(num)}" = "s"]?nonum)
  same => n,GotoIf($["${CALLERID(num)}" = ""]?nonum)
  same => n,GotoIf($["${FILTER(0-9a-zA-Z@._ +,${CALLERID(num)})}" = ""]?nonum)
  same => n,Set(FILTERED_ID=${FILTER(0-9a-zA-Z@._ +,${CALLERID(num)})})
  same => n,Goto(end)
  same => n(nonum),Set(FILTERED_ID=${FILTER(0-9a-zA-Z@._ +,${CALLERID(name)})})
  same => n(end),NoOp()

Be aware that when you have an incoming call you may want to capture the incoming caller ID into the ${FILTERED_ID} variable – but this will not be inherited when you issue the Dial() command.

So if you’re planning to use the incoming caller ID by a dialled extension you need to subsequently create another variable with a leading underscore – such variables are inherited by sub-channels with the prefixed underscore removed. E.g.:

  same => n,Set(_FILTERED_ID=${FILTERED_ID})
  same => Dial(SIP/101&SIP/102, 15, M(write-debug-file))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: