]> git.ipfire.org Git - thirdparty/asterisk.git/log
thirdparty/asterisk.git
9 years agoPJSIP FAX: Fix T.38 automatic reject timer NULL channel pointer dereferences. 74/1774/1
Richard Mudgett [Fri, 26 Jun 2015 15:36:19 +0000 (10:36 -0500)] 
PJSIP FAX: Fix T.38 automatic reject timer NULL channel pointer dereferences.

When a caller calls a FAX number and then hangs up right after the call is
answered then the T.38 re-INVITE automatic reject timer may still be
running after the channel goes away.

* Added session NULL channel checks on the code paths that get executed by
t38_automatic_reject() to prevent a crash when the T.38 re-INVITE
automatic reject timer expires.

ASTERISK-25168
Reported by: Carl Fortin

Change-Id: I07b6cd23815aedce5044f8f32543779e2f7a2403
(cherry picked from commit 8ea214aed782424a884b9a2f67d6dca270854e83)

9 years agoMerge "sched.c: Make not return a sched id of 0." into certified/13.1
Joshua Colp [Thu, 3 Dec 2015 11:50:55 +0000 (05:50 -0600)] 
Merge "sched.c: Make not return a sched id of 0." into certified/13.1

9 years agoMerge topic 'ASTERISK-25476' into certified/13.1
Joshua Colp [Thu, 3 Dec 2015 11:50:24 +0000 (05:50 -0600)] 
Merge topic 'ASTERISK-25476' into certified/13.1

* changes:
  Audit improper usage of scheduler exposed by 5c713fdf18f. (v13 additions)
  Audit improper usage of scheduler exposed by 5c713fdf18f.

9 years agoUnset BRIDGEPEER when leaving a bridge 53/1753/1
Jonathan Rose [Tue, 1 Dec 2015 22:11:07 +0000 (16:11 -0600)] 
Unset BRIDGEPEER when leaving a bridge

Currently if a channel is transferred out of a bridge, the BRIDGEPEER
variable (also BRIDGEPVTCALLID) remain set even once the channel is
out of the bridge. This patch removes these variables when leaving
the bridge.

ASTERISK-25600 #close
Reported by: Mark Michelson

Change-Id: I753ead2fffbfc65427ed4e9244c7066610e546da

9 years agosched.c: Make not return a sched id of 0. 40/1740/2
Richard Mudgett [Mon, 30 Nov 2015 22:42:47 +0000 (16:42 -0600)] 
sched.c: Make not return a sched id of 0.

According to the API doxygen a sched ID of 0 is valid.  Unfortunately, 0
was never returned historically and several users incorrectly coded usage
of the returned sched ID assuming that 0 was invalid.

ASTERISK-25476

Change-Id: Ib19c7ebb44ec9fd393ef6646dea806d4f34e3a20

9 years agoAudit improper usage of scheduler exposed by 5c713fdf18f. (v13 additions) 39/1739/2
Richard Mudgett [Wed, 25 Nov 2015 18:23:47 +0000 (12:23 -0600)] 
Audit improper usage of scheduler exposed by 5c713fdf18f. (v13 additions)

chan_sip.c:
* Initialize mwi subscription scheduler ids earlier because of ASTOBJ to
ao2 conversion.

* Initialize register scheduler ids earlier because of ASTOBJ to ao2
conversion.

chan_skinny.c:
* Fix more scheduler usage for the valid 0 id value.

ASTERISK-25476

Change-Id: If9f0e5d99638b2f9d102d1ebc9c5a14b2d706e95

9 years agoAudit improper usage of scheduler exposed by 5c713fdf18f. 38/1738/2
Richard Mudgett [Tue, 24 Nov 2015 18:44:53 +0000 (12:44 -0600)] 
Audit improper usage of scheduler exposed by 5c713fdf18f.

channels/chan_iax2.c:
* Initialize struct chan_iax2_pvt scheduler ids earlier because of
iax2_destroy_helper().

channels/chan_sip.c:
channels/sip/config_parser.c:
* Fix initialization of scheduler id struct members.  Some off nominal
paths had 0 as a scheduler id to be destroyed when it was never started.

chan_skinny.c:
* Fix some scheduler id comparisons that excluded the valid 0 id.

channel.c:
* Fix channel initialization of the video stream scheduler id.

pbx_dundi.c:
* Fix channel initialization of the packet retransmission scheduler id.

ASTERISK-25476

Change-Id: I07a3449f728f671d326a22fcbd071f150ba2e8c8

9 years agores_sorcery_realtime.c: Fix crash from NULL sorcery object type. 01/1701/1
Richard Mudgett [Mon, 23 Nov 2015 20:27:27 +0000 (14:27 -0600)] 
res_sorcery_realtime.c: Fix crash from NULL sorcery object type.

If the sorcery object type is not found a NULL is returned.
Unfortunately, sorcery_realtime_filter_objectset() will crash after
complaining about not finding the object type and saying to expect errors.

* Use ao2_cleanup() instead of ao2_ref() to prevent the crash.

ASTERISK-25165
Reported by Corey Farrell

Change-Id: Ic3b64453ea3058cb68d5c26d97d4fe7b8eea2e97

9 years agofeatures: Fix crash when transferee hangs up during DTMF attended transfer. 50/1650/1
Richard Mudgett [Tue, 5 May 2015 23:17:54 +0000 (18:17 -0500)] 
features: Fix crash when transferee hangs up during DTMF attended transfer.

A crash happens with this sequence of steps:
1) Party A is connected to party B.
2) Party B starts a DTMF attended transfer.
3) Party A hangs up while party B is dialing party C.

When party A hangs up the bridge that party A and party B are in is
dissolved and party B is kicked out of the bridge.  When party B finishes
dialing party C he attempts to move to the new bridge with party C.  Since
party B is no longer in a bridge the attempted move dereferences a NULL
bridge_channel pointer and crashes.

* Made the hold(), unhold(), ringing(), and the bridge_move() functions
tolerant of the channel not being in a bridge.  The assertion that party B
is always in a bridge is not true if the bridged peer of party B hangs up
and dissolves the bridge.  Being tolerant of not being in a bridge allows
the peer hangup stimulus to be processed by the FSM.

* Made the bridge_move() function return void since where the return value
for a failed move was checked generated a FSM coding ERROR message for a
normal off-nominal condition.

* Eliminated most uses of RAII_VAR in bridge_basic.c.

ASTERISK-25003 #close
Reported by: Artem Volodin

Change-Id: Ie2c1b14e5e647d4ea6de300bf56d69805d7bcada
(cherry picked from commit be1260a35f88faea4fa029d59343b124d250a8a6)

9 years agoMerge "Confbridge: Add a user timeout option" into certified/13.1
Joshua Colp [Tue, 17 Nov 2015 14:12:21 +0000 (08:12 -0600)] 
Merge "Confbridge: Add a user timeout option" into certified/13.1

9 years agoapp_queue: (try_calling): mutex 'qe->chan' freed more times than we've locked! 41/1641/1
Alec Davis [Mon, 16 Nov 2015 10:29:25 +0000 (23:29 +1300)] 
app_queue: (try_calling): mutex 'qe->chan' freed more times than we've locked!

commit aae45acbd (Mark Michelson 2015-04-15 10:38:02 -0500 6525)
refer ASTERISK-24958

above commit removed ast_channel_lock(qe->chan);
but failed to remove corresponding ast_channel_unlock(qe->chan);

ASTERISK-25561 #close
Reported Alec Davis

Change-Id: Ie05f4e2d08912606178bf1fded57cc022c7a2e1a

9 years agoConfbridge: Add a user timeout option 30/1630/3
Mark Michelson [Fri, 13 Nov 2015 20:03:35 +0000 (14:03 -0600)] 
Confbridge: Add a user timeout option

This option adds the ability to specify a timeout, in seconds, for a
participant in a ConfBridge. When the user's timeout has been reached,
the user is ejected from the conference with the CONFBRIDGE_RESULT
channel variable set to "TIMEOUT".

The rationale for this change is that there have been times where we
have seen channels get "stuck" in ConfBridge because a network issue
results in a SIP BYE not being received by Asterisk. While these
channels can be hung up manually via CLI/AMI/ARI, adding some sort of
automatic cleanup of the channels is a nice feature to have.

ASTERISK-25549 #close
Reported by Mark Michelson

Change-Id: I2996b6c5e16a3dda27595f8352abad0bda9c2d98

9 years agoTaskprocessors: Increase high-water mark 24/1624/1
Mark Michelson [Fri, 13 Nov 2015 20:19:35 +0000 (14:19 -0600)] 
Taskprocessors: Increase high-water mark

In practical tests, we have seen certain taskprocessors, specifically
Stasis subscription taskprocessors, cross the recently-added high-water
mark and emit a warning. This high-water mark warning is only intended
to be emitted when things have tanked on the system and things are
heading south quickly. In the practical tests, the Stasis taskprocessors
sometimes had a max depth of 180 tasks in them, and Asterisk wasn't in
any danger at all.

As such, this ups the high-water mark to 500 tasks instead. It also
redefines the SIP threadpool request denial number to be a multiple of
the taskprocessor high-water mark.

Change-Id: Ic8d3e9497452fecd768ac427bb6f58aa616eebce

9 years agores_pjsip distributor: Don't send 503 response to responses. 18/1618/1
Mark Michelson [Thu, 12 Nov 2015 17:17:51 +0000 (11:17 -0600)] 
res_pjsip distributor: Don't send 503 response to responses.

