2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@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.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <support_level>core</support_level>
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/module.h"
32 #include "asterisk/test.h"
33 #include "asterisk/taskprocessor.h"
34 #include "asterisk/manager.h"
35 #include "res_pjsip/include/res_pjsip_private.h"
38 <manager name="PJSIPShowRegistrationsInbound" language="en_US">
40 Lists PJSIP inbound registrations.
45 In response <literal>InboundRegistrationDetail</literal> events showing configuration and status
46 information are raised for each inbound registration object. As well as <literal>AuthDetail</literal>
47 events for each associated auth object. Once all events are completed an
48 <literal>InboundRegistrationDetailComplete</literal> is issued.
54 /*! \brief Internal function which returns the expiration time for a contact */
55 static int registrar_get_expiration(const struct ast_sip_aor *aor, const pjsip_contact_hdr *contact, const pjsip_rx_data *rdata)
57 pjsip_expires_hdr *expires;
58 int expiration = aor->default_expiration;
60 if (contact->expires != -1) {
61 /* Expiration was provided with the contact itself */
62 expiration = contact->expires;
63 } else if ((expires = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL))) {
64 /* Expiration was provided using the Expires header */
65 expiration = expires->ivalue;
68 /* If the value has explicitly been set to 0, do not enforce */
73 /* Enforce the range that we will allow for expiration */
74 if (expiration < aor->minimum_expiration) {
75 expiration = aor->minimum_expiration;
76 } else if (expiration > aor->maximum_expiration) {
77 expiration = aor->maximum_expiration;
83 /*! \brief Structure used for finding contact */
84 struct registrar_contact_details {
85 /*! \brief Pool used for parsing URI */
87 /*! \brief URI being looked for */
91 /*! \brief Callback function for finding a contact */
92 static int registrar_find_contact(void *obj, void *arg, int flags)
94 struct ast_sip_contact *contact = obj;
95 const struct registrar_contact_details *details = arg;
96 pjsip_uri *contact_uri = pjsip_parse_uri(details->pool, (char*)contact->uri, strlen(contact->uri), 0);
98 return (pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR, details->uri, contact_uri) == PJ_SUCCESS) ? CMP_MATCH | CMP_STOP : 0;
101 /*! \brief Internal function which validates provided Contact headers to confirm that they are acceptable, and returns number of contacts */
102 static int registrar_validate_contacts(const pjsip_rx_data *rdata, struct ao2_container *contacts, struct ast_sip_aor *aor, int *added, int *updated, int *deleted)
104 pjsip_contact_hdr *previous = NULL, *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
105 struct registrar_contact_details details = {
106 .pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Contact Comparison", 256, 256),
113 while ((contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
114 int expiration = registrar_get_expiration(aor, contact, rdata);
115 RAII_VAR(struct ast_sip_contact *, existing, NULL, ao2_cleanup);
118 /* The expiration MUST be 0 when a '*' contact is used and there must be no other contact */
119 if ((expiration != 0) || previous) {
120 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);
124 } else if (previous && previous->star) {
125 /* If there is a previous contact and it is a '*' this is a deal breaker */
126 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);
131 if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
135 details.uri = pjsip_uri_get_uri(contact->uri);
137 /* Determine if this is an add, update, or delete for policy enforcement purposes */
138 if (!(existing = ao2_callback(contacts, 0, registrar_find_contact, &details))) {
142 } else if (expiration) {
149 /* The provided contacts are acceptable, huzzah! */
150 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);
154 /*! \brief Callback function which prunes static contacts */
155 static int registrar_prune_static(void *obj, void *arg, int flags)
157 struct ast_sip_contact *contact = obj;
159 return ast_tvzero(contact->expiration_time) ? CMP_MATCH : 0;
162 /*! \brief Internal function used to delete all contacts from an AOR */
163 static int registrar_delete_contact(void *obj, void *arg, int flags)
165 struct ast_sip_contact *contact = obj;
166 const char *aor_name = arg;
168 ast_sip_location_delete_contact(contact);
169 if (!ast_strlen_zero(aor_name)) {
170 ast_verb(3, "Removed contact '%s' from AOR '%s' due to request\n", contact->uri, aor_name);
171 ast_test_suite_event_notify("AOR_CONTACT_REMOVED",
181 /*! \brief Internal function which adds a contact to a response */
182 static int registrar_add_contact(void *obj, void *arg, int flags)
184 struct ast_sip_contact *contact = obj;
185 pjsip_tx_data *tdata = arg;
186 pjsip_contact_hdr *hdr = pjsip_contact_hdr_create(tdata->pool);
189 pj_strdup2_with_null(tdata->pool, &uri, contact->uri);
190 hdr->uri = pjsip_parse_uri(tdata->pool, uri.ptr, uri.slen, PJSIP_PARSE_URI_AS_NAMEADDR);
191 hdr->expires = ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) / 1000;
193 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hdr);
198 /*! \brief Helper function which adds a Date header to a response */
199 static void registrar_add_date_header(pjsip_tx_data *tdata)
203 time_t t = time(NULL);
206 strftime(date, sizeof(date), "%a, %d %b %Y %T GMT", &tm);
208 ast_sip_add_header(tdata, "Date", date);
211 #define SERIALIZER_BUCKETS 59
213 static struct ao2_container *serializers;
215 /*! \brief Serializer with associated aor key */
217 /* Serializer to distribute tasks to */
218 struct ast_taskprocessor *serializer;
219 /* The name of the aor to associate with the serializer */
223 static void serializer_destroy(void *obj)
225 struct serializer *ser = obj;
227 ast_taskprocessor_unreference(ser->serializer);
230 static struct serializer *serializer_create(const char *aor_name)
232 size_t size = strlen(aor_name) + 1;
233 struct serializer *ser = ao2_alloc(
234 sizeof(*ser) + size, serializer_destroy);
240 if (!(ser->serializer = ast_sip_create_serializer())) {
245 strcpy(ser->aor_name, aor_name);
249 static struct serializer *serializer_find_or_create(const char *aor_name)
251 struct serializer *ser = ao2_find(serializers, aor_name, OBJ_SEARCH_KEY);
257 if (!(ser = serializer_create(aor_name))) {
261 ao2_link(serializers, ser);
265 static int serializer_hash(const void *obj, const int flags)
267 const struct serializer *object;
270 switch (flags & OBJ_SEARCH_MASK) {
273 return ast_str_hash(key);
274 case OBJ_SEARCH_OBJECT:
276 return ast_str_hash(object->aor_name);
278 /* Hash can only work on something with a full key. */
284 static int serializer_cmp(void *obj_left, void *obj_right, int flags)
286 const struct serializer *object_left = obj_left;
287 const struct serializer *object_right = obj_right;
288 const char *right_key = obj_right;
291 switch (flags & OBJ_SEARCH_MASK) {
292 case OBJ_SEARCH_OBJECT:
293 right_key = object_right->aor_name;
296 cmp = strcmp(object_left->aor_name, right_key);
298 case OBJ_SEARCH_PARTIAL_KEY:
300 * We could also use a partial key struct containing a length
301 * so strlen() does not get called for every comparison instead.
303 cmp = strncmp(object_left->aor_name, right_key, strlen(right_key));
310 return cmp ? 0 : CMP_MATCH;
313 struct rx_task_data {
314 pjsip_rx_data *rdata;
315 struct ast_sip_endpoint *endpoint;
316 struct ast_sip_aor *aor;
319 static void rx_task_data_destroy(void *obj)
321 struct rx_task_data *task_data = obj;
323 pjsip_rx_data_free_cloned(task_data->rdata);
324 ao2_cleanup(task_data->endpoint);
325 ao2_cleanup(task_data->aor);
328 static struct rx_task_data *rx_task_data_create(pjsip_rx_data *rdata,
329 struct ast_sip_endpoint *endpoint,
330 struct ast_sip_aor *aor)
332 struct rx_task_data *task_data = ao2_alloc(
333 sizeof(*task_data), rx_task_data_destroy);
339 pjsip_rx_data_clone(rdata, 0, &task_data->rdata);
341 task_data->endpoint = endpoint;
342 ao2_ref(task_data->endpoint, +1);
344 task_data->aor = aor;
345 ao2_ref(task_data->aor, +1);
350 static const pj_str_t path_hdr_name = { "Path", 4 };
352 static int build_path_data(struct rx_task_data *task_data, struct ast_str **path_str)
354 pjsip_generic_string_hdr *path_hdr = pjsip_msg_find_hdr_by_name(task_data->rdata->msg_info.msg, &path_hdr_name, NULL);
360 *path_str = ast_str_create(64);
365 ast_str_set(path_str, 0, "%.*s", (int)path_hdr->hvalue.slen, path_hdr->hvalue.ptr);
367 while ((path_hdr = (pjsip_generic_string_hdr *) pjsip_msg_find_hdr_by_name(task_data->rdata->msg_info.msg, &path_hdr_name, path_hdr->next))) {
368 ast_str_append(path_str, 0, ",%.*s", (int)path_hdr->hvalue.slen, path_hdr->hvalue.ptr);
374 static int registrar_validate_path(struct rx_task_data *task_data, struct ast_str **path_str)
376 const pj_str_t path_supported_name = { "path", 4 };
377 pjsip_supported_hdr *supported_hdr;
380 if (!task_data->aor->support_path) {
384 if (build_path_data(task_data, path_str)) {
392 supported_hdr = pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_SUPPORTED, NULL);
393 if (!supported_hdr) {
397 /* Find advertised path support */
398 for (i = 0; i < supported_hdr->count; i++) {
399 if (!pj_stricmp(&supported_hdr->values[i], &path_supported_name)) {
404 /* Path header present, but support not advertised */
408 static int rx_task(void *data)
410 RAII_VAR(struct rx_task_data *, task_data, data, ao2_cleanup);
411 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
413 int added = 0, updated = 0, deleted = 0;
414 pjsip_contact_hdr *contact_hdr = NULL;
415 struct registrar_contact_details details = { 0, };
416 pjsip_tx_data *tdata;
417 pjsip_response_addr addr;
418 const char *aor_name = ast_sorcery_object_get_id(task_data->aor);
419 RAII_VAR(struct ast_str *, path_str, NULL, ast_free);
420 struct ast_sip_contact *response_contact;
422 /* Retrieve the current contacts, we'll need to know whether to update or not */
423 contacts = ast_sip_location_retrieve_aor_contacts(task_data->aor);
425 /* So we don't count static contacts against max_contacts we prune them out from the container */
426 ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, registrar_prune_static, NULL);
428 if (registrar_validate_contacts(task_data->rdata, contacts, task_data->aor, &added, &updated, &deleted)) {
429 /* The provided Contact headers do not conform to the specification */
430 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), task_data->rdata, 400, NULL, NULL, NULL);
431 ast_sip_report_failed_acl(task_data->endpoint, task_data->rdata, "registrar_invalid_contacts_provided");
432 ast_log(LOG_WARNING, "Failed to validate contacts in REGISTER request from '%s'\n",
433 ast_sorcery_object_get_id(task_data->endpoint));
437 if (registrar_validate_path(task_data, &path_str)) {
438 /* Ensure that intervening proxies did not make invalid modifications to the request */
439 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), task_data->rdata, 420, NULL, NULL, NULL);
440 ast_log(LOG_WARNING, "Invalid modifications made to REGISTER request from '%s' by intervening proxy\n",
441 ast_sorcery_object_get_id(task_data->endpoint));
445 if ((MAX(added - deleted, 0) + (!task_data->aor->remove_existing ? ao2_container_count(contacts) : 0)) > task_data->aor->max_contacts) {
446 /* Enforce the maximum number of contacts */
447 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), task_data->rdata, 403, NULL, NULL, NULL);
448 ast_sip_report_failed_acl(task_data->endpoint, task_data->rdata, "registrar_attempt_exceeds_maximum_configured_contacts");
449 ast_log(LOG_WARNING, "Registration attempt from endpoint '%s' to AOR '%s' will exceed max contacts of %d\n",
450 ast_sorcery_object_get_id(task_data->endpoint), ast_sorcery_object_get_id(task_data->aor), task_data->aor->max_contacts);
454 if (!(details.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Contact Comparison", 256, 256))) {
455 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), task_data->rdata, 500, NULL, NULL, NULL);
459 /* Iterate each provided Contact header and add, update, or delete */
460 while ((contact_hdr = pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_CONTACT, contact_hdr ? contact_hdr->next : NULL))) {
462 char contact_uri[PJSIP_MAX_URL_SIZE];
463 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
465 if (contact_hdr->star) {
466 /* A star means to unregister everything, so do so for the possible contacts */
467 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE, registrar_delete_contact, (void *)aor_name);
471 if (!PJSIP_URI_SCHEME_IS_SIP(contact_hdr->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact_hdr->uri)) {
472 /* This registrar only currently supports sip: and sips: URI schemes */
476 expiration = registrar_get_expiration(task_data->aor, contact_hdr, task_data->rdata);
477 details.uri = pjsip_uri_get_uri(contact_hdr->uri);
478 pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, details.uri, contact_uri, sizeof(contact_uri));
480 if (!(contact = ao2_callback(contacts, OBJ_UNLINK, registrar_find_contact, &details))) {
481 /* If they are actually trying to delete a contact that does not exist... be forgiving */
483 ast_verb(3, "Attempted to remove non-existent contact '%s' from AOR '%s' by request\n",
484 contact_uri, aor_name);
488 ast_sip_location_add_contact(task_data->aor, contact_uri, ast_tvadd(ast_tvnow(),
489 ast_samp2tv(expiration, 1)), path_str ? ast_str_buffer(path_str) : NULL);
490 ast_verb(3, "Added contact '%s' to AOR '%s' with expiration of %d seconds\n",
491 contact_uri, aor_name, expiration);
492 ast_test_suite_event_notify("AOR_CONTACT_ADDED",
499 } else if (expiration) {
500 RAII_VAR(struct ast_sip_contact *, updated, ast_sorcery_copy(ast_sip_get_sorcery(), contact), ao2_cleanup);
501 updated->expiration_time = ast_tvadd(ast_tvnow(), ast_samp2tv(expiration, 1));
502 updated->qualify_frequency = task_data->aor->qualify_frequency;
503 updated->authenticate_qualify = task_data->aor->authenticate_qualify;
505 ast_string_field_set(updated, path, ast_str_buffer(path_str));
508 ast_sip_location_update_contact(updated);
509 ast_debug(3, "Refreshed contact '%s' on AOR '%s' with new expiration of %d seconds\n",
510 contact_uri, aor_name, expiration);
511 ast_test_suite_event_notify("AOR_CONTACT_REFRESHED",
519 ast_sip_location_delete_contact(contact);
520 ast_verb(3, "Removed contact '%s' from AOR '%s' due to request\n", contact_uri, aor_name);
521 ast_test_suite_event_notify("AOR_CONTACT_REMOVED",
529 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), details.pool);
531 /* If the AOR is configured to remove any existing contacts that have not been updated/added as a result of this REGISTER
534 if (task_data->aor->remove_existing) {
535 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE, registrar_delete_contact, NULL);
538 /* Update the contacts as things will probably have changed */
539 ao2_cleanup(contacts);
541 contacts = ast_sip_location_retrieve_aor_contacts(task_data->aor);
542 response_contact = ao2_callback(contacts, 0, NULL, NULL);
544 /* Send a response containing all of the contacts (including static) that are present on this AOR */
545 if (ast_sip_create_response(task_data->rdata, 200, response_contact, &tdata) != PJ_SUCCESS) {
546 ao2_cleanup(response_contact);
549 ao2_cleanup(response_contact);
551 /* Add the date header to the response, some UAs use this to set their date and time */
552 registrar_add_date_header(tdata);
554 ao2_callback(contacts, 0, registrar_add_contact, tdata);
556 if (pjsip_get_response_addr(tdata->pool, task_data->rdata, &addr) == PJ_SUCCESS) {
557 ast_sip_send_response(&addr, tdata, task_data->endpoint);
559 pjsip_tx_data_dec_ref(tdata);
565 static pj_bool_t registrar_on_rx_request(struct pjsip_rx_data *rdata)
567 RAII_VAR(struct serializer *, ser, NULL, ao2_cleanup);
568 struct rx_task_data *task_data;
570 RAII_VAR(struct ast_sip_endpoint *, endpoint,
571 ast_pjsip_rdata_get_endpoint(rdata), ao2_cleanup);
572 RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
575 char *configured_aors, *aor_name;
576 RAII_VAR(struct ast_str *, id, NULL, ast_free);
578 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_register_method) || !endpoint) {
582 if (ast_strlen_zero(endpoint->aors)) {
583 /* Short circuit early if the endpoint has no AORs configured on it, which means no registration possible */
584 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
585 ast_sip_report_failed_acl(endpoint, rdata, "registrar_attempt_without_configured_aors");
586 ast_log(LOG_WARNING, "Endpoint '%s' has no configured AORs\n", ast_sorcery_object_get_id(endpoint));
590 if (!PJSIP_URI_SCHEME_IS_SIP(rdata->msg_info.to->uri) && !PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.to->uri)) {
591 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 416, NULL, NULL, NULL);
592 ast_sip_report_failed_acl(endpoint, rdata, "registrar_invalid_uri_in_to_received");
593 ast_log(LOG_WARNING, "Endpoint '%s' attempted to register to an AOR with a non-SIP URI\n", ast_sorcery_object_get_id(endpoint));
597 uri = pjsip_uri_get_uri(rdata->msg_info.to->uri);
598 domain_name = ast_alloca(uri->host.slen + 1);
599 ast_copy_pj_str(domain_name, &uri->host, uri->host.slen + 1);
601 configured_aors = ast_strdupa(endpoint->aors);
603 /* Iterate the configured AORs to see if the user or the user+domain match */
604 while ((aor_name = strsep(&configured_aors, ","))) {
605 struct ast_sip_domain_alias *alias = NULL;
607 if (!pj_strcmp2(&uri->user, aor_name)) {
611 if (!id && !(id = ast_str_create(uri->user.slen + uri->host.slen + 2))) {
615 ast_str_set(&id, 0, "%.*s@", (int)uri->user.slen, uri->user.ptr);
616 if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
617 ast_str_append(&id, 0, "%s", alias->domain);
620 ast_str_append(&id, 0, "%s", domain_name);
623 if (!strcmp(aor_name, ast_str_buffer(id))) {
629 if (ast_strlen_zero(aor_name) || !(aor = ast_sip_location_retrieve_aor(aor_name))) {
630 /* The provided AOR name was not found (be it within the configuration or sorcery itself) */
631 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 404, NULL, NULL, NULL);
632 ast_sip_report_req_no_support(endpoint, rdata, "registrar_requested_aor_not_found");
633 ast_log(LOG_WARNING, "AOR '%.*s' not found for endpoint '%s'\n", (int)uri->user.slen, uri->user.ptr, ast_sorcery_object_get_id(endpoint));
637 if (!aor->max_contacts) {
638 /* Registration is not permitted for this AOR */
639 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
640 ast_sip_report_req_no_support(endpoint, rdata, "registrar_attempt_without_registration_permitted");
641 ast_log(LOG_WARNING, "AOR '%s' has no configured max_contacts. Endpoint '%s' unable to register\n",
642 ast_sorcery_object_get_id(aor), ast_sorcery_object_get_id(endpoint));
646 if (!(ser = serializer_find_or_create(aor_name))) {
647 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
648 ast_sip_report_mem_limit(endpoint, rdata);
649 ast_log(LOG_WARNING, "Endpoint '%s' unable to register on AOR '%s' - could not get serializer\n",
650 ast_sorcery_object_get_id(endpoint), ast_sorcery_object_get_id(aor));
654 if (!(task_data = rx_task_data_create(rdata, endpoint, aor))) {
655 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
656 ast_sip_report_mem_limit(endpoint, rdata);
657 ast_log(LOG_WARNING, "Endpoint '%s' unable to register on AOR '%s' - could not create rx_task_data\n",
658 ast_sorcery_object_get_id(endpoint), ast_sorcery_object_get_id(aor));
662 if (ast_sip_push_task(ser->serializer, rx_task, task_data)) {
663 pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL);
664 ast_sip_report_mem_limit(endpoint, rdata);
665 ast_log(LOG_WARNING, "Endpoint '%s' unable to register on AOR '%s' - could not serialize task\n",
666 ast_sorcery_object_get_id(endpoint), ast_sorcery_object_get_id(aor));
667 ao2_ref(task_data, -1);
672 /* function pointer to callback needs to be within the module
673 in order to avoid problems with an undefined symbol */
674 static int sip_contact_to_str(void *acp, void *arg, int flags)
676 return ast_sip_contact_to_str(acp, arg, flags);
679 static int ami_registrations_aor(void *obj, void *arg, int flags)
681 struct ast_sip_aor *aor = obj;
682 struct ast_sip_ami *ami = arg;
683 int *count = ami->arg;
684 RAII_VAR(struct ast_str *, buf,
685 ast_sip_create_ami_event("InboundRegistrationDetail", ami), ast_free);
691 ast_sip_sorcery_object_to_ami(aor, &buf);
692 ast_str_append(&buf, 0, "Contacts: ");
693 ast_sip_for_each_contact(aor, sip_contact_to_str, &buf);
694 ast_str_append(&buf, 0, "\r\n");
696 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
701 static int ami_registrations_endpoint(void *obj, void *arg, int flags)
703 struct ast_sip_endpoint *endpoint = obj;
704 return ast_sip_for_each_aor(
705 endpoint->aors, ami_registrations_aor, arg);
708 static int ami_registrations_endpoints(void *arg)
710 RAII_VAR(struct ao2_container *, endpoints,
711 ast_sip_get_endpoints(), ao2_cleanup);
717 ao2_callback(endpoints, OBJ_NODATA, ami_registrations_endpoint, arg);
721 static int ami_show_registrations(struct mansession *s, const struct message *m)
724 struct ast_sip_ami ami = { .s = s, .m = m, .arg = &count };
725 astman_send_listack(s, m, "Following are Events for each Inbound "
726 "registration", "start");
728 ami_registrations_endpoints(&ami);
731 "Event: InboundRegistrationDetailComplete\r\n"
732 "EventList: Complete\r\n"
733 "ListItems: %d\r\n\r\n", count);
737 #define AMI_SHOW_REGISTRATIONS "PJSIPShowRegistrationsInbound"
739 static pjsip_module registrar_module = {
740 .name = { "Registrar", 9 },
742 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
743 .on_rx_request = registrar_on_rx_request,
746 static int load_module(void)
748 const pj_str_t STR_REGISTER = { "REGISTER", 8 };
750 if (!(serializers = ao2_container_alloc(
751 SERIALIZER_BUCKETS, serializer_hash, serializer_cmp))) {
752 return AST_MODULE_LOAD_DECLINE;
755 if (ast_sip_register_service(®istrar_module)) {
756 return AST_MODULE_LOAD_DECLINE;
759 if (pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_ALLOW, NULL, 1, &STR_REGISTER) != PJ_SUCCESS) {
760 ast_sip_unregister_service(®istrar_module);
761 return AST_MODULE_LOAD_DECLINE;
764 ast_manager_register_xml(AMI_SHOW_REGISTRATIONS, EVENT_FLAG_SYSTEM,
765 ami_show_registrations);
767 return AST_MODULE_LOAD_SUCCESS;
770 static int unload_module(void)
772 ast_manager_unregister(AMI_SHOW_REGISTRATIONS);
773 ast_sip_unregister_service(®istrar_module);
775 ao2_cleanup(serializers);
779 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Registrar Support",
781 .unload = unload_module,
782 .load_pri = AST_MODPRI_APP_DEPEND,