[Asterisk-Users] Questions about contexts

Rich Adamson radamson at routers.com
Tue Jun 14 20:00:12 MST 2005


> I'm trying to clarify contexts and their uses. I do have a good
> general understanding of them. My question is about "undeclared"
> and "non-existant" contexts.
> 
> If I have a block somewhere (in sip.conf, for example), and it
> has no "context=thiscontext" field, does it just automatically
> use the "default" context? Or is this settable? (I see there is
> an entry for context in the [general] block. I'm guessing this
> can be used to set a global context for that file. But if this
> is omitted, what context does the file get? Is "default" a
> hardcoded fallback?)

Let's see if this helps...  Many of asterisk config definitions
use a "context=" to refer to other sections of config files. In
most cases (but not all) asterisk will _assume_ the use of a 
default context if you don't specify one. The assumed context is
usually "context=default".

In extensions.conf (as an example), if you don't have any [xxxxxx] 
context defined, then all definitions starting from the top of the
file are assumed to be in the [default] context. Same with the
sip.conf file.  That's one reason why those that are interested in
security _don't_ use anything that assumes a default context.

So in sip.conf (as an example), after the [general] section but
before any other defintions, you should have something like
context=bogon-calls to catch any random sip connect attempts. Then
in extensions.conf, have a section like [bogon-calls] that does
something with those sip connect attempts that don't specifically
match other definitions in sip.conf. The extensions.conf entry
might be something like this:
[bogon-calls]                                                                   
 exten => _X.,1,Dial(SIP/3000,15,r)
 exten => _X.,2,Congestion
where you forcibly send unauthorized sip call attempts to a well
defined destination (sip/3000).

Sooner or later asterisk systems that are exposed to the internet
will be hit with random sip and iax connect attempts. Deal with
them now and define all contexts instead of relying on 'default'.

To over simplify this, anytime you see a 'context=' think in terms
of that functioning just like a 'goto' programming statement.

> Someone suggested that I could define a "non-existant" context,
> being one that isn't actually declared, and set that in my
> "context=" line, and calls so directed wouldn't go anywhere.
> Would it be possible to do this with an empty definition (are
> they equivalent?)

Yes. A 'context=nowhere' with [nowhere] defined and nothing in it
will be exactly the same thing as missing the [nowhere]. They 
function the same.
 
> Lastly, I'm trying to get a good handle on "include" contexts.
> If I have:
> [contexta]
> ; commands omitted
> [contextb]
> ; commands omitted
> 
> and I add:
> [contextc]
> include => contexta
> include => contextb
> ; commands omitted
> 
> Then "contexta" and "contextb" are available to "contextc"'s
> dialers, right?

Yes, and that exact approach is also very useful to "force" the
order in which definitions are executed. So, if you used the above
example in extensions.conf, when the statement context=contexc is
seen, it _will_ execute what's in contexta _before_ it attempts
contextb. The approach might apply if you had something like
exten=>_1800XXX. in contexta and exten=1800 in contextb. (Maybe
bad example, but you get the picture, I'm sure.)

> Do I have to make sure there are no overlapping extensions in
> all three contexts?

No, see my bad example above. The bad example assumes you have
a valid extensions 1800, but you allow your users to also dial
1-800-555-1212. Which do you want to check for _first_ in your
dialplan?





More information about the asterisk-users mailing list