Store SIP User-Agent information in contacts.
[asterisk/asterisk.git] / res / res_pjsip / location.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
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/sorcery.h"
27 #include "include/res_pjsip_private.h"
28 #include "asterisk/res_pjsip_cli.h"
29
30 /*! \brief Destructor for AOR */
31 static void aor_destroy(void *obj)
32 {
33         struct ast_sip_aor *aor = obj;
34
35         ao2_cleanup(aor->permanent_contacts);
36         ast_string_field_free_memory(aor);
37 }
38
39 /*! \brief Allocator for AOR */
40 static void *aor_alloc(const char *name)
41 {
42         struct ast_sip_aor *aor = ast_sorcery_generic_alloc(sizeof(struct ast_sip_aor), aor_destroy);
43         if (!aor) {
44                 return NULL;
45         }
46         ast_string_field_init(aor, 128);
47         return aor;
48 }
49
50 /*! \brief Destructor for contact */
51 static void contact_destroy(void *obj)
52 {
53         struct ast_sip_contact *contact = obj;
54
55         ast_string_field_free_memory(contact);
56 }
57
58 /*! \brief Allocator for contact */
59 static void *contact_alloc(const char *name)
60 {
61         struct ast_sip_contact *contact = ast_sorcery_generic_alloc(sizeof(*contact), contact_destroy);
62
63         if (!contact) {
64                 return NULL;
65         }
66
67         if (ast_string_field_init(contact, 256)) {
68                 ao2_cleanup(contact);
69                 return NULL;
70         }
71
72         return contact;
73 }
74
75 struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name)
76 {
77         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name);
78 }
79
80 /*! \brief Internal callback function which deletes and unlinks any expired contacts */
81 static int contact_expire(void *obj, void *arg, int flags)
82 {
83         struct ast_sip_contact *contact = obj;
84
85         /* If the contact has not yet expired it is valid */
86         if (ast_tvdiff_ms(contact->expiration_time, ast_tvnow()) > 0) {
87                 return 0;
88         }
89
90         ast_sip_location_delete_contact(contact);
91
92         return CMP_MATCH;
93 }
94
95 /*! \brief Internal callback function which links static contacts into another container */
96 static int contact_link_static(void *obj, void *arg, int flags)
97 {
98         struct ao2_container *dest = arg;
99
100         ao2_link(dest, obj);
101         return 0;
102 }
103
104 /*! \brief Simple callback function which returns immediately, used to grab the first contact of an AOR */
105 static int contact_find_first(void *obj, void *arg, int flags)
106 {
107         return CMP_MATCH | CMP_STOP;
108 }
109
110 struct ast_sip_contact *ast_sip_location_retrieve_first_aor_contact(const struct ast_sip_aor *aor)
111 {
112         RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
113         struct ast_sip_contact *contact;
114
115         contacts = ast_sip_location_retrieve_aor_contacts(aor);
116         if (!contacts || (ao2_container_count(contacts) == 0)) {
117                 return NULL;
118         }
119
120         contact = ao2_callback(contacts, 0, contact_find_first, NULL);
121         return contact;
122 }
123
124 struct ao2_container *ast_sip_location_retrieve_aor_contacts(const struct ast_sip_aor *aor)
125 {
126         /* Give enough space for ^ at the beginning and ;@ at the end, since that is our object naming scheme */
127         char regex[strlen(ast_sorcery_object_get_id(aor)) + 4];
128         struct ao2_container *contacts;
129
130         snprintf(regex, sizeof(regex), "^%s;@", ast_sorcery_object_get_id(aor));
131
132         if (!(contacts = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "contact", regex))) {
133                 return NULL;
134         }
135
136         /* Prune any expired contacts and delete them, we do this first because static contacts can never expire */
137         ao2_callback(contacts, OBJ_NODATA | OBJ_MULTIPLE | OBJ_UNLINK, contact_expire, NULL);
138
139         /* Add any permanent contacts from the AOR */
140         if (aor->permanent_contacts) {
141                 ao2_callback(aor->permanent_contacts, OBJ_NODATA, contact_link_static, contacts);
142         }
143
144         return contacts;
145 }
146
147 struct ast_sip_contact *ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
148 {
149         char *aor_name;
150         char *rest;
151         struct ast_sip_contact *contact = NULL;
152
153         /* If the location is still empty we have nowhere to go */
154         if (ast_strlen_zero(aor_list) || !(rest = ast_strdupa(aor_list))) {
155                 ast_log(LOG_WARNING, "Unable to determine contacts from empty aor list\n");
156                 return NULL;
157         }
158
159         while ((aor_name = strsep(&rest, ","))) {
160                 RAII_VAR(struct ast_sip_aor *, aor, ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
161                 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
162
163                 if (!aor) {
164                         continue;
165                 }
166                 contact = ast_sip_location_retrieve_first_aor_contact(aor);
167                 /* If a valid contact is available use its URI for dialing */
168                 if (contact) {
169                         break;
170                 }
171         }
172
173         return contact;
174 }
175
176 struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_name)
177 {
178         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "contact", contact_name);
179 }
180
181 int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri,
182                 struct timeval expiration_time, const char *path_info, const char *user_agent)
183 {
184         char name[AST_UUID_STR_LEN];
185         RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
186
187         snprintf(name, sizeof(name), "%s;@%s", ast_sorcery_object_get_id(aor), uri);
188
189         if (!(contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", name))) {
190                 return -1;
191         }
192
193         ast_string_field_set(contact, uri, uri);
194         contact->expiration_time = expiration_time;
195         contact->qualify_frequency = aor->qualify_frequency;
196         contact->authenticate_qualify = aor->authenticate_qualify;
197         if (path_info && aor->support_path) {
198                 ast_string_field_set(contact, path, path_info);
199         }
200
201         if (!ast_strlen_zero(aor->outbound_proxy)) {
202                 ast_string_field_set(contact, outbound_proxy, aor->outbound_proxy);
203         }
204
205         if (!ast_strlen_zero(user_agent)) {
206                 ast_string_field_set(contact, user_agent, user_agent);
207         }
208
209         return ast_sorcery_create(ast_sip_get_sorcery(), contact);
210 }
211
212 int ast_sip_location_update_contact(struct ast_sip_contact *contact)
213 {
214         return ast_sorcery_update(ast_sip_get_sorcery(), contact);
215 }
216
217 int ast_sip_location_delete_contact(struct ast_sip_contact *contact)
218 {
219         return ast_sorcery_delete(ast_sip_get_sorcery(), contact);
220 }
221
222 /*! \brief Custom handler for translating from a string timeval to actual structure */
223 static int expiration_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
224 {
225         struct ast_sip_contact *contact = obj;
226         return ast_get_timeval(var->value, &contact->expiration_time, ast_tv(0, 0), NULL);
227 }
228
229 /*! \brief Custom handler for translating from an actual structure timeval to string */
230 static int expiration_struct2str(const void *obj, const intptr_t *args, char **buf)
231 {
232         const struct ast_sip_contact *contact = obj;
233         return (ast_asprintf(buf, "%lu", contact->expiration_time.tv_sec) < 0) ? -1 : 0;
234 }
235
236 /*! \brief Helper function which validates a permanent contact */
237 static int permanent_contact_validate(void *data)
238 {
239         const char *value = data;
240         pj_pool_t *pool;
241         pj_str_t contact_uri;
242         static const pj_str_t HCONTACT = { "Contact", 7 };
243
244         pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Permanent Contact Validation", 256, 256);
245         if (!pool) {
246                 return -1;
247         }
248
249         pj_strdup2_with_null(pool, &contact_uri, value);
250         if (!pjsip_parse_hdr(pool, &HCONTACT, contact_uri.ptr, contact_uri.slen, NULL)) {
251                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
252                 return -1;
253         }
254
255         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
256         return 0;
257 }
258
259 /*! \brief Custom handler for permanent URIs */
260 static int permanent_uri_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
261 {
262         struct ast_sip_aor *aor = obj;
263         RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
264
265         if (ast_sip_push_task_synchronous(NULL, permanent_contact_validate, (char*)var->value)) {
266                 ast_log(LOG_ERROR, "Permanent URI on aor '%s' with contact '%s' failed to parse\n",
267                         ast_sorcery_object_get_id(aor), var->value);
268                 return -1;
269         }
270
271         if ((!aor->permanent_contacts && !(aor->permanent_contacts = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL))) ||
272                 !(contact = ast_sorcery_alloc(ast_sip_get_sorcery(), "contact", NULL))) {
273                 return -1;
274         }
275
276         ast_string_field_set(contact, uri, var->value);
277         ao2_link(aor->permanent_contacts, contact);
278
279         return 0;
280 }
281
282 int ast_sip_for_each_aor(const char *aors, ao2_callback_fn on_aor, void *arg)
283 {
284         char *copy, *name;
285
286         if (!on_aor || ast_strlen_zero(aors)) {
287                 return 0;
288         }
289
290         copy = ast_strdupa(aors);
291         while ((name = strsep(&copy, ","))) {
292                 RAII_VAR(struct ast_sip_aor *, aor,
293                          ast_sip_location_retrieve_aor(name), ao2_cleanup);
294
295                 if (!aor) {
296                         continue;
297                 }
298
299                 if (on_aor(aor, arg, 0)) {
300                         return -1;
301                 }
302         }
303         ast_free(copy);
304         return 0;
305 }
306
307 int ast_sip_for_each_contact(const struct ast_sip_aor *aor,
308                 ao2_callback_fn on_contact, void *arg)
309 {
310         RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
311         struct ast_sip_contact *contact;
312         struct ao2_iterator i;
313
314         if (!on_contact ||
315             !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
316                 return 0;
317         }
318
319         i = ao2_iterator_init(contacts, 0);
320         while ((contact = ao2_iterator_next(&i))) {
321                 int res;
322
323                 ast_sorcery_object_set_extended(contact, "@aor_id", ast_sorcery_object_get_id(aor));
324                 if ((res = on_contact(contact, arg, 0))) {
325                         ao2_iterator_destroy(&i);
326                         return -1;
327                 }
328         }
329         ao2_iterator_destroy(&i);
330         return 0;
331 }
332
333 int ast_sip_contact_to_str(void *object, void *arg, int flags)
334 {
335         struct ast_sip_contact *contact = object;
336         struct ast_str **buf = arg;
337
338         ast_str_append(buf, 0, "%s/%s,",
339                 ast_sorcery_object_get_extended(contact, "aor_id"), contact->uri);
340
341         return 0;
342 }
343
344 static int sip_aor_to_ami(const struct ast_sip_aor *aor, struct ast_str **buf)
345 {
346         return ast_sip_sorcery_object_to_ami(aor, buf);
347 }
348
349 static int format_ami_aor_handler(void *obj, void *arg, int flags)
350 {
351         struct ast_sip_aor *aor = obj;
352         struct ast_sip_ami *ami = arg;
353         const struct ast_sip_endpoint *endpoint = ami->arg;
354         RAII_VAR(struct ast_str *, buf,
355                  ast_sip_create_ami_event("AorDetail", ami), ast_free);
356
357         int total_contacts;
358         int num_permanent;
359         RAII_VAR(struct ao2_container *, contacts,
360                  ast_sip_location_retrieve_aor_contacts(aor), ao2_cleanup);
361
362         if (!buf) {
363                 return -1;
364         }
365
366         sip_aor_to_ami(aor, &buf);
367         ast_str_append(&buf, 0, "Contacts: ");
368         ast_sip_for_each_contact(aor, ast_sip_contact_to_str, &buf);
369         ast_str_truncate(buf, -1);
370         ast_str_append(&buf, 0, "\r\n");
371
372         total_contacts = ao2_container_count(contacts);
373         num_permanent = aor->permanent_contacts ?
374                 ao2_container_count(aor->permanent_contacts) : 0;
375
376         ast_str_append(&buf, 0, "TotalContacts: %d\r\n", total_contacts);
377         ast_str_append(&buf, 0, "ContactsRegistered: %d\r\n",
378                        total_contacts - num_permanent);
379         ast_str_append(&buf, 0, "EndpointName: %s\r\n",
380                        ast_sorcery_object_get_id(endpoint));
381
382         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
383         return 0;
384 }
385
386 static int format_ami_endpoint_aor(const struct ast_sip_endpoint *endpoint,
387                                    struct ast_sip_ami *ami)
388 {
389         ami->arg = (void *)endpoint;
390         return ast_sip_for_each_aor(endpoint->aors,
391                                     format_ami_aor_handler, ami);
392 }
393
394 struct ast_sip_endpoint_formatter endpoint_aor_formatter = {
395         .format_ami = format_ami_endpoint_aor
396 };
397
398 static struct ao2_container *cli_get_aor_container(void)
399 {
400         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
401         RAII_VAR(struct ao2_container *, s_container, NULL, ao2_cleanup);
402
403         container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "aor",
404                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
405         if (!container) {
406                 return NULL;
407         }
408
409         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
410                 ast_sorcery_object_id_compare, NULL);
411         if (!s_container) {
412                 return NULL;
413         }
414
415         if (ao2_container_dup(s_container, container, 0)) {
416                 return NULL;
417         }
418         ao2_ref(s_container, +1);
419         return s_container;
420 }
421
422 static int populate_contact_container(void *obj, void *arg, int flags)
423 {
424         struct ast_sip_contact *contact = obj;
425         struct ao2_container *container = arg;
426
427         ao2_link(container, contact);
428         return 0;
429 }
430
431 static int gather_aor_contacts(void *obj, void *arg, int flags)
432 {
433         struct ast_sip_aor *aor = obj;
434         struct ao2_container *container = arg;
435         ast_sip_for_each_contact(aor, populate_contact_container, container);
436         return 0;
437 }
438
439 static int cli_contact_compare(const void *left, const void *right, int flags)
440 {
441         const struct ast_sip_contact *left_contact = left;
442         const struct ast_sip_contact *right_contact = right;
443         int rc;
444
445         if (!left_contact || !right_contact) {
446                 return 0;
447         }
448         rc = strcmp(ast_sorcery_object_get_extended(left_contact, "aor_id"),
449                 ast_sorcery_object_get_extended(right_contact, "aor_id"));
450         if (rc) {
451                 return rc;
452         }
453         return strcmp(left_contact->uri, right_contact->uri);
454 }
455
456 static struct ao2_container *cli_get_contact_container(void)
457 {
458         RAII_VAR(struct ao2_container *, parent_container, NULL, ao2_cleanup);
459         struct ao2_container *child_container;
460
461         parent_container =  cli_get_aor_container();
462         if (!parent_container) {
463                 return NULL;
464         }
465
466         child_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
467                 cli_contact_compare, NULL);
468         if (!child_container) {
469                 return NULL;
470         }
471
472         ao2_ref(child_container, +1);
473         ao2_callback(parent_container, OBJ_NODATA, gather_aor_contacts, child_container);
474
475         return child_container;
476 }
477
478 static int cli_contact_iterator(const void *container, ao2_callback_fn callback, void *args)
479 {
480         const struct ast_sip_aor *array = container;
481
482         return ast_sip_for_each_contact(array, callback, args);
483 }
484
485 static int cli_print_contact_header(void *obj, void *arg, int flags)
486 {
487         struct ast_sip_cli_context *context = arg;
488         int indent = CLI_INDENT_TO_SPACES(context->indent_level);
489         int filler = CLI_LAST_TABSTOP - indent - 18;
490
491         if (!context->output_buffer) {
492                 return -1;
493         }
494         ast_str_append(&context->output_buffer, 0,
495                 "%*s:  <Aor/ContactUri%*.*s>  <Status....>  <RTT(ms)..>\n",
496                 indent, "Contact", filler, filler, CLI_HEADER_FILLER);
497
498         return 0;
499 }
500
501 static int cli_print_contact_body(void *obj, void *arg, int flags)
502 {
503         struct ast_sip_contact *contact = obj;
504         struct ast_sip_cli_context *context = arg;
505         char *print_name = NULL;
506         int print_name_len;
507         int indent;
508         int flexwidth;
509         const char *aor_id = ast_sorcery_object_get_extended(contact, "aor_id");
510
511         RAII_VAR(struct ast_sip_contact_status *, status,
512                 ast_sorcery_retrieve_by_id( ast_sip_get_sorcery(), CONTACT_STATUS, ast_sorcery_object_get_id(contact)),
513                 ao2_cleanup);
514
515         if (!context->output_buffer) {
516                 return -1;
517         }
518
519         print_name_len = strlen(aor_id) + strlen(contact->uri) + 2;
520         print_name = ast_alloca(print_name_len);
521         snprintf(print_name, print_name_len, "%s/%s", aor_id, contact->uri);
522
523         indent = CLI_INDENT_TO_SPACES(context->indent_level);
524         flexwidth = CLI_LAST_TABSTOP - indent - 2;
525
526         ast_str_append(&context->output_buffer, 0, "%*s:  %-*.*s  %-12.12s  %11.3f\n",
527                 indent,
528                 "Contact",
529                 flexwidth, flexwidth,
530                 print_name,
531                 (status ? (status->status == AVAILABLE ? "Avail" : "Unavail") : "Unknown"),
532                 (status ? ((long long) status->rtt) / 1000.0 : NAN));
533
534         return 0;
535 }
536
537 static int cli_aor_iterator(const void *container, ao2_callback_fn callback, void *args)
538 {
539         const char *aor_list = container;
540
541         return ast_sip_for_each_aor(aor_list, callback, args);
542 }
543
544 static int cli_print_aor_header(void *obj, void *arg, int flags)
545 {
546         struct ast_sip_cli_context *context = arg;
547         struct ast_sip_cli_formatter_entry *formatter_entry;
548
549         int indent = CLI_INDENT_TO_SPACES(context->indent_level);
550         int filler = CLI_LAST_TABSTOP - indent - 7;
551
552         if (!context->output_buffer) {
553                 return -1;
554         }
555         ast_str_append(&context->output_buffer, 0,
556                 "%*s:  <Aor%*.*s>  <MaxContact>\n",
557                 indent, "Aor", filler, filler, CLI_HEADER_FILLER);
558
559         if (context->recurse) {
560                 context->indent_level++;
561                 formatter_entry = ast_sip_lookup_cli_formatter("contact");
562                 if (formatter_entry && formatter_entry->print_header) {
563                         formatter_entry->print_header(NULL, context, 0);
564                 }
565                 context->indent_level--;
566         }
567         return 0;
568 }
569
570 static int cli_print_aor_body(void *obj, void *arg, int flags)
571 {
572         struct ast_sip_aor *aor = obj;
573         struct ast_sip_cli_context *context = arg;
574         struct ast_sip_cli_formatter_entry *formatter_entry;
575         int indent;
576         int flexwidth;
577
578         if (!context->output_buffer) {
579                 return -1;
580         }
581
582         context->current_aor = aor;
583
584         indent = CLI_INDENT_TO_SPACES(context->indent_level);
585         flexwidth = CLI_LAST_TABSTOP - indent - 12;
586
587         ast_str_append(&context->output_buffer, 0, "%*s:  %-*.*s %12d\n",
588                 indent,
589                 "Aor",
590                 flexwidth, flexwidth,
591                 ast_sorcery_object_get_id(aor), aor->max_contacts);
592
593         if (context->recurse) {
594                 context->indent_level++;
595
596                 formatter_entry = ast_sip_lookup_cli_formatter("contact");
597                 if (formatter_entry && formatter_entry->print_body && formatter_entry->iterator) {
598                         formatter_entry->iterator(aor, formatter_entry->print_body, context);
599                 }
600
601                 context->indent_level--;
602
603                 if (context->indent_level == 0) {
604                         ast_str_append(&context->output_buffer, 0, "\n");
605                 }
606         }
607
608         if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
609                 ast_str_append(&context->output_buffer, 0, "\n");
610                 ast_sip_cli_print_sorcery_objectset(aor, context, 0);
611         }
612
613         return 0;
614 }
615
616 static struct ast_sip_cli_formatter_entry cli_contact_formatter = {
617         .name = "contact",
618         .print_header = cli_print_contact_header,
619         .print_body = cli_print_contact_body,
620         .get_container = cli_get_contact_container,
621         .iterator = cli_contact_iterator,
622         .comparator = cli_contact_compare,
623 };
624
625 static struct ast_sip_cli_formatter_entry cli_aor_formatter = {
626         .name = "aor",
627         .print_header = cli_print_aor_header,
628         .print_body = cli_print_aor_body,
629         .get_container = cli_get_aor_container,
630         .iterator = cli_aor_iterator,
631         .comparator = ast_sorcery_object_id_compare,
632 };
633
634 static struct ast_cli_entry cli_commands[] = {
635         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Aors",
636                 .command = "pjsip list aors",
637                 .usage = "Usage: pjsip list aors\n"
638                                  "       List the configured PJSIP Aors\n"),
639         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aors",
640                 .command = "pjsip show aors",
641                 .usage = "Usage: pjsip show aors\n"
642                                  "       Show the configured PJSIP Aors\n"),
643         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Aor",
644                 .command = "pjsip show aor",
645                 .usage = "Usage: pjsip show aor <id>\n"
646                                  "       Show the configured PJSIP Aor\n"),
647
648         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Contacts",
649                 .command = "pjsip list contacts",
650                 .usage = "Usage: pjsip list contacts\n"
651                                  "       List the configured PJSIP contacts\n"),
652 };
653
654 /*! \brief Initialize sorcery with location support */
655 int ast_sip_initialize_sorcery_location(void)
656 {
657         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
658         ast_sorcery_apply_default(sorcery, "contact", "astdb", "registrar");
659         ast_sorcery_apply_default(sorcery, "aor", "config", "pjsip.conf,criteria=type=aor");
660
661         if (ast_sorcery_object_register(sorcery, "contact", contact_alloc, NULL, NULL) ||
662                 ast_sorcery_object_register(sorcery, "aor", aor_alloc, NULL, NULL)) {
663                 return -1;
664         }
665
666         ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0);
667         ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri));
668         ast_sorcery_object_field_register(sorcery, "contact", "path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, path));
669         ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, 0, 0);
670         ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T,
671                                           PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400);
672         ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy));
673         ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent));
674
675         ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0);
676         ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration));
677         ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration));
678         ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration));
679         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);
680         ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify));
681         ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts));
682         ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing));
683         ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, NULL, 0, 0);
684         ast_sorcery_object_field_register(sorcery, "aor", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, mailboxes));
685         ast_sorcery_object_field_register(sorcery, "aor", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_aor, outbound_proxy));
686         ast_sorcery_object_field_register(sorcery, "aor", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, support_path));
687
688         ast_sip_register_endpoint_formatter(&endpoint_aor_formatter);
689         ast_sip_register_cli_formatter(&cli_contact_formatter);
690         ast_sip_register_cli_formatter(&cli_aor_formatter);
691         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
692         return 0;
693 }
694
695 int ast_sip_destroy_sorcery_location(void)
696 {
697         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
698         ast_sip_unregister_cli_formatter(&cli_contact_formatter);
699         ast_sip_unregister_cli_formatter(&cli_aor_formatter);
700         return 0;
701 }
702