res/res_pjsip/pjsip_options.c: Eliminate excessive RAII_VAR usage.
[asterisk/asterisk.git] / res / res_pjsip / pjsip_options.c
index 461e553..c9b4f8c 100644 (file)
@@ -59,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;
@@ -79,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;
        }
 
@@ -90,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;
        }
 
@@ -118,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);
 }
 
 /*!
@@ -127,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;
        }
 
@@ -146,6 +157,9 @@ 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);
 }
 
 /*!
@@ -183,19 +197,25 @@ static int on_endpoint(void *obj, void *arg, int flags)
        }
 
        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;
+               }
+
+               contacts = ast_sip_location_retrieve_aor_contacts(aor);
+               ao2_ref(aor, -1);
+               if (!contacts) {
                        continue;
                }
 
                contact = ao2_callback(contacts, 0, match_contact_id, looking_for);
+               ao2_ref(contacts, -1);
                if (contact) {
-                       ao2_ref(contact,  -1);
+                       ao2_ref(contact, -1);
                        return CMP_MATCH;
                }
        }
@@ -209,10 +229,13 @@ static int on_endpoint(void *obj, void *arg, int flags)
  */
 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, 0, on_endpoint, looking_for);
+       endpoint = ao2_callback(endpoints, 0, on_endpoint, looking_for);
+       ao2_ref(endpoints, -1);
+       return endpoint;
 }
 
 /*!
@@ -322,6 +345,7 @@ struct sched_data {
 static void sched_data_destructor(void *obj)
 {
        struct sched_data *data = obj;
+
        ao2_cleanup(data->contact);
 }
 /*!
@@ -349,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(NULL, contact);
+       struct ast_sip_contact *contact = obj;
+       int res;
+
+       res = qualify_contact(NULL, contact);
+       ao2_ref(contact, -1);
+       return res;
 }
 
 /*!
@@ -359,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)) {
@@ -403,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);
 }
 
 /*!
@@ -435,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);
 }
 
 /*!
@@ -444,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;
        }
 
@@ -459,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 = {
@@ -643,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;
@@ -656,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_data(contacts, OBJ_NODATA, cli_on_contact, &cli_fd, endpoint);
+               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;
 }
@@ -721,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;
 }
@@ -732,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");
@@ -755,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");
@@ -778,17 +819,60 @@ 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(void)
@@ -835,25 +919,30 @@ static int qualify_and_schedule_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 || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
+               aor = ast_sip_location_retrieve_aor(aor_name);
+               if (!aor) {
                        continue;
                }
 
-               ao2_callback(contacts, OBJ_NODATA, qualify_and_schedule_cb, aor);
-               ao2_ref(contacts, -1);
+               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;
@@ -867,11 +956,69 @@ static void qualify_and_schedule_all(void)
                return;
        }
 
-       ao2_callback(endpoints, OBJ_NODATA,
-                    qualify_and_schedule_all_cb, NULL);
+       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 int format_contact_status_for_aor(void *obj, void *arg, int flags)
+{
+       struct ast_sip_aor *aor = obj;
+
+       return ast_sip_for_each_contact(aor, format_contact_status, arg);
+}
+
+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 };
@@ -884,7 +1031,6 @@ int ast_res_pjsip_init_options_handling(int reload)
        if (!(sched_qualifies = ao2_t_container_alloc(
                QUALIFIED_BUCKETS, sched_qualifies_hash_fn, sched_qualifies_cmp_fn,
                "Create container for scheduled qualifies"))) {
-
                return -1;
        }
 
@@ -903,6 +1049,8 @@ int ast_res_pjsip_init_options_handling(int reload)
                return -1;
        }
 
+       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);
@@ -914,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);
 }