2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Matt Jordan <mjordan@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
25 #include "asterisk/res_pjsip.h"
26 #include "asterisk/channel.h"
27 #include "asterisk/pbx.h"
28 #include "asterisk/astobj2.h"
29 #include "asterisk/cli.h"
30 #include "asterisk/time.h"
31 #include "include/res_pjsip_private.h"
33 #define DEFAULT_LANGUAGE "en"
34 #define DEFAULT_ENCODING "text/plain"
35 #define QUALIFIED_BUCKETS 211
39 * \brief Create a ast_sip_contact_status object.
41 static void *contact_status_alloc(const char *name)
43 struct ast_sip_contact_status *status = ast_sorcery_generic_alloc(sizeof(*status), NULL);
46 ast_log(LOG_ERROR, "Unable to allocate ast_sip_contact_status\n");
50 status->status = UNAVAILABLE;
57 * \brief Retrieve a ast_sip_contact_status object from sorcery creating
60 static struct ast_sip_contact_status *find_or_create_contact_status(const struct ast_sip_contact *contact)
62 struct ast_sip_contact_status *status;
64 status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), CONTACT_STATUS,
65 ast_sorcery_object_get_id(contact));
70 status = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
71 ast_sorcery_object_get_id(contact));
73 ast_log(LOG_ERROR, "Unable to create ast_sip_contact_status for contact %s\n",
78 if (ast_sorcery_create(ast_sip_get_sorcery(), status)) {
79 ast_log(LOG_ERROR, "Unable to persist ast_sip_contact_status for contact %s\n",
90 * \brief Update an ast_sip_contact_status's elements.
92 static void update_contact_status(const struct ast_sip_contact *contact,
93 enum ast_sip_contact_status_type value)
95 struct ast_sip_contact_status *status;
96 struct ast_sip_contact_status *update;
98 status = find_or_create_contact_status(contact);
103 update = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
104 ast_sorcery_object_get_id(status));
106 ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n",
112 update->status = value;
114 /* if the contact is available calculate the rtt as
115 the diff between the last start time and "now" */
116 update->rtt = update->status == AVAILABLE ?
117 ast_tvdiff_us(ast_tvnow(), status->rtt_start) : 0;
119 update->rtt_start = ast_tv(0, 0);
121 if (ast_sorcery_update(ast_sip_get_sorcery(), update)) {
122 ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n",
132 * \brief Initialize the start time on a contact status so the round
133 * trip time can be calculated upon a valid response.
135 static void init_start_time(const struct ast_sip_contact *contact)
137 struct ast_sip_contact_status *status;
138 struct ast_sip_contact_status *update;
140 status = find_or_create_contact_status(contact);
145 update = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
146 ast_sorcery_object_get_id(status));
148 ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n",
154 update->rtt_start = ast_tvnow();
156 if (ast_sorcery_update(ast_sip_get_sorcery(), update)) {
157 ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n",
167 * \brief Match a container contact object with the contact sorcery id looking for.
169 * \param obj pointer to the (user-defined part) of an object.
170 * \param arg callback argument from ao2_callback()
171 * \param flags flags from ao2_callback()
173 * \return Values are a combination of enum _cb_results.
175 static int match_contact_id(void *obj, void *arg, int flags)
177 struct ast_sip_contact *contact = obj;
178 const char *looking_for = arg;
180 return strcmp(ast_sorcery_object_get_id(contact), looking_for) ? 0 : CMP_MATCH;
185 * \brief For an endpoint try to match the given contact sorcery id.
187 static int on_endpoint(void *obj, void *arg, int flags)
189 struct ast_sip_endpoint *endpoint = obj;
190 struct ast_sip_contact *contact;
191 char *looking_for = arg;
195 if (!arg || ast_strlen_zero(endpoint->aors)) {
199 aors = ast_strdupa(endpoint->aors);
200 while ((aor_name = strsep(&aors, ","))) {
201 struct ast_sip_aor *aor;
202 struct ao2_container *contacts;
204 aor = ast_sip_location_retrieve_aor(aor_name);
209 contacts = ast_sip_location_retrieve_aor_contacts(aor);
215 contact = ao2_callback(contacts, 0, match_contact_id, looking_for);
216 ao2_ref(contacts, -1);
218 ao2_ref(contact, -1);
228 * \brief Find an endpoint associated with the given contact.
230 static struct ast_sip_endpoint *find_an_endpoint(struct ast_sip_contact *contact)
232 char *looking_for = (char *) ast_sorcery_object_get_id(contact);
233 struct ao2_container *endpoints = ast_sip_get_endpoints();
234 struct ast_sip_endpoint *endpoint;
236 endpoint = ao2_callback(endpoints, 0, on_endpoint, looking_for);
237 ao2_ref(endpoints, -1);
243 * \brief Receive a response to the qualify contact request.
245 static void qualify_contact_cb(void *token, pjsip_event *e)
247 struct ast_sip_contact *contact = token;
249 switch(e->body.tsx_state.type) {
251 ast_log(LOG_ERROR, "Unexpected PJSIP event %d\n", e->body.tsx_state.type);
253 case PJSIP_EVENT_TRANSPORT_ERROR:
254 case PJSIP_EVENT_TIMER:
255 update_contact_status(contact, UNAVAILABLE);
257 case PJSIP_EVENT_RX_MSG:
258 update_contact_status(contact, AVAILABLE);
261 ao2_cleanup(contact);
266 * \brief Attempt to qualify the contact
268 * \details Sends a SIP OPTIONS request to the given contact in order to make
269 * sure that contact is available.
271 static int qualify_contact(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact)
273 pjsip_tx_data *tdata;
274 RAII_VAR(struct ast_sip_endpoint *, endpoint_local, NULL, ao2_cleanup);
276 if (contact->authenticate_qualify) {
277 endpoint_local = ao2_bump(endpoint);
278 if (!endpoint_local) {
280 * Find the "first" endpoint to completely qualify the contact - any
281 * endpoint that is associated with the contact should do.
283 endpoint_local = find_an_endpoint(contact);
284 if (!endpoint_local) {
285 ast_log(LOG_ERROR, "Unable to find an endpoint to qualify contact %s\n",
292 if (ast_sip_create_request("OPTIONS", NULL, NULL, NULL, contact, &tdata)) {
293 ast_log(LOG_ERROR, "Unable to create request to qualify contact %s\n",
298 /* If an outbound proxy is specified set it on this request */
299 if (!ast_strlen_zero(contact->outbound_proxy) &&
300 ast_sip_set_outbound_proxy(tdata, contact->outbound_proxy)) {
301 pjsip_tx_data_dec_ref(tdata);
302 ast_log(LOG_ERROR, "Unable to apply outbound proxy on request to qualify contact %s\n",
307 init_start_time(contact);
309 ao2_ref(contact, +1);
310 if (ast_sip_send_request(tdata, NULL, endpoint_local, contact, qualify_contact_cb)
312 ast_log(LOG_ERROR, "Unable to send request to qualify contact %s\n",
314 update_contact_status(contact, UNAVAILABLE);
315 ao2_ref(contact, -1);
324 * \brief Scheduling context for sending QUALIFY request at specified intervals.
326 static struct ast_sched_context *sched;
330 * \brief Container to hold all actively scheduled qualifies.
332 static struct ao2_container *sched_qualifies;
336 * \brief Structure to hold qualify contact scheduling information.
339 /*! The scheduling id */
341 /*! The the contact being checked */
342 struct ast_sip_contact *contact;
347 * \brief Destroy the scheduled data and remove from scheduler.
349 static void sched_data_destructor(void *obj)
351 struct sched_data *data = obj;
353 ao2_cleanup(data->contact);
357 * \brief Create the scheduling data object.
359 static struct sched_data *sched_data_create(struct ast_sip_contact *contact)
361 struct sched_data *data;
363 data = ao2_t_alloc(sizeof(*data), sched_data_destructor, contact->uri);
365 ast_log(LOG_ERROR, "Unable to create schedule qualify data for contact %s\n",
370 data->contact = contact;
371 ao2_ref(data->contact, +1);
378 * \brief Send a qualify contact request within a threaded task.
380 static int qualify_contact_task(void *obj)
382 struct ast_sip_contact *contact = obj;
385 res = qualify_contact(NULL, contact);
386 ao2_ref(contact, -1);
392 * \brief Send a scheduled qualify contact request.
394 static int qualify_contact_sched(const void *obj)
396 struct sched_data *data = (struct sched_data *) obj;
398 ao2_ref(data->contact, +1);
399 if (ast_sip_push_task(NULL, qualify_contact_task, data->contact)) {
400 ao2_ref(data->contact, -1);
404 * Always reschedule rather than have a potential race cleaning
405 * up the data object ref between self deletion and an external
413 * \brief Set up a scheduled qualify contact check.
415 static void schedule_qualify(struct ast_sip_contact *contact)
417 struct sched_data *data;
419 data = sched_data_create(contact);
424 ast_assert(contact->qualify_frequency != 0);
426 ao2_t_ref(data, +1, "Ref for qualify_contact_sched() scheduler entry");
427 data->id = ast_sched_add_variable(sched, contact->qualify_frequency * 1000,
428 qualify_contact_sched, data, 0);
430 ao2_t_ref(data, -1, "Cleanup failed scheduler add");
431 ast_log(LOG_ERROR, "Unable to schedule qualify for contact %s\n",
433 } else if (!ao2_link(sched_qualifies, data)) {
434 AST_SCHED_DEL_UNREF(sched, data->id,
435 ao2_t_ref(data, -1, "Cleanup scheduler for failed ao2_link"));
437 ao2_t_ref(data, -1, "Done setting up scheduler entry");
442 * \brief Remove the contact from the scheduler.
444 static void unschedule_qualify(struct ast_sip_contact *contact)
446 struct sched_data *data;
448 data = ao2_find(sched_qualifies, contact, OBJ_UNLINK | OBJ_SEARCH_KEY);
453 AST_SCHED_DEL_UNREF(sched, data->id,
454 ao2_t_ref(data, -1, "Delete scheduler entry ref"));
455 ao2_t_ref(data, -1, "Done with ao2_find ref");
460 * \brief Qualify the given contact and set up scheduling if configured.
462 static void qualify_and_schedule(struct ast_sip_contact *contact)
464 unschedule_qualify(contact);
466 if (contact->qualify_frequency) {
467 ao2_ref(contact, +1);
468 if (ast_sip_push_task(NULL, qualify_contact_task, contact)) {
469 ao2_ref(contact, -1);
472 schedule_qualify(contact);
478 * \brief A new contact has been created make sure it is available.
480 static void contact_created(const void *obj)
482 qualify_and_schedule((struct ast_sip_contact *) obj);
487 * \brief A contact has been deleted remove status tracking.
489 static void contact_deleted(const void *obj)
491 struct ast_sip_contact *contact = (struct ast_sip_contact *) obj;
492 struct ast_sip_contact_status *status;
494 unschedule_qualify(contact);
496 status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), CONTACT_STATUS,
497 ast_sorcery_object_get_id(contact));
502 if (ast_sorcery_delete(ast_sip_get_sorcery(), status)) {
503 ast_log(LOG_ERROR, "Unable to delete ast_sip_contact_status for contact %s\n",
509 static const struct ast_sorcery_observer contact_observer = {
510 .created = contact_created,
511 .deleted = contact_deleted
514 static pj_bool_t options_start(void)
516 sched = ast_sched_context_create();
520 if (ast_sched_start_thread(sched)) {
521 ast_sched_context_destroy(sched);
526 if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "contact", &contact_observer)) {
527 ast_log(LOG_WARNING, "Unable to add contact observer\n");
528 ast_sched_context_destroy(sched);
536 static int sched_qualifies_empty(void *obj, void *arg, int flags)
538 ao2_t_ref(obj, -1, "Release ref held by destroyed scheduler context.");
542 static pj_bool_t options_stop(void)
544 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &contact_observer);
547 ast_sched_context_destroy(sched);
551 /* Empty the container of scheduling data refs. */
552 ao2_callback(sched_qualifies, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
553 sched_qualifies_empty, NULL);
558 static pj_status_t send_options_response(pjsip_rx_data *rdata, int code)
560 pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint();
561 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
562 pjsip_transaction *trans = pjsip_rdata_get_tsx(rdata);
563 pjsip_tx_data *tdata;
564 const pjsip_hdr *hdr;
565 pjsip_response_addr res_addr;
568 /* Make the response object */
569 if ((status = ast_sip_create_response(rdata, code, NULL, &tdata) != PJ_SUCCESS)) {
570 ast_log(LOG_ERROR, "Unable to create response (%d)\n", status);
574 /* Add appropriate headers */
575 if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ACCEPT, NULL))) {
576 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
578 if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_ALLOW, NULL))) {
579 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
581 if ((hdr = pjsip_endpt_get_capability(endpt, PJSIP_H_SUPPORTED, NULL))) {
582 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)pjsip_hdr_clone(tdata->pool, hdr));
586 * XXX TODO: pjsip doesn't care a lot about either of these headers -
587 * while it provides specific methods to create them, they are defined
588 * to be the standard string header creation. We never did add them
589 * in chan_sip, although RFC 3261 says they SHOULD. Hard coded here.
591 ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING);
592 ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE);
595 status = pjsip_dlg_send_response(dlg, trans, tdata);
597 /* Get where to send request. */
598 if ((status = pjsip_get_response_addr(
599 tdata->pool, rdata, &res_addr)) != PJ_SUCCESS) {
600 ast_log(LOG_ERROR, "Unable to get response address (%d)\n",
603 pjsip_tx_data_dec_ref(tdata);
606 status = ast_sip_send_response(&res_addr, tdata,
607 ast_pjsip_rdata_get_endpoint(rdata));
610 if (status != PJ_SUCCESS) {
611 ast_log(LOG_ERROR, "Unable to send response (%d)\n", status);
617 static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
619 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
621 pjsip_sip_uri *sip_ruri;
622 char exten[AST_MAX_EXTENSION];
624 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
625 &pjsip_options_method)) {
629 if (!(endpoint = ast_pjsip_rdata_get_endpoint(rdata))) {
633 ruri = rdata->msg_info.msg->line.req.uri;
634 if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) {
635 send_options_response(rdata, 416);
639 sip_ruri = pjsip_uri_get_uri(ruri);
640 ast_copy_pj_str(exten, &sip_ruri->user, sizeof(exten));
642 if (ast_shutting_down()) {
643 send_options_response(rdata, 503);
644 } else if (!ast_strlen_zero(exten) && !ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) {
645 send_options_response(rdata, 404);
647 send_options_response(rdata, 200);
652 static pjsip_module options_module = {
653 .name = {"Options Module", 14},
655 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
656 .start = options_start,
657 .stop = options_stop,
658 .on_rx_request = options_on_rx_request,
663 * \brief Send qualify request to the given contact.
665 static int cli_on_contact(void *obj, void *arg, void *data, int flags)
667 struct ast_sip_contact *contact = obj;
668 struct ast_sip_endpoint *endpoint = data;
671 ast_cli(*cli_fd, " contact %s\n", contact->uri);
672 qualify_contact(endpoint, contact);
677 * \brief Data pushed to threadpool to qualify endpoints from the CLI
679 struct qualify_data {
680 /*! Endpoint that is being qualified */
681 struct ast_sip_endpoint *endpoint;
682 /*! CLI File descriptor for printing messages */
686 static struct qualify_data *qualify_data_alloc(struct ast_sip_endpoint *endpoint, int cli_fd)
688 struct qualify_data *qual_data;
690 qual_data = ast_malloc(sizeof(*qual_data));
695 qual_data->endpoint = ao2_bump(endpoint);
696 qual_data->cli_fd = cli_fd;
700 static void qualify_data_destroy(struct qualify_data *qual_data)
702 ao2_cleanup(qual_data->endpoint);
708 * \brief For an endpoint iterate over and qualify all aors/contacts
710 static int cli_qualify_contacts(void *data)
714 RAII_VAR(struct qualify_data *, qual_data, data, qualify_data_destroy);
715 struct ast_sip_endpoint *endpoint = qual_data->endpoint;
716 int cli_fd = qual_data->cli_fd;
717 const char *endpoint_name = ast_sorcery_object_get_id(endpoint);
719 if (ast_strlen_zero(endpoint->aors)) {
720 ast_cli(cli_fd, "Endpoint %s has no AoR's configured\n",
725 aors = ast_strdupa(endpoint->aors);
726 while ((aor_name = strsep(&aors, ","))) {
727 struct ast_sip_aor *aor;
728 struct ao2_container *contacts;
730 aor = ast_sip_location_retrieve_aor(aor_name);
735 contacts = ast_sip_location_retrieve_aor_contacts(aor);
737 ast_cli(cli_fd, "Sending qualify to endpoint %s\n", endpoint_name);
738 ao2_callback_data(contacts, OBJ_NODATA, cli_on_contact, &cli_fd, endpoint);
739 ao2_ref(contacts, -1);
747 static char *cli_qualify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
749 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
750 const char *endpoint_name;
751 struct qualify_data *qual_data;
755 e->command = "pjsip qualify";
757 "Usage: pjsip qualify <endpoint>\n"
758 " Send a SIP OPTIONS request to all contacts on the endpoint.\n";
765 return CLI_SHOWUSAGE;
768 endpoint_name = a->argv[2];
770 if (!(endpoint = ast_sorcery_retrieve_by_id(
771 ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
772 ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name);
776 qual_data = qualify_data_alloc(endpoint, a->fd);
781 if (ast_sip_push_task(NULL, cli_qualify_contacts, qual_data)) {
782 qualify_data_destroy(qual_data);
791 * \brief Send qualify request to the given contact.
793 static int ami_contact_cb(void *obj, void *arg, int flags)
795 struct ast_sip_contact *contact = obj;
797 ao2_ref(contact, +1);
798 if (ast_sip_push_task(NULL, qualify_contact_task, contact)) {
799 ao2_ref(contact, -1);
804 static int ami_sip_qualify(struct mansession *s, const struct message *m)
806 const char *endpoint_name = astman_get_header(m, "Endpoint");
807 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
811 if (ast_strlen_zero(endpoint_name)) {
812 astman_send_error(s, m, "Endpoint parameter missing.");
816 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
819 astman_send_error(s, m, "Unable to retrieve endpoint\n");
823 /* send a qualify for all contacts registered with the endpoint */
824 if (ast_strlen_zero(endpoint->aors)) {
825 astman_send_error(s, m, "No AoRs configured for endpoint\n");
829 aors = ast_strdupa(endpoint->aors);
830 while ((aor_name = strsep(&aors, ","))) {
831 struct ast_sip_aor *aor;
832 struct ao2_container *contacts;
834 aor = ast_sip_location_retrieve_aor(aor_name);
839 contacts = ast_sip_location_retrieve_aor_contacts(aor);
841 ao2_callback(contacts, OBJ_NODATA, ami_contact_cb, NULL);
842 ao2_ref(contacts, -1);
848 astman_send_ack(s, m, "Endpoint found, will qualify");
852 static struct ast_cli_entry cli_options[] = {
853 AST_CLI_DEFINE(cli_qualify, "Send an OPTIONS request to a PJSIP endpoint")
856 static int sched_qualifies_hash_fn(const void *obj, int flags)
858 const struct sched_data *object;
859 const struct ast_sip_contact *key;
861 switch (flags & OBJ_SEARCH_MASK) {
865 case OBJ_SEARCH_OBJECT:
867 key = object->contact;
870 /* Hash can only work on something with a full key. */
874 return ast_str_hash(ast_sorcery_object_get_id(key));
877 static int sched_qualifies_cmp_fn(void *obj, void *arg, int flags)
879 const struct sched_data *object_left = obj;
880 const struct sched_data *object_right = arg;
881 struct ast_sip_contact *right_key = arg;
884 switch (flags & OBJ_SEARCH_MASK) {
885 case OBJ_SEARCH_OBJECT:
886 right_key = object_right->contact;
889 cmp = strcmp(ast_sorcery_object_get_id(object_left->contact),
890 ast_sorcery_object_get_id(right_key));
892 case OBJ_SEARCH_PARTIAL_KEY:
893 /* Not supported by container. */
898 * What arg points to is specific to this traversal callback
899 * and has no special meaning to astobj2.
908 * At this point the traversal callback is identical to a sorted
914 int ast_sip_initialize_sorcery_qualify(void)
916 struct ast_sorcery *sorcery = ast_sip_get_sorcery();
918 /* initialize sorcery ast_sip_contact_status resource */
919 ast_sorcery_apply_default(sorcery, CONTACT_STATUS, "memory", NULL);
921 if (ast_sorcery_internal_object_register(sorcery, CONTACT_STATUS,
922 contact_status_alloc, NULL, NULL)) {
923 ast_log(LOG_ERROR, "Unable to register ast_sip_contact_status in sorcery\n");
927 ast_sorcery_object_field_register_nodoc(sorcery, CONTACT_STATUS, "rtt", "0", OPT_UINT_T,
928 1, FLDSET(struct ast_sip_contact_status, status));
929 ast_sorcery_object_field_register_nodoc(sorcery, CONTACT_STATUS, "rtt", "0", OPT_UINT_T,
930 1, FLDSET(struct ast_sip_contact_status, rtt));
935 static int qualify_and_schedule_cb(void *obj, void *arg, int flags)
937 struct ast_sip_contact *contact = obj;
938 struct ast_sip_aor *aor = arg;
940 contact->qualify_frequency = aor->qualify_frequency;
941 contact->authenticate_qualify = aor->authenticate_qualify;
943 qualify_and_schedule(contact);
950 * \brief Qualify and schedule an endpoint's contacts
952 * \details For the given endpoint retrieve its list of aors, qualify all
953 * contacts, and schedule for checks if configured.
955 static int qualify_and_schedule_all_cb(void *obj, void *arg, int flags)
957 struct ast_sip_endpoint *endpoint = obj;
961 if (ast_strlen_zero(endpoint->aors)) {
965 aors = ast_strdupa(endpoint->aors);
966 while ((aor_name = strsep(&aors, ","))) {
967 struct ast_sip_aor *aor;
968 struct ao2_container *contacts;
970 aor = ast_sip_location_retrieve_aor(aor_name);
975 contacts = ast_sip_location_retrieve_aor_contacts(aor);
977 ao2_callback(contacts, OBJ_NODATA, qualify_and_schedule_cb, aor);
978 ao2_ref(contacts, -1);
987 static void qualify_and_schedule_all(void)
989 struct ao2_container *endpoints = ast_sip_get_endpoints();
995 ao2_callback(endpoints, OBJ_NODATA, qualify_and_schedule_all_cb, NULL);
996 ao2_ref(endpoints, -1);
999 static const char *status_map [] = {
1000 [UNAVAILABLE] = "Unreachable",
1001 [AVAILABLE] = "Reachable",
1004 static int format_contact_status(void *obj, void *arg, int flags)
1006 struct ast_sip_contact_wrapper *wrapper = obj;
1007 struct ast_sip_contact *contact = wrapper->contact;
1008 struct ast_sip_ami *ami = arg;
1009 struct ast_sip_contact_status *status;
1010 struct ast_str *buf;
1011 const struct ast_sip_endpoint *endpoint = ami->arg;
1013 buf = ast_sip_create_ami_event("ContactStatusDetail", ami);
1018 status = ast_sorcery_retrieve_by_id(
1019 ast_sip_get_sorcery(), CONTACT_STATUS,
1020 ast_sorcery_object_get_id(contact));
1022 ast_str_append(&buf, 0, "AOR: %s\r\n", wrapper->aor_id);
1023 ast_str_append(&buf, 0, "URI: %s\r\n", contact->uri);
1025 ast_str_append(&buf, 0, "Status: %s\r\n", status_map[status->status]);
1026 ast_str_append(&buf, 0, "RoundtripUsec: %" PRId64 "\r\n", status->rtt);
1028 ast_str_append(&buf, 0, "Status: Unknown\r\n");
1029 ast_str_append(&buf, 0, "RoundtripUsec: N/A\r\n");
1031 ast_str_append(&buf, 0, "EndpointName: %s\r\n",
1032 ast_sorcery_object_get_id(endpoint));
1033 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1037 ao2_cleanup(status);
1041 static int format_contact_status_for_aor(void *obj, void *arg, int flags)
1043 struct ast_sip_aor *aor = obj;
1045 return ast_sip_for_each_contact(aor, format_contact_status, arg);
1048 static int format_ami_contact_status(const struct ast_sip_endpoint *endpoint,
1049 struct ast_sip_ami *ami)
1051 ami->arg = (void *)endpoint;
1052 return ast_sip_for_each_aor(endpoint->aors, format_contact_status_for_aor, ami);
1055 static struct ast_sip_endpoint_formatter contact_status_formatter = {
1056 .format_ami = format_ami_contact_status
1059 int ast_res_pjsip_init_options_handling(int reload)
1061 static const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
1064 qualify_and_schedule_all();
1068 sched_qualifies = ao2_t_container_alloc(QUALIFIED_BUCKETS,
1069 sched_qualifies_hash_fn, sched_qualifies_cmp_fn,
1070 "Create container for scheduled qualifies");
1071 if (!sched_qualifies) {
1075 if (pjsip_endpt_register_module(ast_sip_get_pjsip_endpoint(), &options_module) != PJ_SUCCESS) {
1076 ao2_cleanup(sched_qualifies);
1077 sched_qualifies = NULL;
1081 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW,
1082 NULL, 1, &STR_OPTIONS) != PJ_SUCCESS) {
1083 pjsip_endpt_unregister_module(ast_sip_get_pjsip_endpoint(), &options_module);
1084 ao2_cleanup(sched_qualifies);
1085 sched_qualifies = NULL;
1089 ast_sip_register_endpoint_formatter(&contact_status_formatter);
1090 ast_manager_register2("PJSIPQualify", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_sip_qualify, NULL, NULL, NULL);
1091 ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options));
1093 qualify_and_schedule_all();
1098 void ast_res_pjsip_cleanup_options_handling(void)
1100 ast_cli_unregister_multiple(cli_options, ARRAY_LEN(cli_options));
1101 ast_manager_unregister("PJSIPQualify");
1102 ast_sip_unregister_endpoint_formatter(&contact_status_formatter);
1104 pjsip_endpt_unregister_module(ast_sip_get_pjsip_endpoint(), &options_module);
1105 ao2_cleanup(sched_qualifies);
1106 sched_qualifies = NULL;