Debian Codegraph @

res/res_pjsip/location.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Joshua Colp <[email protected]>
7 *
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.
13 *
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.
17 */
18
19 #include "asterisk.h"
20 #include <pjsip.h>
21 #include <pjlib.h>
22
23 #include "asterisk/res_pjsip.h"
24 #include "asterisk/logger.h"
25 #include "asterisk/astobj2.h"
26 #include "asterisk/paths.h"
27 #include "asterisk/sorcery.h"
28 #include "asterisk/taskprocessor.h"
29 #include "include/res_pjsip_private.h"
30 #include "asterisk/res_pjsip_cli.h"
31 #include "asterisk/statsd.h"
32 #include "asterisk/named_locks.h"
33
34 #include "asterisk/res_pjproject.h"
35
36 static int pj_max_hostname = PJ_MAX_HOSTNAME;
37 static int pjsip_max_url_size = PJSIP_MAX_URL_SIZE;
38
39 /*! \brief Destructor for AOR */
40 static void aor_destroy(void *obj)
41 {
42 struct ast_sip_aor *aor = obj;
43
44 ao2_cleanup(aor->permanent_contacts);
45 ast_string_field_free_memory(aor);
46 ast_free(aor->voicemail_extension);
47 }
48
49 /*! \brief Allocator for AOR */
50 static void *aor_alloc(const char *name)
51 {
52 void *lock;
53 struct ast_sip_aor *aor;
54
55 lock = ast_named_lock_get(AST_NAMED_LOCK_TYPE_MUTEX, "aor", name);
56 if (!lock) {
57 return NULL;
58 }
59
60 aor = ast_sorcery_lockable_alloc(sizeof(struct ast_sip_aor), aor_destroy, lock);
61 ao2_ref(lock, -1);
62
63 if (!aor) {
64 return NULL;
65 }
66 ast_string_field_init(aor, 128);
67
68 return aor;
69 }
70
71 /*! \brief Internal callback function which destroys the specified contact */
72 static int destroy_contact(void *obj, void *arg, int flags)
73 {
74 struct ast_sip_contact *contact = obj;
75
76 ast_sip_location_delete_contact(contact);
77
78 return CMP_MATCH;
79 }
80
81 static void aor_deleted_observer(const void *object)
82 {
83 const struct ast_sip_aor *aor = object;
84 const char *aor_id = ast_sorcery_object_get_id(object);
85 /* Give enough space for ;@ at the end, since that is our object naming scheme */
86 size_t prefix_len = strlen(aor_id) + sizeof(";@") - 1;
87 char prefix[prefix_len + 1];
88 struct ao2_container *contacts;
89
90 if (aor->permanent_contacts) {
91 ao2_callback(aor->permanent_contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, destroy_contact, NULL);
92 }
93
94 sprintf(prefix, "%s;@", aor_id); /* Safe */
95 if (!(contacts = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), "contact", prefix, prefix_len))) {
96 return;
97 }
98 /* Destroy any contacts that may still exist that were made for this AoR */
99 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, destroy_contact, NULL);
100
101 ao2_ref(contacts, -1);
102 }
103
104 /*! \brief Observer for contacts so state can be updated on respective endpoints */
105 static const struct ast_sorcery_observer aor_observer = {
106 .deleted = aor_deleted_observer,
107 };
108
109
110 /*! \brief Destructor for contact */
111 static void contact_destroy(void *obj)
112 {
113 struct ast_sip_contact *contact = obj;
114
115 ast_string_field_free_memory(contact);
116 ao2_cleanup(contact->endpoint);
117 }
118
119 /*! \brief Allocator for contact */
120 static void *contact_alloc(const char *name)
121 {
122 struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
123 char *id = ast_strdupa(name);
124 char *aor = id;
125 char *aor_separator = NULL;
126
127 if (!contact) {
128 return NULL;
129 }
130
131 if (ast_string_field_init(contact, 256)) {
132 ao2_cleanup(contact);
133 return NULL;
134 }
135
136 /* Dynamic contacts are delimited with ";@" and static ones with "@@" */
137 if ((aor_separator = strstr(id, ";@")) || (aor_separator = strstr(id, "@@"))) {
138 *aor_separator = '\0';
139 }
140 ast_assert(aor_separator != NULL);
141
142 ast_string_field_set(contact, aor, aor);
143
144 return contact;
145 }
146
147 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
148 {
149 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
150 }
151
152 /*! \brief Internal callback function which deletes and unlinks any expired contacts */
153 static int contact_expire(void *obj, void *arg, int flags)
154 {
155 struct ast_sip_contact *contact = obj;
156
157 /* If the contact has not yet expired it is valid */
158 if (ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) > 0) {
159 return 0;
160 }
161
162 ast_sip_location_delete_contact(contact);
163
164 return CMP_MATCH;
165 }
166
167 /*! \brief Internal callback function which links static contacts into another container */
168 static int contact_link_static(void *obj, void *arg, int flags)
169 {
170 struct ao2_container *dest = arg;
171
172 ao2_link(dest, obj);
173 return 0;
174 }
175
176 /*! \brief Internal callback function which removes any contact which is unreachable */
177 static int contact_remove_unreachable(void *obj, void *arg, int flags)
178 {
179 struct ast_sip_contact *contact = obj;
180 struct ast_sip_contact_status *status;
181 int unreachable;
182
183 status = ast_sip_get_contact_status(contact);
184 if (!status) {
185 return 0;
186 }
187
188 unreachable = (status->status == UNAVAILABLE);
189 ao2_ref(status, -1);
190
191 return unreachable ? CMP_MATCH : 0;
192 }
193
194 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)
195 {
196 return ast_sip_location_retrieve_first_aor_contact_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
197 }
198
199 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact_filtered(const struct ast_sip_aor *aor,
200 unsigned int flags)
201 {
202 struct ao2_container *contacts;
203 struct ast_sip_contact *contact = NULL;
204
205 contacts = ast_sip_location_retrieve_aor_contacts_filtered(aor, flags);
206 if (contacts && ao2_container_count(contacts)) {
207 /* Get the first AOR contact in the container. */
208 contact = ao2_callback(contacts, 0, NULL, NULL);
209 }
210 ao2_cleanup(contacts);
211 return contact;
212 }
213
214 struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock(const struct ast_sip_aor *aor)
215 {
216 return ast_sip_location_retrieve_aor_contacts_nolock_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
217 }
218
219 struct ao2_container *ast_sip_location_retrieve_aor_contacts_nolock_filtered(const struct ast_sip_aor *aor,
220 unsigned int flags)
221 {
222 /* Give enough space for ;@ at the end, since that is our object naming scheme */
223 size_t prefix_len = strlen(ast_sorcery_object_get_id(aor)) + sizeof(";@") - 1;
224 char prefix[prefix_len + 1];
225 struct ao2_container *contacts;
226
227 sprintf(prefix, "%s;@", ast_sorcery_object_get_id(aor)); /* Safe */
228 if (!(contacts = ast_sorcery_retrieve_by_prefix(ast_sip_get_sorcery(), "contact", prefix, prefix_len))) {
229 return NULL;
230 }
231
232 /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */
233 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL);
234
235 /* Add any permanent contacts from the AOR */
236 if (aor->permanent_contacts) {
237 ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts);
238 }
239
240 if (flags & AST_SIP_CONTACT_FILTER_REACHABLE) {
241 ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_remove_unreachable, NULL);
242 }
243
244 return contacts;
245 }
246
247 struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
248 {
249 return ast_sip_location_retrieve_aor_contacts_filtered(aor, AST_SIP_CONTACT_FILTER_DEFAULT);
250 }
251
252 struct ao2_container *ast_sip_location_retrieve_aor_contacts_filtered(const struct ast_sip_aor *aor,
253 unsigned int flags)
254 {
255 struct ao2_container *contacts;
256
257 /* ao2_lock / ao2_unlock do not actually write aor since it has an ao2 lockobj. */
258 ao2_lock((void*)aor);
259 contacts = ast_sip_location_retrieve_aor_contacts_nolock_filtered(aor, flags);
260 ao2_unlock((void*)aor);
261
262 return contacts;
263 }
264
265
266 void ast_sip_location_retrieve_contact_and_aor_from_list(const char *aor_list, struct ast_sip_aor **aor,
267 struct ast_sip_contact **contact)
268 {
269 ast_sip_location_retrieve_contact_and_aor_from_list_filtered(aor_list, AST_SIP_CONTACT_FILTER_DEFAULT, aor, contact);
270 }
271
272 void ast_sip_location_retrieve_contact_and_aor_from_list_filtered(const char *aor_list, unsigned int flags,
273 struct ast_sip_aor **aor, struct ast_sip_contact **contact)
274 {
275 char *aor_name;
276 char *rest;
277
278 /* If the location is still empty we have nowhere to go */
279 if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
280 ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
281 return;
282 }
283
284 *aor = NULL;
285 *contact = NULL;
286
287 while ((aor_name = ast_strip(strsep(&rest, ",")))) {
288 *aor = ast_sip_location_retrieve_aor(aor_name);
289
290 if (!(*aor)) {
291 continue;
292 }
293 *contact = ast_sip_location_retrieve_first_aor_contact_filtered(*aor, flags);
294 /* If a valid contact is available use its URI for dialing */
295 if (*contact) {
296 break;
297 }
298
299 ao2_ref(*aor, -1);
300 *aor = NULL;
301 }
302 }
303
304 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
305 {
306 struct ast_sip_aor *aor;
307 struct ast_sip_contact *contact;
308
309 ast_sip_location_retrieve_contact_and_aor_from_list(aor_list, &aor, &contact);
310
311 ao2_cleanup(aor);
312
313 return contact;
314 }
315
316 static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags);
317 static int cli_contact_populate_container(void *obj, void *arg, int flags);
318
319 static int gather_contacts_for_aor(void *obj, void *arg, int flags)
320 {
321 struct ao2_container *aor_contacts;
322 struct ast_sip_aor *aor = obj;
323 struct ao2_container *container = arg;
324
325 aor_contacts = ast_sip_location_retrieve_aor_contacts(aor);
326 if (!aor_contacts) {
327 return 0;
328 }
329 ao2_callback(aor_contacts, OBJ_MULTIPLE | OBJ_NODATA, cli_contact_populate_container,
330 container);
331 ao2_ref(aor_contacts, -1);
332 return CMP_MATCH;
333 }
334
335 struct ao2_container *ast_sip_location_retrieve_contacts_from_aor_list(const char *aor_list)
336 {
337 struct ao2_container *contacts;
338
339 contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
340 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
341 if (!contacts) {
342 return NULL;
343 }
344
345 ast_sip_for_each_aor(aor_list, gather_contacts_for_aor, contacts);
346
347 return contacts;
348 }
349
350 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
351 {
352 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
353 }
354
355 struct ast_sip_contact *ast_sip_location_create_contact(struct ast_sip_aor *aor,
356 const char *uri, struct timeval expiration_time, const char *path_info,
357 const char *user_agent, const char *via_addr, int via_port, const char *call_id,
358 int prune_on_boot, struct ast_sip_endpoint *endpoint)
359 {
360 struct ast_sip_contact *contact;
361 char name[MAX_OBJECT_FIELD * 2 + 3];
362 char hash[33];
363
364 ast_md5_hash(hash, uri);
365 snprintf(name, sizeof(name), "%s;@%s", ast_sorcery_object_get_id(aor), hash);
366
367 contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", name);
368 if (!contact) {
369 return NULL;
370 }
371
372 ast_string_field_set(contact, uri, uri);
373 contact->expiration_time = expiration_time;
374 contact->qualify_frequency = aor->qualify_frequency;
375 contact->qualify_timeout = aor->qualify_timeout;
376 contact->qualify_2xx_only = aor->qualify_2xx_only;
377 contact->authenticate_qualify = aor->authenticate_qualify;
378 if (path_info && aor->support_path) {
379 ast_string_field_set(contact, path, path_info);
380 }
381
382 if (!ast_strlen_zero(aor->outbound_proxy)) {
383 ast_string_field_set(contact, outbound_proxy, aor->outbound_proxy);
384 }
385
386 if (!ast_strlen_zero(user_agent)) {
387 ast_string_field_set(contact, user_agent, user_agent);
388 }
389
390 if (!ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
391 ast_string_field_set(contact, reg_server, ast_config_AST_SYSTEM_NAME);
392 }
393
394 if (!ast_strlen_zero(via_addr)) {
395 ast_string_field_set(contact, via_addr, via_addr);
396 }
397 contact->via_port = via_port;
398
399 if (!ast_strlen_zero(call_id)) {
400 ast_string_field_set(contact, call_id, call_id);
401 }
402
403 contact->endpoint = ao2_bump(endpoint);
404 if (endpoint) {
405 ast_string_field_set(contact, endpoint_name, ast_sorcery_object_get_id(endpoint));
406 }
407
408 contact->prune_on_boot = prune_on_boot;
409
410 if (ast_sorcery_create(ast_sip_get_sorcery(), contact)) {
411 ao2_ref(contact, -1);
412 return NULL;
413 }
414 return contact;
415 }
416
417 int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri,
418 struct timeval expiration_time, const char *path_info, const char *user_agent,
419 const char *via_addr, int via_port, const char *call_id,
420 struct ast_sip_endpoint *endpoint)
421 {
422 struct ast_sip_contact *contact;
423
424 contact = ast_sip_location_create_contact(aor, uri, expiration_time, path_info,
425 user_agent, via_addr, via_port, call_id, 0, endpoint);
426 ao2_cleanup(contact);
427 return contact ? 0 : -1;
428 }
429
430 int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
431 struct timeval expiration_time, const char *path_info, const char *user_agent,
432 const char *via_addr, int via_port, const char *call_id,
433 struct ast_sip_endpoint *endpoint)
434 {
435 int res;
436
437 ao2_lock(aor);
438 res = ast_sip_location_add_contact_nolock(aor, uri, expiration_time, path_info, user_agent,
439 via_addr, via_port, call_id,
440 endpoint);
441 ao2_unlock(aor);
442
443 return res;
444 }
445
446 int ast_sip_location_update_contact(struct ast_sip_contact *contact)
447 {
448 return ast_sorcery_update(ast_sip_get_sorcery(), contact);
449 }
450
451 int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
452 {
453 return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
454 }
455
456 static int prune_boot_contacts_cb(void *obj, void *arg, int flags)
457 {
458 struct ast_sip_contact *contact = obj;
459
460 if (contact->prune_on_boot
461 && !strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {
462 ast_verb(3, "Removed contact '%s' from AOR '%s' due to system boot\n",
463 contact->uri, contact->aor);
464 ast_sip_location_delete_contact(contact);
465 }
466
467 return 0;
468 }
469
470 void ast_sip_location_prune_boot_contacts(void)
471 {
472 struct ao2_container *contacts;
473
474 contacts = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "contact",
475 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
476 if (contacts) {
477 ao2_callback(contacts, 0, prune_boot_contacts_cb, NULL);
478 ao2_ref(contacts, -1);
479 }
480 }
481
482 /*! \brief Custom handler for translating from a string timeval to actual structure */
483 static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
484 {
485 struct ast_sip_contact *contact = obj;
486 return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
487 }
488
489 /*! \brief Custom handler for translating from an actual structure timeval to string */
490 static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
491 {
492 const struct ast_sip_contact *contact = obj;
493 char secs[AST_TIME_T_LEN];
494
495 ast_time_t_to_string(contact->expiration_time.tv_sec, secs, sizeof(secs));
496 return (ast_asprintf(buf, "%s", secs) < 0) ? -1 : 0;
497 }
498
499 static int permanent_uri_sort_fn(const void *obj_left, const void *obj_right, int flags)
500 {
501 const struct ast_sip_contact *object_left = obj_left;
502 const struct ast_sip_contact *object_right = obj_right;
503 const char *right_key = obj_right;
504 int cmp;
505
506 switch (flags & OBJ_SEARCH_MASK) {
507 case OBJ_SEARCH_OBJECT:
508 right_key = ast_sorcery_object_get_id(object_right);
509 /* Fall through */
510 case OBJ_SEARCH_KEY:
511 cmp = strcmp(ast_sorcery_object_get_id(object_left), right_key);
512 break;
513 case OBJ_SEARCH_PARTIAL_KEY:
514 /*
515 * We could also use a partial key struct containing a length
516 * so strlen() does not get called for every comparison instead.
517 */
518 cmp = strncmp(ast_sorcery_object_get_id(object_left), right_key, strlen(right_key));
519 break;
520 default:
521 /* Sort can only work on something with a full or partial key. */
522 ast_assert(0);
523 cmp = 0;
524 break;
525 }
526 return cmp;
527 }
528
529 int ast_sip_validate_uri_length(const char *contact_uri)
530 {
531 int max_length = pj_max_hostname - 1;
532 char *contact = ast_strdupa(contact_uri);
533 char *host;
534 char *at;
535 int theres_a_port = 0;
536
537 if (strlen(contact_uri) > pjsip_max_url_size - 1) {
538 return -1;
539 }
540
541 contact = ast_strip_quoted(contact, "<", ">");
542
543 if (!strncasecmp(contact, "sip:", 4)) {
544 host = contact + 4;
545 } else if (!strncasecmp(contact, "sips:", 5)) {
546 host = contact + 5;
547 } else {
548 /* Not a SIP URI */
549 return -1;
550 }
551
552 at = strchr(contact, '@');
553 if (at) {
554 /* sip[s]:user@host */
555 host = at + 1;
556 }
557
558 if (host[0] == '[') {
559 /* Host is an IPv6 address. Just get up to the matching bracket */
560 char *close_bracket;
561
562 close_bracket = strchr(host, ']');
563 if (!close_bracket) {
564 return -1;
565 }
566 close_bracket++;
567 if (*close_bracket == ':') {
568 theres_a_port = 1;
569 }
570 *close_bracket = '\0';
571 } else {
572 /* uri parameters could contain ';' so trim them off first */
573 host = strsep(&host, ";?");
574 /* Host is FQDN or IPv4 address. Need to find closing delimiter */
575 if (strchr(host, ':')) {
576 theres_a_port = 1;
577 host = strsep(&host, ":");
578 }
579 }
580
581 if (!theres_a_port) {
582 max_length -= strlen("_sips.tcp.");
583 }
584
585 if (strlen(host) > max_length) {
586 return -1;
587 }
588
589 return 0;
590 }
591
592 /*! \brief Custom handler for permanent URIs */
593 static int permanent_uri_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
594 {
595 struct ast_sip_aor *aor = obj;
596 const char *aor_id = ast_sorcery_object_get_id(aor);
597 char *contacts;
598 char *contact_uri;
599
600 if (ast_strlen_zero(var->value)) {
601 return 0;
602 }
603
604 contacts = ast_strdupa(var->value);
605 while ((contact_uri = ast_strip(strsep(&contacts, ",")))) {
606 struct ast_sip_contact *contact;
607 struct ast_sip_contact_status *status;
608 char hash[33];
609 char contact_id[strlen(aor_id) + sizeof(hash) + 2];
610
611 if (ast_strlen_zero(contact_uri)) {
612 continue;
613 }
614
615 if (ast_sip_validate_uri_length(contact_uri)) {
616 ast_log(LOG_ERROR, "Contact uri or hostname length exceeds pjproject limit or is not a sip(s) uri: %s\n", contact_uri);
617 return -1;
618 }
619
620 if (!aor->permanent_contacts) {
621 aor->permanent_contacts = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK,
622 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, permanent_uri_sort_fn, NULL);
623 if (!aor->permanent_contacts) {
624 return -1;
625 }
626 }
627
628 ast_md5_hash(hash, contact_uri);
629 snprintf(contact_id, sizeof(contact_id), "%s@@%s", aor_id, hash);
630 contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", contact_id);
631 if (!contact) {
632 return -1;
633 }
634
635 ast_string_field_set(contact, uri, contact_uri);
636
637 status = ast_res_pjsip_find_or_create_contact_status(contact);
638 if (!status) {
639 ao2_ref(contact, -1);
640 return -1;
641 }
642 ao2_ref(status, -1);
643
644 ao2_link(aor->permanent_contacts, contact);
645 ao2_ref(contact, -1);
646 }
647
648 return 0;
649 }
650
651 static int contact_to_var_list(void *object, void *arg, int flags)
652 {
653 struct ast_sip_contact_wrapper *wrapper = object;
654 struct ast_variable **var = arg;
655
656 ast_variable_list_append(&*var, ast_variable_new("contact", wrapper->contact->uri, ""));
657
658 return 0;
659 }
660
661 static int contacts_to_var_list(const void *obj, struct ast_variable **fields)
662 {
663 const struct ast_sip_aor *aor = obj;
664
665 ast_sip_for_each_contact(aor, contact_to_var_list, fields);
666
667 return 0;
668 }
669
670 static int voicemail_extension_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
671 {
672 struct ast_sip_aor *aor = obj;
673
674 aor->voicemail_extension = ast_strdup(var->value);
675
676 return aor->voicemail_extension ? 0 : -1;
677 }
678
679 static int voicemail_extension_to_str(const void *obj, const intptr_t *args, char **buf)
680 {
681 const struct ast_sip_aor *aor = obj;
682
683 *buf = ast_strdup(aor->voicemail_extension);
684
685 return 0;
686 }
687
688 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
689 {
690 char *copy;
691 char *name;
692 int res;
693
694 if (!on_aor || ast_strlen_zero(aors)) {
695 return 0;
696 }
697
698 copy = ast_strdupa(aors);
699 while ((name = ast_strip(strsep(&copy, ",")))) {
700 struct ast_sip_aor *aor;
701
702 aor = ast_sip_location_retrieve_aor(name);
703 if (aor) {
704 res = on_aor(aor, arg, 0);
705 ao2_ref(aor, -1);
706 if (res) {
707 return -1;
708 }
709 }
710 }
711 return 0;
712 }
713
714 static void contact_wrapper_destroy(void *obj)
715 {
716 struct ast_sip_contact_wrapper *wrapper = obj;
717
718 ast_free(wrapper->aor_id);
719 ast_free(wrapper->contact_id);
720 ao2_cleanup(wrapper->contact);
721 }
722
723 int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
724 ao2_callback_fn on_contact, void *arg)
725 {
726 struct ao2_container *contacts;
727 struct ao2_iterator i;
728 int res = 0;
729 void *object = NULL;
730
731 if (!on_contact ||
732 !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
733 return 0;
734 }
735
736 i = ao2_iterator_init(contacts, 0);
737 while ((object = ao2_iterator_next(&i))) {
738 RAII_VAR(struct ast_sip_contact *, contact, object, ao2_cleanup);
739 RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
740 const char *aor_id = ast_sorcery_object_get_id(aor);
741
742 wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),
743 contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
744 if (!wrapper) {
745 res = -1;
746 break;
747 }
748 wrapper->contact_id = ast_malloc(strlen(aor_id) + strlen(contact->uri) + 2);
749 if (!wrapper->contact_id) {
750 res = -1;
751 break;
752 }
753 sprintf(wrapper->contact_id, "%s/%s", aor_id, contact->uri);
754 wrapper->aor_id = ast_strdup(aor_id);
755 if (!wrapper->aor_id) {
756 res = -1;
757 break;
758 }
759 wrapper->contact = contact;
760 ao2_bump(wrapper->contact);
761
762 if ((res = on_contact(wrapper, arg, 0))) {
763 break;
764 }
765 }
766 ao2_iterator_destroy(&i);
767 ao2_ref(contacts, -1);
768 return res;
769 }
770
771 int ast_sip_contact_to_str(void *object, void *arg, int flags)
772 {
773 struct ast_sip_contact_wrapper *wrapper = object;
774 struct ast_str **buf = arg;
775
776 ast_str_append(buf, 0, "%s,", wrapper->contact_id);
777
778 return 0;
779 }
780
781 static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
782 {
783 struct ast_variable *objset;
784 struct ast_variable *i;
785
786 objset = ast_sorcery_objectset_create2(ast_sip_get_sorcery(), aor,
787 AST_HANDLER_ONLY_STRING);
788 if (!objset) {
789 return -1;
790 }
791
792 ast_str_append(buf, 0, "ObjectType: %s\r\n",
793 ast_sorcery_object_get_type(aor));
794 ast_str_append(buf, 0, "ObjectName: %s\r\n",
795 ast_sorcery_object_get_id(aor));
796
797 for (i = objset; i; i = i->next) {
798 char *camel = ast_to_camel_case(i->name);
799
800 if (strcmp(camel, "Contact") == 0) {
801 ast_free(camel);
802 camel = NULL;
803 }
804 ast_str_append(buf, 0, "%s: %s\r\n", S_OR(camel, "Contacts"), i->value);
805 ast_free(camel);
806 }
807
808 ast_variables_destroy(objset);
809 return 0;
810 }
811
812 static int contacts_to_str(const void *obj, const intptr_t *args, char **buf)
813 {
814 const struct ast_sip_aor *aor = obj;
815 struct ast_str *str;
816
817 str = ast_str_create(MAX_OBJECT_FIELD);
818 if (!str) {
819 *buf = NULL;
820 return -1;
821 }
822
823 ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &str);
824 ast_str_truncate(str, -1);
825
826 *buf = ast_strdup(ast_str_buffer(str));
827 ast_free(str);
828
829 return *buf ? 0 : -1;
830 }
831
832 static int format_ami_aor_handler(void *obj, void *arg, int flags)
833 {
834 struct ast_sip_aor *aor = obj;
835 struct ast_sip_ami *ami = arg;
836 const struct ast_sip_endpoint *endpoint = ami->arg;
837 struct ast_str *buf;
838 struct ao2_container *contacts;
839 int total_contacts;
840 int num_permanent;
841
842 buf = ast_sip_create_ami_event("AorDetail", ami);
843 if (!buf) {
844 return -1;
845 }
846 contacts = ast_sip_location_retrieve_aor_contacts(aor);
847 if (!contacts) {
848 ast_free(buf);
849 return -1;
850 }
851
852 sip_aor_to_ami(aor, &buf);
853 total_contacts = ao2_container_count(contacts);
854 num_permanent = aor->permanent_contacts ?
855 ao2_container_count(aor->permanent_contacts) : 0;
856
857 ast_str_append(&buf, 0, "TotalContacts: %d\r\n", total_contacts);
858 ast_str_append(&buf, 0, "ContactsRegistered: %d\r\n",
859 total_contacts - num_permanent);
860 ast_str_append(&buf, 0, "EndpointName: %s\r\n",
861 ast_sorcery_object_get_id(endpoint));
862
863 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
864 ami->count++;
865
866 ast_free(buf);
867 ao2_ref(contacts, -1);
868 return 0;
869 }
870
871 static int format_ami_endpoint_aor(const struct ast_sip_endpoint *endpoint,
872 struct ast_sip_ami *ami)
873 {
874 ami->arg = (void *)endpoint;
875 return ast_sip_for_each_aor(endpoint->aors,
876 format_ami_aor_handler, ami);
877 }
878
879 struct ast_sip_endpoint_formatter endpoint_aor_formatter = {
880 .format_ami = format_ami_endpoint_aor
881 };
882
883 static struct ao2_container *cli_aor_get_container(const char *regex)
884 {
885 struct ao2_container *container;
886 struct ao2_container *s_container;
887
888 container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "aor", regex);
889 if (!container) {
890 return NULL;
891 }
892
893 /* Create a sorted container of aors. */
894 s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
895 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
896 if (s_container
897 && ao2_container_dup(s_container, container, 0)) {
898 ao2_ref(s_container, -1);
899 s_container = NULL;
900 }
901 ao2_ref(container, -1);
902
903 return s_container;
904 }
905
906 static int cli_contact_populate_container(void *obj, void *arg, int flags)
907 {
908 ao2_link(arg, obj);
909
910 return 0;
911 }
912
913 static int cli_aor_gather_contacts(void *obj, void *arg, int flags)
914 {
915 struct ast_sip_aor *aor = obj;
916
917 return ast_sip_for_each_contact(aor, cli_contact_populate_container, arg);
918 }
919
920 static const char *cli_contact_get_id(const void *obj)
921 {
922 const struct ast_sip_contact_wrapper *wrapper = obj;
923 return wrapper->contact_id;
924 }
925
926 static int cli_contact_sort(const void *obj, const void *arg, int flags)
927 {
928 const struct ast_sip_contact_wrapper *left_wrapper = obj;
929 const struct ast_sip_contact_wrapper *right_wrapper = arg;
930 const char *right_key = arg;
931 int cmp = 0;
932
933 switch (flags & OBJ_SEARCH_MASK) {
934 case OBJ_SEARCH_OBJECT:
935 right_key = right_wrapper->contact_id;
936 /* Fall through */
937 case OBJ_SEARCH_KEY:
938 cmp = strcmp(left_wrapper->contact_id, right_key);
939 break;
940 case OBJ_SEARCH_PARTIAL_KEY:
941 cmp = strncmp(left_wrapper->contact_id, right_key, strlen(right_key));
942 break;
943 default:
944 cmp = 0;
945 break;
946 }
947
948 return cmp;
949 }
950
951 static int cli_contact_compare(void *obj, void *arg, int flags)
952 {
953 const struct ast_sip_contact_wrapper *left_wrapper = obj;
954 const struct ast_sip_contact_wrapper *right_wrapper = arg;
955 const char *right_key = arg;
956 int cmp = 0;
957
958 switch (flags & OBJ_SEARCH_MASK) {
959 case OBJ_SEARCH_OBJECT:
960 right_key = right_wrapper->contact_id;
961 /* Fall through */
962 case OBJ_SEARCH_KEY:
963 if (strcmp(left_wrapper->contact_id, right_key) == 0) {;
964 cmp = CMP_MATCH | CMP_STOP;
965 }
966 break;
967 case OBJ_SEARCH_PARTIAL_KEY:
968 if (strncmp(left_wrapper->contact_id, right_key, strlen(right_key)) == 0) {
969 cmp = CMP_MATCH;
970 }
971 break;
972 default:
973 cmp = 0;
974 break;
975 }
976
977 return cmp;
978 }
979
980 static int cli_contact_iterate(void *container, ao2_callback_fn callback, void *args)
981 {
982 return ast_sip_for_each_contact(container, callback, args);
983 }
984
985 static int cli_filter_contacts(void *obj, void *arg, int flags)
986 {
987 struct ast_sip_contact_wrapper *wrapper = obj;
988 regex_t *regexbuf = arg;
989
990 if (!regexec(regexbuf, wrapper->contact_id, 0, NULL, 0)) {
991 return 0;
992 }
993
994 return CMP_MATCH;
995 }
996
997 static int cli_gather_contact(void *obj, void *arg, int flags)
998 {
999 struct ast_sip_contact *contact = obj;
1000 RAII_VAR(struct ast_sip_contact_wrapper *, wrapper, NULL, ao2_cleanup);
1001
1002 if (strcmp(contact->reg_server, ast_config_AST_SYSTEM_NAME ?: "")) {
1003 return 0;
1004 }
1005
1006 wrapper = ao2_alloc_options(sizeof(struct ast_sip_contact_wrapper),
1007 contact_wrapper_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
1008 if (!wrapper) {
1009 return -1;
1010 }
1011
1012 wrapper->contact_id = ast_malloc(strlen(contact->aor) + strlen(contact->uri) + 2);
1013 if (!wrapper->contact_id) {
1014 return -1;
1015 }
1016 sprintf(wrapper->contact_id, "%s/%s", contact->aor, contact->uri);
1017
1018 wrapper->aor_id = ast_strdup(contact->aor);
1019 if (!wrapper->aor_id) {
1020 return -1;
1021 }
1022
1023 wrapper->contact = ao2_bump(contact);
1024
1025 ao2_link(arg, wrapper);
1026
1027 return 0;
1028 }
1029
1030 static struct ao2_container *cli_contact_get_container(const char *regex)
1031 {
1032 RAII_VAR(struct ao2_container *, aors, NULL, ao2_cleanup);
1033 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
1034 RAII_VAR(struct ast_variable *, var_aor, NULL, ast_variables_destroy);
1035 struct ao2_container *contacts_container;
1036 regex_t regexbuf;
1037
1038 if (!(var_aor = ast_variable_new("contact !=", "", ""))) {
1039 return NULL;
1040 }
1041
1042 /* Retrieving all the contacts may result in finding the same contact multiple
1043 * times. So that they don't get displayed multiple times we only allow a
1044 * single one to be placed into the container.
1045 */
1046 contacts_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
1047 cli_contact_sort, cli_contact_compare);
1048 if (!contacts_container) {
1049 return NULL;
1050 }
1051
1052 contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex);
1053 if (!contacts) {
1054 ao2_ref(contacts_container, -1);
1055 return NULL;
1056 }
1057 ao2_callback(contacts, OBJ_NODATA, cli_gather_contact, contacts_container);
1058
1059 aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(),
1060 "aor", AST_RETRIEVE_FLAG_MULTIPLE, var_aor);
1061 if (!aors) {
1062 ao2_ref(contacts_container, -1);
1063 return NULL;
1064 }
1065
1066 ao2_callback(aors, OBJ_NODATA, cli_aor_gather_contacts, contacts_container);
1067
1068 if (!ast_strlen_zero(regex)) {
1069 if (regcomp(&regexbuf, regex, REG_EXTENDED | REG_NOSUB)) {
1070 ao2_ref(contacts_container, -1);
1071 return NULL;
1072 }
1073 ao2_callback(contacts_container, OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA, cli_filter_contacts, &regexbuf);
1074 regfree(&regexbuf);
1075 }
1076
1077 return contacts_container;
1078 }
1079
1080 static void *cli_contact_retrieve_by_id(const char *id)
1081 {
1082 struct ao2_container *container;
1083 void *obj;
1084
1085 container = cli_contact_get_container("");
1086 if (!container) {
1087 return NULL;
1088 }
1089
1090 obj = ao2_find(container, id, OBJ_SEARCH_KEY);
1091 ao2_ref(container, -1);
1092 return obj;
1093 }
1094
1095 static int cli_contact_print_header(void *obj, void *arg, int flags)
1096 {
1097 struct ast_sip_cli_context *context = arg;
1098 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1099 int filler = CLI_LAST_TABSTOP - indent - 23;
1100
1101 ast_assert(context->output_buffer != NULL);
1102
1103 ast_str_append(&context->output_buffer, 0,
1104 "%*s: <Aor/ContactUri%*.*s> <Hash....> <Status> <RTT(ms)..>\n",
1105 indent, "Contact", filler, filler, CLI_HEADER_FILLER);
1106
1107 return 0;
1108 }
1109
1110 static int cli_contact_print_body(void *obj, void *arg, int flags)
1111 {
1112 struct ast_sip_contact_wrapper *wrapper = obj;
1113 struct ast_sip_contact *contact = wrapper->contact;
1114 struct ast_sip_cli_context *context = arg;
1115 int indent;
1116 int flexwidth;
1117 const char *contact_id = ast_sorcery_object_get_id(contact);
1118 const char *hash_start = contact_id + strlen(contact->aor) + 2;
1119 struct ast_sip_contact_status *status;
1120
1121 ast_assert(contact->uri != NULL);
1122 ast_assert(context->output_buffer != NULL);
1123
1124 status = ast_sip_get_contact_status(contact);
1125
1126 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1127 flexwidth = CLI_LAST_TABSTOP - indent - 9 - strlen(contact->aor) + 1;
1128
1129 ast_str_append(&context->output_buffer, 0, "%*s: %s/%-*.*s %-10.10s %-7.7s %11.3f\n",
1130 indent,
1131 "Contact",
1132 contact->aor,
1133 flexwidth, flexwidth,
1134 contact->uri,
1135 hash_start,
1136 ast_sip_get_contact_short_status_label(status ? status->status : UNKNOWN),
1137 (status && (status->status == AVAILABLE)) ? ((long long) status->rtt) / 1000.0 : NAN);
1138
1139 if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1140 ast_str_append(&context->output_buffer, 0, "\n");
1141 ast_sip_cli_print_sorcery_objectset(contact, context, 0);
1142 }
1143
1144 ao2_cleanup(status);
1145 return 0;
1146 }
1147
1148 static int cli_aor_iterate(void *container, ao2_callback_fn callback, void *args)
1149 {
1150 const char *aor_list = container;
1151
1152 return ast_sip_for_each_aor(aor_list, callback, args);
1153 }
1154
1155 static void *cli_aor_retrieve_by_id(const char *id)
1156 {
1157 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", id);
1158 }
1159
1160 static const char *cli_aor_get_id(const void *obj)
1161 {
1162 return ast_sorcery_object_get_id(obj);
1163 }
1164
1165 static int cli_aor_print_header(void *obj, void *arg, int flags)
1166 {
1167 struct ast_sip_cli_context *context = arg;
1168 int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1169 int filler = CLI_LAST_TABSTOP - indent - 7;
1170
1171 ast_assert(context->output_buffer != NULL);
1172
1173 ast_str_append(&context->output_buffer, 0,
1174 "%*s: <Aor%*.*s> <MaxContact>\n",
1175 indent, "Aor", filler, filler, CLI_HEADER_FILLER);
1176
1177 if (context->recurse) {
1178 struct ast_sip_cli_formatter_entry *formatter_entry;
1179
1180 context->indent_level++;
1181 formatter_entry = ast_sip_lookup_cli_formatter("contact");
1182 if (formatter_entry) {
1183 formatter_entry->print_header(NULL, context, 0);
1184 ao2_ref(formatter_entry, -1);
1185 }
1186 context->indent_level--;
1187 }
1188
1189 return 0;
1190 }
1191
1192 static int cli_aor_print_body(void *obj, void *arg, int flags)
1193 {
1194 struct ast_sip_aor *aor = obj;
1195 struct ast_sip_cli_context *context = arg;
1196 int indent;
1197 int flexwidth;
1198
1199 ast_assert(context->output_buffer != NULL);
1200
1201 // context->current_aor = aor;
1202
1203 indent = CLI_INDENT_TO_SPACES(context->indent_level);
1204 flexwidth = CLI_LAST_TABSTOP - indent - 12;
1205
1206 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %12u\n",
1207 indent,
1208 "Aor",
1209 flexwidth, flexwidth,
1210 ast_sorcery_object_get_id(aor), aor->max_contacts);
1211
1212 if (context->recurse) {
1213 struct ast_sip_cli_formatter_entry *formatter_entry;
1214
1215 context->indent_level++;
1216
1217 formatter_entry = ast_sip_lookup_cli_formatter("contact");
1218 if (formatter_entry) {
1219 formatter_entry->iterate(aor, formatter_entry->print_body, context);
1220 ao2_ref(formatter_entry, -1);
1221 }
1222
1223 context->indent_level--;
1224
1225 if (context->indent_level == 0) {
1226 ast_str_append(&context->output_buffer, 0, "\n");
1227 }
1228 }
1229
1230 if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1231 ast_str_append(&context->output_buffer, 0, "\n");
1232 ast_sip_cli_print_sorcery_objectset(aor, context, 0);
1233 }
1234
1235 return 0;
1236 }
1237
1238 static struct ao2_container *cli_get_aors(void)
1239 {
1240 struct ao2_container *aors;
1241
1242 aors = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "aor",
1243 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1244
1245 return aors;
1246 }
1247
1248 static int format_ami_aorlist_handler(void *obj, void *arg, int flags)
1249 {
1250 struct ast_sip_aor *aor = obj;
1251 struct ast_sip_ami *ami = arg;
1252 struct ast_str *buf;
1253
1254 buf = ast_sip_create_ami_event("AorList", ami);
1255 if (!buf) {
1256 return -1;
1257 }
1258
1259 sip_aor_to_ami(aor, &buf);
1260
1261 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1262 ami->count++;
1263
1264 ast_free(buf);
1265
1266 return 0;
1267 }
1268
1269 static int ami_show_aors(struct mansession *s, const struct message *m)
1270 {
1271 struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
1272 struct ao2_container *aors;
1273
1274 aors = cli_get_aors();
1275 if (!aors) {
1276 astman_send_error(s, m, "Could not get AORs\n");
1277 return 0;
1278 }
1279
1280 if (!ao2_container_count(aors)) {
1281 astman_send_error(s, m, "No AORs found\n");
1282 ao2_ref(aors, -1);
1283 return 0;
1284 }
1285
1286 astman_send_listack(s, m, "A listing of AORs follows, presented as AorList events",
1287 "start");
1288
1289 ao2_callback(aors, OBJ_NODATA, format_ami_aorlist_handler, &ami);
1290
1291 astman_send_list_complete_start(s, m, "AorListComplete", ami.count);
1292 astman_send_list_complete_end(s);
1293
1294 ao2_ref(aors, -1);
1295
1296 return 0;
1297 }
1298
1299 static struct ast_cli_entry cli_commands[] = {
1300 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Aors",
1301 .command = "pjsip list aors",
1302 .usage = "Usage: pjsip list aors [ like <pattern> ]\n"
1303 " List the configured PJSIP Aors\n"
1304 " Optional regular expression pattern is used to filter the list.\n"),
1305 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aors",
1306 .command = "pjsip show aors",
1307 .usage = "Usage: pjsip show aors [ like <pattern> ]\n"
1308 " Show the configured PJSIP Aors\n"
1309 " Optional regular expression pattern is used to filter the list.\n"),
1310 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aor",
1311 .command = "pjsip show aor",
1312 .usage = "Usage: pjsip show aor <id>\n"
1313 " Show the configured PJSIP Aor\n"),
1314
1315 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Contacts",
1316 .command = "pjsip list contacts",
1317 .usage = "Usage: pjsip list contacts [ like <pattern> ]\n"
1318 " List the configured PJSIP contacts\n"
1319 " Optional regular expression pattern is used to filter the list.\n"),
1320 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contacts",
1321 .command = "pjsip show contacts",
1322 .usage = "Usage: pjsip show contacts [ like <pattern> ]\n"
1323 " Show the configured PJSIP contacts\n"
1324 " Optional regular expression pattern is used to filter the list.\n"),
1325 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Contact",
1326 .command = "pjsip show contact",
1327 .usage = "Usage: pjsip show contact\n"
1328 " Show the configured PJSIP contact\n"),
1329 };
1330
1331 struct ast_sip_cli_formatter_entry *contact_formatter;
1332 struct ast_sip_cli_formatter_entry *aor_formatter;
1333
1334 /*! \brief Always create a contact_status for each contact */
1335 static int contact_apply_handler(const struct ast_sorcery *sorcery, void *object)
1336 {
1337 struct ast_sip_contact_status *status;
1338 struct ast_sip_contact *contact = object;
1339
1340 if (ast_strlen_zero(contact->uri)) {
1341 ast_log(LOG_ERROR, "A URI on dynamic contact '%s' is empty\n",
1342 ast_sorcery_object_get_id(contact));
1343 return -1;
1344 }
1345 status = ast_res_pjsip_find_or_create_contact_status(contact);
1346 ao2_cleanup(status);
1347
1348 return status ? 0 : -1;
1349 }
1350
1351 static int aor_apply_outbound_proxy(void *obj, void *arg, int flags)
1352 {
1353 struct ast_sip_contact *contact = obj;
1354 struct ast_sip_aor *aor = arg;
1355
1356 ast_string_field_set(contact, outbound_proxy, aor->outbound_proxy);
1357
1358 return 0;
1359 }
1360
1361 static int aor_apply_handler(const struct ast_sorcery *sorcery, void *object)
1362 {
1363 struct ast_sip_aor *aor = object;
1364
1365 if (!aor->permanent_contacts || ast_strlen_zero(aor->outbound_proxy)) {
1366 return 0;
1367 }
1368
1369 ao2_callback(aor->permanent_contacts, OBJ_NODATA | OBJ_MULTIPLE, aor_apply_outbound_proxy, aor);
1370
1371 return 0;
1372 }
1373
1374 /*! \brief Initialize sorcery with location support */
1375 int ast_sip_initialize_sorcery_location(void)
1376 {
1377 struct ast_sorcery *sorcery = ast_sip_get_sorcery();
1378 int i;
1379
1380 ast_pjproject_get_buildopt("PJ_MAX_HOSTNAME", "%d", &pj_max_hostname);
1381 /* As of pjproject 2.4.5, PJSIP_MAX_URL_SIZE isn't exposed yet but we try anyway. */
1382 ast_pjproject_get_buildopt("PJSIP_MAX_URL_SIZE", "%d", &pjsip_max_url_size);
1383
1384 ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
1385 ast_sorcery_object_set_congestion_levels(sorcery, "contact", -1,
1386 3 * AST_TASKPROCESSOR_HIGH_WATER_LEVEL);
1387 ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor");
1388
1389 if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, contact_apply_handler) ||
1390 ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, aor_apply_handler)) {
1391 return -1;
1392 }
1393
1394 ast_sorcery_observer_add(sorcery, "aor", &aor_observer);
1395
1396 ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
1397 ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
1398 ast_sorcery_object_field_register(sorcery, "contact", "path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, path));
1399 ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, NULL, 0, 0);
1400 ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
1401 PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
1402 ast_sorcery_object_field_register(sorcery, "contact", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_contact, qualify_timeout));
1403 ast_sorcery_object_field_register(sorcery, "contact", "qualify_2xx_only", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_contact, qualify_2xx_only));
1404 ast_sorcery_object_field_register(sorcery, "contact", "authenticate_qualify", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_contact, authenticate_qualify));
1405 ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
1406 ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent));
1407 ast_sorcery_object_field_register(sorcery, "contact", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, endpoint_name));
1408 ast_sorcery_object_field_register(sorcery, "contact", "reg_server", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, reg_server));
1409 ast_sorcery_object_field_register(sorcery, "contact", "via_addr", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, via_addr));
1410 ast_sorcery_object_field_register(sorcery, "contact", "via_port", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_contact, via_port));
1411 ast_sorcery_object_field_register(sorcery, "contact", "call_id", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, call_id));
1412 ast_sorcery_object_field_register(sorcery, "contact", "prune_on_boot", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_contact, prune_on_boot));
1413
1414 ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
1415 ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration));
1416 ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration));
1417 ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
1418 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);
1419 ast_sorcery_object_field_register(sorcery, "aor", "qualify_timeout", "3.0", OPT_DOUBLE_T, 0, FLDSET(struct ast_sip_aor, qualify_timeout));
1420 ast_sorcery_object_field_register(sorcery, "aor", "qualify_2xx_only", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, qualify_2xx_only));
1421 ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
1422 ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
1423 ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
1424 ast_sorcery_object_field_register(sorcery, "aor", "remove_unavailable", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_unavailable));
1425 ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, contacts_to_str, contacts_to_var_list, 0, 0);
1426 ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
1427 ast_sorcery_object_field_register_custom(sorcery, "aor", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
1428 ast_sorcery_object_field_register(sorcery, "aor", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, outbound_proxy));
1429 ast_sorcery_object_field_register(sorcery, "aor", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, support_path));
1430
1431 ast_sip_register_endpoint_formatter(&endpoint_aor_formatter);
1432
1433 contact_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1434 if (!contact_formatter) {
1435 ast_log(LOG_ERROR, "Unable to allocate memory for contact_formatter\n");
1436 return -1;
1437 }
1438 contact_formatter->name = "contact";
1439 contact_formatter->print_header = cli_contact_print_header;
1440 contact_formatter->print_body = cli_contact_print_body;
1441 contact_formatter->get_container = cli_contact_get_container;
1442 contact_formatter->iterate = cli_contact_iterate;
1443 contact_formatter->get_id = cli_contact_get_id;
1444 contact_formatter->retrieve_by_id = cli_contact_retrieve_by_id;
1445
1446 aor_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1447 if (!aor_formatter) {
1448 ast_log(LOG_ERROR, "Unable to allocate memory for aor_formatter\n");
1449 return -1;
1450 }
1451 aor_formatter->name = "aor";
1452 aor_formatter->print_header = cli_aor_print_header;
1453 aor_formatter->print_body = cli_aor_print_body;
1454 aor_formatter->get_container = cli_aor_get_container;
1455 aor_formatter->iterate = cli_aor_iterate;
1456 aor_formatter->get_id = cli_aor_get_id;
1457 aor_formatter->retrieve_by_id = cli_aor_retrieve_by_id;
1458
1459 ast_sip_register_cli_formatter(contact_formatter);
1460 ast_sip_register_cli_formatter(aor_formatter);
1461 ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1462
1463 if (ast_manager_register_xml("PJSIPShowAors", EVENT_FLAG_SYSTEM, ami_show_aors)) {
1464 return -1;
1465 }
1466
1467 /*
1468 * Reset StatsD gauges in case we didn't shut down cleanly.
1469 * Note that this must done here, as contacts will create the contact_status
1470 * object before PJSIP options handling is initialized.
1471 */
1472 for (i = 0; i <= REMOVED; i++) {
1473 ast_statsd_log_full_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, 0, 1.0, ast_sip_get_contact_status_label(i));
1474 }
1475
1476 return 0;
1477 }
1478
1479 int ast_sip_destroy_sorcery_location(void)
1480 {
1481 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "aor", &aor_observer);
1482 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1483 ast_sip_unregister_cli_formatter(contact_formatter);
1484 ast_sip_unregister_cli_formatter(aor_formatter);
1485 ast_manager_unregister("PJSIPShowAors");
1486
1487 ast_sip_unregister_endpoint_formatter(&endpoint_aor_formatter);
1488
1489 return 0;
1490 }