res/res_pjsip/pjsip_options.c: Eliminate excessive RAII_VAR usage.
[asterisk/asterisk.git] / res / res_pjsip / pjsip_options.c
index 0409c15..c9b4f8c 100644 (file)
@@ -34,8 +34,6 @@
 #define DEFAULT_ENCODING "text/plain"
 #define QUALIFIED_BUCKETS 211
 
-static int qualify_contact(struct ast_sip_contact *contact);
-
 /*!
  * \internal
  * \brief Create a ast_sip_contact_status object.
@@ -61,18 +59,17 @@ static void *contact_status_alloc(const char *name)
  */
 static struct ast_sip_contact_status *find_or_create_contact_status(const struct ast_sip_contact *contact)
 {
-       struct ast_sip_contact_status *status = ast_sorcery_retrieve_by_id(
-               ast_sip_get_sorcery(), CONTACT_STATUS,
-               ast_sorcery_object_get_id(contact));
+       struct ast_sip_contact_status *status;
 
+       status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(contact));
        if (status) {
                return status;
        }
 
-       if (!(status = ast_sorcery_alloc(
-                     ast_sip_get_sorcery(), CONTACT_STATUS,
-                     ast_sorcery_object_get_id(contact)))) {
-
+       status = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(contact));
+       if (!status) {
                ast_log(LOG_ERROR, "Unable to create ast_sip_contact_status for contact %s\n",
                        contact->uri);
                return NULL;
@@ -81,6 +78,7 @@ static struct ast_sip_contact_status *find_or_create_contact_status(const struct
        if (ast_sorcery_create(ast_sip_get_sorcery(), status)) {
                ast_log(LOG_ERROR, "Unable to persist ast_sip_contact_status for contact %s\n",
                        contact->uri);
+               ao2_ref(status, -1);
                return NULL;
        }
 
@@ -92,18 +90,22 @@ static struct ast_sip_contact_status *find_or_create_contact_status(const struct
  * \brief Update an ast_sip_contact_status's elements.
  */
 static void update_contact_status(const struct ast_sip_contact *contact,
-                                 enum ast_sip_contact_status_type value)
+       enum ast_sip_contact_status_type value)
 {
-       RAII_VAR(struct ast_sip_contact_status *, status,
-                find_or_create_contact_status(contact), ao2_cleanup);
+       struct ast_sip_contact_status *status;
+       struct ast_sip_contact_status *update;
 
-       RAII_VAR(struct ast_sip_contact_status *, update, ast_sorcery_alloc(
-                     ast_sip_get_sorcery(), CONTACT_STATUS,
-                     ast_sorcery_object_get_id(status)), ao2_cleanup);
+       status = find_or_create_contact_status(contact);
+       if (!status) {
+               return;
+       }
 
+       update = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(status));
        if (!update) {
                ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n",
                        contact->uri);
+               ao2_ref(status, -1);
                return;
        }
 
@@ -120,6 +122,9 @@ static void update_contact_status(const struct ast_sip_contact *contact,
                ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n",
                        contact->uri);
        }
+
+       ao2_ref(update, -1);
+       ao2_ref(status, -1);
 }
 
 /*!
@@ -129,16 +134,20 @@ static void update_contact_status(const struct ast_sip_contact *contact,
  */
 static void init_start_time(const struct ast_sip_contact *contact)
 {
-       RAII_VAR(struct ast_sip_contact_status *, status,
-                find_or_create_contact_status(contact), ao2_cleanup);
+       struct ast_sip_contact_status *status;
+       struct ast_sip_contact_status *update;
 
-       RAII_VAR(struct ast_sip_contact_status *, update, ast_sorcery_alloc(
-                     ast_sip_get_sorcery(), CONTACT_STATUS,
-                     ast_sorcery_object_get_id(status)), ao2_cleanup);
+       status = find_or_create_contact_status(contact);
+       if (!status) {
+               return;
+       }
 
+       update = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(status));
        if (!update) {
                ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n",
                        contact->uri);
+               ao2_ref(status, -1);
                return;
        }
 
@@ -148,33 +157,65 @@ static void init_start_time(const struct ast_sip_contact *contact)
                ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n",
                        contact->uri);
        }
