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