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.
23 #include "asterisk/res_pjsip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/astobj2.h"
26 #include "asterisk/sorcery.h"
27 #include "include/res_pjsip_private.h"
29 /*! \brief Destructor for AOR */
30 static void aor_destroy(void *obj)
32 struct ast_sip_aor *aor = obj;
34 ao2_cleanup(aor->permanent_contacts);
35 ast_string_field_free_memory(aor);
38 /*! \brief Allocator for AOR */
39 static void *aor_alloc(const char *name)
41 struct ast_sip_aor *aor = ast_sorcery_generic_alloc(sizeof(struct ast_sip_aor), aor_destroy);
45 ast_string_field_init(aor, 128);
49 /*! \brief Destructor for contact */
50 static void contact_destroy(void *obj)
52 struct ast_sip_contact *contact = obj;
54 ast_string_field_free_memory(contact);
57 /*! \brief Allocator for contact */
58 static void *contact_alloc(const char *name)
60 struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
66 if (ast_string_field_init(contact, 256)) {
74 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
76 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
79 /*! \brief Internal callback function which deletes and unlinks any expired contacts */
80 static int contact_expire(void *obj, void *arg, int flags)
82 struct ast_sip_contact *contact = obj;
84 /* If the contact has not yet expired it is valid */
85 if (ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) > 0) {
89 ast_sip_location_delete_contact(contact);
94 /*! \brief Internal callback function which links static contacts into another container */
95 static int contact_link_static(void *obj, void *arg, int flags)
97 struct ao2_container *dest = arg;
99 ao2_link_flags(dest, obj, OBJ_NOLOCK);
103 /*! \brief Simple callback function which returns immediately, used to grab the first contact of an AOR */
104 static int contact_find_first(void *obj, void *arg, int flags)
106 return CMP_MATCH | CMP_STOP;
109 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)
111 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
112 struct ast_sip_contact *contact;
114 contacts = ast_sip_location_retrieve_aor_contacts(aor);
115 if (!contacts || (ao2_container_count(contacts) == 0)) {
119 contact = ao2_callback(contacts, OBJ_NOLOCK, contact_find_first, NULL);
123 struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
125 /* Give enough space for ^ at the beginning and ;@ at the end, since that is our object naming scheme */
126 char regex[strlen(ast_sorcery_object_get_id(aor)) + 4];
127 struct ao2_container *contacts;
129 snprintf(regex, sizeof(regex), "^%s;@", ast_sorcery_object_get_id(aor));
131 if (!(contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex))) {
135 /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */
136 ao2_callback(contacts, OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL);
138 /* Add any permanent contacts from the AOR */
139 if (aor->permanent_contacts) {
140 ao2_callback(aor->permanent_contacts, OBJ_NOLOCK | OBJ_NODATA, contact_link_static, contacts);
146 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
150 struct ast_sip_contact *contact = NULL;
152 /* If the location is still empty we have nowhere to go */
153 if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
154 ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
158 while ((aor_name = strsep(&rest, ","))) {
159 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
160 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
165 contact = ast_sip_location_retrieve_first_aor_contact(aor);
166 /* If a valid contact is available use its URI for dialing */
175 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
177 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
180 int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time)
182 char name[AST_UUID_STR_LEN];
183 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
185 snprintf(name, sizeof(name), "%s;@%s", ast_sorcery_object_get_id(aor), uri);
187 if (!(contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", name))) {
191 ast_string_field_set(contact, uri, uri);
192 contact->expiration_time = expiration_time;
193 contact->qualify_frequency = aor->qualify_frequency;
194 contact->authenticate_qualify = aor->authenticate_qualify;
196 return ast_sorcery_create(ast_sip_get_sorcery(), contact);
199 int ast_sip_location_update_contact(struct ast_sip_contact *contact)
201 return ast_sorcery_update(ast_sip_get_sorcery(), contact);
204 int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
206 return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
209 /*! \brief Custom handler for translating from a string timeval to actual structure */
210 static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
212 struct ast_sip_contact *contact = obj;
213 return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
216 /*! \brief Custom handler for translating from an actual structure timeval to string */
217 static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
219 const struct ast_sip_contact *contact = obj;
220 return (ast_asprintf(buf, "%lu", contact->expiration_time.tv_sec) < 0) ? -1 : 0;
223 /*! \brief Custom handler for permanent URIs */
224 static int permanent_uri_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
226 struct ast_sip_aor *aor = obj;
227 RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
229 pj_str_t contact_uri;
230 static const pj_str_t HCONTACT = { "Contact", 7 };
232 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Permanent Contact Validation", 256, 256);
237 pj_strdup2_with_null(pool, &contact_uri, var->value);
238 if (!pjsip_parse_hdr(pool, &HCONTACT, contact_uri.ptr, contact_uri.slen, NULL)) {
239 ast_log(LOG_ERROR, "Permanent URI on aor '%s' with contact '%s' failed to parse\n",
240 ast_sorcery_object_get_id(aor), var->value);
241 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
245 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
247 if ((!aor->permanent_contacts && !(aor->permanent_contacts = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) ||
248 !(contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", NULL))) {
252 ast_string_field_set(contact, uri, var->value);
253 ao2_link_flags(aor->permanent_contacts, contact, OBJ_NOLOCK);
258 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
262 if (!on_aor || ast_strlen_zero(aors)) {
266 copy = ast_strdupa(aors);
267 while ((name = strsep(©, ","))) {
268 RAII_VAR(struct ast_sip_aor *, aor,
269 ast_sip_location_retrieve_aor(name), ao2_cleanup);
275 if (on_aor(aor, arg, 0)) {
283 int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
284 on_contact_t on_contact, void *arg)
286 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
287 struct ast_sip_contact *contact;
289 struct ao2_iterator i;
292 !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
296 num = ao2_container_count(contacts);
297 i = ao2_iterator_init(contacts, 0);
298 while ((contact = ao2_iterator_next(&i))) {
299 int res = on_contact(aor, contact, --num == 0, arg);
301 ao2_ref(contact, -1);
306 ao2_iterator_destroy(&i);
310 int ast_sip_contact_to_str(const struct ast_sip_aor *aor,
311 const struct ast_sip_contact *contact,
314 struct ast_str **buf = arg;
316 ast_str_append(buf, 0, "%s/%s",
317 ast_sorcery_object_get_id(aor), contact->uri);
320 ast_str_append(buf, 0, ",");
326 static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
328 return ast_sip_sorcery_object_to_ami(aor, buf);
331 static int format_ami_aor_handler(void *obj, void *arg, int flags)
333 const struct ast_sip_aor *aor = obj;
334 struct ast_sip_ami *ami = arg;
335 const struct ast_sip_endpoint *endpoint = ami->arg;
336 RAII_VAR(struct ast_str *, buf,
337 ast_sip_create_ami_event("AorDetail", ami), ast_free);
340 RAII_VAR(struct ao2_container *, contacts,
341 ast_sip_location_retrieve_aor_contacts(aor), ao2_cleanup);
347 sip_aor_to_ami(aor, &buf);
348 ast_str_append(&buf, 0, "Contacts: ");
349 ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &buf);
350 ast_str_append(&buf, 0, "\r\n");
352 num = ao2_container_count(contacts);
353 ast_str_append(&buf, 0, "TotalContacts: %d\r\n", num);
354 ast_str_append(&buf, 0, "ContactsRegistered: %d\r\n",
355 num - ao2_container_count(aor->permanent_contacts));
356 ast_str_append(&buf, 0, "EndpointName: %s\r\n",
357 ast_sorcery_object_get_id(endpoint));
359 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
363 static int format_ami_endpoint_aor(const struct ast_sip_endpoint *endpoint,
364 struct ast_sip_ami *ami)
366 ami->arg = (void *)endpoint;
367 return ast_sip_for_each_aor(endpoint->aors,
368 format_ami_aor_handler, ami);
371 struct ast_sip_endpoint_formatter endpoint_aor_formatter = {
372 .format_ami = format_ami_endpoint_aor
375 /*! \brief Initialize sorcery with location support */
376 int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery)
378 ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
379 ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor");
381 if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, NULL) ||
382 ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, NULL)) {
386 ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
387 ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
388 ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, 0, 0);
389 ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
390 PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
392 ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
393 ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration));
394 ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration));
395 ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
396 ast_sorcery_object_field_register(sorcery, "aor", "qualify_frequency", 0, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_aor, qualify_frequency), 0, 86400);
397 ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
398 ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
399 ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
400 ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, NULL, 0, 0);
401 ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
403 ast_sip_register_endpoint_formatter(&endpoint_aor_formatter);