When the SIP threadpool is backed up with tasks, we send 503 responses
to ensure that we don't try to overload ourselves. The problem is that
we were not insuring that we were not trying to send a 503 to an
incoming SIP response.

This change makes it so that we only send the 503 on incoming requests.

Change-Id: Ie2b418d89c0e453cc6c2b5c7d543651c981e1404

9 years agoMerge "Further fixes to improper usage of scheduler" into certified/13.1
Matt Jordan [Thu, 12 Nov 2015 17:16:58 +0000 (11:16 -0600)] 
Merge "Further fixes to improper usage of scheduler" into certified/13.1

9 years agoFurther fixes to improper usage of scheduler 15/1615/1
Steve Davies [Wed, 11 Nov 2015 10:16:22 +0000 (10:16 +0000)] 
Further fixes to improper usage of scheduler

When ASTERISK-25449 was closed, a number of scheduler issues mentioned in
the comments were missed. These have since beed raised in ASTERISK-25476
and elsewhere.

This patch attempts to collect all of the scheduler issues discovered so
far and address them sensibly.

ASTERISK-25476 #close

Change-Id: I87a77d581e2e0d91d33b4b2fbff80f64a566d05b
(cherry picked from commit 07583c288828a496cd7730b55112128fea31eaef)

9 years agores_pjsip: Deny requests when threadpool queue is backed up. 08/1608/2
Mark Michelson [Wed, 11 Nov 2015 23:11:53 +0000 (17:11 -0600)] 
res_pjsip: Deny requests when threadpool queue is backed up.

We have observed situations where the SIP threadpool may become
deadlocked. However, because incoming traffic is still arriving, the SIP
threadpool's queue can continue to grow, eventually running the system
out of memory.

This change makes it so that incoming traffic gets rejected with a 503
response if the queue is backed up too much.

Change-Id: I4e736d48a2ba79fd1f8056c0dcd330e38e6a3816

9 years agothreadpool: Handle worker thread transitioning to dead when going active. 04/1604/1
Joshua Colp [Wed, 11 Nov 2015 17:04:08 +0000 (13:04 -0400)] 
threadpool: Handle worker thread transitioning to dead when going active.

This change adds handling of dead worker threads when moving them
to be active. When this happens the worker thread is removed from
both the active and idle threads container. If no threads are able
to be moved to active then the pool grows as configured.

A unit test has also been added which thrashes the idle timeout
and thread activation to exploit any race conditions between the
two.

ASTERISK-25546 #close

Change-Id: I6c455f9a40de60d9e86458d447b548fb52ba1143

9 years agotaskprocessor: Add high water mark warnings 73/1573/1
Jonathan Rose [Tue, 3 Nov 2015 22:19:43 +0000 (16:19 -0600)] 
taskprocessor: Add high water mark warnings

If a taskprocessor's queue grows large, this can indicate that there
may be a problem with tasks not leaving the processor or else that
the number of available task processors for a given type of task is
too low. This patch makes it so that if a taskprocessor's task queue
grows above 100 queued tasks that it will emit a warning message.
Warning messages are emitted only once per task processor.

ASTERISK-25518 #close
Reported by: Jonathan Rose

Change-Id: Ib1607c35d18c1d6a0575b3f0e3ff5d932fd6600c

9 years agoapp_dial: Hold reference to calling channel formats when dialing outbound. 69/1569/1
Joshua Colp [Tue, 23 Jun 2015 16:21:41 +0000 (13:21 -0300)] 
app_dial: Hold reference to calling channel formats when dialing outbound.

Currently when requesting a channel the native formats of the
calling channel are provided to the core for usage when dialing
the outbound channel. This occurs without holding the channel lock
or keeping a reference to the formats. This is problematic as
the channel driver may end up changing the formats during this time.
In the case of chan_sip this happens when an SDP negotiation
completes.

This change makes it so app_dial keeps a reference to the native
formats of the calling channel which guarantees that they will
remain valid for the period of time needed.

ASTERISK-25172 #close

Change-Id: I2f0a67bd0d5d14c3bdbaae552b4b1613a283f0db
(cherry picked from commit 3b2b004d699b8cc7b808f62536bb2bc4db8b4e0e)

9 years agomain/dial: Protect access to the format_cap structure of the requesting channel 67/1567/1
Matt Jordan [Wed, 4 Nov 2015 20:31:28 +0000 (14:31 -0600)] 
main/dial: Protect access to the format_cap structure of the requesting channel

When a dial attempt is made that involves a requesting channel, we previously
were not:
a) Protecting access to the native format capabilities structure on the
   requesting channel. That is inherently unsafe.
b) Reference bumping the lifetime of the format capabilities structure.

In both cases, something else could sneak in, blow away the format
capabilities, and we'd be holding onto an invalid format_cap structure. When
the newly created channel attempts to construct its format capabilities, things
go poorly.

This patch:
a) Ensures that we get a reference to the native format capabilities while
   the requesting channel is locked
b) Holds a reference to the native format capabilities during the creation
   of the new channel.

ASTERISK-25522 #close

Change-Id: I0bfb7ba8b9711f4158cbeaae96edf9626e88a54f

9 years agores_pjsip: Set threadpool max size default to 50. 51/1551/1
Mark Michelson [Mon, 2 Nov 2015 23:19:21 +0000 (17:19 -0600)] 
res_pjsip: Set threadpool max size default to 50.

During a stress test of subscriptions, a huge blast of
subscription-related traffic resulted in the threadpool expanding to a
ridiculous number of threads. The balooning of threads resulted in an
increase of memory, which led to a crash due to being out of memory.

An easy fix for the particular test was to limit the size of the
threadpool, thus reining in the amount of memory that would be used. It
was decided that there really is no downside to having a non-infinite
default value for the maximum size of the threadpool, so this change
introduces 50 threads as the maximum threadpool size for the SIP
threadpool.

ASTERISK-25513 #close
Reported by John Bigelow

Change-Id: If0b9514f1d9b172540ce1a6e2f2ffa1f2b6119be

9 years agoMerge "res_pjsip_pubsub: Prevent sending NOTIFY on destroyed dialog." into certified...
Matt Jordan [Sun, 25 Oct 2015 15:14:50 +0000 (10:14 -0500)] 
Merge "res_pjsip_pubsub: Prevent sending NOTIFY on destroyed dialog." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Ensure dialog lock balance." into certified/13.1
Matt Jordan [Sun, 25 Oct 2015 15:13:19 +0000 (10:13 -0500)] 
Merge "res_pjsip_pubsub: Ensure dialog lock balance." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Prevent crashes on final NOTIFY." into certified/13.1
Matt Jordan [Sun, 25 Oct 2015 15:12:52 +0000 (10:12 -0500)] 
Merge "res_pjsip_pubsub: Prevent crashes on final NOTIFY." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Remove serializer when sending final NOTIFY." into certified...
Matt Jordan [Sun, 25 Oct 2015 15:12:31 +0000 (10:12 -0500)] 
Merge "res_pjsip_pubsub: Remove serializer when sending final NOTIFY." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Fix crash on destruction of empty subscription tree." into...
Matt Jordan [Sun, 25 Oct 2015 15:12:16 +0000 (10:12 -0500)] 
Merge "res_pjsip_pubsub: Fix crash on destruction of empty subscription tree." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Solidify lifetime and ownership of objects." into certified...
Matt Jordan [Sun, 25 Oct 2015 15:11:21 +0000 (10:11 -0500)] 
Merge "res_pjsip_pubsub: Solidify lifetime and ownership of objects." into certified/13.1

9 years agoMerge "res_pjsip_pubsub: Set the endpoint on SUBSCRIBE dialogs." into certified/13.1
Matt Jordan [Sun, 25 Oct 2015 15:11:18 +0000 (10:11 -0500)] 
Merge "res_pjsip_pubsub: Set the endpoint on SUBSCRIBE dialogs." into certified/13.1

9 years agoalembic: Bad down revision in add_default_from_user script 17/1517/1
Kevin Harwell [Fri, 23 Oct 2015 21:53:51 +0000 (16:53 -0500)] 
alembic: Bad down revision in add_default_from_user script

The down revision wasn't set correct in the add_default_from_user script.
This patch points it to the correct revision.

Change-Id: Ied45786db265a1d4fb350ef0dd33b4d043c9a74d

9 years agoMerge "res_pjsip_outbound_registration: registration stops due to fatal 4xx response...
Joshua Colp [Fri, 23 Oct 2015 20:35:25 +0000 (15:35 -0500)] 
Merge "res_pjsip_outbound_registration: registration stops due to fatal 4xx response" into certified/13.1

9 years agores_pjsip_outbound_registration: registration stops due to fatal 4xx response 74/1474/5
Kevin Harwell [Wed, 21 Oct 2015 17:35:08 +0000 (12:35 -0500)] 
res_pjsip_outbound_registration: registration stops due to fatal 4xx response

During outbound registration it is possible to receive a fatal (any permanent/
non-temporary 4xx, 5xx, 6xx) response from the registrar that is simply due
to a problem with the registrar itself. Upon receiving the failure response
Asterisk terminates outbound registration for the given endpoint.

This patch adds an option, 'fatal_retry_interval', that when set continues
outbound registration at the given interval up to 'max_retries' upon receiving
a fatal response.

