Fix a race condition where a canceled call was answered.

RFC 5407 section 3.1.2 details a scenario where a UAC sends
a CANCEL at the same time that a UAS sends a 200 OK for the
INVITE that the UAC is canceling. When this occurs, it is the
role of the UAC to immediately send a BYE to terminate
the call.

This scenario was reproducible by have a Digium phone with two lines
place a call to a second phone that forwarded the call to the second
line on the original phone. The Digium phone, upon realizing that it
was connecting to itself, would attempt to cancel the call. The timing
of this happened to trigger the aforementioned race condition about
80% of the time. Asterisk was not doing its job of sending a BYE
when receiving a 200 OK on a cancelled INVITE. The result was that
the ast_channel structure was destroyed but the underlying SIP
session, as well as the PJSIP inv_session and dialog, were still
alive. Attempting to perform an action such as a transfer, once in
this state, would result in Asterisk crashing.

The circumstances are now detected properly and the session is ended
as recommended in RFC 5407.

(closes issue AST-1209)
reported by John Bigelow
........

Merged revisions 397945 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: http://svn.asterisk.org/svn/asterisk/trunk@397956 f38db490-d61c-443f-a65b-d21fe96a405b
Fix when the subscription_terminated callback is called for subscription handlers.

The previous placement would result in the resubscribe() callback called instead of
the subscription_terminated() callback being called when a subscription was ended
via a SUBSCRIBE request. This would result in confusing PJSIP and having it throw
an assertion.
........

Merged revisions 397955 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: http://svn.asterisk.org/svn/asterisk/trunk@397957 f38db490-d61c-443f-a65b-d21fe96a405b
Verbose logging discrepancies

Refactored cases where a combination of ast_verbose/options_verbose were
present.  Also in general tried to eliminate, in as many places as possible,
where the options_verbose global variable was being used.  Refactored the way
local and remote consoles handle verbose message logging in an attempt to
solve the various discrepancies that sometimes would show between the two.

(closes issue AST-1193)
Reported by: Guenther Kelleter
Review: https://reviewboard.asterisk.org/r/2798/
........

Merged revisions 397948 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 397958 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: http://svn.asterisk.org/svn/asterisk/trunk@397959 f38db490-d61c-443f-a65b-d21fe96a405b
Fix method for creating activities string in PIDF bodies.

The previous method did not allocate enough space to create
the entire string, but adjusted the string's slen value to
be larger than the actual allocation. This resulted in garbled
text in NOTIFY requests from Asterisk.

This method allocates the proper amount of space first and then
writes the content into the buffer.
........

Merged revisions 397960 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: http://svn.asterisk.org/svn/asterisk/trunk@397962 f38db490-d61c-443f-a65b-d21fe96a405b