summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/app_voicemail.c16
-rw-r--r--include/asterisk/_private.h1
-rw-r--r--include/asterisk/named_locks.h105
-rw-r--r--include/asterisk/pbx.h46
-rw-r--r--include/asterisk/res_pjsip.h43
-rw-r--r--main/asterisk.c5
-rw-r--r--main/core_unreal.c5
-rw-r--r--main/named_locks.c142
-rw-r--r--main/pbx.c120
-rw-r--r--res/res_pjsip/location.c44
-rw-r--r--res/res_pjsip_registrar.c55
-rw-r--r--res/res_pjsip_registrar_expire.c22
-rw-r--r--tests/test_named_lock.c151
-rw-r--r--third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch30
14 files changed, 690 insertions, 95 deletions
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index 0755c44b0..ce99cc9eb 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -580,6 +580,8 @@ static AST_LIST_HEAD_STATIC(vmstates, vmstate);
#define INTRO "vm-intro"
+#define MAX_MAIL_BODY_CONTENT_SIZE 134217728L // 128 Mbyte
+
#define MAXMSG 100
#define MAXMSGLIMIT 9999
@@ -3624,8 +3626,8 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
char *body_content;
char *body_decoded;
char *fn = is_intro ? vms->introfn : vms->fn;
- unsigned long len;
- unsigned long newlen;
+ unsigned long len = 0;
+ unsigned long newlen = 0;
char filename[256];
if (!body || body == NIL)
@@ -3634,12 +3636,18 @@ static int save_body(BODY *body, struct vm_state *vms, char *section, char *form
ast_mutex_lock(&vms->lock);
body_content = mail_fetchbody(vms->mailstream, vms->msgArray[vms->curmsg], section, &len);
ast_mutex_unlock(&vms->lock);
- if (body_content != NIL) {
+ if (len > MAX_MAIL_BODY_CONTENT_SIZE) {
+ ast_log(AST_LOG_ERROR,
+ "Msgno %ld, section %s. The body's content size %ld is huge (max %ld). User:%s, mailbox %s\n",
+ vms->msgArray[vms->curmsg], section, len, MAX_MAIL_BODY_CONTENT_SIZE, vms->imapuser, vms->username);
+ return -1;
+ }
+ if (body_content != NIL && len) {
snprintf(filename, sizeof(filename), "%s.%s", fn, format);
/* ast_debug(1, body_content); */
body_decoded = rfc822_base64((unsigned char *) body_content, len, &newlen);
/* If the body of the file is empty, return an error */
- if (!newlen) {
+ if (!newlen || !body_decoded) {
return -1;
}
write_file(filename, (char *) body_decoded, newlen);
diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h
index 2148a2082..ebee96cc4 100644
--- a/include/asterisk/_private.h
+++ b/include/asterisk/_private.h
@@ -38,6 +38,7 @@ int dnsmgr_reload(void); /*!< Provided by dnsmgr.c */
void threadstorage_init(void); /*!< Provided by threadstorage.c */
int ast_device_state_engine_init(void); /*!< Provided by devicestate.c */
int astobj2_init(void); /*!< Provided by astobj2.c */
+int ast_named_locks_init(void); /*!< Provided by named_locks.c */
int ast_file_init(void); /*!< Provided by file.c */
int ast_features_init(void); /*!< Provided by features.c */
void ast_autoservice_init(void); /*!< Provided by autoservice.c */
diff --git a/include/asterisk/named_locks.h b/include/asterisk/named_locks.h
new file mode 100644
index 000000000..0fe07d992
--- /dev/null
+++ b/include/asterisk/named_locks.h
@@ -0,0 +1,105 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Named Locks
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ */
+
+#ifndef INCLUDE_ASTERISK_NAMED_LOCKS_H_
+#define INCLUDE_ASTERISK_NAMED_LOCKS_H_
+
+#include "asterisk/astobj2.h"
+
+/*!
+ * \defgroup named_locks Named mutex and read-write locks
+ * @{
+ * \page NamedLocks Named mutex and read-write locks
+ * \since 13.9.0
+ *
+ * Locking some objects like sorcery objects can be tricky because the underlying
+ * ao2 object may not be the same for all callers. For instance, two threads that
+ * call ast_sorcery_retrieve_by_id on the same aor name might actually get 2 different
+ * ao2 objects if the underlying wizard had to rehydrate the aor from a database.
+ * Locking one ao2 object doesn't have any effect on the other even if those objects
+ * had locks in the first place
+ *
+ * Named locks allow access control by name. Now an aor named "1000" can be locked and
+ * any other thread attempting to lock the aor named "1000" will wait regardless of whether
+ * the underlying ao2 object is the same or not.
+ *
+ * To use a named lock:
+ * Call ast_named_lock_get with the appropriate keyspace and key.
+ * Use the standard ao2 lock/unlock functions as needed.
+ * Call ast_named_lock_put when you're finished with it.
+ */
+
+/*!
+ * \brief Which type of lock to request.
+ */
+enum ast_named_lock_type {
+ /*! Request a named mutex. */
+ AST_NAMED_LOCK_TYPE_MUTEX = AO2_ALLOC_OPT_LOCK_MUTEX,
+ /*! Request a named read/write lock. */
+ AST_NAMED_LOCK_TYPE_RWLOCK = AO2_ALLOC_OPT_LOCK_RWLOCK,
+};
+
+struct ast_named_lock;
+
+struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
+ enum ast_named_lock_type lock_type, const char *keyspace, const char *key);
+
+int __ast_named_lock_put(const char *filename, int lineno, const char *func,
+ struct ast_named_lock *lock);
+
+/*!
+ * \brief Geta named lock handle
+ * \since 13.9.0
+ *
+ * \param lock_type One of ast_named_lock_type
+ * \param keyspace
+ * \param key
+ * \retval A pointer to an ast_named_lock structure
+ * \retval NULL on error
+ *
+ * \note
+ * keyspace and key can be anything. For sorcery objects, keyspace could be the object type
+ * and key could be the object id.
+ */
+#define ast_named_lock_get(lock_type, keyspace, key) \
+ __ast_named_lock_get(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock_type, \
+ keyspace, key)
+
+/*!
+ * \brief Put a named lock handle away
+ * \since 13.9.0
+ *
+ * \param lock The pointer to the ast_named_lock structure returned by ast_named_lock_get
+ * \retval 0 Success
+ * \retval -1 Failure
+ */
+#define ast_named_lock_put(lock) \
+ __ast_named_lock_put(__FILE__, __LINE__, __PRETTY_FUNCTION__, lock)
+
+/*!
+ * @}
+ */
+
+#endif /* INCLUDE_ASTERISK_NAMED_LOCKS_H_ */
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index 599745878..d722e123f 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -573,70 +573,80 @@ int ast_hint_presence_state(struct ast_channel *c, const char *context, const ch
const char *ast_extension_state2str(int extension_state);
/*!
- * \brief Registers a state change callback with destructor.
+ * \brief Add watcher for extension states with destructor.
* \since 1.8.9
* \since 10.1.0
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
- * \param destroy_cb callback to call when registration destroyed.
- * \param data to pass to callback
+ * \param destroy_cb callback to call when the watcher is destroyed.
+ * \param data to pass to callbacks
+ *
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
*
* \note The change_cb is called if the state of an extension is changed.
*
- * \note The destroy_cb is called when the registration is
- * deleted so the registerer can release any associated
- * resources.
+ * \note The destroy_cb is called when the watcher is deleted so the
+ * watcher can release any associated resources.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_destroy(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data);
/*!
- * \brief Registers an extended state change callback with destructor.
+ * \brief Add watcher for extended extension states with destructor.
* \since 11
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
- * \param destroy_cb callback to call when registration destroyed.
- * \param data to pass to callback
+ * \param destroy_cb callback to call when the watcher is destroyed.
+ * \param data to pass to callbacks
+ *
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
*
* \note The change_cb is called if the state of an extension is changed.
* The extended state is passed to the callback in the device_state_info
* member of ast_state_cb_info.
*
- * \note The destroy_cb is called when the registration is
- * deleted so the registerer can release any associated
- * resources.
+ * \note The destroy_cb is called when the watcher is deleted so the
+ * watcher can release any associated resources.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_destroy_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data);
/*!
- * \brief Registers a state change callback
+ * \brief Add watcher for extension states.
*
* \param context which context to look in
* \param exten which extension to get state
* \param change_cb callback to call if state changed
* \param data to pass to callback
*
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
+ *
* \note The change_cb is called if the state of an extension is changed.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data);
/*!
- * \brief Registers an extended state change callback
+ * \brief Add watcher for extended extension states.
* \since 11
*
* \param context which context to look in
@@ -644,20 +654,24 @@ int ast_extension_state_add(const char *context, const char *exten,
* \param change_cb callback to call if state changed
* \param data to pass to callback
*
+ * \note If context and exten are NULL then the added watcher is global.
+ * The change_cb is called for every extension's state change.
+ *
* \note The change_cb is called if the state of an extension is changed.
* The extended state is passed to the callback in the device_state_info
* member of ast_state_cb_info.
*
* \retval -1 on failure
+ * \retval 0 Global watcher added successfully
* \retval ID on success
*/
int ast_extension_state_add_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data);
/*!
- * \brief Deletes a registered state change callback by ID
+ * \brief Deletes a state change watcher by ID
*
- * \param id of the registered state callback to delete
+ * \param id of the state watcher to delete (0 for global watcher)
* \param change_cb callback to call if state changed (Used if id == 0 (global))
*
* \retval 0 success
diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h
index fbb9bdcbe..3a9d61e4c 100644
--- a/include/asterisk/res_pjsip.h
+++ b/include/asterisk/res_pjsip.h
@@ -1004,10 +1004,28 @@ struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct
*
* \retval NULL if no contacts available
* \retval non-NULL if contacts available
+ *
+ * \warning
+ * Since this function prunes expired contacts before returning, it holds a named write
+ * lock on the aor. If you already hold the lock, call ast_sip_location_retrieve_aor_contacts_nolock instead.
*/
struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor);
/*!
+ * \brief Retrieve all contacts currently available for an AOR without locking the AOR
+ * \since 13.9.0
+ *
+ * \param aor Pointer to the AOR
+ *
+ * \retval NULL if no contacts available
+ * \retval non-NULL if contacts available
+ *
+ * \warning
+ * This function should only be called if you already hold a named write lock on the aor.
+ */
+struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock(const struct ast_sip_aor *aor);
+
+/*!
* \brief Retrieve the first bound contact from a list of AORs
*
* \param aor_list A comma-separated list of AOR names
@@ -1057,12 +1075,37 @@ struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_na
*
* \retval -1 failure
* \retval 0 success
+ *
+ * \warning
+ * This function holds a named write lock on the aor. If you already hold the lock
+ * you should call ast_sip_location_add_contact_nolock instead.
*/
int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
struct timeval expiration_time, const char *path_info, const char *user_agent,
struct ast_sip_endpoint *endpoint);
/*!
+ * \brief Add a new contact to an AOR without locking the AOR
+ * \since 13.9.0
+ *
+ * \param aor Pointer to the AOR
+ * \param uri Full contact URI
+ * \param expiration_time Optional expiration time of the contact
+ * \param path_info Path information
+ * \param user_agent User-Agent header from REGISTER request
+ * \param endpoint The endpoint that resulted in the contact being added
+ *
+ * \retval -1 failure
+ * \retval 0 success
+ *
+ * \warning
+ * This function should only be called if you already hold a named write lock on the aor.
+ */
+int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri,
+ struct timeval expiration_time, const char *path_info, const char *user_agent,
+ struct ast_sip_endpoint *endpoint);
+
+/*!
* \brief Update a contact
*
* \param contact New contact object with details
diff --git a/main/asterisk.c b/main/asterisk.c
index 5901d579c..ea998d492 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -4391,6 +4391,11 @@ static void asterisk_daemon(int isroot, const char *runuser, const char *rungrou
exit(1);
}
+ if (ast_named_locks_init()) {
+ printf("Failed: ast_named_locks_init\n%s", term_quit());
+ exit(1);
+ }
+
if (ast_opt_console) {
if (el_hist == NULL || el == NULL)
ast_el_initialize();
diff --git a/main/core_unreal.c b/main/core_unreal.c
index fc311ec5d..e9b7a8d66 100644
--- a/main/core_unreal.c
+++ b/main/core_unreal.c
@@ -566,6 +566,11 @@ int ast_unreal_indicate(struct ast_channel *ast, int condition, const void *data
res = -1;
}
break;
+ case AST_CONTROL_PVT_CAUSE_CODE:
+ /* Return -1 so that asterisk core will correctly set up hangupcauses. */
+ unreal_queue_indicate(p, ast, condition, data, datalen);
+ res = -1;
+ break;
default:
res = unreal_queue_indicate(p, ast, condition, data, datalen);
break;
diff --git a/main/named_locks.c b/main/named_locks.c
new file mode 100644
index 000000000..b977b553c
--- /dev/null
+++ b/main/named_locks.c
@@ -0,0 +1,142 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Named Locks
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_REGISTER_FILE()
+
+#include "asterisk/_private.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/named_locks.h"
+#include "asterisk/utils.h"
+
+struct ao2_container *named_locks;
+#define NAMED_LOCKS_BUCKETS 101
+
+struct ast_named_lock {
+ char key[0];
+};
+
+static int named_locks_hash(const void *obj, const int flags)
+{
+ const struct ast_named_lock *lock = obj;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_KEY:
+ return ast_str_hash(obj);
+ case OBJ_SEARCH_OBJECT:
+ return ast_str_hash(lock->key);
+ default:
+ /* Hash can only work on something with a full key. */
+ ast_assert(0);
+ return 0;
+ }
+}
+
+static int named_locks_cmp(void *obj_left, void *obj_right, int flags)
+{
+ const struct ast_named_lock *object_left = obj_left;
+ const struct ast_named_lock *object_right = obj_right;
+ const char *right_key = obj_right;
+ int cmp;
+
+ switch (flags & OBJ_SEARCH_MASK) {
+ case OBJ_SEARCH_OBJECT:
+ right_key = object_right->key;
+ /* Fall through */
+ case OBJ_SEARCH_KEY:
+ cmp = strcmp(object_left->key, right_key);
+ break;
+ case OBJ_SEARCH_PARTIAL_KEY:
+ cmp = strncmp(object_left->key, right_key, strlen(right_key));
+ break;
+ default:
+ cmp = 0;
+ break;
+ }
+
+ return cmp ? 0 : CMP_MATCH;
+}
+
+static void named_locks_shutdown(void)
+{
+ ao2_cleanup(named_locks);
+}
+
+int ast_named_locks_init(void)
+{
+ named_locks = ao2_container_alloc_hash(0, 0, NAMED_LOCKS_BUCKETS, named_locks_hash, NULL,
+ named_locks_cmp);
+ if (!named_locks) {
+ return -1;
+ }
+
+ ast_register_cleanup(named_locks_shutdown);
+
+ return 0;
+}
+
+struct ast_named_lock *__ast_named_lock_get(const char *filename, int lineno, const char *func,
+ enum ast_named_lock_type lock_type, const char *keyspace, const char *key)
+{
+ struct ast_named_lock *lock = NULL;
+ int concat_key_buff_len = strlen(keyspace) + strlen(key) + 2;
+ char *concat_key = ast_alloca(concat_key_buff_len);
+
+ sprintf(concat_key, "%s-%s", keyspace, key); /* Safe */
+
+ ao2_lock(named_locks);
+ lock = ao2_find(named_locks, concat_key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
+ if (lock) {
+ ao2_unlock(named_locks);
+ ast_assert((ao2_options_get(lock) & AO2_ALLOC_OPT_LOCK_MASK) == lock_type);
+ return lock;
+ }
+
+ lock = ao2_alloc_options(sizeof(*lock) + concat_key_buff_len, NULL, lock_type);
+ if (lock) {
+ strcpy(lock->key, concat_key); /* Safe */
+ ao2_link_flags(named_locks, lock, OBJ_NOLOCK);
+ }
+ ao2_unlock(named_locks);
+
+ return lock;
+}
+
+int __ast_named_lock_put(const char *filename, int lineno, const char *func,
+ struct ast_named_lock *lock)
+{
+ if (!lock) {
+ return -1;
+ }
+
+ ao2_lock(named_locks);
+ if (ao2_ref(lock, -1) == 2) {
+ ao2_unlink_flags(named_locks, lock, OBJ_NOLOCK);
+ }
+ ao2_unlock(named_locks);
+
+ return 0;
+}
diff --git a/main/pbx.c b/main/pbx.c
index b166978a1..6b0069c06 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3252,7 +3252,8 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
{
struct ao2_iterator cb_iter;
struct ast_state_cb *state_cb;
- int state, same_state;
+ int state;
+ int same_state;
struct ao2_container *device_state_info;
int first_extended_cb_call = 1;
char context_name[AST_MAX_CONTEXT];
@@ -3291,7 +3292,8 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
device_state_info = alloc_device_state_info();
state = ast_extension_state3(*hint_app, device_state_info);
- if ((same_state = state == hint->laststate) && (~state & AST_EXTENSION_RINGING)) {
+ same_state = state == hint->laststate;
+ if (same_state && (~state & AST_EXTENSION_RINGING)) {
ao2_cleanup(device_state_info);
return;
}
@@ -3300,17 +3302,19 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
hint->laststate = state; /* record we saw the change */
/* For general callbacks */
- cb_iter = ao2_iterator_init(statecbs, 0);
- for (; !same_state && (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
- execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint,
- NULL);
+ if (!same_state) {
+ cb_iter = ao2_iterator_init(statecbs, 0);
+ for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
+ execute_state_callback(state_cb->change_cb,
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint,
+ NULL);
+ }
+ ao2_iterator_destroy(&cb_iter);
}
- ao2_iterator_destroy(&cb_iter);
/* For extension callbacks */
/* extended callbacks are called when the state changed or when AST_STATE_RINGING is
@@ -3325,12 +3329,12 @@ static void device_state_notify_callbacks(struct ast_hint *hint, struct ast_str
}
if (state_cb->extended || !same_state) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint,
- state_cb->extended ? device_state_info : NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint,
+ state_cb->extended ? device_state_info : NULL);
}
}
ao2_iterator_destroy(&cb_iter);
@@ -3422,12 +3426,12 @@ static void presence_state_notify_callbacks(
cb_iter = ao2_iterator_init(statecbs, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_PRESENCE,
- hint,
- NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_PRESENCE,
+ hint,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
@@ -3435,12 +3439,12 @@ static void presence_state_notify_callbacks(
cb_iter = ao2_iterator_init(hint->callbacks, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) {
execute_state_callback(state_cb->change_cb,
- context_name,
- exten_name,
- state_cb->data,
- AST_HINT_UPDATE_PRESENCE,
- hint,
- NULL);
+ context_name,
+ exten_name,
+ state_cb->data,
+ AST_HINT_UPDATE_PRESENCE,
+ hint,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
}
@@ -3460,27 +3464,32 @@ static int handle_hint_change_message_type(struct stasis_message *msg, enum ast_
hint = stasis_message_data(msg);
- if (reason == AST_HINT_UPDATE_DEVICE) {
+ switch (reason) {
+ case AST_HINT_UPDATE_DEVICE:
device_state_notify_callbacks(hint, &hint_app);
- } else if (reason == AST_HINT_UPDATE_PRESENCE) {
- char *presence_subtype = NULL;
- char *presence_message = NULL;
- int state;
+ break;
+ case AST_HINT_UPDATE_PRESENCE:
+ {
+ char *presence_subtype = NULL;
+ char *presence_message = NULL;
+ int state;
- state = extension_presence_state_helper(
- hint->exten, &presence_subtype, &presence_message);
+ state = extension_presence_state_helper(
+ hint->exten, &presence_subtype, &presence_message);
+ {
+ struct ast_presence_state_message presence_state = {
+ .state = state > 0 ? state : AST_PRESENCE_INVALID,
+ .subtype = presence_subtype,
+ .message = presence_message
+ };
- {
- struct ast_presence_state_message presence_state = {
- .state = state > 0 ? state : AST_PRESENCE_INVALID,
- .subtype = presence_subtype,
- .message = presence_message
- };
- presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
- }
+ presence_state_notify_callbacks(msg, hint, &hint_app, &presence_state);
+ }
- ast_free(presence_subtype);
- ast_free(presence_message);
+ ast_free(presence_subtype);
+ ast_free(presence_message);
+ }
+ break;
}
ast_free(hint_app);
@@ -3655,28 +3664,24 @@ static int extension_state_add_destroy(const char *context, const char *exten,
return id;
}
-/*! \brief Add watcher for extension states with destructor */
int ast_extension_state_add_destroy(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 0);
}
-/*! \brief Add watcher for extension states */
int ast_extension_state_add(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, NULL, data, 0);
}
-/*! \brief Add watcher for extended extension states with destructor */
int ast_extension_state_add_destroy_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
{
return extension_state_add_destroy(context, exten, change_cb, destroy_cb, data, 1);
}
-/*! \brief Add watcher for extended extension states */
int ast_extension_state_add_extended(const char *context, const char *exten,
ast_state_cb_type change_cb, void *data)
{
@@ -3698,7 +3703,6 @@ static int find_hint_by_cb_id(void *obj, void *arg, int flags)
return 0;
}
-/*! \brief ast_extension_state_del: Remove a watcher from the callback list */
int ast_extension_state_del(int id, ast_state_cb_type change_cb)
{
struct ast_state_cb *p_cur;
@@ -3904,12 +3908,12 @@ static int ast_add_hint(struct ast_exten *e)
cb_iter = ao2_iterator_init(statecbs, 0);
for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
execute_state_callback(state_cb->change_cb,
- ast_get_context_name(ast_get_extension_context(e)),
- ast_get_extension_name(e),
- state_cb->data,
- AST_HINT_UPDATE_DEVICE,
- hint_new,
- NULL);
+ ast_get_context_name(ast_get_extension_context(e)),
+ ast_get_extension_name(e),
+ state_cb->data,
+ AST_HINT_UPDATE_DEVICE,
+ hint_new,
+ NULL);
}
ao2_iterator_destroy(&cb_iter);
}
diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c
index 735b18f46..3413708e9 100644
--- a/res/res_pjsip/location.c
+++ b/res/res_pjsip/location.c
@@ -27,6 +27,7 @@
#include "include/res_pjsip_private.h"
#include "asterisk/res_pjsip_cli.h"
#include "asterisk/statsd.h"
+#include "asterisk/named_locks.h"
/*! \brief Destructor for AOR */
static void aor_destroy(void *obj)
@@ -173,7 +174,7 @@ struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct
return contact;
}
-struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
+struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock(const struct ast_sip_aor *aor)
{
/* Give enough space for ^ at the beginning and ;@ at the end, since that is our object naming scheme */
char regex[strlen(ast_sorcery_object_get_id(aor)) + 4];
@@ -196,6 +197,24 @@ struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_si
return contacts;
}
+struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
+{
+ struct ao2_container *contacts;
+ struct ast_named_lock *lock;
+
+ lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_RWLOCK, "aor", ast_sorcery_object_get_id(aor));
+ if (!lock) {
+ return NULL;
+ }
+
+ ao2_wrlock(lock);
+ contacts = ast_sip_location_retrieve_aor_contacts_nolock(aor);
+ ao2_unlock(lock);
+ ast_named_lock_put(lock);
+
+ return contacts;
+}
+
void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
struct ast_sip_contact **contact)
{
@@ -279,7 +298,7 @@ struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_na
return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
}
-int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
+int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri,
struct timeval expiration_time, const char *path_info, const char *user_agent,
struct ast_sip_endpoint *endpoint)
{
@@ -316,6 +335,27 @@ int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
return ast_sorcery_create(ast_sip_get_sorcery(), contact);
}
+int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
+ struct timeval expiration_time, const char *path_info, const char *user_agent,
+ struct ast_sip_endpoint *endpoint)
+{
+ int res;
+ struct ast_named_lock *lock;
+
+ lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_RWLOCK, "aor", ast_sorcery_object_get_id(aor));
+ if (!lock) {
+ return -1;
+ }
+
+ ao2_wrlock(lock);
+ res = ast_sip_location_add_contact_nolock(aor, uri, expiration_time, path_info, user_agent,
+ endpoint);
+ ao2_unlock(lock);
+ ast_named_lock_put(lock);
+
+ return res;
+}
+
int ast_sip_location_update_contact(struct ast_sip_contact *contact)
{
return ast_sorcery_update(ast_sip_get_sorcery(), contact);
diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c
index fb2b9daba..4a42102dc 100644
--- a/res/res_pjsip_registrar.c
+++ b/res/res_pjsip_registrar.c
@@ -32,6 +32,7 @@
#include "asterisk/test.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/manager.h"
+#include "asterisk/named_locks.h"
#include "res_pjsip/include/res_pjsip_private.h"
/*** DOCUMENTATION
@@ -412,27 +413,21 @@ static int registrar_validate_path(struct rx_task_data *task_data, struct ast_st
return -1;
}
-static int rx_task(void *data)
+static int rx_task_core(struct rx_task_data *task_data, struct ao2_container *contacts,
+ const char *aor_name)
{
static const pj_str_t USER_AGENT = { "User-Agent", 10 };
- RAII_VAR(struct rx_task_data *, task_data, data, ao2_cleanup);
- RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
-
int added = 0, updated = 0, deleted = 0;
pjsip_contact_hdr *contact_hdr = NULL;
struct registrar_contact_details details = { 0, };
pjsip_tx_data *tdata;
- const char *aor_name = ast_sorcery_object_get_id(task_data->aor);
RAII_VAR(struct ast_str *, path_str, NULL, ast_free);
struct ast_sip_contact *response_contact;
char *user_agent = NULL;
pjsip_user_agent_hdr *user_agent_hdr;
pjsip_expires_hdr *expires_hdr;
- /* Retrieve the current contacts, we'll need to know whether to update or not */
- contacts = ast_sip_location_retrieve_aor_contacts(task_data->aor);
-
/* So we don't count static contacts against max_contacts we prune them out from the container */
ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, registrar_prune_static, NULL);
@@ -503,7 +498,7 @@ static int rx_task(void *data)
continue;
}
- if (ast_sip_location_add_contact(task_data->aor, contact_uri, ast_tvadd(ast_tvnow(),
+ if (ast_sip_location_add_contact_nolock(task_data->aor, contact_uri, ast_tvadd(ast_tvnow(),
ast_samp2tv(expiration, 1)), path_str ? ast_str_buffer(path_str) : NULL,
user_agent, task_data->endpoint)) {
ast_log(LOG_ERROR, "Unable to bind contact '%s' to AOR '%s'\n",
@@ -545,7 +540,7 @@ static int rx_task(void *data)
if (ast_sip_location_update_contact(contact_update)) {
ast_log(LOG_ERROR, "Failed to update contact '%s' expiration time to %d seconds.\n",
contact->uri, expiration);
- ast_sorcery_delete(ast_sip_get_sorcery(), contact);
+ ast_sip_location_delete_contact(contact);
continue;
}
ast_debug(3, "Refreshed contact '%s' on AOR '%s' with new expiration of %d seconds\n",
@@ -584,15 +579,14 @@ static int rx_task(void *data)
ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE, registrar_delete_contact, NULL);
}
- /* Update the contacts as things will probably have changed */
- ao2_cleanup(contacts);
-
- contacts = ast_sip_location_retrieve_aor_contacts(task_data->aor);
+ /* Re-retrieve contacts. Caller will clean up the original container. */
+ contacts = ast_sip_location_retrieve_aor_contacts_nolock(task_data->aor);
response_contact = ao2_callback(contacts, 0, NULL, NULL);
/* Send a response containing all of the contacts (including static) that are present on this AOR */
if (ast_sip_create_response(task_data->rdata, 200, response_contact, &tdata) != PJ_SUCCESS) {
ao2_cleanup(response_contact);
+ ao2_cleanup(contacts);
return PJ_TRUE;
}
ao2_cleanup(response_contact);
@@ -601,6 +595,7 @@ static int rx_task(void *data)
registrar_add_date_header(tdata);
ao2_callback(contacts, 0, registrar_add_contact, tdata);
+ ao2_cleanup(contacts);
if ((expires_hdr = pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL))) {
expires_hdr = pjsip_expires_hdr_create(tdata->pool, registrar_get_expiration(task_data->aor, NULL, task_data->rdata));
@@ -612,6 +607,38 @@ static int rx_task(void *data)
return PJ_TRUE;
}
+static int rx_task(void *data)
+{
+ int res;
+ struct rx_task_data *task_data = data;
+ struct ao2_container *contacts = NULL;
+ struct ast_named_lock *lock;
+ const char *aor_name = ast_sorcery_object_get_id(task_data->aor);
+
+ lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_RWLOCK, "aor", aor_name);
+ if (!lock) {
+ ao2_cleanup(task_data);
+ return PJ_TRUE;
+ }
+
+ ao2_wrlock(lock);
+ contacts = ast_sip_location_retrieve_aor_contacts_nolock(task_data->aor);
+ if (!contacts) {
+ ao2_unlock(lock);
+ ast_named_lock_put(lock);
+ ao2_cleanup(task_data);
+ return PJ_TRUE;
+ }
+
+ res = rx_task_core(task_data, contacts, aor_name);
+ ao2_cleanup(contacts);
+ ao2_unlock(lock);
+ ast_named_lock_put(lock);
+ ao2_cleanup(task_data);
+
+ return res;
+}
+
static pj_bool_t registrar_on_rx_request(struct pjsip_rx_data *rdata)
{
RAII_VAR(struct serializer *, ser, NULL, ao2_cleanup);
diff --git a/res/res_pjsip_registrar_expire.c b/res/res_pjsip_registrar_expire.c
index bdb86e50e..0fddbda91 100644
--- a/res/res_pjsip_registrar_expire.c
+++ b/res/res_pjsip_registrar_expire.c
@@ -30,6 +30,7 @@
#include "asterisk/res_pjsip.h"
#include "asterisk/module.h"
+#include "asterisk/named_locks.h"
/*! \brief Thread keeping things alive */
static pthread_t check_thread = AST_PTHREADT_NULL;
@@ -41,8 +42,23 @@ static unsigned int check_interval;
static int expire_contact(void *obj, void *arg, int flags)
{
struct ast_sip_contact *contact = obj;
+ struct ast_named_lock *lock;
- ast_sorcery_delete(ast_sip_get_sorcery(), contact);
+ lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_RWLOCK, "aor", contact->aor);
+ if (!lock) {
+ return 0;
+ }
+
+ /*
+ * We need to check the expiration again with the aor lock held
+ * in case another thread is attempting to renew the contact.
+ */
+ ao2_wrlock(lock);
+ if (ast_tvdiff_ms(ast_tvnow(), contact->expiration_time) > 0) {
+ ast_sip_location_delete_contact(contact);
+ }
+ ao2_unlock(lock);
+ ast_named_lock_put(lock);
return 0;
}
@@ -91,6 +107,7 @@ static void expiration_global_loaded(const char *object_type)
} else {
if (check_thread != AST_PTHREADT_NULL) {
pthread_kill(check_thread, SIGURG);
+ pthread_join(check_thread, NULL);
check_thread = AST_PTHREADT_NULL;
ast_debug(3, "Interval = 0, shutting thread down\n");
}
@@ -105,7 +122,10 @@ static struct ast_sorcery_observer expiration_global_observer = {
static int unload_module(void)
{
if (check_thread != AST_PTHREADT_NULL) {
+ check_interval = 0;
pthread_kill(check_thread, SIGURG);
+ pthread_join(check_thread, NULL);
+
check_thread = AST_PTHREADT_NULL;
}
diff --git a/tests/test_named_lock.c b/tests/test_named_lock.c
new file mode 100644
index 000000000..9e383088e
--- /dev/null
+++ b/tests/test_named_lock.c
@@ -0,0 +1,151 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2016, Fairview 5 Engineering, LLC
+ *
+ * George Joseph <george.joseph@fairview5.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Named Lock unit tests
+ *
+ * \author George Joseph <george.joseph@fairview5.com>
+ *
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+#include <signal.h>
+
+#include "asterisk/test.h"
+#include "asterisk/utils.h"
+#include "asterisk/module.h"
+#include "asterisk/lock.h"
+#include "asterisk/named_locks.h"
+
+static void *lock_thread(void *data)
+{
+ struct ast_named_lock *lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", data);
+
+ if (!lock) {
+ return NULL;
+ }
+
+ ao2_lock(lock);
+ usleep(3000000);
+ ao2_unlock(lock);
+
+ ast_named_lock_put(lock);
+
+ return NULL;
+}
+
+AST_TEST_DEFINE(named_lock_test)
+{
+ enum ast_test_result_state res = AST_TEST_FAIL;
+ struct ast_named_lock *lock1 = NULL;
+ struct ast_named_lock *lock2 = NULL;
+ pthread_t thread1;
+ pthread_t thread2;
+ struct timeval start_time;
+ int64_t duration;
+
+ switch(cmd) {
+ case TEST_INIT:
+ info->name = "named_lock_test";
+ info->category = "/main/lock/";
+ info->summary = "Named Lock test";
+ info->description =
+ "Tests that named locks operate as expected";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ ast_test_status_update(test, "This test should take about 3 seconds\n");
+
+ /* 2 locks/threads to make sure they're independent */
+ ast_pthread_create(&thread1, NULL, lock_thread, "lock_1");
+ ast_pthread_create(&thread2, NULL, lock_thread, "lock_2");
+
+ lock1 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_1");
+ ast_test_validate_cleanup(test, lock1 != NULL, res, fail);
+
+ lock2 = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "lock_test", "lock_2");
+ ast_test_validate_cleanup(test, lock2 != NULL, res, fail);
+
+ usleep(1000000);
+
+ /* These should both fail */
+ if (!ao2_trylock(lock1)) {
+ ast_test_status_update(test, "ao2_trylock on lock1 succeeded when it should have failed\n");
+ ao2_unlock(lock1);
+ goto fail;
+ }
+
+ if (!ao2_trylock(lock2)) {
+ ast_test_status_update(test, "ao2_trylock on lock2 succeeded when it should have failed\n");
+ ao2_unlock(lock2);
+ goto fail;
+ }
+
+ start_time = ast_tvnow();
+
+ /* These should both succeed eventually */
+ if (ao2_lock(lock1)) {
+ ast_test_status_update(test, "ao2_lock on lock1 failed\n");
+ goto fail;
+ }
+ ao2_unlock(lock1);
+
+ if (ao2_lock(lock2)) {
+ ast_test_status_update(test, "ao2_lock on lock2 failed\n");
+ goto fail;
+ }
+ ao2_unlock(lock2);
+
+ duration = ast_tvdiff_ms(ast_tvnow(), start_time);
+ ast_test_validate_cleanup(test, duration > 1500 && duration < 3500, res, fail);
+
+ res = AST_TEST_PASS;
+
+fail:
+
+ ast_named_lock_put(lock1);
+ ast_named_lock_put(lock2);
+
+ pthread_join(thread1, NULL);
+ pthread_join(thread2, NULL);
+
+ return res;
+}
+
+
+static int unload_module(void)
+{
+ AST_TEST_UNREGISTER(named_lock_test);
+ return 0;
+}
+
+static int load_module(void)
+{
+ AST_TEST_REGISTER(named_lock_test);
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Named Lock test module");
diff --git a/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch b/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch
new file mode 100644
index 000000000..60c27cb1a
--- /dev/null
+++ b/third-party/pjproject/patches/0001-sip_parser.c-Fix-pjsip_VIA_PARAM_SPEC_ESC.patch
@@ -0,0 +1,30 @@
+From 0fc7ef5f01be9cc74d184c3ca3a973ff1ef44c93 Mon Sep 17 00:00:00 2001
+From: George Joseph <george.joseph@fairview5.com>
+Date: Sun, 10 Apr 2016 12:54:06 -0600
+Subject: [PATCH] sip_parser.c: Fix pjsip_VIA_PARAM_SPEC_ESC
+
+pjsip_VIA_PARAM_SPEC_ESC should have been pjsip_TOKEN_SPEC_ESC + ":" but
+instead of appending ":" to pjsip_VIA_PARAM_SPEC_ESC it was being appended
+to pjsip_VIA_PARAM_SPEC again. This was causing parsing of Via headers
+to fail when an ipv6 address was in a "received" param and
+PJSIP_UNESCAPE_IN_PLACE was used. Probably just a copy/paste error.
+---
+ pjsip/src/pjsip/sip_parser.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/pjsip/src/pjsip/sip_parser.c b/pjsip/src/pjsip/sip_parser.c
+index 378c22f..c18faa3 100644
+--- a/pjsip/src/pjsip/sip_parser.c
++++ b/pjsip/src/pjsip/sip_parser.c
+@@ -327,7 +327,7 @@ static pj_status_t init_parser()
+
+ status = pj_cis_dup(&pconst.pjsip_VIA_PARAM_SPEC_ESC, &pconst.pjsip_TOKEN_SPEC_ESC);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+- pj_cis_add_str(&pconst.pjsip_VIA_PARAM_SPEC, ":");
++ pj_cis_add_str(&pconst.pjsip_VIA_PARAM_SPEC_ESC, ":");
+
+ status = pj_cis_dup(&pconst.pjsip_HOST_SPEC, &pconst.pjsip_ALNUM_SPEC);
+ PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
+--
+2.5.5
+