ASTERISK-25485 #close

Change-Id: Ibc2c7b47164ac89cc803433c0bbe7063bfa143a2

9 years agoMerge "Add missing failure checks to ast_str_set_va() callers." into certified/13.1
Joshua Colp [Fri, 23 Oct 2015 11:51:24 +0000 (06:51 -0500)] 
Merge "Add missing failure checks to ast_str_set_va() callers." into certified/13.1

9 years agoMerge "res_pjsip: Need to use the same serializer for a pjproject SIP transaction...
Joshua Colp [Fri, 23 Oct 2015 11:50:39 +0000 (06:50 -0500)] 
Merge "res_pjsip: Need to use the same serializer for a pjproject SIP transaction." into certified/13.1

9 years agoMerge "Fix issue with AST_THREADSTORAGE_RAW when DEBUG_THREADLOCALS is enabled."...
Joshua Colp [Fri, 23 Oct 2015 11:50:29 +0000 (06:50 -0500)] 
Merge "Fix issue with AST_THREADSTORAGE_RAW when DEBUG_THREADLOCALS is enabled." into certified/13.1

9 years agoMerge "Replace most uses of ast_register_atexit with ast_register_cleanup." into...
Joshua Colp [Fri, 23 Oct 2015 11:49:04 +0000 (06:49 -0500)] 
Merge "Replace most uses of ast_register_atexit with ast_register_cleanup." into certified/13.1

9 years agoMerge "res_pjsip: Move URI validation to use time." into certified/13.1
Joshua Colp [Fri, 23 Oct 2015 11:48:55 +0000 (06:48 -0500)] 
Merge "res_pjsip: Move URI validation to use time." into certified/13.1

9 years agoformat_cap: Detect vector allocation failures. 14/1514/1
Mark Michelson [Thu, 22 Oct 2015 22:07:55 +0000 (17:07 -0500)] 
format_cap: Detect vector allocation failures.

A crash was seen on a system that ran out of memory due to Asterisk not
checking for vector allocation failures in format_cap.c. With this
change, if either of the AST_VECTOR_INIT calls fail, we will return a
value indicating failure.

Change-Id: Ieb9c59f39dfde6d11797a92b45e0cf8ac5722bc8

9 years agores_pjsip_pubsub: Prevent sending NOTIFY on destroyed dialog. 12/1512/1
Mark Michelson [Fri, 2 Oct 2015 20:32:09 +0000 (15:32 -0500)] 
res_pjsip_pubsub: Prevent sending NOTIFY on destroyed dialog.

A certain situation can result in our attempting to send a NOTIFY on a
destroyed dialog. Say we attempt to send a NOTIFY to a subscriber, but
that subscriber has dropped off the network. We end up retransmitting
that NOTIFY until the appropriate SIP timer says to destroy the NOTIFY
transaction. When the pjsip evsub code is told that the transaction has
been terminated, it responds in kind by alerting us that the
subscription has been terminated, destroying the subscription, and then
removing its reference to the dialog, thus destroying the dialog.

The problem is that when we get told that the subscription is being
terminated, we detect that we have not sent a terminating NOTIFY
request, so we queue up such a NOTIFY to be sent out. By the time that
queued NOTIFY gets sent, the dialog has been destroyed, so attempting to
send that NOTIFY can result in a crash.

The fix being introduced here is actually a reintroduction of something
the pubsub code used to employ. We hold a reference to the dialog and
wait to decrement our reference to the dialog until our subscription
tree object is destroyed. This way, we can send messages on the dialog
even if the PJSIP evsub code wants to terminate earlier than we would
like.

In doing this, some NULL checks for subscription tree dialogs have been
removed since NULL dialogs are no longer actually possible.

Change-Id: I013f43cddd9408bb2a31b77f5db87a7972bfe1e5

9 years agores_pjsip_pubsub: Ensure dialog lock balance. 11/1511/1
Mark Michelson [Tue, 29 Sep 2015 19:53:22 +0000 (14:53 -0500)] 
res_pjsip_pubsub: Ensure dialog lock balance.

When sending a NOTIFY, we lock the dialog and then unlock the dialog
when finished. A recent change made it so that the subscription tree's
dialog pointer will be set NULL when sending the final NOTIFY request
out. This means that when we attempt to unlock the dialog, we pass a
NULL pointer to pjsip_dlg_dec_lock(). The result is that the dialog
remains locked after we think we have unlocked it. When a response to
the NOTIFY arrives, the monitor thread attempts to lock the dialog, but
it cannot because we never released the dialog lock. This results in
Asterisk being unable to process incoming SIP traffic any longer.

The fix in this patch is to use a local pointer to save off the pointer
value of the subscription tree's dialog when locking and unlocking the
dialog. This way, if the subscription tree's dialog pointer is NULLed
out, the local pointer will still have point to the proper place and the
dialog lock will be unlocked as we expect.

Change-Id: I7ddb3eaed7276cceb9a65daca701c3d5e728e63a

9 years agores_pjsip_pubsub: Prevent crashes on final NOTIFY. 10/1510/1
Mark Michelson [Mon, 28 Sep 2015 21:36:25 +0000 (16:36 -0500)] 
res_pjsip_pubsub: Prevent crashes on final NOTIFY.

The SIP dialog is removed from the subscription tree when the final
NOTIFY is sent. However, after the final NOTIFY is sent, the persistence
update function still attempts to access the cseq from the dialog,
resulting in a crash.

This fix removes the subscription persistence at the same time that the
dialog is removed from the subscription tree. This way, there is no
attempt to update persistence when the subscription is being destroyed.

Change-Id: Ibb46977a6cef9c51dc95f40f43446e3d11eed5bb

9 years agores_pjsip_pubsub: Remove serializer when sending final NOTIFY. 09/1509/1
Mark Michelson [Thu, 17 Sep 2015 22:28:30 +0000 (17:28 -0500)] 
res_pjsip_pubsub: Remove serializer when sending final NOTIFY.

There have been crashes seen where a taskprocessor's listener is NULL
unexpectedly.

Looking at backtraces, the problem was specifically seen in PJSIP
serializers.

Subscriptions make the mistake of removing a serializer from a dialog
during subscription tree destruction. Since subscription trees are
reference-counted, guaranteeing the circumstances behind the destruction
are not possible. This makes it so that the dialog serializer can be
removed while not holding the dialog lock. This makes it possible for
the distributor to get a pointer to the dialog serializer and have that
serializer get freed out from under it.

The fix for this is to remove the serializer from a subscription dialog
when sending the final NOTIFY. This guarantees that the serializer is
removed with the dialog lock held. By doing this, we guarantee that if
the distributor gains access to the dialog's serializer, it will not be
possible for the serializer to get freed by another thread.

Change-Id: I21f5dac33529f65cec45679bdace60670800ff66

9 years agores_pjsip_pubsub: Fix crash on destruction of empty subscription tree. 08/1508/1
Mark Michelson [Wed, 2 Sep 2015 14:14:19 +0000 (09:14 -0500)] 
res_pjsip_pubsub: Fix crash on destruction of empty subscription tree.

If an old persistent subscription is recreated but then immediately
destroyed because it is out of date, the subscription tree will have no
leaf subscriptions on it. This was resulting in a crash when attempting
to destroy the subscription tree.

A simple NULL check fixes this problem.

Change-Id: I85570b9e2bcc7260a3fe0ad85904b2a9bf36d2ac

9 years agores_pjsip_pubsub: Solidify lifetime and ownership of objects. 07/1507/1
Mark Michelson [Tue, 1 Sep 2015 20:47:19 +0000 (15:47 -0500)] 
res_pjsip_pubsub: Solidify lifetime and ownership of objects.

There have been crashes and general instability seen in the pubsub code,
so this patch introduces three changes to increase the stability.

First, the ownership model for subscriptions has been modified. Due to
RLS, subscriptions are stored in memory as a tree structure. Prior to my
patch, the PJSIP subscription was the owner of the subscription tree.
When the PJSIP subscription told us that it was terminating, we started
destroying the subscription tree along with all of the individual leaf
subscriptions that belong to the tree. The problem with this model is
that the two actors in play here, the PJSIP subscription and the
individual leaf subscriptions, need to have joint ownership of the
subscription tree. So now, the PJSIP subscription and the individual
leaf subscriptions each have a reference to the subscription tree. This
way, we will not actually free memory until no players are left that
care. The PJSIP subscription is a bigger stakeholder, in that if the
PJSIP subscription's reference to the subscription tree is removed, the
subscription tree instructs the leaf subscriptions to shut down and drop
their references to the subscription tree when possible. The individual
leaf subscriptions, upon being told to shut down, can drop their stasis
subscriptions or whatever they use to learn of new state, and then drop
their reference to the subscription tree once they are ready to die.

Second, the lifetime of a PJSIP subscription's reference to our
subscription tree has been altered. As I learned from doing a deep dive,
the PJSIP evsub code can tell Asterisk multiple times that the
subscription has been terminated, and not all of these times
are especially helpful. I have altered the message flow that we use for
SIP subscriptions such that we will always drop the PJSIP subscription's
reference to the subscription tree when we send the NOTIFY that
terminates a SIP subscription. This also means that we will now queue
NOTIFY requests to be sent after responding to incoming SUBSCRIBEs so
that we can have predictable state changes from the PJSIP evsub code.

