d399f0b47fe01c9084c755b26818252d9e1f9c68
[asterisk/asterisk.git] / res / res_pjsip / pjsip_configuration.c
1 /*
2  * sip_cli_commands.c
3  *
4  *  Created on: Jan 25, 2013
5  *      Author: mjordan
6  */
7
8 #include "asterisk.h"
9
10 #include <pjsip.h>
11 #include <pjsip_ua.h>
12
13 #include "asterisk/res_pjsip.h"
14 #include "include/res_pjsip_private.h"
15 #include "asterisk/res_pjsip_cli.h"
16 #include "asterisk/acl.h"
17 #include "asterisk/manager.h"
18 #include "asterisk/astobj2.h"
19 #include "asterisk/utils.h"
20 #include "asterisk/sorcery.h"
21 #include "asterisk/callerid.h"
22 #include "asterisk/test.h"
23 #include "asterisk/statsd.h"
24 #include "asterisk/pbx.h"
25
26 /*! \brief Number of buckets for persistent endpoint information */
27 #define PERSISTENT_BUCKETS 53
28
29 /*! \brief Persistent endpoint information */
30 struct sip_persistent_endpoint {
31         /*! \brief Asterisk endpoint itself */
32         struct ast_endpoint *endpoint;
33         /*! \brief AORs that we should react to */
34         char *aors;
35 };
36
37 /*! \brief Container for persistent endpoint information */
38 static struct ao2_container *persistent_endpoints;
39
40 static struct ast_sorcery *sip_sorcery;
41
42 /*! \brief Hashing function for persistent endpoint information */
43 static int persistent_endpoint_hash(const void *obj, const int flags)
44 {
45         const struct sip_persistent_endpoint *persistent = obj;
46         const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
47
48         return ast_str_hash(id);
49 }
50
51 /*! \brief Comparison function for persistent endpoint information */
52 static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
53 {
54         const struct sip_persistent_endpoint *persistent1 = obj;
55         const struct sip_persistent_endpoint *persistent2 = arg;
56         const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
57
58         return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
59 }
60
61 /*! \brief Internal function for changing the state of an endpoint */
62 static void endpoint_update_state(struct ast_endpoint *endpoint, enum ast_endpoint_state state)
63 {
64         struct ast_json *blob;
65         char *regcontext;
66
67         /* If there was no state change, don't publish anything. */
68         if (ast_endpoint_get_state(endpoint) == state) {
69                 return;
70         }
71
72         regcontext = ast_sip_get_regcontext();
73
74         if (state == AST_ENDPOINT_ONLINE) {
75                 ast_endpoint_set_state(endpoint, AST_ENDPOINT_ONLINE);
76                 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
77
78                 if (!ast_strlen_zero(regcontext)) {
79                         if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(endpoint), 1, NULL)) {
80                                 ast_add_extension(regcontext, 1, ast_endpoint_get_resource(endpoint), 1, NULL, NULL,
81                                         "Noop", ast_strdup(ast_endpoint_get_resource(endpoint)), ast_free_ptr, "SIP");
82                         }
83                 }
84
85                 ast_verb(2, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(endpoint));
86         } else {
87                 ast_endpoint_set_state(endpoint, AST_ENDPOINT_OFFLINE);
88                 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
89
90                 if (!ast_strlen_zero(regcontext)) {
91                         struct pbx_find_info q = { .stacklen = 0 };
92
93                         if (pbx_find_extension(NULL, NULL, &q, regcontext, ast_endpoint_get_resource(endpoint), 1, NULL, "", E_MATCH)) {
94                                 ast_context_remove_extension(regcontext, ast_endpoint_get_resource(endpoint), 1, NULL);
95                         }
96                 }
97
98                 ast_verb(2, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(endpoint));
99         }
100
101         ast_free(regcontext);
102
103         ast_endpoint_blob_publish(endpoint, ast_endpoint_state_type(), blob);
104         ast_json_unref(blob);
105         ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(endpoint));
106 }
107
108 static void endpoint_publish_contact_status(struct ast_endpoint *endpoint, struct ast_sip_contact_status *contact)
109 {
110         struct ast_json *blob;
111         char rtt[32];
112
113         snprintf(rtt, sizeof(rtt), "%" PRId64, contact->rtt);
114         blob = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
115                 "contact_status", ast_sip_get_contact_status_label(contact->status),
116                 "aor", contact->aor,
117                 "uri", contact->uri,
118                 "roundtrip_usec", rtt,
119                 "endpoint_name", ast_endpoint_get_resource(endpoint));
120         if (blob) {
121                 ast_endpoint_blob_publish(endpoint, ast_endpoint_contact_state_type(), blob);
122                 ast_json_unref(blob);
123         }
124 }
125
126 /*! \brief Callback function for publishing the status of an endpoint */
127 static int persistent_endpoint_publish_status(void *obj, void *arg, int flags)
128 {
129         struct sip_persistent_endpoint *persistent = obj;
130         struct ast_endpoint *endpoint = persistent->endpoint;
131         struct ast_sip_contact_status *status = arg;
132
133         /* If the status' aor isn't one of the endpoint's, we skip */
134         if (!strstr(persistent->aors, status->aor)) {
135                 return 0;
136         }
137
138         endpoint_publish_contact_status(endpoint, status);
139         return 0;
140 }
141
142 /*! \brief Callback function for changing the state of an endpoint */
143 static int persistent_endpoint_update_state(void *obj, void *arg, int flags)
144 {
145         struct sip_persistent_endpoint *persistent = obj;
146         struct ast_endpoint *endpoint = persistent->endpoint;
147         struct ast_sip_contact_status *status = arg;
148         struct ao2_container *contacts;
149         struct ao2_iterator iter;
150         struct ast_sip_contact *contact;
151         enum ast_endpoint_state state = AST_ENDPOINT_OFFLINE;
152
153         /* If the status' aor isn't one of the endpoint's, we skip */
154         if (!strstr(persistent->aors, status->aor)) {
155                 return 0;
156         }
157
158         endpoint_publish_contact_status(endpoint, status);
159
160         /* Find all the contacts for this endpoint.  If ANY are available,
161          * mark the endpoint as ONLINE.
162          */
163         contacts = ast_sip_location_retrieve_contacts_from_aor_list(persistent->aors);
164         if (contacts) {
165                 iter = ao2_iterator_init(contacts, 0);
166                 while (state == AST_ENDPOINT_OFFLINE && (contact = ao2_iterator_next(&iter))) {
167                         struct ast_sip_contact_status *contact_status;
168                         const char *contact_id = ast_sorcery_object_get_id(contact);
169
170                         contact_status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
171                                 CONTACT_STATUS, contact_id);
172
173                         if (contact_status && contact_status->status != UNAVAILABLE) {
174                                 state = AST_ENDPOINT_ONLINE;
175                         }
176                         ao2_cleanup(contact_status);
177                         ao2_ref(contact, -1);
178                 }
179                 ao2_iterator_destroy(&iter);
180                 ao2_ref(contacts, -1);
181         }
182
183         endpoint_update_state(endpoint, state);
184
185         return 0;
186 }
187
188 /*! \brief Function called when a contact is created */
189 static void persistent_endpoint_contact_created_observer(const void *object)
190 {
191         const struct ast_sip_contact *contact = object;
192         struct ast_sip_contact_status *contact_status;
193
194         contact_status = ast_sorcery_alloc(ast_sip_get_sorcery(), CONTACT_STATUS,
195                 ast_sorcery_object_get_id(contact));
196         if (!contact_status) {
197                 ast_log(LOG_ERROR, "Unable to create ast_sip_contact_status for contact %s/%s\n",
198                         contact->aor, contact->uri);
199                 return;
200         }
201         ast_string_field_set(contact_status, uri, contact->uri);
202
203         contact_status->status = CREATED;
204
205         ast_verb(2, "Contact %s/%s has been created\n", contact->aor, contact->uri);
206
207         ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, contact_status);
208         ao2_cleanup(contact_status);
209 }
210
211 /*! \brief Function called when a contact is deleted */
212 static void persistent_endpoint_contact_deleted_observer(const void *object)
213 {
214         const struct ast_sip_contact *contact = object;
215         struct ast_sip_contact_status *contact_status;
216
217         contact_status = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), CONTACT_STATUS, ast_sorcery_object_get_id(contact));
218         if (!contact_status) {
219                 ast_log(LOG_ERROR, "Unable to find ast_sip_contact_status for contact %s/%s\n",
220                         contact->aor, contact->uri);
221                 return;
222         }
223
224         ast_verb(2, "Contact %s/%s has been deleted\n", contact->aor, contact->uri);
225         ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE,
226                 "-1", 1.0, ast_sip_get_contact_status_label(contact_status->status));
227         ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE,
228                 "+1", 1.0, ast_sip_get_contact_status_label(REMOVED));
229
230         ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, contact_status);
231         ast_sorcery_delete(ast_sip_get_sorcery(), contact_status);
232         ao2_cleanup(contact_status);
233 }
234
235 /*! \brief Observer for contacts so state can be updated on respective endpoints */
236 static const struct ast_sorcery_observer state_contact_observer = {
237         .created = persistent_endpoint_contact_created_observer,
238         .deleted = persistent_endpoint_contact_deleted_observer,
239 };
240
241 /*! \brief Function called when a contact_status is updated */
242 static void persistent_endpoint_contact_status_observer(const void *object)
243 {
244         struct ast_sip_contact_status *contact_status = (struct ast_sip_contact_status *)object;
245
246         if (contact_status->refresh) {
247                 /* We are only re-publishing the contact status. */
248                 ao2_callback(persistent_endpoints, OBJ_NODATA,
249                         persistent_endpoint_publish_status, contact_status);
250                 return;
251         }
252
253         /* If rtt_start is set (this is the outgoing OPTIONS), ignore. */
254         if (contact_status->rtt_start.tv_sec > 0) {
255                 return;
256         }
257
258         if (contact_status->status != contact_status->last_status) {
259                 ast_verb(3, "Contact %s/%s is now %s.  RTT: %.3f msec\n",
260                         contact_status->aor, contact_status->uri,
261                         ast_sip_get_contact_status_label(contact_status->status),
262                         contact_status->rtt / 1000.0);
263
264                 ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE,
265                         "-1", 1.0, ast_sip_get_contact_status_label(contact_status->last_status));
266                 ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE,
267                         "+1", 1.0, ast_sip_get_contact_status_label(contact_status->status));
268
269                 ast_test_suite_event_notify("AOR_CONTACT_UPDATE",
270                         "Contact: %s\r\n"
271                         "Status: %s",
272                         ast_sorcery_object_get_id(contact_status),
273                         ast_sip_get_contact_status_label(contact_status->status));
274
275                 ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state,
276                         contact_status);
277         } else {
278                 ast_debug(3, "Contact %s/%s status didn't change: %s, RTT: %.3f msec\n",
279                         contact_status->aor, contact_status->uri,
280                         ast_sip_get_contact_status_label(contact_status->status),
281                         contact_status->rtt / 1000.0);
282         }
283
284         ast_statsd_log_full_va("PJSIP.contacts.%s.rtt", AST_STATSD_TIMER,
285                 contact_status->status != AVAILABLE ? -1 : contact_status->rtt / 1000,
286                 1.0,
287                 ast_sorcery_object_get_id(contact_status));
288 }
289
290 /*! \brief Observer for contacts so state can be updated on respective endpoints */
291 static const struct ast_sorcery_observer state_contact_status_observer = {
292         .updated = persistent_endpoint_contact_status_observer,
293 };
294
295 static void endpoint_deleted_observer(const void *object)
296 {
297         const struct ast_sip_endpoint *endpoint = object;
298
299         ao2_find(persistent_endpoints, ast_endpoint_get_resource(endpoint->persistent), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
300 }
301
302 static const struct ast_sorcery_observer endpoint_observers = {
303         .deleted = endpoint_deleted_observer,
304 };
305
306 static int endpoint_acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
307 {
308         struct ast_sip_endpoint *endpoint = obj;
309         int error = 0;
310         int ignore;
311
312         if (ast_strlen_zero(var->value)) return 0;
313
314         if (!strncmp(var->name, "contact_", 8)) {
315                 ast_append_acl(var->name + 8, var->value, &endpoint->contact_acl, &error, &ignore);
316         } else {
317                 ast_append_acl(var->name, var->value, &endpoint->acl, &error, &ignore);
318         }
319
320         return error;
321 }
322
323 static int acl_to_str(const void *obj, const intptr_t *args, char **buf)
324 {
325         const struct ast_sip_endpoint *endpoint = obj;
326         struct ast_acl_list *acl_list;
327         struct ast_acl *first_acl;
328
329         if (endpoint && !ast_acl_list_is_empty(acl_list=endpoint->acl)) {
330                 AST_LIST_LOCK(acl_list);
331                 first_acl = AST_LIST_FIRST(acl_list);
332                 if (ast_strlen_zero(first_acl->name)) {
333                         *buf = "deny/permit";
334                 } else {
335                         *buf = first_acl->name;
336                 }
337                 AST_LIST_UNLOCK(acl_list);
338         }
339
340         *buf = ast_strdup(*buf);
341         return 0;
342 }
343
344 static int contact_acl_to_str(const void *obj, const intptr_t *args, char **buf)
345 {
346         const struct ast_sip_endpoint *endpoint = obj;
347         struct ast_acl_list *acl_list;
348         struct ast_acl *first_acl;
349
350         if (endpoint && !ast_acl_list_is_empty(acl_list=endpoint->contact_acl)) {
351                 AST_LIST_LOCK(acl_list);
352                 first_acl = AST_LIST_FIRST(acl_list);
353                 if (ast_strlen_zero(first_acl->name)) {
354                         *buf = "deny/permit";
355                 } else {
356                         *buf = first_acl->name;
357                 }
358                 AST_LIST_UNLOCK(acl_list);
359         }
360
361         *buf = ast_strdup(*buf);
362         return 0;
363 }
364
365 static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
366 {
367         struct ast_sip_endpoint *endpoint = obj;
368
369         if (!strcasecmp(var->value, "rfc4733")) {
370                 endpoint->dtmf = AST_SIP_DTMF_RFC_4733;
371         } else if (!strcasecmp(var->value, "inband")) {
372                 endpoint->dtmf = AST_SIP_DTMF_INBAND;
373         } else if (!strcasecmp(var->value, "info")) {
374                 endpoint->dtmf = AST_SIP_DTMF_INFO;
375         } else if (!strcasecmp(var->value, "auto")) {
376                 endpoint->dtmf = AST_SIP_DTMF_AUTO;
377         } else if (!strcasecmp(var->value, "none")) {
378                 endpoint->dtmf = AST_SIP_DTMF_NONE;
379         } else {
380                 return -1;
381         }
382
383         return 0;
384 }
385
386 static int dtmf_to_str(const void *obj, const intptr_t *args, char **buf)
387 {
388         const struct ast_sip_endpoint *endpoint = obj;
389
390         switch (endpoint->dtmf) {
391         case AST_SIP_DTMF_RFC_4733 :
392                 *buf = "rfc4733"; break;
393         case AST_SIP_DTMF_INBAND :
394                 *buf = "inband"; break;
395         case AST_SIP_DTMF_INFO :
396                 *buf = "info"; break;
397        case AST_SIP_DTMF_AUTO :
398                 *buf = "auto"; break;
399         default:
400                 *buf = "none";
401         }
402
403         *buf = ast_strdup(*buf);
404         return 0;
405 }
406
407 static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
408 {
409         struct ast_sip_endpoint *endpoint = obj;
410
411         /* clear all */
412         endpoint->extensions.flags &= ~(PJSIP_INV_SUPPORT_100REL | PJSIP_INV_REQUIRE_100REL);
413
414         if (ast_true(var->value)) {
415                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
416         } else if (!strcasecmp(var->value, "required")) {
417                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
418         } else if (!ast_false(var->value)){
419                 return -1;
420         }
421
422         return 0;
423 }
424
425 static int prack_to_str(const void *obj, const intptr_t *args, char **buf)
426 {
427         const struct ast_sip_endpoint *endpoint = obj;
428
429         if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_100REL) {
430                 *buf = "required";
431         } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_100REL) {
432                 *buf = "yes";
433         } else {
434                 *buf = "no";
435         }
436
437         *buf = ast_strdup(*buf);
438         return 0;
439 }
440
441 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
442 {
443         struct ast_sip_endpoint *endpoint = obj;
444
445         /* clear all */
446         endpoint->extensions.flags &= ~(PJSIP_INV_SUPPORT_TIMER | PJSIP_INV_REQUIRE_TIMER
447                                         | PJSIP_INV_ALWAYS_USE_TIMER);
448
449         /* set only the specified flag and let pjsip normalize if needed */
450         if (ast_true(var->value)) {
451                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
452         } else if (!strcasecmp(var->value, "required")) {
453                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
454         } else if (!strcasecmp(var->value, "always") || !strcasecmp(var->value, "forced")) {
455                 endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
456         } else if (!ast_false(var->value)) {
457                 return -1;
458         }
459
460         return 0;
461 }
462
463 static int timers_to_str(const void *obj, const intptr_t *args, char **buf)
464 {
465         const struct ast_sip_endpoint *endpoint = obj;
466
467         if (endpoint->extensions.flags & PJSIP_INV_ALWAYS_USE_TIMER) {
468                 *buf = "always";
469         } else if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_TIMER) {
470                 *buf = "required";
471         } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_TIMER) {
472                 *buf = "yes";
473         } else {
474                 *buf = "no";
475         }
476
477         *buf = ast_strdup(*buf);
478         return 0;
479 }
480
481 void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
482 {
483         int i;
484         size_t size;
485
486         if (!auths) {
487                 return;
488         }
489
490         size = AST_VECTOR_SIZE(auths);
491
492         for (i = 0; i < size; ++i) {
493                 const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
494                 ast_free((char *) name);
495         }
496         AST_VECTOR_FREE(auths);
497 }
498
499 int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
500 {
501         char *auth_names = ast_strdupa(value);
502         char *val;
503
504         ast_assert(auths != NULL);
505
506         if (AST_VECTOR_SIZE(auths)) {
507                 ast_sip_auth_vector_destroy(auths);
508         }
509         if (AST_VECTOR_INIT(auths, 1)) {
510                 return -1;
511         }
512
513         while ((val = ast_strip(strsep(&auth_names, ",")))) {
514                 if (ast_strlen_zero(val)) {
515                         continue;
516                 }
517
518                 val = ast_strdup(val);
519                 if (!val) {
520                         goto failure;
521                 }
522                 if (AST_VECTOR_APPEND(auths, val)) {
523                         goto failure;
524                 }
525         }
526         return 0;
527
528 failure:
529         ast_sip_auth_vector_destroy(auths);
530         return -1;
531 }
532
533 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
534 {
535         struct ast_sip_endpoint *endpoint = obj;
536
537         return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
538 }
539
540 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
541 {
542         struct ast_sip_endpoint *endpoint = obj;
543
544         return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
545 }
546
547 int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
548 {
549         if (!auths || !AST_VECTOR_SIZE(auths)) {
550                 return 0;
551         }
552
553         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
554                 return -1;
555         }
556
557         /* I feel like accessing the vector's elem array directly is cheating...*/
558         ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
559         return 0;
560 }
561
562 static int inbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
563 {
564         const struct ast_sip_endpoint *endpoint = obj;
565         return ast_sip_auths_to_str(&endpoint->inbound_auths, buf);
566 }
567
568 static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
569 {
570         const struct ast_sip_endpoint *endpoint = obj;
571         return ast_sip_auths_to_str(&endpoint->outbound_auths, buf);
572 }
573
574 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
575 {
576         struct ast_sip_endpoint *endpoint = obj;
577         char *idents = ast_strdupa(var->value);
578         char *val;
579         enum ast_sip_endpoint_identifier_type method;
580
581         /*
582          * If there's already something in the vector when we get here,
583          * it's the default value so we need to clean it out.
584          */
585         if (AST_VECTOR_SIZE(&endpoint->ident_method_order)) {
586                 AST_VECTOR_RESET(&endpoint->ident_method_order, AST_VECTOR_ELEM_CLEANUP_NOOP);
587                 endpoint->ident_method = 0;
588         }
589
590         while ((val = ast_strip(strsep(&idents, ",")))) {
591                 if (ast_strlen_zero(val)) {
592                         continue;
593                 }
594
595                 if (!strcasecmp(val, "username")) {
596                         method = AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
597                 } else  if (!strcasecmp(val, "auth_username")) {
598                         method = AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME;
599                 } else {
600                         ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
601                                         val, ast_sorcery_object_get_id(endpoint));
602                         AST_VECTOR_RESET(&endpoint->ident_method_order, AST_VECTOR_ELEM_CLEANUP_NOOP);
603                         endpoint->ident_method = 0;
604                         return -1;
605                 }
606
607                 endpoint->ident_method |= method;
608                 AST_VECTOR_APPEND(&endpoint->ident_method_order, method);
609         }
610
611         return 0;
612 }
613
614 static int ident_to_str(const void *obj, const intptr_t *args, char **buf)
615 {
616         const struct ast_sip_endpoint *endpoint = obj;
617         int methods;
618         char *method;
619         int i;
620         int j = 0;
621
622         methods = AST_VECTOR_SIZE(&endpoint->ident_method_order);
623         if (!methods) {
624                 return 0;
625         }
626
627         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
628                 return -1;
629         }
630
631         for (i = 0; i < methods; i++) {
632                 switch (AST_VECTOR_GET(&endpoint->ident_method_order, i)) {
633                 case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME :
634                         method = "username";
635                         break;
636                 case AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME :
637                         method = "auth_username";
638                         break;
639                 default:
640                         continue;
641                 }
642                 j = sprintf(*buf + j, "%s%s", method, i < methods - 1 ? "," : "");
643         }
644
645         return 0;
646 }
647
648 static int redirect_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
649 {
650         struct ast_sip_endpoint *endpoint = obj;
651
652         if (!strcasecmp(var->value, "user")) {
653                 endpoint->redirect_method = AST_SIP_REDIRECT_USER;
654         } else if (!strcasecmp(var->value, "uri_core")) {
655                 endpoint->redirect_method = AST_SIP_REDIRECT_URI_CORE;
656         } else if (!strcasecmp(var->value, "uri_pjsip")) {
657                 endpoint->redirect_method = AST_SIP_REDIRECT_URI_PJSIP;
658         } else {
659                 ast_log(LOG_ERROR, "Unrecognized redirect method %s specified for endpoint %s\n",
660                         var->value, ast_sorcery_object_get_id(endpoint));
661                 return -1;
662         }
663
664         return 0;
665 }
666
667 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
668 {
669         struct ast_sip_endpoint *endpoint = obj;
670
671         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
672                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
673         } else if (!strcasecmp(var->value, "update")) {
674                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
675         } else {
676                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
677                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
678                 return -1;
679         }
680         return 0;
681 }
682
683 static const char *id_configuration_refresh_methods[] = {
684         [AST_SIP_SESSION_REFRESH_METHOD_INVITE] = "invite",
685         [AST_SIP_SESSION_REFRESH_METHOD_UPDATE] = "update"
686 };
687
688 static int direct_media_method_to_str(const void *obj, const intptr_t *args, char **buf)
689 {
690         const struct ast_sip_endpoint *endpoint = obj;
691         if (ARRAY_IN_BOUNDS(endpoint->id.refresh_method, id_configuration_refresh_methods)) {
692                 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
693         }
694         return 0;
695 }
696
697 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
698 {
699         struct ast_sip_endpoint *endpoint = obj;
700
701         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
702                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
703         } else if (!strcasecmp(var->value, "update")) {
704                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
705         } else {
706                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
707                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
708                 return -1;
709         }
710         return 0;
711 }
712
713 static int connected_line_method_to_str(const void *obj, const intptr_t *args, char **buf)
714 {
715         const struct ast_sip_endpoint *endpoint = obj;
716         *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
717         return 0;
718 }
719
720 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
721 {
722         struct ast_sip_endpoint *endpoint = obj;
723
724         if (!strcasecmp(var->value, "none")) {
725                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
726         } else if (!strcasecmp(var->value, "outgoing")) {
727                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
728         } else if (!strcasecmp(var->value, "incoming")) {
729                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
730         } else {
731                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
732                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
733                 return -1;
734         }
735
736         return 0;
737 }
738
739 static const char *direct_media_glare_mitigation_map[] = {
740         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE] = "none",
741         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING] = "outgoing",
742         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING] = "incoming"
743 };
744
745 static int direct_media_glare_mitigation_to_str(const void *obj, const intptr_t *args, char **buf)
746 {
747         const struct ast_sip_endpoint *endpoint = obj;
748         if (ARRAY_IN_BOUNDS(endpoint->media.direct_media.glare_mitigation, direct_media_glare_mitigation_map)) {
749                 *buf = ast_strdup(direct_media_glare_mitigation_map[endpoint->media.direct_media.glare_mitigation]);
750         }
751
752         return 0;
753 }
754
755 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
756 {
757         struct ast_sip_endpoint *endpoint = obj;
758         char cid_name[80] = { '\0' };
759         char cid_num[80] = { '\0' };
760
761         ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
762         if (!ast_strlen_zero(cid_name)) {
763                 endpoint->id.self.name.str = ast_strdup(cid_name);
764                 if (!endpoint->id.self.name.str) {
765                         return -1;
766                 }
767                 endpoint->id.self.name.valid = 1;
768         }
769         if (!ast_strlen_zero(cid_num)) {
770                 endpoint->id.self.number.str = ast_strdup(cid_num);
771                 if (!endpoint->id.self.number.str) {
772                         return -1;
773                 }
774                 endpoint->id.self.number.valid = 1;
775         }
776         return 0;
777 }
778
779 static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
780 {
781         const struct ast_sip_endpoint *endpoint = obj;
782         const char *name = S_COR(endpoint->id.self.name.valid,
783                                  endpoint->id.self.name.str, NULL);
784         const char *number = S_COR(endpoint->id.self.number.valid,
785                                    endpoint->id.self.number.str, NULL);
786
787         /* make sure size is at least 10 - that should cover the "<unknown>"
788            case as well as any additional formatting characters added in
789            the name and/or number case. */
790         int size = 10;
791         size += name ? strlen(name) : 0;
792         size += number ? strlen(number) : 0;
793
794         if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
795                 return -1;
796         }
797
798         ast_callerid_merge(*buf, size + 1, name, number, NULL);
799         return 0;
800 }
801
802 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
803 {
804         struct ast_sip_endpoint *endpoint = obj;
805         int callingpres = ast_parse_caller_presentation(var->value);
806         if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
807                 return -1;
808         }
809         endpoint->id.self.number.presentation = callingpres;
810         endpoint->id.self.name.presentation = callingpres;
811         return 0;
812 }
813
814 static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
815 {
816         const struct ast_sip_endpoint *endpoint = obj;
817         const char *presentation = ast_named_caller_presentation(
818                 endpoint->id.self.name.presentation);
819
820         *buf = ast_strdup(presentation);
821         return 0;
822 }
823
824 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
825 {
826         struct ast_sip_endpoint *endpoint = obj;
827         endpoint->id.self.tag = ast_strdup(var->value);
828         return endpoint->id.self.tag ? 0 : -1;
829 }
830
831 static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
832 {
833         const struct ast_sip_endpoint *endpoint = obj;
834         *buf = ast_strdup(endpoint->id.self.tag);
835         return 0;
836 }
837
838 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
839 {
840         struct ast_sip_endpoint *endpoint = obj;
841
842         if (!strcasecmp("no", var->value)) {
843                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
844         } else if (!strcasecmp("sdes", var->value)) {
845                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
846         } else if (!strcasecmp("dtls", var->value)) {
847                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
848                 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
849         } else {
850                 return -1;
851         }
852
853         return 0;
854 }
855
856 static const char *media_encryption_map[] = {
857         [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
858         [AST_SIP_MEDIA_ENCRYPT_NONE] = "no",
859         [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
860         [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
861 };
862
863 static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
864 {
865         const struct ast_sip_endpoint *endpoint = obj;
866         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
867                 *buf = ast_strdup(media_encryption_map[
868                                           endpoint->media.rtp.encryption]);
869         }
870         return 0;
871 }
872
873 static int group_handler(const struct aco_option *opt,
874                          struct ast_variable *var, void *obj)
875 {
876         struct ast_sip_endpoint *endpoint = obj;
877
878         if (!strncmp(var->name, "call_group", 10)) {
879                 endpoint->pickup.callgroup = ast_get_group(var->value);
880         } else if (!strncmp(var->name, "pickup_group", 12)) {
881                 endpoint->pickup.pickupgroup = ast_get_group(var->value);
882         } else {
883                 return -1;
884         }
885
886         return 0;
887 }
888
889 static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
890 {
891         const struct ast_sip_endpoint *endpoint = obj;
892
893         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
894                 return -1;
895         }
896
897         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
898         return 0;
899 }
900
901 static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
902 {
903         const struct ast_sip_endpoint *endpoint = obj;
904
905         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
906                 return -1;
907         }
908
909         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
910         return 0;
911 }
912
913 static int named_groups_handler(const struct aco_option *opt,
914                                 struct ast_variable *var, void *obj)
915 {
916         struct ast_sip_endpoint *endpoint = obj;
917
918         if (!strncmp(var->name, "named_call_group", 16)) {
919                 if (ast_strlen_zero(var->value)) {
920                         endpoint->pickup.named_callgroups =
921                                 ast_unref_namedgroups(endpoint->pickup.named_callgroups);
922                 } else if (!(endpoint->pickup.named_callgroups =
923                       ast_get_namedgroups(var->value))) {
924                         return -1;
925                 }
926         } else if (!strncmp(var->name, "named_pickup_group", 18)) {
927                 if (ast_strlen_zero(var->value)) {
928                         endpoint->pickup.named_pickupgroups =
929                                 ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
930                 } else if (!(endpoint->pickup.named_pickupgroups =
931                       ast_get_namedgroups(var->value))) {
932                         return -1;
933                 }
934         } else {
935                 return -1;
936         }
937
938         return 0;
939 }
940
941 static int named_callgroups_to_str(const void *obj, const intptr_t *args, char **buf)
942 {
943         const struct ast_sip_endpoint *endpoint = obj;
944         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
945
946         ast_print_namedgroups(&str, endpoint->pickup.named_callgroups);
947         *buf = ast_strdup(ast_str_buffer(str));
948         return 0;
949 }
950
951 static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
952 {
953         const struct ast_sip_endpoint *endpoint = obj;
954         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
955
956         ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
957         *buf = ast_strdup(ast_str_buffer(str));
958         return 0;
959 }
960
961 static int dtls_handler(const struct aco_option *opt,
962                          struct ast_variable *var, void *obj)
963 {
964         struct ast_sip_endpoint *endpoint = obj;
965         char *name = ast_strdupa(var->name);
966         char *front, *back, *buf = name;
967
968         /* strip out underscores in the name */
969         front = strtok_r(buf, "_", &back);
970         while (front) {
971                 int size = strlen(front);
972                 ast_copy_string(buf, front, size + 1);
973                 buf += size;
974                 front = strtok_r(NULL, "_", &back);
975         }
976
977         return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
978 }
979
980 static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
981 {
982         const struct ast_sip_endpoint *endpoint = obj;
983         *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.verify));
984         return 0;
985 }
986
987 static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
988 {
989         const struct ast_sip_endpoint *endpoint = obj;
990
991         return ast_asprintf(
992                 buf, "%u", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
993 }
994
995 static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
996 {
997         const struct ast_sip_endpoint *endpoint = obj;
998         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
999         return 0;
1000 }
1001
1002 static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
1003 {
1004         const struct ast_sip_endpoint *endpoint = obj;
1005         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
1006         return 0;
1007 }
1008
1009 static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
1010 {
1011         const struct ast_sip_endpoint *endpoint = obj;
1012         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
1013         return 0;
1014 }
1015
1016 static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
1017 {
1018         const struct ast_sip_endpoint *endpoint = obj;
1019         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
1020         return 0;
1021 }
1022
1023 static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
1024 {
1025         const struct ast_sip_endpoint *endpoint = obj;
1026         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
1027         return 0;
1028 }
1029
1030 static const char *ast_rtp_dtls_setup_map[] = {
1031         [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
1032         [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
1033         [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
1034         [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
1035 };
1036
1037 static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
1038 {
1039         const struct ast_sip_endpoint *endpoint = obj;
1040         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
1041                 *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
1042         }
1043         return 0;
1044 }
1045
1046 static const char *ast_rtp_dtls_fingerprint_map[] = {
1047         [AST_RTP_DTLS_HASH_SHA256] = "SHA-256",
1048         [AST_RTP_DTLS_HASH_SHA1] = "SHA-1",
1049 };
1050
1051 static int dtlsfingerprint_to_str(const void *obj, const intptr_t *args, char **buf)
1052 {
1053         const struct ast_sip_endpoint *endpoint = obj;
1054         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.hash, ast_rtp_dtls_fingerprint_map)) {
1055                 *buf = ast_strdup(ast_rtp_dtls_fingerprint_map[endpoint->media.rtp.dtls_cfg.hash]);
1056         }
1057         return 0;
1058 }
1059
1060 static int t38udptl_ec_handler(const struct aco_option *opt,
1061         struct ast_variable *var, void *obj)
1062 {
1063         struct ast_sip_endpoint *endpoint = obj;
1064
1065         if (!strcmp(var->value, "none")) {
1066                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
1067         } else if (!strcmp(var->value, "fec")) {
1068                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
1069         } else if (!strcmp(var->value, "redundancy")) {
1070                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
1071         } else {
1072                 return -1;
1073         }
1074
1075         return 0;
1076 }
1077
1078 static const char *ast_t38_ec_modes_map[] = {
1079         [UDPTL_ERROR_CORRECTION_NONE] = "none",
1080         [UDPTL_ERROR_CORRECTION_FEC] = "fec",
1081         [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
1082 };
1083
1084 static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
1085 {
1086         const struct ast_sip_endpoint *endpoint = obj;
1087         if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
1088                 *buf = ast_strdup(ast_t38_ec_modes_map[
1089                                           endpoint->media.t38.error_correction]);
1090         }
1091         return 0;
1092 }
1093
1094 static int tos_handler(const struct aco_option *opt,
1095         struct ast_variable *var, void *obj)
1096 {
1097         struct ast_sip_endpoint *endpoint = obj;
1098         unsigned int value;
1099
1100         if (ast_str2tos(var->value, &value)) {
1101                 ast_log(LOG_ERROR, "Error configuring endpoint '%s' - Could not "
1102                         "interpret '%s' value '%s'\n",
1103                         ast_sorcery_object_get_id(endpoint), var->name, var->value);
1104                 return -1;
1105         }
1106
1107         if (!strcmp(var->name, "tos_audio")) {
1108                 endpoint->media.tos_audio = value;
1109         } else if (!strcmp(var->name, "tos_video")) {
1110                 endpoint->media.tos_video = value;
1111         } else {
1112                 /* If we reach this point, someone called the tos_handler when they shouldn't have. */
1113                 ast_assert(0);
1114                 return -1;
1115         }
1116         return 0;
1117 }
1118
1119 static int tos_audio_to_str(const void *obj, const intptr_t *args, char **buf)
1120 {
1121         const struct ast_sip_endpoint *endpoint = obj;
1122
1123         if (ast_asprintf(buf, "%u", endpoint->media.tos_audio) == -1) {
1124                 return -1;
1125         }
1126         return 0;
1127 }
1128
1129 static int tos_video_to_str(const void *obj, const intptr_t *args, char **buf)
1130 {
1131         const struct ast_sip_endpoint *endpoint = obj;
1132
1133         if (ast_asprintf(buf, "%u", endpoint->media.tos_video) == -1) {
1134                 return -1;
1135         }
1136         return 0;
1137 }
1138
1139 static int set_var_handler(const struct aco_option *opt,
1140         struct ast_variable *var, void *obj)
1141 {
1142         struct ast_sip_endpoint *endpoint = obj;
1143         struct ast_variable *new_var;
1144         char *name;
1145         char *val;
1146
1147         if (ast_strlen_zero(var->value)) {
1148                 return 0;
1149         }
1150
1151         name = ast_strdupa(var->value);
1152         val = strchr(name, '=');
1153
1154         if (!val) {
1155                 return -1;
1156         }
1157
1158         *val++ = '\0';
1159
1160         if (!(new_var = ast_variable_new(name, val, ""))) {
1161                 return -1;
1162         }
1163
1164         ast_variable_list_append(&endpoint->channel_vars, new_var);
1165
1166         return 0;
1167 }
1168
1169 static int set_var_to_str(const void *obj, const intptr_t *args, char **buf)
1170 {
1171         struct ast_str *str = ast_str_create(MAX_OBJECT_FIELD);
1172         const struct ast_sip_endpoint *endpoint = obj;
1173         struct ast_variable *var;
1174
1175         for (var = endpoint->channel_vars; var; var = var->next) {
1176                 ast_str_append(&str, 0, "%s=%s,", var->name, var->value);
1177         }
1178
1179         *buf = ast_strdup(ast_str_truncate(str, -1));
1180         ast_free(str);
1181         return 0;
1182 }
1183
1184 static int set_var_to_vl(const void *obj, struct ast_variable **fields)
1185 {
1186         const struct ast_sip_endpoint *endpoint = obj;
1187         if (endpoint->channel_vars) {
1188                 *fields = ast_variables_dup(endpoint->channel_vars);
1189         }
1190         return 0;
1191 }
1192
1193 static int voicemail_extension_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1194 {
1195         struct ast_sip_endpoint *endpoint = obj;
1196
1197         endpoint->subscription.mwi.voicemail_extension = ast_strdup(var->value);
1198
1199         return endpoint->subscription.mwi.voicemail_extension ? 0 : -1;
1200 }
1201
1202 static int voicemail_extension_to_str(const void *obj, const intptr_t *args, char **buf)
1203 {
1204         const struct ast_sip_endpoint *endpoint = obj;
1205
1206         *buf = ast_strdup(endpoint->subscription.mwi.voicemail_extension);
1207
1208         return 0;
1209 }
1210
1211 static void *sip_nat_hook_alloc(const char *name)
1212 {
1213         return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
1214 }
1215
1216 /*! \brief Destructor function for persistent endpoint information */
1217 static void persistent_endpoint_destroy(void *obj)
1218 {
1219         struct sip_persistent_endpoint *persistent = obj;
1220
1221         ast_endpoint_shutdown(persistent->endpoint);
1222         ast_free(persistent->aors);
1223 }
1224
1225 int ast_sip_persistent_endpoint_update_state(const char *endpoint_name, enum ast_endpoint_state state)
1226 {
1227         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
1228         SCOPED_AO2LOCK(lock, persistent_endpoints);
1229
1230         if (!(persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_KEY | OBJ_NOLOCK))) {
1231                 return -1;
1232         }
1233
1234         endpoint_update_state(persistent->endpoint, state);
1235
1236         return 0;
1237 }
1238
1239 /*! \brief Internal function which finds (or creates) persistent endpoint information */
1240 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
1241 {
1242         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
1243         SCOPED_AO2LOCK(lock, persistent_endpoints);
1244
1245         if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
1246                 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
1247                         return NULL;
1248                 }
1249
1250                 if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
1251                         return NULL;
1252                 }
1253
1254                 persistent->aors = ast_strdup(endpoint->aors);
1255
1256                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
1257
1258                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
1259         }
1260
1261         ao2_ref(persistent->endpoint, +1);
1262         return persistent->endpoint;
1263 }
1264
1265 /*! \brief Callback function for when an object is finalized */
1266 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
1267 {
1268         struct ast_sip_endpoint *endpoint = obj;
1269
1270         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
1271                 return -1;
1272         }
1273
1274         if (endpoint->extensions.timer.min_se < 90) {
1275                 ast_log(LOG_ERROR, "Session timer minimum expires time must be 90 or greater on endpoint '%s'\n",
1276                         ast_sorcery_object_get_id(endpoint));
1277                 return -1;
1278         } else if (endpoint->extensions.timer.sess_expires < endpoint->extensions.timer.min_se) {
1279                 ast_log(LOG_ERROR, "Session timer expires must be greater than minimum session expires time on endpoint '%s'\n",
1280                         ast_sorcery_object_get_id(endpoint));
1281                 return -1;
1282         }
1283
1284         return 0;
1285 }
1286
1287 const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
1288 {
1289         char device[MAX_OBJECT_FIELD];
1290
1291         snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
1292         return ast_devstate2str(ast_device_state(device));
1293 }
1294
1295 struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
1296         const struct ast_sip_endpoint *endpoint)
1297 {
1298         return ast_endpoint_latest_snapshot(
1299                 ast_endpoint_get_tech(endpoint->persistent),
1300                 ast_endpoint_get_resource(endpoint->persistent));
1301 }
1302
1303 int ast_sip_for_each_channel_snapshot(
1304         const struct ast_endpoint_snapshot *endpoint_snapshot,
1305         ao2_callback_fn on_channel_snapshot, void *arg)
1306 {
1307         int num, num_channels = endpoint_snapshot->num_channels;
1308
1309         if (!on_channel_snapshot || !num_channels) {
1310                 return 0;
1311         }
1312
1313         for (num = 0; num < num_channels; ++num) {
1314                 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1315                 int res;
1316
1317                 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1318                 if (!snapshot) {
1319                         continue;
1320                 }
1321
1322                 res = on_channel_snapshot(snapshot, arg, 0);
1323                 if (res) {
1324                         return -1;
1325                 }
1326         }
1327         return 0;
1328 }
1329
1330 int ast_sip_for_each_channel(
1331         const struct ast_sip_endpoint *endpoint,
1332         ao2_callback_fn on_channel_snapshot, void *arg)
1333 {
1334         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1335         return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
1336 }
1337
1338 static int active_channels_to_str_cb(void *object, void *arg, int flags)
1339 {
1340         const struct ast_channel_snapshot *snapshot = object;
1341         struct ast_str **buf = arg;
1342         ast_str_append(buf, 0, "%s,", snapshot->name);
1343         return 0;
1344 }
1345
1346 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
1347                                    struct ast_str **str)
1348 {
1349
1350         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1351                  ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1352
1353         if (endpoint_snapshot) {
1354                 return;
1355         }
1356
1357         ast_sip_for_each_channel_snapshot(endpoint_snapshot,
1358                                           active_channels_to_str_cb, str);
1359         ast_str_truncate(*str, -1);
1360 }
1361
1362 #define AMI_DEFAULT_STR_SIZE 512
1363
1364 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1365 {
1366         struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1367
1368         if (!(buf)) {
1369                 astman_send_error_va(ami->s, ami->m, "Unable create event "
1370                                      "for %s\n", event);
1371                 return NULL;
1372         }
1373
1374         ast_str_set(&buf, 0, "Event: %s\r\n", event);
1375         if (!ast_strlen_zero(ami->action_id)) {
1376                 ast_str_append(&buf, 0, "ActionID: %s\r\n", ami->action_id);
1377         }
1378         return buf;
1379 }
1380
1381 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1382 {
1383         ast_str_append(buf, 0, "ObjectType: %s\r\n",
1384                        ast_sorcery_object_get_type(obj));
1385         ast_str_append(buf, 0, "ObjectName: %s\r\n",
1386                        ast_sorcery_object_get_id(obj));
1387 }
1388
1389 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1390 {
1391         RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
1392                          ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
1393         struct ast_variable *i;
1394
1395         if (!objset) {
1396                 return -1;
1397         }
1398
1399         sip_sorcery_object_ami_set_type_name(obj, buf);
1400
1401         for (i = objset; i; i = i->next) {
1402                 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1403                 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1404         }
1405
1406         return 0;
1407 }
1408
1409 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1410 {
1411         struct ast_sip_aor *aor = obj;
1412         struct ast_str **buf = arg;
1413
1414         ast_str_append(buf, 0, "Contacts: ");
1415         ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1416         ast_str_append(buf, 0, "\r\n");
1417
1418         return 0;
1419 }
1420
1421 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1422                                struct ast_str **buf)
1423 {
1424         if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1425                 return -1;
1426         }
1427
1428         ast_str_append(buf, 0, "DeviceState: %s\r\n",
1429                        ast_sip_get_device_state(endpoint));
1430
1431         ast_str_append(buf, 0, "ActiveChannels: ");
1432         active_channels_to_str(endpoint, buf);
1433         ast_str_append(buf, 0, "\r\n");
1434
1435         return 0;
1436 }
1437
1438 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1439                                struct ast_sip_ami *ami)
1440 {
1441         RAII_VAR(struct ast_str *, buf,
1442                  ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1443
1444         if (!buf) {
1445                 return -1;
1446         }
1447
1448         sip_endpoint_to_ami(endpoint, &buf);
1449         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1450         return 0;
1451 }
1452
1453 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1454 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1455
1456 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1457 {
1458         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"),
1459                 .count = 0, };
1460         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1461         const char *endpoint_name = astman_get_header(m, "Endpoint");
1462         int count = 0;
1463
1464         if (ast_strlen_zero(endpoint_name)) {
1465                 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1466                         AMI_SHOW_ENDPOINT);
1467                 return 0;
1468         }
1469
1470         if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1471                 endpoint_name += 6;
1472         }
1473
1474         if (!(endpoint = ast_sorcery_retrieve_by_id(
1475                       ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1476                 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1477                         endpoint_name);
1478                 return 0;
1479         }
1480
1481         astman_send_listack(s, m, "Following are Events for each object associated with the the Endpoint",
1482                 "start");
1483
1484         /* the endpoint detail needs to always come first so apply as such */
1485         if (format_ami_endpoint(endpoint, &ami) ||
1486             ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1487                 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1488                         endpoint_name);
1489         }
1490
1491         astman_send_list_complete_start(s, m, "EndpointDetailComplete", ami.count + 1);
1492         astman_send_list_complete_end(s);
1493
1494         return 0;
1495 }
1496
1497 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1498                                   struct ast_str **buf)
1499 {
1500         char *str = NULL;
1501         if (ast_sip_auths_to_str(auths, &str)) {
1502                 return -1;
1503         }
1504         ast_str_append(buf, 0, "%s", str ? str : "");
1505         ast_free(str);
1506         return 0;
1507 }
1508
1509 static int format_ami_endpoints(void *obj, void *arg, int flags)
1510 {
1511
1512         struct ast_sip_endpoint *endpoint = obj;
1513         struct ast_sip_ami *ami = arg;
1514         RAII_VAR(struct ast_str *, buf,
1515                  ast_sip_create_ami_event("EndpointList", ami), ast_free);
1516
1517         if (!buf) {
1518                 return CMP_STOP;
1519         }
1520
1521         sip_sorcery_object_ami_set_type_name(endpoint, &buf);
1522         ast_str_append(&buf, 0, "Transport: %s\r\n",
1523                        endpoint->transport);
1524         ast_str_append(&buf, 0, "Aor: %s\r\n",
1525                        endpoint->aors);
1526
1527         ast_str_append(&buf, 0, "Auths: ");
1528         format_str_append_auth(&endpoint->inbound_auths, &buf);
1529         ast_str_append(&buf, 0, "\r\n");
1530
1531         ast_str_append(&buf, 0, "OutboundAuths: ");
1532         format_str_append_auth(&endpoint->outbound_auths, &buf);
1533         ast_str_append(&buf, 0, "\r\n");
1534
1535         ast_sip_for_each_aor(endpoint->aors,
1536                              sip_endpoints_aors_ami, &buf);
1537
1538         ast_str_append(&buf, 0, "DeviceState: %s\r\n",
1539                        ast_sip_get_device_state(endpoint));
1540
1541         ast_str_append(&buf, 0, "ActiveChannels: ");
1542         active_channels_to_str(endpoint, &buf);
1543         ast_str_append(&buf, 0, "\r\n");
1544
1545         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1546         return 0;
1547 }
1548
1549 static int ami_show_endpoints(struct mansession *s, const struct message *m)
1550 {
1551         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
1552         RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
1553         int num;
1554
1555         endpoints = ast_sip_get_endpoints();
1556         if (!endpoints) {
1557                 astman_send_error(s, m, "Could not get endpoints\n");
1558                 return 0;
1559         }
1560
1561         if (!(num = ao2_container_count(endpoints))) {
1562                 astman_send_error(s, m, "No endpoints found\n");
1563                 return 0;
1564         }
1565
1566         astman_send_listack(s, m, "A listing of Endpoints follows, presented as EndpointList events",
1567                 "start");
1568
1569         ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
1570
1571         astman_send_list_complete_start(s, m, "EndpointListComplete", num);
1572         astman_send_list_complete_end(s);
1573         return 0;
1574 }
1575
1576 static struct ao2_container *cli_endpoint_get_container(const char *regex)
1577 {
1578         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
1579         struct ao2_container *s_container;
1580
1581         container = ast_sorcery_retrieve_by_regex(sip_sorcery, "endpoint", regex);
1582         if (!container) {
1583                 return NULL;
1584         }
1585
1586         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
1587                 (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
1588         if (!s_container) {
1589                 return NULL;
1590         }
1591
1592         if (ao2_container_dup(s_container, container, 0)) {
1593                 ao2_ref(s_container, -1);
1594                 return NULL;
1595         }
1596
1597         return s_container;
1598 }
1599
1600 static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
1601 {
1602         ao2_callback(obj, OBJ_NODATA, callback, args);
1603
1604         return 0;
1605 }
1606
1607 static void *cli_endpoint_retrieve_by_id(const char *id)
1608 {
1609         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
1610 }
1611
1612 static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
1613 {
1614         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1615
1616         formatter_entry = ast_sip_lookup_cli_formatter(type);
1617         if (formatter_entry) {
1618                 formatter_entry->print_header(NULL, context, 0);
1619         }
1620 }
1621
1622 static int cli_endpoint_print_header(void *obj, void *arg, int flags)
1623 {
1624         struct ast_sip_cli_context *context = arg;
1625
1626         ast_assert(context->output_buffer != NULL);
1627
1628         ast_str_append(&context->output_buffer, 0,
1629                         " Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>\n");
1630
1631         if (context->recurse) {
1632                 context->indent_level++;
1633                 cli_endpoint_print_child_header("auth", context);
1634                 cli_endpoint_print_child_header("aor", context);
1635                 cli_endpoint_print_child_header("transport", context);
1636                 cli_endpoint_print_child_header("identify", context);
1637                 cli_endpoint_print_child_header("channel", context);
1638                 context->indent_level--;
1639         }
1640
1641         return 0;
1642 }
1643
1644 static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
1645 {
1646         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1647
1648         formatter_entry = ast_sip_lookup_cli_formatter(type);
1649         if (formatter_entry) {
1650                 formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
1651         }
1652 }
1653
1654 static int cli_endpoint_print_body(void *obj, void *arg, int flags)
1655 {
1656         struct ast_sip_endpoint *endpoint = obj;
1657         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1658         struct ast_sip_cli_context *context = arg;
1659         const char *id = ast_sorcery_object_get_id(endpoint);
1660         char *print_name = NULL;
1661         int print_name_len;
1662         char *number = S_COR(endpoint->id.self.number.valid,
1663                 endpoint->id.self.number.str, NULL);
1664         int indent;
1665         int flexwidth;
1666
1667         ast_assert(context->output_buffer != NULL);
1668
1669         if (number) {
1670                 print_name_len = strlen(id) + strlen(number) + 2;
1671                 if (!(print_name = alloca(print_name_len))) {
1672                         return -1;
1673                 }
1674                 snprintf(print_name, print_name_len, "%s/%s", id, number);
1675         }
1676
1677         indent = CLI_INDENT_TO_SPACES(context->indent_level);
1678         flexwidth = CLI_LAST_TABSTOP - indent - 2;
1679
1680         ast_str_append(&context->output_buffer, 0, "%*s:  %-*.*s  %-12.12s  %d of %.0f\n",
1681                 indent, "Endpoint",
1682                 flexwidth, flexwidth, print_name ? print_name : id,
1683                 ast_sip_get_device_state(endpoint),
1684                 endpoint_snapshot->num_channels,
1685                 (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
1686                                                                                                                 INFINITY
1687                                                                                                                 );
1688
1689         if (context->recurse) {
1690                 context->indent_level++;
1691
1692                 context->auth_direction = "Out";
1693                 cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
1694                 context->auth_direction = "In";
1695                 cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
1696
1697                 cli_endpoint_print_child_body("aor", endpoint->aors, context);
1698                 cli_endpoint_print_child_body("transport", endpoint, context);
1699                 cli_endpoint_print_child_body("identify", endpoint, context);
1700                 cli_endpoint_print_child_body("channel", endpoint, context);
1701
1702                 context->indent_level--;
1703
1704                 if (context->indent_level == 0) {
1705                         ast_str_append(&context->output_buffer, 0, "\n");
1706                 }
1707         }
1708
1709         if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1710                 ast_str_append(&context->output_buffer, 0, "\n");
1711                 ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
1712         }
1713
1714         return 0;
1715 }
1716
1717 static struct ast_cli_entry cli_commands[] = {
1718         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
1719                 .command = "pjsip list endpoints",
1720                 .usage = "Usage: pjsip list endpoints [ like <pattern> ]\n"
1721                                 "       List the configured PJSIP endpoints\n"
1722                                 "       Optional regular expression pattern is used to filter the list.\n"),
1723         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
1724                 .command = "pjsip show endpoints",
1725                 .usage = "Usage: pjsip show endpoints [ like <pattern> ]\n"
1726                                 "       List(detailed) the configured PJSIP endpoints\n"
1727                                 "       Optional regular expression pattern is used to filter the list.\n"),
1728         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
1729                 .command = "pjsip show endpoint",
1730                 .usage = "Usage: pjsip show endpoint <id>\n"
1731                                  "       Show the configured PJSIP endpoint\n"),
1732 };
1733
1734 struct ast_sip_cli_formatter_entry *channel_formatter;
1735 struct ast_sip_cli_formatter_entry *endpoint_formatter;
1736
1737 static int on_load_endpoint(void *obj, void *arg, int flags)
1738 {
1739         return sip_endpoint_apply_handler(sip_sorcery, obj);
1740 }
1741
1742 static void load_all_endpoints(void)
1743 {
1744         struct ao2_container *endpoints;
1745
1746         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1747         if (endpoints) {
1748                 ao2_callback(endpoints, OBJ_NODATA, on_load_endpoint, NULL);
1749                 ao2_ref(endpoints, -1);
1750         }
1751 }
1752
1753 int ast_res_pjsip_initialize_configuration(void)
1754 {
1755         if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
1756             ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
1757                 return -1;
1758         }
1759
1760         if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
1761                 return -1;
1762         }
1763
1764         if (!(sip_sorcery = ast_sorcery_open())) {
1765                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
1766                 return -1;
1767         }
1768
1769         ast_sip_initialize_cli();
1770
1771         if (ast_sip_initialize_sorcery_auth()) {
1772                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
1773                 ast_sorcery_unref(sip_sorcery);
1774                 sip_sorcery = NULL;
1775                 return -1;
1776         }
1777
1778         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
1779         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
1780
1781         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
1782                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
1783                 ast_sorcery_unref(sip_sorcery);
1784                 sip_sorcery = NULL;
1785                 return -1;
1786         }
1787
1788         if (ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL)) {
1789                 ast_log(LOG_ERROR, "Failed to register nat_hook\n");
1790         }
1791
1792         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
1793         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
1794         ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
1795         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
1796         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
1797         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
1798         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
1799         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
1800         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
1801         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
1802         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
1803         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
1804         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
1805         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
1806         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
1807         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
1808         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_min_se", "90", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.min_se));
1809         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_sess_expires", "1800", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, extensions.timer.sess_expires));
1810         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
1811         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
1812         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
1813         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
1814         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "bind_rtp_to_media_address", "no", OPT_BOOL_T, 1, STRFLDSET(struct ast_sip_endpoint, media.bind_rtp_to_media_address));
1815         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, ident_to_str, NULL, 0, 0);
1816         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
1817         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, direct_media_method_to_str, NULL, 0, 0);
1818         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, connected_line_method_to_str, NULL, 0, 0);
1819         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, direct_media_glare_mitigation_to_str, NULL, 0, 0);
1820         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disable_direct_media_on_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.disable_on_nat));
1821         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
1822         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "allowed_not_screened", caller_id_privacy_handler, caller_id_privacy_to_str, NULL, 0, 0);
1823         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
1824         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
1825         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
1826         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
1827         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
1828         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rpid_immediate", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.rpid_immediate));
1829         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
1830         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
1831         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
1832         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
1833         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_subscribe_replaces_unsolicited", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.subscribe_replaces_unsolicited));
1834         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
1835         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
1836         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
1837         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_use_received_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_received_transport));
1838         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_keepalive", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.keepalive));
1839         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout));
1840         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout_hold", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout_hold));
1841         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
1842         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
1843         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
1844         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
1845         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
1846         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
1847         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "device_state_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
1848         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
1849         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
1850         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
1851         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
1852         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
1853         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
1854         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
1855         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
1856         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
1857         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
1858         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
1859         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "user_eq_phone", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, usereqphone));
1860         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_passthrough", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, moh_passthrough));
1861         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
1862         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
1863         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
1864         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
1865         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
1866         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
1867         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
1868         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
1869         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
1870         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
1871         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
1872         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
1873         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "no", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
1874         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "0", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
1875         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
1876         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
1877         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
1878         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
1879         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
1880         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
1881         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_fingerprint", "", dtls_handler, dtlsfingerprint_to_str, NULL, 0, 0);
1882         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "srtp_tag_32", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.srtp_tag_32));
1883         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_encryption_optimistic", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.encryption_optimistic));
1884         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "g726_non_standard", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.g726_non_standard));
1885         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, NULL, 0, 0);
1886         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
1887         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, message_context));
1888         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, accountcode));
1889         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1890         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1891         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "acl", "", endpoint_acl_handler, acl_to_str, NULL, 0, 0);
1892         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1893         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1894         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_acl", "", endpoint_acl_handler, contact_acl_to_str, NULL, 0, 0);
1895         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subscribe_context", "", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct ast_sip_endpoint, subscription.context));
1896
1897         if (ast_sip_initialize_sorcery_transport()) {
1898                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
1899                 ast_sorcery_unref(sip_sorcery);
1900                 sip_sorcery = NULL;
1901                 return -1;
1902         }
1903
1904         if (ast_sip_initialize_sorcery_location()) {
1905                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
1906                 ast_sorcery_unref(sip_sorcery);
1907                 sip_sorcery = NULL;
1908                 return -1;
1909         }
1910
1911         if (ast_sip_initialize_sorcery_qualify()) {
1912                 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
1913                 ast_sorcery_unref(sip_sorcery);
1914                 sip_sorcery = NULL;
1915                 return -1;
1916         }
1917
1918         ast_sorcery_observer_add(sip_sorcery, "endpoint", &endpoint_observers);
1919         ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
1920         ast_sorcery_observer_add(sip_sorcery, CONTACT_STATUS, &state_contact_status_observer);
1921
1922         if (ast_sip_initialize_sorcery_domain_alias()) {
1923                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
1924                 ast_sorcery_unref(sip_sorcery);
1925                 sip_sorcery = NULL;
1926                 return -1;
1927         }
1928
1929         if (ast_sip_initialize_sorcery_global()) {
1930                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
1931                 ast_sorcery_unref(sip_sorcery);
1932                 sip_sorcery = NULL;
1933                 return -1;
1934         }
1935
1936         endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1937         if (!endpoint_formatter) {
1938                 ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
1939                 ast_sorcery_unref(sip_sorcery);
1940                 sip_sorcery = NULL;
1941                 return -1;
1942         }
1943         endpoint_formatter->name = "endpoint";
1944         endpoint_formatter->print_header = cli_endpoint_print_header;
1945         endpoint_formatter->print_body = cli_endpoint_print_body;
1946         endpoint_formatter->get_container = cli_endpoint_get_container;
1947         endpoint_formatter->iterate = cli_endpoint_iterate;
1948         endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
1949         endpoint_formatter->get_id = ast_sorcery_object_get_id;
1950
1951         ast_sip_register_cli_formatter(endpoint_formatter);
1952         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1953
1954         ast_sorcery_load(sip_sorcery);
1955
1956         load_all_endpoints();
1957
1958         return 0;
1959 }
1960
1961 void ast_res_pjsip_destroy_configuration(void)
1962 {
1963         if (!sip_sorcery) {
1964                 return;
1965         }
1966
1967         ast_sorcery_observer_remove(sip_sorcery, CONTACT_STATUS, &state_contact_status_observer);
1968         ast_sorcery_observer_remove(sip_sorcery, "contact", &state_contact_observer);
1969         ast_sip_destroy_sorcery_global();
1970         ast_sip_destroy_sorcery_location();
1971         ast_sip_destroy_sorcery_auth();
1972         ast_sip_destroy_sorcery_transport();
1973         ast_sorcery_unref(sip_sorcery);
1974         sip_sorcery = NULL;
1975         ast_manager_unregister(AMI_SHOW_ENDPOINT);
1976         ast_manager_unregister(AMI_SHOW_ENDPOINTS);
1977         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1978         ast_sip_unregister_cli_formatter(endpoint_formatter);
1979         ast_sip_destroy_cli();
1980         ao2_cleanup(persistent_endpoints);
1981 }
1982
1983 int ast_res_pjsip_reload_configuration(void)
1984 {
1985         if (sip_sorcery) {
1986                 ast_sorcery_reload(sip_sorcery);
1987         }
1988         return 0;
1989 }
1990
1991 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
1992 {
1993         ast_string_field_free_memory(&subscription->mwi);
1994         ast_free(subscription->mwi.voicemail_extension);
1995 }
1996
1997 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
1998 {
1999         ast_string_field_free_memory(&info->recording);
2000 }
2001
2002 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
2003 {
2004         ast_string_field_free_memory(&media->rtp);
2005         ast_string_field_free_memory(media);
2006 }
2007
2008 static void endpoint_destructor(void* obj)
2009 {
2010         struct ast_sip_endpoint *endpoint = obj;
2011
2012         ast_string_field_free_memory(endpoint);
2013
2014         ao2_ref(endpoint->media.codecs, -1);
2015         subscription_configuration_destroy(&endpoint->subscription);
2016         info_configuration_destroy(&endpoint->info);
2017         media_configuration_destroy(&endpoint->media);
2018         ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
2019         ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
2020         ast_party_id_free(&endpoint->id.self);
2021         endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
2022         endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
2023         ao2_cleanup(endpoint->persistent);
2024         ast_variables_destroy(endpoint->channel_vars);
2025         AST_VECTOR_FREE(&endpoint->ident_method_order);
2026 }
2027
2028 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
2029 {
2030         return ast_string_field_init(&subscription->mwi, 64);
2031 }
2032
2033 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
2034 {
2035         return ast_string_field_init(&info->recording, 32);
2036 }
2037
2038 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
2039 {
2040         return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
2041 }
2042
2043 void *ast_sip_endpoint_alloc(const char *name)
2044 {
2045         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
2046         if (!endpoint) {
2047                 return NULL;
2048         }
2049         if (ast_string_field_init(endpoint, 64)) {
2050                 ao2_cleanup(endpoint);
2051                 return NULL;
2052         }
2053         if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2054                 ao2_cleanup(endpoint);
2055                 return NULL;
2056         }
2057         if (init_subscription_configuration(&endpoint->subscription)) {
2058                 ao2_cleanup(endpoint);
2059                 return NULL;
2060         }
2061         if (init_info_configuration(&endpoint->info)) {
2062                 ao2_cleanup(endpoint);
2063                 return NULL;
2064         }
2065         if (init_media_configuration(&endpoint->media)) {
2066                 ao2_cleanup(endpoint);
2067                 return NULL;
2068         }
2069         ast_party_id_init(&endpoint->id.self);
2070
2071         if (AST_VECTOR_INIT(&endpoint->ident_method_order, 1)) {
2072                 return NULL;
2073         }
2074
2075         return endpoint;
2076 }
2077
2078 struct ao2_container *ast_sip_get_endpoints(void)
2079 {
2080         struct ao2_container *endpoints;
2081
2082         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2083
2084         return endpoints;
2085 }
2086
2087 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
2088 {
2089         RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
2090         return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
2091                 sip_sorcery, "endpoint", name);
2092 }
2093
2094 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
2095 {
2096         int i;
2097
2098         for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
2099                 /* Using AST_VECTOR_GET is safe since the vector is immutable */
2100                 const char *name = AST_VECTOR_GET(auths, i);
2101                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
2102                 if (!out[i]) {
2103                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
2104                         return -1;
2105                 }
2106         }
2107
2108         return 0;
2109 }
2110
2111 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
2112 {
2113         int i;
2114         for (i = 0; i < num_auths; ++i) {
2115                 ao2_cleanup(auths[i]);
2116         }
2117 }
2118
2119 struct ast_sorcery *ast_sip_get_sorcery(void)
2120 {
2121         return sip_sorcery;
2122 }