+
+       ao2_ref(update, -1);
+       ao2_ref(status, -1);
 }
 
 /*!
  * \internal
- * \brief For an endpoint try to match on a given contact.
+ * \brief Match a container contact object with the contact sorcery id looking for.
+ *
+ * \param obj pointer to the (user-defined part) of an object.
+ * \param arg callback argument from ao2_callback()
+ * \param flags flags from ao2_callback()
+ *
+ * \return Values are a combination of enum _cb_results.
+ */
+static int match_contact_id(void *obj, void *arg, int flags)
+{
+       struct ast_sip_contact *contact = obj;
+       const char *looking_for = arg;
+
+       return strcmp(ast_sorcery_object_get_id(contact), looking_for) ? 0 : CMP_MATCH;
+}
+
+/*!
+ * \internal
+ * \brief For an endpoint try to match the given contact sorcery id.
  */
 static int on_endpoint(void *obj, void *arg, int flags)
 {
        struct ast_sip_endpoint *endpoint = obj;
-       char *aor_name, *aors;
+       struct ast_sip_contact *contact;
+       char *looking_for = arg;
+       char *aor_name;
+       char *aors;
 
        if (!arg || ast_strlen_zero(endpoint->aors)) {
                return 0;
        }
 
        aors = ast_strdupa(endpoint->aors);
-
        while ((aor_name = strsep(&aors, ","))) {
-               RAII_VAR(struct ast_sip_aor *, aor,
-                        ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
-               RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
+               struct ast_sip_aor *aor;
+               struct ao2_container *contacts;
+
+               aor = ast_sip_location_retrieve_aor(aor_name);
+               if (!aor) {
+                       continue;
+               }
 
-               if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
+               contacts = ast_sip_location_retrieve_aor_contacts(aor);
+               ao2_ref(aor, -1);
+               if (!contacts) {
                        continue;
                }
 
-               if (ao2_find(contacts, arg, OBJ_NODATA | OBJ_POINTER)) {
+               contact = ao2_callback(contacts, 0, match_contact_id, looking_for);
+               ao2_ref(contacts, -1);
+               if (contact) {
+                       ao2_ref(contact, -1);
                        return CMP_MATCH;
                }
        }
@@ -184,14 +225,17 @@ static int on_endpoint(void *obj, void *arg, int flags)
 
 /*!
  * \internal
- * \brief Find endpoints associated with the given contact.
+ * \brief Find an endpoint associated with the given contact.
  */
-static struct ao2_container *find_endpoints(struct ast_sip_contact *contact)
+static struct ast_sip_endpoint *find_an_endpoint(struct ast_sip_contact *contact)
 {
-       RAII_VAR(struct ao2_container *, endpoints,
-                ast_sip_get_endpoints(), ao2_cleanup);
+       char *looking_for = (char *) ast_sorcery_object_get_id(contact);
+       struct ao2_container *endpoints = ast_sip_get_endpoints();
+       struct ast_sip_endpoint *endpoint;
 
-       return ao2_callback(endpoints, OBJ_MULTIPLE, on_endpoint, contact);
+       endpoint = ao2_callback(endpoints, 0, on_endpoint, looking_for);
+       ao2_ref(endpoints, -1);
+       return endpoint;
 }
 
 /*!
@@ -200,59 +244,49 @@ static struct ao2_container *find_endpoints(struct ast_sip_contact *contact)
  */
 static void qualify_contact_cb(void *token, pjsip_event *e)
 {
-       RAII_VAR(struct ast_sip_contact *, contact, token, ao2_cleanup);
-       RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
-       RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-
-       pjsip_transaction *tsx = e->body.tsx_state.tsx;
-       pjsip_rx_data *challenge = e->body.tsx_state.src.rdata;
-       pjsip_tx_data *tdata;
+       struct ast_sip_contact *contact = token;
 
        switch(e->body.tsx_state.type) {
        case PJSIP_EVENT_TRANSPORT_ERROR:
        case PJSIP_EVENT_TIMER:
                update_contact_status(contact, UNAVAILABLE);
-               return;
-       default:
                break;
-       }
-
-       if (!contact->authenticate_qualify || (tsx->status_code != 401 &&
-                                              tsx->status_code != 407)) {
+       default:
                update_contact_status(contact, AVAILABLE);
-               return;
-       }
-
-       /* try to find endpoints that are associated with the contact */
-       if (!(endpoints = find_endpoints(contact))) {
-               ast_log(LOG_ERROR, "No endpoints found for contact %s, cannot authenticate",
-                       contact->uri);
-               return;
-       }
-
-       /* find "first" endpoint in order to authenticate - actually any
-          endpoint should do that matched on the contact */
-       endpoint = ao2_callback(endpoints, 0, NULL, NULL);
-
-       if (!ast_sip_create_request_with_auth(&endpoint->outbound_auths,
-                                             challenge, tsx, &tdata)) {
-               pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata,
-                                        -1, NULL, NULL);
+               break;
        }
+       ao2_cleanup(contact);
 }
 
 /*!
  * \internal
  * \brief Attempt to qualify the contact
  *
- * \detail Sends a SIP OPTIONS request to the given contact in order to make
+ * \details Sends a SIP OPTIONS request to the given contact in order to make
  *         sure that contact is available.
  */
-static int qualify_contact(struct ast_sip_contact *contact)
+static int qualify_contact(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact)
 {
        pjsip_tx_data *tdata;
+       RAII_VAR(struct ast_sip_endpoint *, endpoint_local, NULL, ao2_cleanup);
+
+       if (contact->authenticate_qualify) {
+               endpoint_local = ao2_bump(endpoint);
+               if (!endpoint_local) {
+                       /*
+                        * Find the "first" endpoint to completely qualify the contact - any
+                        * endpoint that is associated with the contact should do.
+                        */
+                       endpoint_local = find_an_endpoint(contact);
+                       if (!endpoint_local) {
+                               ast_log(LOG_ERROR, "Unable to find an endpoint to qualify contact %s\n",
+                                       contact->uri);
+                               return -1;
+                       }
+               }
+       }
 
-       if (ast_sip_create_request("OPTIONS", NULL, NULL, contact->uri, &tdata)) {
+       if (ast_sip_create_request("OPTIONS", NULL, NULL, NULL, contact, &tdata)) {
                ast_log(LOG_ERROR, "Unable to create request to qualify contact %s\n",
                        contact->uri);
                return -1;
@@ -270,11 +304,11 @@ static int qualify_contact(struct ast_sip_contact *contact)
        init_start_time(contact);
 
        ao2_ref(contact, +1);
-       if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(),
-                                    tdata, -1, contact, qualify_contact_cb) != PJ_SUCCESS) {
-               /* The callback will be called so we don't need to drop the contact ref*/
+       if (ast_sip_send_request(tdata, NULL, endpoint_local, contact, qualify_contact_cb)
+               != PJ_SUCCESS) {
                ast_log(LOG_ERROR, "Unable to send request to qualify contact %s\n",
                        contact->uri);
+               ao2_ref(contact, -1);
                return -1;
        }
 
@@ -311,6 +345,7 @@ struct sched_data {
 static void sched_data_destructor(void *obj)
 {
        struct sched_data *data = obj;
+
        ao2_cleanup(data->contact);
 }
 /*!
@@ -338,8 +373,12 @@ static struct sched_data *sched_data_create(struct ast_sip_contact *contact)
  */
 static int qualify_contact_task(void *obj)
 {
-       RAII_VAR(struct ast_sip_contact *, contact, obj, ao2_cleanup);
-       return qualify_contact(contact);
+       struct ast_sip_contact *contact = obj;
+       int res;
+
+       res = qualify_contact(NULL, contact);
+       ao2_ref(contact, -1);
+       return res;
 }
 
 /*!
@@ -348,7 +387,7 @@ static int qualify_contact_task(void *obj)
  */
 static int qualify_contact_sched(const void *obj)
 {
-       struct sched_data *data = (struct sched_data *)obj;
+       struct sched_data *data = (struct sched_data *) obj;
 
        ao2_ref(data->contact, +1);
        if (ast_sip_push_task(NULL, qualify_contact_task, data->contact)) {
@@ -392,14 +431,15 @@ static void schedule_qualify(struct ast_sip_contact *contact)
  */
 static void unschedule_qualify(struct ast_sip_contact *contact)
 {
-       RAII_VAR(struct sched_data *, data, ao2_find(
-                        sched_qualifies, contact, OBJ_UNLINK), ao2_cleanup);
+       struct sched_data *data;
 
+       data = ao2_find(sched_qualifies, contact, OBJ_UNLINK | OBJ_SEARCH_KEY);
        if (!data) {
                return;
        }
 
        AST_SCHED_DEL_UNREF(sched, data->id, ao2_cleanup(data));
+       ao2_ref(data, -1);
 }
 
 /*!
@@ -424,7 +464,7 @@ static void qualify_and_schedule(struct ast_sip_contact *contact)
  */
 static void contact_created(const void *obj)
 {
-       qualify_and_schedule((struct ast_sip_contact *)obj);
+       qualify_and_schedule((struct ast_sip_contact *) obj);
 }
 
 /*!
@@ -433,14 +473,14 @@ static void contact_created(const void *obj)
  */
 static void contact_deleted(const void *obj)
 {
-       struct ast_sip_contact *contact = (struct ast_sip_contact *)obj;
-       RAII_VAR(struct ast_sip_contact_status *, status, NULL, ao2_cleanup);
+       struct ast_sip_contact *contact = (struct ast_sip_contact *) obj;
+       struct ast_sip_contact_status *status;
 
        unschedule_qualify(contact);
 
-       if (!(status = ast_sorcery_retrieve_by_id(
-                     ast_sip_get_sorcery(), CONTACT_STATUS,
-                     ast_sorcery_object_get_id(contact)))) {
+       status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(contact));
+       if (!status) {
                return;
        }
 
@@ -448,6 +488,7 @@ static void contact_deleted(const void *obj)
                ast_log(LOG_ERROR, "Unable to delete ast_sip_contact_status for contact %s\n",
                        contact->uri);
        }
+       ao2_ref(status, -1);
 }
 
 static const struct ast_sorcery_observer contact_observer = {
@@ -489,8 +530,7 @@ static pj_status_t send_options_response(pjsip_rx_data *rdata, int code)
        pj_status_t status;
 
        /* Make the response object */
-       if ((status = pjsip_endpt_create_response(
-                    endpt, rdata, code, NULL, &tdata) != PJ_SUCCESS)) {
+       if ((status = ast_sip_create_response(rdata, code, NULL, &tdata) != PJ_SUCCESS)) {
                ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
                return status;
        }
@@ -527,8 +567,8 @@ static pj_status_t send_options_response(pjsip_rx_data *rdata, int code)
                        pjsip_tx_data_dec_ref(tdata);
                        return status;
                }
-               status = pjsip_endpt_send_response(endpt, &res_addr, tdata,
-                                                  NULL, NULL);
+               status = ast_sip_send_response(&res_addr, tdata,
+                                                  ast_pjsip_rdata_get_endpoint(rdata));
        }
 
        if (status != PJ_SUCCESS) {
@@ -586,12 +626,14 @@ static pjsip_module options_module = {
  * \internal
  * \brief Send qualify request to the given contact.
  */
-static int cli_on_contact(void *obj, void *arg, int flags)
+static int cli_on_contact(void *obj, void *arg, void *data, int flags)
 {
        struct ast_sip_contact *contact = obj;
+       struct ast_sip_endpoint *endpoint = data;
        int *cli_fd = arg;
+
        ast_cli(*cli_fd, " contact %s\n", contact->uri);
-       qualify_contact(contact);
+       qualify_contact(endpoint, contact);
        return 0;
 }
 
@@ -631,7 +673,8 @@ static void qualify_data_destroy(struct qualify_data *qual_data)
  */
 static int cli_qualify_contacts(void *data)
 {
-       char *aor_name, *aors;
+       char *aors;
+       char *aor_name;
        RAII_VAR(struct qualify_data *, qual_data, data, qualify_data_destroy);
        struct ast_sip_endpoint *endpoint = qual_data->endpoint;
        int cli_fd = qual_data->cli_fd;
@@ -644,18 +687,23 @@ static int cli_qualify_contacts(void *data)
        }
 
        aors = ast_strdupa(endpoint->aors);
-
        while ((aor_name = strsep(&aors, ","))) {
-               RAII_VAR(struct ast_sip_aor *, aor,
-                        ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
-               RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
+               struct ast_sip_aor *aor;
+               struct ao2_container *contacts;
 
-               if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
+               aor = ast_sip_location_retrieve_aor(aor_name);
+               if (!aor) {
                        continue;
                }
 
-               ast_cli(cli_fd, "Sending qualify to endpoint %s\n", endpoint_name);
-               ao2_callback(contacts, OBJ_NODATA, cli_on_contact, &cli_fd);
+               contacts = ast_sip_location_retrieve_aor_contacts(aor);
+               if (contacts) {
+                       ast_cli(cli_fd, "Sending qualify to endpoint %s\n", endpoint_name);
+                       ao2_callback_data(contacts, OBJ_NODATA, cli_on_contact, &cli_fd, endpoint);
+                       ao2_ref(contacts, -1);
+               }
+
+               ao2_ref(aor, -1);
        }
        return 0;
 }
@@ -709,9 +757,10 @@ static char *cli_qualify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *
 static int ami_contact_cb(void *obj, void *arg, int flags)
 {
        struct ast_sip_contact *contact = obj;
+
        ao2_ref(contact, +1);
        if (ast_sip_push_task(NULL, qualify_contact_task, contact)) {
-               ao2_cleanup(contact);
+               ao2_ref(contact, -1);
        }
        return 0;
 }
@@ -720,16 +769,15 @@ static int ami_sip_qualify(struct mansession *s, const struct message *m)
 {
        const char *endpoint_name = astman_get_header(m, "Endpoint");
        RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
-       char *aor_name, *aors;
+       char *aors;
+       char *aor_name;
 
        if (ast_strlen_zero(endpoint_name)) {
                astman_send_error(s, m, "Endpoint parameter missing.");
                return 0;
        }
 
-       endpoint = ast_sorcery_retrieve_by_id(
-               ast_sip_get_sorcery(),
-               "endpoint",
+       endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
                endpoint_name);
        if (!endpoint) {
                astman_send_error(s, m, "Unable to retrieve endpoint\n");
@@ -743,17 +791,22 @@ static int ami_sip_qualify(struct mansession *s, const struct message *m)
        }
 
        aors = ast_strdupa(endpoint->aors);
-
        while ((aor_name = strsep(&aors, ","))) {
-               RAII_VAR(struct ast_sip_aor *, aor,
-                        ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
-               RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
+               struct ast_sip_aor *aor;
+               struct ao2_container *contacts;
 
-               if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
+               aor = ast_sip_location_retrieve_aor(aor_name);
+               if (!aor) {
                        continue;
                }
 
-               ao2_callback(contacts, OBJ_NODATA, ami_contact_cb, NULL);
+               contacts = ast_sip_location_retrieve_aor_contacts(aor);
+               if (contacts) {
+                       ao2_callback(contacts, OBJ_NODATA, ami_contact_cb, NULL);
+                       ao2_ref(contacts, -1);
+               }
+
+               ao2_ref(aor, -1);
        }
 
        astman_send_ack(s, m, "Endpoint found, will qualify");
@@ -766,21 +819,66 @@ static struct ast_cli_entry cli_options[] = {
 
 static int sched_qualifies_hash_fn(const void *obj, int flags)
 {
-       const struct sched_data *data = obj;
+       const struct sched_data *object;
+       const struct ast_sip_contact *key;
 
-       return ast_str_hash(ast_sorcery_object_get_id(data->contact));
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_KEY:
+               key = obj;
+               break;
+       case OBJ_SEARCH_OBJECT:
+               object = obj;
+               key = object->contact;
+               break;
+       default:
+               /* Hash can only work on something with a full key. */
+               ast_assert(0);
+               return 0;
+       }
+       return ast_str_hash(ast_sorcery_object_get_id(key));
 }
 
 static int sched_qualifies_cmp_fn(void *obj, void *arg, int flags)
 {
-       struct sched_data *data = obj;
-
-       return !strcmp(ast_sorcery_object_get_id(data->contact),
-                      ast_sorcery_object_get_id(arg));
+       const struct sched_data *object_left = obj;
+       const struct sched_data *object_right = arg;
+       struct ast_sip_contact *right_key = arg;
+       int cmp;
+
+       switch (flags & OBJ_SEARCH_MASK) {
+       case OBJ_SEARCH_OBJECT:
+               right_key = object_right->contact;
+               /* Fall through */
+       case OBJ_SEARCH_KEY:
+               cmp = strcmp(ast_sorcery_object_get_id(object_left->contact),
+                       ast_sorcery_object_get_id(right_key));
+               break;
+       case OBJ_SEARCH_PARTIAL_KEY:
+               /* Not supported by container. */
+               ast_assert(0);
+               return 0;
+       default:
+               /*
+                * What arg points to is specific to this traversal callback
+                * and has no special meaning to astobj2.
+                */
+               cmp = 0;
+               break;
+       }
+       if (cmp) {
+               return 0;
+       }
+       /*
+        * At this point the traversal callback is identical to a sorted
+        * container.
+        */
+       return CMP_MATCH;
 }
 
-int ast_sip_initialize_sorcery_qualify(struct ast_sorcery *sorcery)
+int ast_sip_initialize_sorcery_qualify(void)
 {
+       struct ast_sorcery *sorcery = ast_sip_get_sorcery();
+
        /* initialize sorcery ast_sip_contact_status resource */
        ast_sorcery_apply_default(sorcery, CONTACT_STATUS, "memory", NULL);
 
@@ -804,6 +902,7 @@ static int qualify_and_schedule_cb(void *obj, void *arg, int flags)
        struct ast_sip_aor *aor = arg;
 
        contact->qualify_frequency = aor->qualify_frequency;
+       contact->authenticate_qualify = aor->authenticate_qualify;
 
        qualify_and_schedule(contact);
 
@@ -812,64 +911,129 @@ static int qualify_and_schedule_cb(void *obj, void *arg, int flags)
 
 /*!
  * \internal
- * \brief Qualify and schedule an endpoint's permanent contacts
+ * \brief Qualify and schedule an endpoint's contacts
  *
- * \detail For the given endpoint retrieve its list of aors, qualify all
- *         permanent contacts, and schedule for checks if configured.
+ * \details For the given endpoint retrieve its list of aors, qualify all
+ *         contacts, and schedule for checks if configured.
  */
-static int qualify_and_schedule_permanent_cb(void *obj, void *arg, int flags)
+static int qualify_and_schedule_all_cb(void *obj, void *arg, int flags)
 {
        struct ast_sip_endpoint *endpoint = obj;
-       char *aor_name, *aors;
+       char *aors;
+       char *aor_name;
 
        if (ast_strlen_zero(endpoint->aors)) {
                return 0;
        }
 
        aors = ast_strdupa(endpoint->aors);
-
        while ((aor_name = strsep(&aors, ","))) {
-               RAII_VAR(struct ast_sip_aor *, aor,
-                        ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
+               struct ast_sip_aor *aor;
+               struct ao2_container *contacts;
 
-               if (!aor || !aor->permanent_contacts) {
+               aor = ast_sip_location_retrieve_aor(aor_name);
+               if (!aor) {
                        continue;
                }
-               ao2_callback(aor->permanent_contacts, OBJ_NODATA, qualify_and_schedule_cb, aor);
+
+               contacts = ast_sip_location_retrieve_aor_contacts(aor);
+               if (contacts) {
+                       ao2_callback(contacts, OBJ_NODATA, qualify_and_schedule_cb, aor);
+                       ao2_ref(contacts, -1);
+               }
+
+               ao2_ref(aor, -1);
+       }
+
+       return 0;
+}
+
+static void qualify_and_schedule_all(void)
+{
+       struct ao2_container *endpoints = ast_sip_get_endpoints();
+
+       if (!endpoints) {
+               return;
+       }
+
+       ao2_callback(endpoints, OBJ_NODATA, qualify_and_schedule_all_cb, NULL);
+       ao2_ref(endpoints, -1);
+}
+
+static const char *status_map [] = {
+       [UNAVAILABLE] = "Unreachable",
+       [AVAILABLE] = "Reachable",
+};
+
+static int format_contact_status(void *obj, void *arg, int flags)
+{
+       struct ast_sip_contact_wrapper *wrapper = obj;
+       struct ast_sip_contact *contact = wrapper->contact;
+       struct ast_sip_ami *ami = arg;
+       struct ast_sip_contact_status *status;
+       struct ast_str *buf;
+       const struct ast_sip_endpoint *endpoint = ami->arg;
+
+       buf = ast_sip_create_ami_event("ContactStatusDetail", ami);
+       if (!buf) {
+               return -1;
+       }
+
+       status = ast_sorcery_retrieve_by_id(
+               ast_sip_get_sorcery(), CONTACT_STATUS,
+               ast_sorcery_object_get_id(contact));
+
+       ast_str_append(&buf, 0, "AOR: %s\r\n", wrapper->aor_id);
+       ast_str_append(&buf, 0, "URI: %s\r\n", contact->uri);
+       if (status) {
+               ast_str_append(&buf, 0, "Status: %s\r\n", status_map[status->status]);
+               ast_str_append(&buf, 0, "RoundtripUsec: %" PRId64 "\r\n", status->rtt);
+       } else {
+               ast_str_append(&buf, 0, "Status: Unknown\r\n");
+               ast_str_append(&buf, 0, "RoundtripUsec: N/A\r\n");
        }
+       ast_str_append(&buf, 0, "EndpointName: %s\r\n",
+                       ast_sorcery_object_get_id(endpoint));
+       astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
 
+       ast_free(buf);
+       ao2_cleanup(status);
        return 0;
 }
 
-static void qualify_and_schedule_permanent(void)
+static int format_contact_status_for_aor(void *obj, void *arg, int flags)
 {
-       RAII_VAR(struct ao2_container *, endpoints,
-                ast_sip_get_endpoints(), ao2_cleanup);
+       struct ast_sip_aor *aor = obj;
+
+       return ast_sip_for_each_contact(aor, format_contact_status, arg);
+}
 
-       ao2_callback(endpoints, OBJ_NODATA,
-                    qualify_and_schedule_permanent_cb, NULL);
+static int format_ami_contact_status(const struct ast_sip_endpoint *endpoint,
+               struct ast_sip_ami *ami)
+{
+       ami->arg = (void *)endpoint;
+       return ast_sip_for_each_aor(endpoint->aors, format_contact_status_for_aor, ami);
 }
 
+static struct ast_sip_endpoint_formatter contact_status_formatter = {
+       .format_ami = format_ami_contact_status
+};
+
 int ast_res_pjsip_init_options_handling(int reload)
 {
        const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
 
-       if (sched_qualifies) {
-               ao2_t_ref(sched_qualifies, -1, "Remove old scheduled qualifies");
+       if (reload) {
+               qualify_and_schedule_all();
+               return 0;
        }
 
        if (!(sched_qualifies = ao2_t_container_alloc(
                QUALIFIED_BUCKETS, sched_qualifies_hash_fn, sched_qualifies_cmp_fn,
                "Create container for scheduled qualifies"))) {
-
                return -1;
        }
 
-       if (reload) {
-               qualify_and_schedule_permanent();
-               return 0;
-       }
-
        if (pjsip_endpt_register_module(ast_sip_get_pjsip_endpoint(), &options_module) != PJ_SUCCESS) {
                options_stop();
                return -1;
@@ -885,7 +1049,9 @@ int ast_res_pjsip_init_options_handling(int reload)
                return -1;
        }
 
-       qualify_and_schedule_permanent();
+       ast_sip_register_endpoint_formatter(&contact_status_formatter);
+
+       qualify_and_schedule_all();
        ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options));
        ast_manager_register2("PJSIPQualify", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_sip_qualify, NULL, NULL, NULL);
 
@@ -896,4 +1062,5 @@ void ast_res_pjsip_cleanup_options_handling(void)
 {
        ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options));
        ast_manager_unregister("PJSIPQualify");
+       ast_sip_unregister_endpoint_formatter(&contact_status_formatter);
 }