Third, the synchronization of operations has been improved. PJSIP can
call into our code from a serializer thread (e.g. upon receiving an
incoming request) or from the monitor thread (e.g. when a subscription
times out). Because of this, there is the possibility of competing
threads stepping on each other. PJSIP attempts to do some
synchronization on its own by always keeping the dialog lock held when
it calls into us. However, since we end up pushing tasks into the
serializer, the result was that serialized operations were not grabbing
the dialog lock and could, as a result, step on something that was being
attempted by a different thread. Now we ensure that serialized
operations grab the dialog lock, then check for extenuating
circumstances, then proceed with their operation if they can.

Change-Id: Iff2990c40178dad9cc5f6a5c7f76932ec644b2e5

9 years agores_pjsip_pubsub: Set the endpoint on SUBSCRIBE dialogs. 06/1506/1
Mark Michelson [Mon, 20 Apr 2015 19:30:47 +0000 (14:30 -0500)] 
res_pjsip_pubsub: Set the endpoint on SUBSCRIBE dialogs.

When SUBSCRIBE dialogs were established, we never associated
the endpoint that created the subscription with the dialog
we end up creating. In most cases, this ended up not causing
any problems.

The actual bug that was observed was that when a device that
was behind NAT established a subscription with Asterisk, Asterisk
would end up sending in-dialog NOTIFY requests to the device's
private IP addres instead of the public address of the NAT router.

When Asterisk receives the initial SUBSCRIBE from the device,
res_pjsip_nat rewrites the contact to the public address on which the
SUBSCRIBE was received. This allows for the dialog to have its target
address set to the proper public address. Asterisk then would send a 200
OK response to the SUBSCRIBE, then a NOTIFY with the initial
subscription state. The device would then send a 200 OK response to
Asterisk's NOTIFY.

Here's where things went wrong. When the 200 OK arrived, res_pjsip_nat
did not rewrite the address in the Contact header. Then, when the PJSIP
dialog layer processed the 200 OK, PJSIP would perform a comparison
between the IP address in the Contact header and its saved target
address for the dialog. Since they differed, PJSIP would update the
target dialog address to be the address in the Contact header. From this
point, if Asterisk needed to send a NOTIFY to the device, the result was
that the NOTIFY would be sent to the private address that the device
placed in the Contact header.

The reason why res_pjsip_nat did not rewrite the address when it
received the 200 OK response was that it could not associate the
incoming response with a configured endpoint. This is because on a
response, the only way to associate the response to an endpoint is by
finding the dialog that the response is associated with and then finding
the endpoint that is associated with that dialog. We do not perform
endpoint lookups on responses. res_pjsip_pubsub skipped the step of
associating the endpoint with the dialog we created, so res_pjsip_nat
could not find the associated endpoint and therefore couldn't rewrite
the contact.

This commit message is like 50x longer than the actual fix.

ASTERISK 24981 #close
Reported by Mark Michelson

Change-Id: I2b963c58c063bae293e038406f7d044a8a5377cd

9 years agoAdd missing failure checks to ast_str_set_va() callers. 81/1481/1
Richard Mudgett [Mon, 19 Oct 2015 20:27:40 +0000 (15:27 -0500)] 
Add missing failure checks to ast_str_set_va() callers.

Change-Id: I0c2cdcd53727bdc6634095c61294807255bd278f

9 years agores_pjsip: Move URI validation to use time. 71/1471/1
Joshua Colp [Wed, 21 Oct 2015 16:44:17 +0000 (13:44 -0300)] 
res_pjsip: Move URI validation to use time.

In a realtime based system with a limited number of threadpool threads
it is possible for a deadlock to occur. This happens when permanent
endpoint state is updated, which will cause database queries to be done.
These queries may result in URI validation being done which is done
synchronously using a PJSIP thread. If all PJSIP threads are in use
processing traffic they themselves may be blocked waiting to get the
permanent endpoint container lock when identifying an endpoint.

This change moves URI validation to occur at use time instead of
configuration time. While this comes at a cost of not seeing a problem
until you use it it does solve the underlying deadlock problem.

ASTERISK-25486 #close

Change-Id: I2d7d167af987d23b3e8199e4a68f3359eba4c76a

9 years agoReplace most uses of ast_register_atexit with ast_register_cleanup. 66/1466/1
Corey Farrell [Thu, 26 Mar 2015 22:19:21 +0000 (22:19 +0000)] 
Replace most uses of ast_register_atexit with ast_register_cleanup.

Since 'core stop now' and 'core restart now' do not stop modules,
it is unsafe for most of the core to run cleanups.  Originally all
cleanups used ast_register_atexit, and were only changed when it
was shown to be unsafe.  ast_register_atexit is now used only when
absolutely required to prevent corruption and close child processes.

Exceptions that need to use ast_register_atexit:
* CDR: Flush records.
* res_musiconhold: Kill external applications.
* AstDB: Close the DB.
* canary_exit: Kill canary process.

ASTERISK-24142 #close
Reported by: David Brillert

ASTERISK-24683 #close
Reported by: Peter Katzmann

ASTERISK-24805 #close
Reported by: Badalian Vyacheslav

ASTERISK-24881 #close
Reported by: Corey Farrell

Review: https://reviewboard.asterisk.org/r/4500/
Review: https://reviewboard.asterisk.org/r/4501/
........

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

Change-Id: I6a67336050dea74327d79cdd6f7c7ea34d0b473e
git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/13@433497

9 years agocontrib/scripts/autosupport: Update for Asterisk 13 56/1456/1
Matt Jordan [Tue, 20 Oct 2015 00:59:50 +0000 (19:59 -0500)] 
contrib/scripts/autosupport: Update for Asterisk 13

This patch adds some minor tweaks for autosupport to update it for Asterisk 13.
This includes:
* Finally removing most references to Zaptel
* Adding support for some additional 'core' commands, and fixing nomenclature
  that generally hasn't been used for some time
* Adding some PJSIP/SIP commands to gather endpoints/peers and active channels

Change-Id: Ic997b418cbd9313588b6608e50f47b0ce6f4f1f1
(cherry picked from commit 9fc9777fa34753fb38991d42d8dbed516e907ca2)

9 years agores_pjsip: Need to use the same serializer for a pjproject SIP transaction. 52/1452/1
Richard Mudgett [Fri, 5 Jun 2015 20:37:33 +0000 (15:37 -0500)] 
res_pjsip: Need to use the same serializer for a pjproject SIP transaction.

All send/receive processing for a SIP transaction needs to be done under
the same threadpool serializer to prevent reentrancy problems inside
pjproject and res_pjsip.

* Add threadpool API call to get the current serializer associated with
the worker thread.

* Pick a serializer from a pool of default serializers if the caller of
res_pjsip.c:ast_sip_push_task() does not provide one.

This is a simple way to ensure that all outgoing SIP request messages are
processed under a serializer.  Otherwise, any place where a pushed task is
done that would result in an outgoing out-of-dialog request would need to
be modified to supply a serializer.  Serializers from the default
serializer pool are picked in a round robin sequence for simplicity.

A side effect is that the default serializer pool will limit the growth of
the thread pool from random tasks.  This is not necessarily a bad thing.

* Made pjsip_distributor.c save the thread's serializer name on the
outgoing request tdata struct so the response can be processed under the
same serializer.

This is a cherry-pick from master.

**** ASTERISK-25115 Change-Id: Iea71c16ce1132017b5791635e198b8c27973f40a

NOTE: session_inv_on_state_changed() is disassociating the dialog from the
session when the invite dialog becomes PJSIP_INV_STATE_DISCONNECTED.
Unfortunately this is a tad too soon because our BYE request transaction
has not completed yet.

This is a cherry-pick from v13.

ASTERISK-25183 #close
Reported by: Matt Jordan

Change-Id: I8bad0ae1daf18d75b8c9e55874244b7962df2d0a

9 years agoFix issue with AST_THREADSTORAGE_RAW when DEBUG_THREADLOCALS is enabled. 51/1451/1
Richard Mudgett [Fri, 2 Oct 2015 22:05:16 +0000 (17:05 -0500)] 
Fix issue with AST_THREADSTORAGE_RAW when DEBUG_THREADLOCALS is enabled.

When DEBUG_THREADLOCALS is enabled it causes the threadlocal cleanup to be
called as a function.  This causes a compile error with raw threadstorage as
it uses NULL for cleanup.  This fix uses a macro that provides NULL when
DEBUG_THREADLOCALS is disabled, and replaces the call to "c_cleanup(data);"
with "{};" when DEBUG_THREADLOCALS is enabled.

ASTERISK-24975 #close
Reported by: Ashley Sanders

**** ASTERISK-24975 Change-Id: I3ef7428ee402816d9fcefa1b3b95830c00d5c402

Cherry-pick from v13 with additional definitions of
AST_THREADSTORAGE_RAW(), ast_threadstorage_get_ptr() and
ast_threadstorage_set_ptr() from
commit d01706ce1ee518118456d5673f529204bdac73bb.

Change-Id: I3222102d005f76744561b95a3b97700d82a5ee58

9 years agoconfig.c: Fix off-nominal memory leak. 30/1430/1
Richard Mudgett [Mon, 12 Oct 2015 16:21:39 +0000 (11:21 -0500)] 
config.c: Fix off-nominal memory leak.

Change-Id: I06e346e9a5c63cc5071e7eda537310c4b43bffe0

