Summary:ASTERISK-19161: [patch] Add function REGISTRANT() that retrieves the peer that auto-registered an extension
Reporter:Barry Miller (bjm)Labels:patch
Date Opened:2012-01-03 12:46:19.000-0600Date Closed:
Versions:SVN 13.18.4 Frequency of
Environment:Attachments:( 0) registrant-diffs
Description:This little function REGISTRANT([extension][@context]) will return the name of the SIP peer that caused <extension> to be auto-registered, or the null string if the extension does not exist or was not created by SIP.  Defaults are the current extension and context.

Use of regcontext + regexten can be very useful.  Peers can have handy names like MAC address or device serial number, while extensions can be human-friendly.  This is also helpful in foiling extension enumeration probing attacks.  But at present, there is no simple way to do this.  Hard-coding in the dialplan is cumbersome and error prone, adding the association to astdb is the same, and using 'setvar= MYDEV=dev_A' is redundant and won't work in cases like "Dial(DAHDI/1&SIP/251)".  These methods also cause problems if sip.conf has multiple regcontexts, and regexten adds the same extension to different contexts which are included based on time of day.  REGISTRANT() simply returns information already in sip.conf (if regcontext is used).

REGISTRANT() works based on the fact that when chan_sip auto-registers an extension, it thoughtfully creates "<extension>, 1, Noop(<peername>)".  So we just find priority 1 of <extension>, and if its registrant is "SIP" and it executes "Noop", we return <peername>, otherwise "".  I stuck this in main/pbx.c because a) it's small, and b) struct ast_exten isn't really defined anywhere else.

I believe the most common use in the dialplan would be something like:

 include => sip-autoreg  ; regextens are in the range 250-299
 exten => _2[5-9]X, 2, Dial(SIP/${REGISTRANT()})

And it's easy to write a subroutine that converts a pseudo-dialstring like "DAHDI/1&251" into "DAHDI/1&SIP/002699ABD83D".