9 years agoconfig.c: Fix potential memory corruption after [section](+). 29/1429/1
Richard Mudgett [Mon, 12 Oct 2015 16:20:29 +0000 (11:20 -0500)] 
config.c: Fix potential memory corruption after [section](+).

The memory corruption could happen if the [section](+) is the last section
in the file with trailing comments.  In this case process_text_line() has
left *last_cat is set to newcat and newcat is destroyed.

Change-Id: I0d1d999f553986f591becd000e7cc6ddfb978d93

9 years agoconfig.c: Fix #include after [section](+). 28/1428/1
Richard Mudgett [Mon, 12 Oct 2015 16:21:55 +0000 (11:21 -0500)] 
config.c: Fix #include after [section](+).

An #include right after a [section](+) would associate any variable
assignments before a new section in the #include with the wrong section.

* Fix section association by setting the current section to the appended
section.

* Fix '+' and '!' section flag interaction corner case depending upon
which flag came first.  If the '!' came first then it would be ignored.
If the '!' came after then it would affect the appended section.  The '!'
will now no longer be ignored.

ASTERISK-25461 #close
Reported by: Sean Pimental

Change-Id: Ic9d3191c8758048e2cbce6432f854b32531731c3

9 years agoMerge "chan_pjsip: Fix crash on reINVITE before initial INVITE completes." into certi...
Joshua Colp [Thu, 8 Oct 2015 18:48:26 +0000 (13:48 -0500)] 
Merge "chan_pjsip: Fix crash on reINVITE before initial INVITE completes." into certified/13.1

9 years agores/res_rtp_asterisk: Fix assignment after ao2 decrement 04/1404/1
Matt Jordan [Wed, 7 Oct 2015 01:43:58 +0000 (20:43 -0500)] 
res/res_rtp_asterisk: Fix assignment after ao2 decrement

When we decide we will no longer schedule an RTCP write, we remove the
reference to the RTP instance, then assign -1 to the stored scheduler ID
in case something else comes along and wants to see if anything is scheduled.

That scheduler ID is on the RTP instance. After 60a9172d7ef2 was merged to
fix the regression introduced by 3cf0f29310, this improper assignment on a
potentially destroyed object started getting tripped on the build agents.

Frankly, this should have been crashing a lot more often earlier. I can only
assume that the timing was changed just enough by both changes to start
actually hitting this problem.

As it is, simply moving the assignment prior to the ao2 deference is sufficient
to keep the RTP instance from being referenced when it is very, truly,
aboslutely dead.

(Note that it is still good practice to assign -1 to the scheduler ID when we
know we won't be scheduling it again, as the ao2 deref *may* not always destroy
the ao2 object.)

ASTERISK-25449

Change-Id: Ie6d3cb4adc7b1a6c078b1c38c19fc84cf787cda7

9 years agochan_pjsip: Fix crash on reINVITE before initial INVITE completes. 93/1393/1
Richard Mudgett [Mon, 5 Oct 2015 21:53:44 +0000 (16:53 -0500)] 
chan_pjsip: Fix crash on reINVITE before initial INVITE completes.

Apparently some endpoints attempt to send a reINVITE before completing the
initial INVITE transaction.  In this case PJSIP responds appropriately to
the reINVITE with a 491 INVITE request pending.  Unfortunately chan_pjsip
is using the initial INVITE transaction state to determine if an INVITE is
the initial INVITE or a reINVITE.  Since the initial INVITE transaction
has not been confirmed yet chan_pjsip thinks the reINVITE is an initial
INVITE and starts another PBX thread on the channel.  The extra PBX thread
ensures that hilarity ensues.

* Fix checks for a reINVITE on incoming requests to look for the presence
of a to-tag instead of the initial INVITE transaction state.

* Made caller_id_incoming_request() determine what to do if there is a
channel on the session or not.  After a channel is created it is too late
to just store the new party id on the session because the session's party
id has already been copied to the channel's caller id.

ASTERISK-25404 #close
Reported by: Chet Stevens

Change-Id: Ie78201c304a2b13226f3a4ce59908beecc2c68be

9 years agoFix improper usage of scheduler exposed by 5c713fdf18f 82/1382/2
Matt Jordan [Tue, 6 Oct 2015 02:34:41 +0000 (21:34 -0500)] 
Fix improper usage of scheduler exposed by 5c713fdf18f

When 5c713fdf18f was merged, it allowed for scheduled items to have an ID of
'0' returned. While this was valid per the documentation for the API, it was
apparently never returned previously. As a result, several users of the
scheduler API viewed the result as being invalid, causing them to reschedule
already scheduled items or otherwise fail in interesting ways.

This patch corrects the users such that they view '0' as valid, and a returned
ID of -1 as being invalid.

Note that the failing HEP RTCP tests now pass with this patch. These tests
failed due to a duplicate scheduling of the RTCP transmissions.

ASTERISK-25449 #close

Change-Id: I019a9aa8b6997584f66876331675981ac9e07e39

9 years agores_sorcery_memory_cache.c: Fix deadlock with scheduler. 65/1365/1
Richard Mudgett [Wed, 30 Sep 2015 22:28:19 +0000 (17:28 -0500)] 
res_sorcery_memory_cache.c: Fix deadlock with scheduler.

A deadlock can happen when a sorcery object is being expired from the
memory cache when at the same time another object is being placed into the
memory cache.  There are a couple other variations on this theme that
could cause the deadlock.  Basically if an object is being expired from
the sorcery memory cache at the same time as another thread tries to
update the next object expiration timer the deadlock can happen.

* Add a deadlock avoidance loop in expire_objects_from_cache() to check if
someone is trying to remove the scheduler callback from the scheduler.

ASTERISK-25441 #close

Change-Id: Iec7b0bdb81a72b39477727b1535b2539ad0cf4dc

9 years agores_sorcery_memory_cache.c: Replace inline code with function. 64/1364/1
Richard Mudgett [Thu, 1 Oct 2015 19:30:06 +0000 (14:30 -0500)] 
res_sorcery_memory_cache.c: Replace inline code with function.

Make sorcery_memory_cache_close() call remove_all_from_cache() instead of
partially inlining it.

ASTERISK-25441

Change-Id: I1aa6cb425b1a4307096f3f914d17af8ec179a74c

9 years agores_sorcery_memory_cache.c: Shutdown in a less crash potential order. 63/1363/1
Richard Mudgett [Thu, 1 Oct 2015 19:27:34 +0000 (14:27 -0500)] 
res_sorcery_memory_cache.c: Shutdown in a less crash potential order.

Basically you should shutdown in the opposite order of how you setup since
later setup pieces likely depend on earlier setup pieces.  e.g.,
Registering your external API with the rest of the system should be the
last thing setup and the first thing unregistered during shutdown.

Change-Id: I5715765b723100c8d3c2642e9e72cc7ad5ad115e

9 years agores_sorcery_memory_cache.c: Misc tweaks. 62/1362/1
Richard Mudgett [Wed, 30 Sep 2015 22:27:54 +0000 (17:27 -0500)] 
res_sorcery_memory_cache.c: Misc tweaks.

Change-Id: I8cd32dffbb4f33bb0c39518d6e4c991e73573160

9 years agores_sorcery_memory_cache.c: Made use OBJ_SEARCH_MASK. 61/1361/1
Richard Mudgett [Wed, 30 Sep 2015 22:27:23 +0000 (17:27 -0500)] 
res_sorcery_memory_cache.c: Made use OBJ_SEARCH_MASK.

Change-Id: Ibca6574dc3c213b29cc93486e01ccd51f5caa46c

9 years agores/res_pjsip_dlg_options: Add a module to handle in-dialog OPTIONS requests 50/1350/1
Matthew Jordan [Thu, 9 Apr 2015 15:42:16 +0000 (15:42 +0000)] 
res/res_pjsip_dlg_options: Add a module to handle in-dialog OPTIONS requests

This patch adds a new session supplement that handles in-dialog OPTIONS
requests. Said OPTIONS requests are sent a 200 OK, as an endpoint lookup
for the OPTIONS request would already have been done by the time the
session supplement receives the inbound request.

ASTERISK-24862 #close
Reported by: yaron nahum
patches:
  res_pjsip_dlg_options.c submitted by yaron nahum (License 6676)

Change-Id: Iefc901a7c5c88d9d4b853188f85092d9eb7b6ada

9 years agoapp_queue.c: Force COLP update if outgoing channel name changed. 29/1329/1
Richard Mudgett [Thu, 24 Sep 2015 19:56:24 +0000 (14:56 -0500)] 
app_queue.c: Force COLP update if outgoing channel name changed.

* When a call is answered and the outgoing channel name has changed then
force a connected line update because the channel is no longer the same.
The channel was masqueraded into by another channel.  This is usually
because of a call pickup.

Note: Forwarded calls are handled in a controlled manner so the original
channel name is replaced with the forwarded channel.

ASTERISK-25423 #close
Reported by: John Hardin

Change-Id: Ie275ea9e99c092ad369db23e0feb08c44498c172

9 years agoapp_queue.c: Factor out a connected line update routine. 28/1328/1
Richard Mudgett [Thu, 24 Sep 2015 19:20:24 +0000 (14:20 -0500)] 
app_queue.c: Factor out a connected line update routine.

Replace inlined code with update_connected_line_from_peer().

ASTERISK-25423
Reported by: John Hardin

Change-Id: I33bbd033596fcb0208d41d8970369b4e87b806f3

9 years agoapp_dial.c: Make 'A' option pass COLP updates. 27/1327/1
Richard Mudgett [Thu, 24 Sep 2015 18:27:44 +0000 (13:27 -0500)] 
app_dial.c: Make 'A' option pass COLP updates.

While the 'A' option is playing the announcement file allow the caller and
peer to exchange COLP update frames.

ASTERISK-25423
Reported by: John Hardin

Change-Id: Iac6cf89b56d26452c6bb88e9363622bbf23895f9

9 years agoapp_dial.c: Force COLP update if outgoing channel name changed. 26/1326/1
Richard Mudgett [Thu, 24 Sep 2015 17:59:08 +0000 (12:59 -0500)] 
app_dial.c: Force COLP update if outgoing channel name changed.

* When a call is answered and the outgoing channel name has changed then
force a connected line update because the channel is no longer the same.
The channel was masqueraded into by another channel.  This is usually
because of a call pickup.

Note: Forwarded calls are handled in a controlled manner so the original
channel name is replaced with the forwarded channel.

ASTERISK-25423
Reported by: John Hardin

Change-Id: I2e01f7a698fbbc8c26344a59c2be40c6cd98b00c

9 years agoapp_dial.c: Factor out a connected line update routine. 25/1325/1
Richard Mudgett [Thu, 24 Sep 2015 17:37:07 +0000 (12:37 -0500)] 
app_dial.c: Factor out a connected line update routine.

Replace inlined code with update_connected_line_from_peer().

ASTERISK-25423
Reported by: John Hardin

Change-Id: Ia14f18def417645cd7fb453e1bdac682630a5091

9 years agoDo not swallow frames on channels leaving bridges. 07/1307/1
Mark Michelson [Thu, 24 Sep 2015 19:49:46 +0000 (14:49 -0500)] 
Do not swallow frames on channels leaving bridges.

When leaving a bridge, indications on a channel could be swallowed by
the internal indication logic because it appears that the channel is on
its way to be hung up anyway. One such situation where this is
detrimental is when channels on hold are redirected out of a bridge. The
AST_CONTROL_UNHOLD indication from the bridging code is swallowed,
leaving the channel in question to still appear to be on hold.

The fix here is to modify the logic inside ast_indicate_data() to not
drop the indication if the channel is simply leaving a bridge. This way,
channels on hold redirected out of a bridge revert to their expected "in
use" state after the redirection.

ASTERISK-25418 #close
Reported by Mark Michelson

Change-Id: If6115204dfa0551c050974ee138fabd15f978949

9 years agoapp_page.c: Fix crash when forwarding with a predial handler. 01/1301/1
Richard Mudgett [Tue, 22 Sep 2015 22:08:49 +0000 (17:08 -0500)] 
app_page.c: Fix crash when forwarding with a predial handler.

Page uses the async method of dialing with the dial API.  When a call gets
forwarded there is no calling channel available.  If the predial handler
was set then the calling channel could not be put into auto-service
for the forwarded call because it doesn't exist.  A crash is the result.

* Moved the callee predial parameter string processing to before the
string is passed to the dial API rather than having the dial API do it.
There are a few benefits do doing this.  The first is the predial
parameter string processing doesn't need to be done for each channel
called by the dial API.  The second is in async mode and the forwarded
channel is to have the predial handler executed on it then the
non-existent calling channel does not need to be present to process the
predial parameter string.

* Don't start auto-service on a non-existent calling channel to execute
the predial handler when the dial API is in async mode and forwarding a
call.

ASTERISK-25384 #close
Reported by: Chet Stevens

Change-Id: If53892b286d29f6cf955e2545b03dcffa2610981

9 years agoResolve race conditions involving Stasis bridges. 98/1298/1
Mark Michelson [Thu, 18 Jun 2015 18:16:29 +0000 (13:16 -0500)] 
Resolve race conditions involving Stasis bridges.

This resolves two observed race conditions.

First, a bit of background on what the Stasis application does:

1a Creates a stasis_app_control structure. This structure is linked into
   a global container and can be looked up using a channel's unique ID.
2a Puts the channel in an event loop. The event loop can exit either
   because the stasis_app_control structure has been marked done, or
   because of some other factor, such as a hangup. In the event loop, the
   stasis_app_control determines if any specific ARI commands need to be
   run on the channel and will run them from this thread.
3a Checks if the channel is bridged. If the channel is bridged, then
   ast_bridge_depart() is called since channels that are added to Stasis
   bridges are always imparted as departable.
4a Unlink the stasis_app_control from the container.

When an ARI command is received by Asterisk, the following occurs
1b A thread is spawned to handle the HTTP request
2b The stasis_app_control(s) that corresponds to the channel(s) in the
   request is/are retrieved. If the stasis_app_control cannot be
   retrieved, then it is assumed that the channel in question has exited
   the Stasis app or perhaps was never in Stasis in the first place.
3b A command is queued onto the stasis_app_control, and the channel's
   event loop thread is signaled to run the command.
4b While most ARI commands do nothing further, some, such as adding or
   removing channels from a bridge, will block until the command they
   issued has been completed by the channel's event loop.

The first race condition that is solved by this patch involves a crash
that can occur due to faulty detection of the channel's bridged status
in step 3a. What can happen is that in step 2a, the event loop may run
the ast_bridge_impart() function to asynchronously place the channel
into a bridge, then immediately exit the event loop because the channel
has hung up. In step 3a, we would detect that the channel was not
bridged and would not call ast_bridge_depart(). The reason that the
channel did not appear to be bridged was that the depart_thread that is
spawned by ast_bridge_impart() had not yet started. That is the thread
where the channel is marked as being bridged. Since we did not call
ast_bridge_depart(), the Stasis application would exit, and then the
channel would be destroyed Then the depart_thread would start up and
try to manipulate the destroyed channel, causing a crash.

The fix for this is to switch from using ast_channel_is_bridged() to
checking the NULLity of ast_channel_internal_bridge_channel() to
determine if ast_bridge_depart() needs to be called. The channel's
internal bridge_channel is set when ast_bridge_impart() is called and
is NULLed by the call to ast_bridge_depart(). If the channel's internal
bridge_channel is non-NULL, then the channel must have been imparted
into the bridge and needs to be departed, even if the actual bridging
operation has not yet started. By departing the channel when necessary,
the thread that is running the Stasis application will block until the
bridge gives the okay that the depart_thread has exited.

The second race condition that is solved by this patch involves a leak
of HTTP handler threads. The problem was that step 2b would successfully
retrieve a stasis_app_control structure. Then step 2a would exit the
channel from the event loop due to a hangup. Steps 3a and 4a would
execute, and then finally steps 3b and 4b would. The problem is that at
step 4b, when attempting to add a channel to a bridge, the thread would
block forever since the channel would never execute the queued command
since it was finished with the event loop. This meant that the HTTP
handling thread would be leaked, along with any references that thread
may have owned (in my case, I was seeing bridges leaked).

The fix for this is to hone in better on when the channel has exited the
event loop. The stasis_app_control structure has an is_done field that
is now set at each point where the channel may exit the event loop. If
step 2b retrieves a valid stasis_app_control structure but the control
is marked as done, then the attempted operation exits immediately since
there will be nothing to service the attempted command.

ASTERISK-25091 #close
Reported by Ilya Trikoz

Change-Id: If66265b73b4c9f8f58599124d777fedc54576628

9 years agoMerge "app_record: RECORDED_FILE variable not being populated" into certified/13.1
Matt Jordan [Tue, 22 Sep 2015 12:40:25 +0000 (07:40 -0500)] 
Merge "app_record: RECORDED_FILE variable not being populated" into certified/13.1

9 years agoapp_record: RECORDED_FILE variable not being populated 92/1292/1
Kevin Harwell [Mon, 21 Sep 2015 23:06:15 +0000 (18:06 -0500)] 
app_record: RECORDED_FILE variable not being populated

The RECORDED_FILE variable is empty unless a '%d' is specified in the filename.
This patch makes it so the variable is always set to the filename.

ASTERISK-25410 #close

Change-Id: I4ec826d8eb582ae2ad184e717be8668b74d37653

9 years agopbx: Update device and presence state when changing a hint extension. 65/1265/3
Joshua Colp [Wed, 16 Sep 2015 13:22:35 +0000 (10:22 -0300)] 
pbx: Update device and presence state when changing a hint extension.

When changing a hint extension without removing the hint first the
device state and presence state is not updated. This causes the state
of the hint to be that of the previous extension and not the current
one. This state is kept until a state change occurs as a result of
something (presence state change, device state change).

This change updates the hint with the current device and presence
state of the new extension when it is changed. Any state callbacks
which may have been added before the hint extension is changed are
also informed of the new device and presence state if either have
changed.

ASTERISK-25394 #close

Change-Id: If268f1110290e502c73dd289c9e7e7b27bc8432f

9 years agores_pjsip_pubsub: Eliminate race during initial NOTIFY. 67/1267/2
Mark Michelson [Wed, 16 Sep 2015 22:36:32 +0000 (17:36 -0500)] 
res_pjsip_pubsub: Eliminate race during initial NOTIFY.

There is a slim chance of a race condition occurring where two threads
can both attempt to manipulate the same area.

Thread A can be handling an incoming initial SUBSCRIBE request. Thread A
lets the specific subscription handler know that the subscription has
been established.

At this point, Thread B may detect a state change on the subscribed
resource and queue up a notification task on Thread C, the subscription
serializer thread.

Now Thread A attempts to generate the initial NOTIFY request to send to
the subscriber at the same time that Thread C attempts to generate a
state change NOTIFY request to send to the subscriber.

The result is that Threads A and C can step on the same memory area,
resulting in a crash. The crash has been observed as happening when
attempting to allocate more space to hold the body for the NOTIFY.

The solution presented here is to queue the subscription establishment
and initial NOTIFY generation onto the subscription serializer thread
(Thread C in the above scenario). This way, there is no way that a state
change notification can occur before the initial NOTIFY is sent, and if
there is a quick succession of NOTIFYs, we can guarantee that the two
NOTIFY requests will be sent in succession.

Change-Id: I5a89a77b5f2717928c54d6efb9955e5f6f5cf815

9 years agoscheduler: Use queue for allocating sched IDs. 57/1257/4
Mark Michelson [Thu, 10 Sep 2015 22:19:26 +0000 (17:19 -0500)] 
scheduler: Use queue for allocating sched IDs.

It has been observed that on long-running busy systems, a scheduler
context can eventually hit INT_MAX for its assigned IDs and end up
overflowing into a very low negative number. When this occurs, this can
result in odd behaviors, because a negative return is interpreted by
callers as being a failure. However, the item actually was successfully
scheduled. The result may be that a freed item remains in the scheduler,
resulting in a crash at some point in the future.

The scheduler can overflow because every time that an item is added to
the scheduler, a counter is bumped and that counter's current value is
assigned as the new item's ID.

This patch introduces a new method for assigning scheduler IDs. Instead
of assigning from a counter, a queue of available IDs is maintained.
When assigning a new ID, an ID is pulled from the queue. When a
scheduler item is released, its ID is pushed back onto the queue. This
way, IDs may be reused when they become available, and the growth of ID
numbers is directly related to concurrent activity within a scheduler
context rather than the uptime of the system.

Change-Id: I532708eef8f669d823457d7fefdad9a6078b99b2

9 years agores_pjsip: Copy default_from_user to avoid crash. 37/1237/1
Mark Michelson [Thu, 10 Sep 2015 14:49:45 +0000 (09:49 -0500)] 
res_pjsip: Copy default_from_user to avoid crash.

The default_from_user retrieval function was pulling the
default_from_user from the global configuration struct in an unsafe way.
If using a database as a backend configuration store, the global
configuration struct is short-lived, so grabbing a pointer from it
results in referencing freed memory.

The fix here is to copy the default_from_user value out of the global
configuration struct.

Thanks go to John Hardin for discovering this problem and proposing the
patch on which this fix is based.

ASTERISK-25390 #close
Reported by Mark Michelson

Change-Id: I6b96067a495c1259da768f4012d44e03e7c6148c

9 years agores_pjsip: Validate that contact uris start with sip: or sips: 32/1232/1
George Joseph [Thu, 23 Apr 2015 14:16:45 +0000 (08:16 -0600)] 
res_pjsip:  Validate that contact uris start with sip: or sips:

Currently we use pjsip_parse_hdr to validate contact uris but it
appears that it allows uris without a scheme if there's a port
supplied.  I.E myexample.com will fail but myexample.com:5060 will
pass even though it has no scheme.  This causes SEGVs later on
whenever the uri is used.

To prevent this, permanent_contact_validate has been updated to check
that the scheme is either 'sip' or 'sips'.

2 uses of possibly-null endpoint have also been fixed in
create_out_of_dialog_request.

ASTERISK-24999

Change-Id: Ifc17d16a4923e1045d37fe51e43bbe29fa556ca2
Reported-by: Brad Latus
(cherry picked from commit 75666ad7c608ad9968a216a8f0a5832bf85b785c)

9 years agoParkAndAnnounce: Add variable inheritance 27/1227/1
Jonathan Rose [Thu, 3 Sep 2015 19:07:35 +0000 (14:07 -0500)] 
ParkAndAnnounce: Add variable inheritance

In Asterisk 11, the announcer channel would receive channel variables
from the channel being parked by means of normal channel inheritance.
This functionality was lost during the big res_parking project in
Asterisk 12. This patch restores that functionality.

ASTERISK-25369 #close
Review: https://gerrit.asterisk.org/#/c/1180/

Change-Id: Ie47e618330114ad2ea91e2edcef1cb6f341eed6e

9 years agotaskprocessor: Fix race condition between unreferencing and finding. 26/1226/1
Joshua Colp [Sat, 29 Aug 2015 15:36:35 +0000 (12:36 -0300)] 
taskprocessor: Fix race condition between unreferencing and finding.

When unreferencing a taskprocessor its reference count is checked
to determine if it should be unlinked from the taskprocessors
container and its listener shut down. In between the time when the
reference count is checked and unlinking it is possible for
another thread to jump in, find it, and get a reference to it. If
the thread then uses the taskprocessor it may find that it is not
in the state it expects.

This change locks the taskprocessors container during almost the
entire unreference operation to ensure that any other thread which
may attempt to find the taskprocessor has to wait.

ASTERISK-25295

Change-Id: Icb842db82fe1cf238da55df92e95938a4419377c
(cherry picked from commit a676ba2aad5525926ae31b8317b95ae52cbbabbb)

9 years agoMerge "res_pjsip: Change default from user value." into certified/13.1
Matt Jordan [Mon, 7 Sep 2015 16:37:17 +0000 (11:37 -0500)] 
Merge "res_pjsip: Change default from user value." into certified/13.1

9 years agores_pjsip: Change default from user value. 00/1200/3
Mark Michelson [Fri, 4 Sep 2015 19:40:38 +0000 (14:40 -0500)] 
res_pjsip: Change default from user value.

When Asterisk sends an outbound SIP request, if there is no direct
reason to place a specific value for the username in the From header,
Asterisk would generate a UUID. For example, this would happen when
sending outbound OPTIONS requests when qualifying or when sending
outbound INVITE requests when originating (if no explicit caller ID were
provided). The issue is that some SIP providers reject these sorts of
requests with a "Name too long" error response.

This patch aims to fix this by changing the default outbound username in
From headers to "asterisk". This value can be overridden by changing the
default_from_user option in the global options if desired.

ASTERISK-25377 #close
Reported by Mark Michelson

Change-Id: I6a4d34a56ff73ff4f661b0075aeba5461b7f3190

9 years agoMessage.c: Clear message channel frames on cleanup 86/1186/1
Jonathan Rose [Wed, 13 May 2015 20:41:07 +0000 (15:41 -0500)] 
Message.c: Clear message channel frames on cleanup

The message channel is a special channel that doesn't actually process frames.
However, certain actions can cause frames to be placed in the channel's read
queue including the Hangup application which is called on the channel after
each message is processed. Since the channel will continually be reused for
many messages, it's necessary to flush these frames at some point.

ASTERISK-25083 #close
Reported by: Jonathan Rose

Change-Id: Idf18df73ccd8c220be38743335b5c79c2a4c0d0f
(cherry picked from commit 02c513058905dae19f28393ea840a47ae4a9e66d)

9 years agores_pjsip: Fix contact refleak on stateful responses. 79/1179/1
Mark Michelson [Wed, 2 Sep 2015 22:26:14 +0000 (17:26 -0500)] 
res_pjsip: Fix contact refleak on stateful responses.

When sending a stateful response, creation of the transaction can fail,
most commonly because we are trying to create a transaction from a
retransmitted request. When creation of the transaction fails, we end up
leaking a reference to a contact that was bumped when the response was
created.

This patch adds the missing deref and fixes the reference leak.

Change-Id: I2f97ad512aeb1b17e87ca29ae0abacb4d6395f07

9 years agopbx: Fix crash when issuing "core show hints" with long pattern match. 75/1175/1
Joshua Colp [Wed, 2 Sep 2015 17:41:10 +0000 (14:41 -0300)] 
pbx: Fix crash when issuing "core show hints" with long pattern match.

When issuing the "core show hints" CLI command a combination of both
the hint extension and context is created. This uses a fixed size
buffer expecting that the extension will not exceed maximum extension
length. When the extension is actually a pattern match this constraint
does not hold true, and the extension may exceed the maximum extension
length. In this case extra characters are written past the end of the
fixed size buffer.

This change makes it so the construction of the combined hint extension
and context can not exceed the size of the buffer.

ASTERISK-25367 #close

Change-Id: Idfa1b95d0d4dc38e675be7c1de8900b3f981f499

9 years agoPJSIP XML, XPIDF: Fix buffer size overwrite memory corruption error. 72/1172/1
Richard Mudgett [Thu, 2 Jul 2015 19:51:29 +0000 (14:51 -0500)] 
PJSIP XML, XPIDF: Fix buffer size overwrite memory corruption error.

When res_pjsip body generator modules were generating XML or XPIDF
response bodies, there was a chance that the generated body would be the
exact size of the supplied buffer.  Adding the nul string terminator would
then write beyond the end of the buffer and potentially corrupt memory.

* Fix MALLOC_DEBUG high fence violations caused by adding a nul string
terminator on the end of a buffer for XML or XPIDF response bodies.

* Made calls to pj_xml_print() safer if the XML prolog is requested.  Due
to a bug in pjproject, the return value could be -1 _or_
AST_PJSIP_XML_PROLOG_LEN if the supplied buffer is not large enough.

* Updated the doxygen comment of AST_PJSIP_XML_PROLOG_LEN to describe the
return value of pj_xml_print() when the supplied buffer is not large
enough.

ASTERISK-25168
Reported by: Carl Fortin

Change-Id: Id70e1d373a6a2b2bd9e678b5cbc5e55b308981de

9 years agores_pjsip_pubsub: re-re-fix persistent subscription storage. 69/1169/2
Mark Michelson [Tue, 1 Sep 2015 14:05:34 +0000 (09:05 -0500)] 
res_pjsip_pubsub: re-re-fix persistent subscription storage.

A recent change to res_pjsip_pubsub switched to using pjsip_msg_print as
a means of writing an appropriate packet to persistent storage. While
this partially solved the issue, it had its own problems.
pjsip_msg_print will always add a Content-Length header to the message
it prints. Frequent restarts of Asterisk can result in persistent
subscriptions being written with five or more Content-Length headers. In
addition, sometimes some apparent corruption of individual headers could
be seen.

This aims to fix the problem by not running a parsed message through an
interpreter but rather by taking the raw message and saving it. The
logic for what to save is going to be different depending on whether a
SUBSCRIBE was received from the wire or if it was pulled from
persistence. When receiving a packet from the wire, when using a
streaming transport, the rdata->pkt_info.packet may contain multiple SIP
messages or fragments. However, the rdata->msg_info.msg_buf will always
contain the current SIP message to be processed. When pulling from
persistence, though, the rdata->msg_info.msg_buf will be NULL since no
transport actually handled the packet. However, since we know that we
will always ever pull one SIP message from persistence, we are free to
save directly from rdata->pkt_info.packet instead.

ASTERISK-25365 #close
Reported by Mark Michelson

Change-Id: I33153b10d0b4dc8e3801aaaee2f48173b867855b

9 years agoFix deadlock on presence state changes. 67/1167/1
Mark Michelson [Mon, 31 Aug 2015 20:24:17 +0000 (15:24 -0500)] 
Fix deadlock on presence state changes.

A deadlock was observed where three threads were competing for different
locks:

* One thread held the hints lock and was attempting to lock a specific
  hint.
* One thread was holding the specific hint's lock and was attempting to
  lock the contexts lock
* One thread was holding the contexts lock and attempting to lock the
  hints lock.

Clearly the second thread was doing the wrong thing here. The fix for
this is to make sure that the hint's lock is not held on presence state
changes. Something similar is already done (and commented about) for
device state changes.

ASTERISK-25362 #close
Reported by Mark Michelson

Change-Id: I15ec2416b92978a4c0c08273b2d46cb21aff97e2

9 years agoMerge "res_pjsip_sdp_rtp: Fix multiple keepalive scheduled items." into certified...
Matt Jordan [Sat, 29 Aug 2015 17:30:54 +0000 (12:30 -0500)] 
Merge "res_pjsip_sdp_rtp: Fix multiple keepalive scheduled items." into certified/13.1

9 years agores_pjsip_sdp_rtp: Fix multiple keepalive scheduled items. 61/1161/1
Joshua Colp [Sat, 29 Aug 2015 01:22:45 +0000 (22:22 -0300)] 
res_pjsip_sdp_rtp: Fix multiple keepalive scheduled items.

The keepalive support in res_pjsip_sdp_rtp currently assumes
that a stream will only be negotiated once. This is false.
If the stream is replaced and later added back it can be
negotiated again causing multiple keepalive scheduled items
to exist. This change explicitly deletes the existing
keepalive scheduled item before adding the new one.

The res_pjsip_sdp_rtp module also does not stop RTP
keepalives or timeout timer if the stream has been
replaced. This change adds a callback to the session media
interface to allow a media stream to be stopped without
the resources being destroyed. This allows the scheduled
items and RTP to be stopped when the stream no longer
exists.

ASTERISK-25356 #close

Change-Id: Ibe6a7cc0927c87326fd5f1c0d4ad889dbfbea1de

9 years agosched: ast_sched_del may return prematurely due to spurious wakeup 58/1158/1
Joshua Colp [Sat, 29 Aug 2015 00:57:14 +0000 (21:57 -0300)] 
sched: ast_sched_del may return prematurely due to spurious wakeup

When deleting a scheduled item if the item in question is currently
executing the ast_sched_del function waits until it has completed.
This is accomplished using ast_cond_wait. Unfortunately the
ast_cond_wait function can suffer from spurious wakeups so the
predicate needs to be checked after it returns to make sure it has
really woken up as a result of being signaled.

This change adds a loop around the ast_cond_wait to make sure that
it only exits when the executing task has really completed.

ASTERISK-25355 #close

Change-Id: I51198270eb0b637c956c61aa409f46283432be61

9 years agoMerge "Local channels: Alternate solution to ringback problem." into certified/13.1
Joshua Colp [Fri, 28 Aug 2015 11:30:55 +0000 (06:30 -0500)] 
Merge "Local channels: Alternate solution to ringback problem." into certified/13.1

9 years agoMerge "res_pjsip: Add common ast_sip_get_host_ip API." into certified/13.1
Joshua Colp [Thu, 27 Aug 2015 20:42:05 +0000 (15:42 -0500)] 
Merge "res_pjsip: Add common ast_sip_get_host_ip API." into certified/13.1

9 years agoLocal channels: Alternate solution to ringback problem. 50/1150/1
Mark Michelson [Thu, 23 Jul 2015 18:11:42 +0000 (13:11 -0500)] 
Local channels: Alternate solution to ringback problem.

Commit 54b25c80c8387aea9eb20f9f4f077486cbdf3e5d solved an issue where a
specific scenario involving local channels and a native local RTP bridge
could result in ringback still being heard on a calling channel even
after the call is bridged.

That commit caused many tests in the testsuite to fail with alarming
consequences, such as not sending DialBegin and DialEnd events, and
giving incorrect hangup causes during calls.

This commit reverts the previous commit and implements and alternate
solution. This new solution involves only passing AST_CONTROL_RINGING
frames across local channels if the local channel is in AST_STATE_RING.
Otherwise, the frame does not traverse the local channels. By doing
this, we can ensure that a playtones generator does not get started on
the calling channel but rather is started on the local channel on which
the ringing frame was initially indicated.

ASTERISK-25250 #close
Reported by Etienne Lessard

Change-Id: I3bc87a18a38eb2b68064f732d098edceb5c19f39

9 years agochan_sip: Allow call pickup to set the hangup cause. 43/1143/1
Joshua Colp [Wed, 26 Aug 2015 10:40:32 +0000 (07:40 -0300)] 
chan_sip: Allow call pickup to set the hangup cause.

The call pickup implementation in chan_sip currently sets the channel
hangup cause to "normal clearing" if call pickup is successfully
performed. This action overwrites the "answered elsewhere" hangup cause
set by the call pickup code and can result in the SIP device in
question showing a missed call when it should not.

This change sets the hangup cause to "normal clearing" as a
default initially but allows the call pickup to change it as
needed.

ASTERISK-25346 #close

Change-Id: I00ac2c269cee9e29586ee2c65e83c70e52a02cff

9 years agores_pjsip: Add common ast_sip_get_host_ip API. 38/1138/2
Joshua Colp [Tue, 25 Aug 2015 12:17:34 +0000 (09:17 -0300)] 
res_pjsip: Add common ast_sip_get_host_ip API.

Modules commonly used the pj_gethostip function for retrieving the
IP address of the host. This function does not cache the result and may
result in a DNS lookup occurring, or additional work. If the DNS
server is unreachable or network issues arise this can cause the
pj_gethostip function to block for a period of time.

This change adds an ast_sip_get_host_ip and ast_sip_get_host_ip_string
function which does the same thing but caches the host IP address at
module load time. This results in no additional work being done each
time the local host IP address is needed.

ASTERISK-25342 #close

Change-Id: I3205deb679b01fa5ac05a94b623bfd620a2abe1e

9 years agoMerge "bridge: Kick channel from bridge if hung up during action." into certified...
Mark Michelson [Mon, 24 Aug 2015 22:16:33 +0000 (17:16 -0500)] 
Merge "bridge: Kick channel from bridge if hung up during action." into certified/13.1

9 years agobridge: Kick channel from bridge if hung up during action. 35/1135/1
Joshua Colp [Mon, 24 Aug 2015 11:21:37 +0000 (08:21 -0300)] 
bridge: Kick channel from bridge if hung up during action.

When executing an action in a bridge it is possible for the
channel to be hung up without the bridge becoming aware of it.
This is most easily reproducible by hanging up when the bridge
is streaming DTMF due to a feature timeout. This change makes
it so after action execution the channel is checked to determine
if it has been hung up and if it has it is kicked from the bridge.

ASTERISK-25341 #close

Change-Id: I6dd8b0c3f5888da1c57afed9e8a802ae0a053062

9 years agores_pjsip_pubsub: On recreated notify fail deleted sub_tree is referenced 31/1131/1
Joshua Colp [Mon, 24 Aug 2015 16:04:57 +0000 (13:04 -0300)] 
res_pjsip_pubsub: On recreated notify fail deleted sub_tree is referenced

When recreating a subscription it is possible for a freed sub_tree
to be referenced when the initial NOTIFY fails to be created.

Change-Id: I681c215309aad01b21d611c2de47b3b0a6022788