d88024a80b074a23cfbf92b3e0f7c22cad6ee4de
[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_free(endpoint->id.self.name.str);
595         endpoint->id.self.name.str = NULL;
596         endpoint->id.self.name.valid = 0;
597         ast_free(endpoint->id.self.number.str);
598         endpoint->id.self.number.str = NULL;
599         endpoint->id.self.number.valid = 0;
600
601         ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
602         if (!ast_strlen_zero(cid_name)) {
603                 endpoint->id.self.name.str = ast_strdup(cid_name);
604                 if (!endpoint->id.self.name.str) {
605                         return -1;
606                 }
607                 endpoint->id.self.name.valid = 1;
608         }
609         if (!ast_strlen_zero(cid_num)) {
610                 endpoint->id.self.number.str = ast_strdup(cid_num);
611                 if (!endpoint->id.self.number.str) {
612                         return -1;
613                 }
614                 endpoint->id.self.number.valid = 1;
615         }
616         return 0;
617 }
618
619 static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
620 {
621         const struct ast_sip_endpoint *endpoint = obj;
622         const char *name = S_COR(endpoint->id.self.name.valid,
623                                  endpoint->id.self.name.str, NULL);
624         const char *number = S_COR(endpoint->id.self.number.valid,
625                                    endpoint->id.self.number.str, NULL);
626
627         /* make sure size is at least 10 - that should cover the "<unknown>"
628            case as well as any additional formatting characters added in
629            the name and/or number case. */
630         int size = 10;
631         size += name ? strlen(name) : 0;
632         size += number ? strlen(number) : 0;
633
634         if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
635                 return -1;
636         }
637
638         ast_callerid_merge(*buf, size + 1, name, number, NULL);
639         return 0;
640 }
641
642 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
643 {
644         struct ast_sip_endpoint *endpoint = obj;
645         int callingpres = ast_parse_caller_presentation(var->value);
646         if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
647                 return -1;
648         }
649         endpoint->id.self.number.presentation = callingpres;
650         endpoint->id.self.name.presentation = callingpres;
651         return 0;
652 }
653
654 static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
655 {
656         const struct ast_sip_endpoint *endpoint = obj;
657         const char *presentation = ast_named_caller_presentation(
658                 endpoint->id.self.name.presentation);
659
660         *buf = ast_strdup(presentation);
661         return 0;
662 }
663
664 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
665 {
666         struct ast_sip_endpoint *endpoint = obj;
667
668         ast_free(endpoint->id.self.tag);
669         endpoint->id.self.tag = ast_strdup(var->value);
670
671         return endpoint->id.self.tag ? 0 : -1;
672 }
673
674 static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
675 {
676         const struct ast_sip_endpoint *endpoint = obj;
677         *buf = ast_strdup(endpoint->id.self.tag);
678         return 0;
679 }
680
681 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
682 {
683         struct ast_sip_endpoint *endpoint = obj;
684
685         if (!strcasecmp("no", var->value)) {
686                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
687         } else if (!strcasecmp("sdes", var->value)) {
688                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
689         } else if (!strcasecmp("dtls", var->value)) {
690                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
691                 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
692         } else {
693                 return -1;
694         }
695
696         return 0;
697 }
698
699 static const char *media_encryption_map[] = {
700         [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
701         [AST_SIP_MEDIA_ENCRYPT_NONE] = "no",
702         [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
703         [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
704 };
705
706 static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
707 {
708         const struct ast_sip_endpoint *endpoint = obj;
709         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
710                 *buf = ast_strdup(media_encryption_map[
711                                           endpoint->media.rtp.encryption]);
712         }
713         return 0;
714 }
715
716 static int group_handler(const struct aco_option *opt,
717                          struct ast_variable *var, void *obj)
718 {
719         struct ast_sip_endpoint *endpoint = obj;
720
721         if (!strncmp(var->name, "call_group", 10)) {
722                 endpoint->pickup.callgroup = ast_get_group(var->value);
723         } else if (!strncmp(var->name, "pickup_group", 12)) {
724                 endpoint->pickup.pickupgroup = ast_get_group(var->value);
725         } else {
726                 return -1;
727         }
728
729         return 0;
730 }
731
732 static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
733 {
734         const struct ast_sip_endpoint *endpoint = obj;
735
736         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
737                 return -1;
738         }
739
740         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
741         return 0;
742 }
743
744 static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
745 {
746         const struct ast_sip_endpoint *endpoint = obj;
747
748         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
749                 return -1;
750         }
751
752         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
753         return 0;
754 }
755
756 static int named_groups_handler(const struct aco_option *opt,
757                                 struct ast_variable *var, void *obj)
758 {
759         struct ast_sip_endpoint *endpoint = obj;
760
761         if (!strncmp(var->name, "named_call_group", 16)) {
762                 if (ast_strlen_zero(var->value)) {
763                         endpoint->pickup.named_callgroups =
764                                 ast_unref_namedgroups(endpoint->pickup.named_callgroups);
765                 } else if (!(endpoint->pickup.named_callgroups =
766                       ast_get_namedgroups(var->value))) {
767                         return -1;
768                 }
769         } else if (!strncmp(var->name, "named_pickup_group", 18)) {
770                 if (ast_strlen_zero(var->value)) {
771                         endpoint->pickup.named_pickupgroups =
772                                 ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
773                 } else if (!(endpoint->pickup.named_pickupgroups =
774                       ast_get_namedgroups(var->value))) {
775                         return -1;
776                 }
777         } else {
778                 return -1;
779         }
780
781         return 0;
782 }
783
784 static int named_callgroups_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_callgroups);
790         *buf = ast_strdup(ast_str_buffer(str));
791         return 0;
792 }
793
794 static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
795 {
796         const struct ast_sip_endpoint *endpoint = obj;
797         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
798
799         ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
800         *buf = ast_strdup(ast_str_buffer(str));
801         return 0;
802 }
803
804 static int dtls_handler(const struct aco_option *opt,
805                          struct ast_variable *var, void *obj)
806 {
807         struct ast_sip_endpoint *endpoint = obj;
808         char *name = ast_strdupa(var->name);
809         char *front = NULL;
810         char *back = NULL;
811         char *buf = name;
812
813         /* strip out underscores in the name */
814         front = strtok_r(buf, "_", &back);
815         while (front) {
816                 int size = strlen(front);
817                 ast_copy_string(buf, front, size + 1);
818                 buf += size;
819                 front = strtok_r(NULL, "_", &back);
820         }
821
822         return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
823 }
824
825 static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
826 {
827         const struct ast_sip_endpoint *endpoint = obj;
828         *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.verify));
829         return 0;
830 }
831
832 static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
833 {
834         const struct ast_sip_endpoint *endpoint = obj;
835
836         return ast_asprintf(
837                 buf, "%u", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
838 }
839
840 static int dtlsautogeneratecert_to_str(const void *obj, const intptr_t *args, char **buf)
841 {
842         const struct ast_sip_endpoint *endpoint = obj;
843         *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.ephemeral_cert));
844         return 0;
845 }
846
847 static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
848 {
849         const struct ast_sip_endpoint *endpoint = obj;
850         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
851         return 0;
852 }
853
854 static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
855 {
856         const struct ast_sip_endpoint *endpoint = obj;
857         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
858         return 0;
859 }
860
861 static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
862 {
863         const struct ast_sip_endpoint *endpoint = obj;
864         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
865         return 0;
866 }
867
868 static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
869 {
870         const struct ast_sip_endpoint *endpoint = obj;
871         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
872         return 0;
873 }
874
875 static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
876 {
877         const struct ast_sip_endpoint *endpoint = obj;
878         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
879         return 0;
880 }
881
882 static const char *ast_rtp_dtls_setup_map[] = {
883         [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
884         [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
885         [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
886         [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
887 };
888
889 static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
890 {
891         const struct ast_sip_endpoint *endpoint = obj;
892         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
893                 *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
894         }
895         return 0;
896 }
897
898 static const char *ast_rtp_dtls_fingerprint_map[] = {
899         [AST_RTP_DTLS_HASH_SHA256] = "SHA-256",
900         [AST_RTP_DTLS_HASH_SHA1] = "SHA-1",
901 };
902
903 static int dtlsfingerprint_to_str(const void *obj, const intptr_t *args, char **buf)
904 {
905         const struct ast_sip_endpoint *endpoint = obj;
906         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.hash, ast_rtp_dtls_fingerprint_map)) {
907                 *buf = ast_strdup(ast_rtp_dtls_fingerprint_map[endpoint->media.rtp.dtls_cfg.hash]);
908         }
909         return 0;
910 }
911
912 static int t38udptl_ec_handler(const struct aco_option *opt,
913         struct ast_variable *var, void *obj)
914 {
915         struct ast_sip_endpoint *endpoint = obj;
916
917         if (!strcmp(var->value, "none")) {
918                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
919         } else if (!strcmp(var->value, "fec")) {
920                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
921         } else if (!strcmp(var->value, "redundancy")) {
922                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
923         } else {
924                 return -1;
925         }
926
927         return 0;
928 }
929
930 static const char *ast_t38_ec_modes_map[] = {
931         [UDPTL_ERROR_CORRECTION_NONE] = "none",
932         [UDPTL_ERROR_CORRECTION_FEC] = "fec",
933         [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
934 };
935
936 static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
937 {
938         const struct ast_sip_endpoint *endpoint = obj;
939         if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
940                 *buf = ast_strdup(ast_t38_ec_modes_map[
941                                           endpoint->media.t38.error_correction]);
942         }
943         return 0;
944 }
945
946 static int tos_handler(const struct aco_option *opt,
947         struct ast_variable *var, void *obj)
948 {
949         struct ast_sip_endpoint *endpoint = obj;
950         unsigned int value;
951
952         if (ast_str2tos(var->value, &value)) {
953                 ast_log(LOG_ERROR, "Error configuring endpoint '%s' - Could not "
954                         "interpret '%s' value '%s'\n",
955                         ast_sorcery_object_get_id(endpoint), var->name, var->value);
956                 return -1;
957         }
958
959         if (!strcmp(var->name, "tos_audio")) {
960                 endpoint->media.tos_audio = value;
961         } else if (!strcmp(var->name, "tos_video")) {
962                 endpoint->media.tos_video = value;
963         } else {
964                 /* If we reach this point, someone called the tos_handler when they shouldn't have. */
965                 ast_assert(0);
966                 return -1;
967         }
968         return 0;
969 }
970
971 static int tos_audio_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_audio) == -1) {
976                 return -1;
977         }
978         return 0;
979 }
980
981 static int tos_video_to_str(const void *obj, const intptr_t *args, char **buf)
982 {
983         const struct ast_sip_endpoint *endpoint = obj;
984
985         if (ast_asprintf(buf, "%u", endpoint->media.tos_video) == -1) {
986                 return -1;
987         }
988         return 0;
989 }
990
991 static int from_user_handler(const struct aco_option *opt,
992         struct ast_variable *var, void *obj)
993 {
994         struct ast_sip_endpoint *endpoint = obj;
995         /* Valid non-alphanumeric characters for URI */
996         char *valid_uri_marks = "-._~%!$&'()*+,;=:";
997         const char *val;
998
999         for (val = var->value; *val; val++) {
1000                 if (!isalpha(*val) && !isdigit(*val) && !strchr(valid_uri_marks, *val)) {
1001                         ast_log(LOG_ERROR, "Error configuring endpoint '%s' - '%s' field "
1002                         "contains invalid character '%c'\n",
1003                         ast_sorcery_object_get_id(endpoint), var->name, *val);
1004                         return -1;
1005                 }
1006         }
1007
1008         ast_string_field_set(endpoint, fromuser, var->value);
1009
1010         return 0;
1011 }
1012
1013 static int from_user_to_str(const void *obj, const intptr_t *args, char **buf)
1014 {
1015         const struct ast_sip_endpoint *endpoint = obj;
1016
1017         *buf = ast_strdup(endpoint->fromuser);
1018
1019         return 0;
1020 }
1021
1022 static int set_var_handler(const struct aco_option *opt,
1023         struct ast_variable *var, void *obj)
1024 {
1025         struct ast_sip_endpoint *endpoint = obj;
1026         struct ast_variable *new_var;
1027         char *name;
1028         char *val;
1029
1030         if (ast_strlen_zero(var->value)) {
1031                 return 0;
1032         }
1033
1034         name = ast_strdupa(var->value);
1035         val = strchr(name, '=');
1036
1037         if (!val) {
1038                 return -1;
1039         }
1040
1041         *val++ = '\0';
1042
1043         if (!(new_var = ast_variable_new(name, val, ""))) {
1044                 return -1;
1045         }
1046
1047         ast_variable_list_append(&endpoint->channel_vars, new_var);
1048
1049         return 0;
1050 }
1051
1052 static int set_var_to_str(const void *obj, const intptr_t *args, char **buf)
1053 {
1054         struct ast_str *str = ast_str_create(MAX_OBJECT_FIELD);
1055         const struct ast_sip_endpoint *endpoint = obj;
1056         struct ast_variable *var;
1057
1058         for (var = endpoint->channel_vars; var; var = var->next) {
1059                 ast_str_append(&str, 0, "%s=%s,", var->name, var->value);
1060         }
1061
1062         *buf = ast_strdup(ast_str_truncate(str, -1));
1063         ast_free(str);
1064         return 0;
1065 }
1066
1067 static int set_var_to_vl(const void *obj, struct ast_variable **fields)
1068 {
1069         const struct ast_sip_endpoint *endpoint = obj;
1070         if (endpoint->channel_vars) {
1071                 *fields = ast_variables_dup(endpoint->channel_vars);
1072         }
1073         return 0;
1074 }
1075
1076 static int voicemail_extension_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1077 {
1078         struct ast_sip_endpoint *endpoint = obj;
1079
1080         ast_free(endpoint->subscription.mwi.voicemail_extension);
1081         endpoint->subscription.mwi.voicemail_extension = ast_strdup(var->value);
1082
1083         return endpoint->subscription.mwi.voicemail_extension ? 0 : -1;
1084 }
1085
1086 static int voicemail_extension_to_str(const void *obj, const intptr_t *args, char **buf)
1087 {
1088         const struct ast_sip_endpoint *endpoint = obj;
1089
1090         *buf = ast_strdup(endpoint->subscription.mwi.voicemail_extension);
1091
1092         return 0;
1093 }
1094
1095 static int contact_user_handler(const struct aco_option *opt,
1096         struct ast_variable *var, void *obj)
1097 {
1098         struct ast_sip_endpoint *endpoint = obj;
1099
1100         ast_free(endpoint->contact_user);
1101         endpoint->contact_user = ast_strdup(var->value);
1102
1103         return endpoint->contact_user ? 0 : -1;
1104 }
1105
1106 static int contact_user_to_str(const void *obj, const intptr_t *args, char **buf)
1107 {
1108         const struct ast_sip_endpoint *endpoint = obj;
1109
1110         *buf = ast_strdup(endpoint->contact_user);
1111         if (!(*buf)) {
1112                 return -1;
1113         }
1114
1115         return 0;
1116 }
1117
1118 static void *sip_nat_hook_alloc(const char *name)
1119 {
1120         return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
1121 }
1122
1123 /*! \brief Destructor function for persistent endpoint information */
1124 static void persistent_endpoint_destroy(void *obj)
1125 {
1126         struct sip_persistent_endpoint *persistent = obj;
1127
1128         ast_endpoint_shutdown(persistent->endpoint);
1129 }
1130
1131 int ast_sip_persistent_endpoint_update_state(const char *endpoint_name, enum ast_endpoint_state state)
1132 {
1133         struct sip_persistent_endpoint *persistent;
1134         struct ast_json *blob;
1135         char *regcontext;
1136
1137         persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1138         if (!persistent) {
1139                 return -1;
1140         }
1141
1142         /* If there was no state change, don't publish anything. */
1143         if (ast_endpoint_get_state(persistent->endpoint) == state) {
1144                 ao2_ref(persistent, -1);
1145                 return 0;
1146         }
1147
1148         regcontext = ast_sip_get_regcontext();
1149
1150         if (state == AST_ENDPOINT_ONLINE) {
1151                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
1152                 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
1153
1154                 if (!ast_strlen_zero(regcontext)) {
1155                         if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL)) {
1156                                 ast_add_extension(regcontext, 1, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, NULL,
1157                                         "Noop", ast_strdup(ast_endpoint_get_resource(persistent->endpoint)), ast_free_ptr, "SIP");
1158                         }
1159                 }
1160
1161                 ast_verb(2, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(persistent->endpoint));
1162         } else {
1163                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1164                 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
1165
1166                 if (!ast_strlen_zero(regcontext)) {
1167                         struct pbx_find_info q = { .stacklen = 0 };
1168
1169                         if (pbx_find_extension(NULL, NULL, &q, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, "", E_MATCH)) {
1170                                 ast_context_remove_extension(regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL);
1171                         }
1172                 }
1173
1174                 ast_verb(2, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(persistent->endpoint));
1175         }
1176
1177         ast_free(regcontext);
1178
1179         ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
1180         ast_json_unref(blob);
1181         ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
1182
1183         ao2_ref(persistent, -1);
1184
1185         return 0;
1186 }
1187
1188 void ast_sip_persistent_endpoint_publish_contact_state(const char *endpoint_name, const struct ast_sip_contact_status *contact_status)
1189 {
1190         struct sip_persistent_endpoint *persistent;
1191         struct ast_json *blob;
1192         char rtt[32];
1193
1194         persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1195         if (!persistent) {
1196                 return;
1197         }
1198
1199         snprintf(rtt, sizeof(rtt), "%" PRId64, contact_status->rtt);
1200         blob = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
1201                 "contact_status", ast_sip_get_contact_status_label(contact_status->status),
1202                 "aor", contact_status->aor,
1203                 "uri", contact_status->uri,
1204                 "roundtrip_usec", rtt,
1205                 "endpoint_name", ast_endpoint_get_resource(persistent->endpoint));
1206         if (blob) {
1207                 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_contact_state_type(), blob);
1208                 ast_json_unref(blob);
1209         }
1210
1211         ao2_ref(persistent, -1);
1212 }
1213
1214 /*! \brief Internal function which finds (or creates) persistent endpoint information */
1215 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
1216 {
1217         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
1218         SCOPED_AO2LOCK(lock, persistent_endpoints);
1219
1220         persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint),
1221                 OBJ_SEARCH_KEY | OBJ_NOLOCK);
1222         if (!persistent) {
1223                 persistent = ao2_alloc_options(sizeof(*persistent), persistent_endpoint_destroy,
1224                         AO2_ALLOC_OPT_LOCK_NOLOCK);
1225                 if (!persistent) {
1226                         return NULL;
1227                 }
1228
1229                 persistent->endpoint = ast_endpoint_create("PJSIP",
1230                         ast_sorcery_object_get_id(endpoint));
1231                 if (!persistent->endpoint) {
1232                         return NULL;
1233                 }
1234
1235                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1236
1237                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
1238         }
1239
1240         ao2_ref(persistent->endpoint, +1);
1241         return persistent->endpoint;
1242 }
1243
1244 /*! \brief Callback function for when an object is finalized */
1245 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
1246 {
1247         struct ast_sip_endpoint *endpoint = obj;
1248
1249         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
1250                 return -1;
1251         }
1252
1253         if (endpoint->extensions.timer.min_se < 90) {
1254                 ast_log(LOG_ERROR, "Session timer minimum expires time must be 90 or greater on endpoint '%s'\n",
1255                         ast_sorcery_object_get_id(endpoint));
1256                 return -1;
1257         } else if (endpoint->extensions.timer.sess_expires < endpoint->extensions.timer.min_se) {
1258                 ast_log(LOG_ERROR, "Session timer expires must be greater than minimum session expires time on endpoint '%s'\n",
1259                         ast_sorcery_object_get_id(endpoint));
1260                 return -1;
1261         }
1262
1263         if (ast_rtp_dtls_cfg_validate(&endpoint->media.rtp.dtls_cfg)) {
1264                 return -1;
1265         }
1266
1267         endpoint->media.topology = ast_stream_topology_create_from_format_cap(endpoint->media.codecs);
1268         if (!endpoint->media.topology) {
1269                 return -1;
1270         }
1271
1272         endpoint->media.rtcp_mux |= endpoint->media.bundle;
1273
1274         /*
1275          * If webrtc has been enabled then enable those attributes, and default
1276          * some, that are needed in order for webrtc to work.
1277          */
1278         endpoint->media.bundle |= endpoint->media.webrtc;
1279         endpoint->media.rtcp_mux |= endpoint->media.webrtc;
1280         endpoint->media.rtp.use_avpf |= endpoint->media.webrtc;
1281         endpoint->media.rtp.ice_support |= endpoint->media.webrtc;
1282         endpoint->media.rtp.use_received_transport |= endpoint->media.webrtc;
1283
1284         if (endpoint->media.webrtc) {
1285                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
1286                 endpoint->media.rtp.dtls_cfg.enabled = 1;
1287                 endpoint->media.rtp.dtls_cfg.default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
1288                 endpoint->media.rtp.dtls_cfg.verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
1289
1290                 if (ast_strlen_zero(endpoint->media.rtp.dtls_cfg.certfile)) {
1291                         /* If no certificate has been specified, try to automatically create one */
1292                         endpoint->media.rtp.dtls_cfg.ephemeral_cert = 1;
1293                 }
1294         }
1295
1296         return 0;
1297 }
1298
1299 const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
1300 {
1301         char device[MAX_OBJECT_FIELD];
1302
1303         snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
1304         return ast_devstate2str(ast_device_state(device));
1305 }
1306
1307 struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
1308         const struct ast_sip_endpoint *endpoint)
1309 {
1310         return ast_endpoint_latest_snapshot(
1311                 ast_endpoint_get_tech(endpoint->persistent),
1312                 ast_endpoint_get_resource(endpoint->persistent));
1313 }
1314
1315 int ast_sip_for_each_channel_snapshot(
1316         const struct ast_endpoint_snapshot *endpoint_snapshot,
1317         ao2_callback_fn on_channel_snapshot, void *arg)
1318 {
1319         int num, num_channels = endpoint_snapshot->num_channels;
1320
1321         if (!on_channel_snapshot || !num_channels) {
1322                 return 0;
1323         }
1324
1325         for (num = 0; num < num_channels; ++num) {
1326                 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1327                 int res;
1328
1329                 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1330                 if (!snapshot) {
1331                         continue;
1332                 }
1333
1334                 res = on_channel_snapshot(snapshot, arg, 0);
1335                 if (res) {
1336                         return -1;
1337                 }
1338         }
1339         return 0;
1340 }
1341
1342 int ast_sip_for_each_channel(
1343         const struct ast_sip_endpoint *endpoint,
1344         ao2_callback_fn on_channel_snapshot, void *arg)
1345 {
1346         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1347         return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
1348 }
1349
1350 static int active_channels_to_str_cb(void *object, void *arg, int flags)
1351 {
1352         const struct ast_channel_snapshot *snapshot = object;
1353         struct ast_str **buf = arg;
1354         ast_str_append(buf, 0, "%s,", snapshot->base->name);
1355         return 0;
1356 }
1357
1358 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
1359                                    struct ast_str **str)
1360 {
1361
1362         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1363                  ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1364
1365         if (endpoint_snapshot) {
1366                 return;
1367         }
1368
1369         ast_sip_for_each_channel_snapshot(endpoint_snapshot,
1370                                           active_channels_to_str_cb, str);
1371         ast_str_truncate(*str, -1);
1372 }
1373
1374 #define AMI_DEFAULT_STR_SIZE 512
1375
1376 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1377 {
1378         struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1379
1380         if (!(buf)) {
1381                 astman_send_error_va(ami->s, ami->m, "Unable create event "
1382                                      "for %s\n", event);
1383                 return NULL;
1384         }
1385
1386         ast_str_set(&buf, 0, "Event: %s\r\n", event);
1387         if (!ast_strlen_zero(ami->action_id)) {
1388                 ast_str_append(&buf, 0, "ActionID: %s\r\n", ami->action_id);
1389         }
1390         return buf;
1391 }
1392
1393 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1394 {
1395         ast_str_append(buf, 0, "ObjectType: %s\r\n",
1396                        ast_sorcery_object_get_type(obj));
1397         ast_str_append(buf, 0, "ObjectName: %s\r\n",
1398                        ast_sorcery_object_get_id(obj));
1399 }
1400
1401 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1402 {
1403         RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
1404                          ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
1405         struct ast_variable *i;
1406
1407         if (!objset) {
1408                 return -1;
1409         }
1410
1411         sip_sorcery_object_ami_set_type_name(obj, buf);
1412
1413         for (i = objset; i; i = i->next) {
1414                 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1415                 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1416         }
1417
1418         return 0;
1419 }
1420
1421 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1422 {
1423         struct ast_sip_aor *aor = obj;
1424         struct ast_str **buf = arg;
1425
1426         ast_str_append(buf, 0, "Contacts: ");
1427         ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1428         ast_str_append(buf, 0, "\r\n");
1429
1430         return 0;
1431 }
1432
1433 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1434                                struct ast_str **buf)
1435 {
1436         if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1437                 return -1;
1438         }
1439
1440         ast_str_append(buf, 0, "DeviceState: %s\r\n",
1441                        ast_sip_get_device_state(endpoint));
1442
1443         ast_str_append(buf, 0, "ActiveChannels: ");
1444         active_channels_to_str(endpoint, buf);
1445         ast_str_append(buf, 0, "\r\n");
1446
1447         return 0;
1448 }
1449
1450 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1451                                struct ast_sip_ami *ami)
1452 {
1453         RAII_VAR(struct ast_str *, buf,
1454                  ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1455
1456         if (!buf) {
1457                 return -1;
1458         }
1459
1460         sip_endpoint_to_ami(endpoint, &buf);
1461         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1462         return 0;
1463 }
1464
1465 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1466 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1467
1468 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1469 {
1470         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"),
1471                 .count = 0, };
1472         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1473         const char *endpoint_name = astman_get_header(m, "Endpoint");
1474         int count = 0;
1475
1476         if (ast_strlen_zero(endpoint_name)) {
1477                 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1478                         AMI_SHOW_ENDPOINT);
1479                 return 0;
1480         }
1481
1482         if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1483                 endpoint_name += 6;
1484         }
1485
1486         if (!(endpoint = ast_sorcery_retrieve_by_id(
1487                       ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1488                 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1489                         endpoint_name);
1490                 return 0;
1491         }
1492
1493         astman_send_listack(s, m, "Following are Events for each object associated with the Endpoint",
1494                 "start");
1495
1496         /* the endpoint detail needs to always come first so apply as such */
1497         if (format_ami_endpoint(endpoint, &ami) ||
1498             ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1499                 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1500                         endpoint_name);
1501         }
1502
1503         astman_send_list_complete_start(s, m, "EndpointDetailComplete", ami.count + 1);
1504         astman_send_list_complete_end(s);
1505
1506         return 0;
1507 }
1508
1509 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1510                                   struct ast_str **buf)
1511 {
1512         char *str = NULL;
1513         if (ast_sip_auths_to_str(auths, &str)) {
1514                 return -1;
1515         }
1516         ast_str_append(buf, 0, "%s", str ? str : "");
1517         ast_free(str);
1518         return 0;
1519 }
1520
1521 static int format_ami_endpoints(void *obj, void *arg, int flags)
1522 {
1523
1524         struct ast_sip_endpoint *endpoint = obj;
1525         struct ast_sip_ami *ami = arg;
1526         RAII_VAR(struct ast_str *, buf,
1527                  ast_sip_create_ami_event("EndpointList", ami), ast_free);
1528
1529         if (!buf) {
1530                 return CMP_STOP;
1531         }
1532
1533         sip_sorcery_object_ami_set_type_name(endpoint, &buf);
1534         ast_str_append(&buf, 0, "Transport: %s\r\n",
1535                        endpoint->transport);
1536         ast_str_append(&buf, 0, "Aor: %s\r\n",
1537                        endpoint->aors);
1538
1539         ast_str_append(&buf, 0, "Auths: ");
1540         format_str_append_auth(&endpoint->inbound_auths, &buf);
1541         ast_str_append(&buf, 0, "\r\n");
1542
1543         ast_str_append(&buf, 0, "OutboundAuths: ");
1544         format_str_append_auth(&endpoint->outbound_auths, &buf);
1545         ast_str_append(&buf, 0, "\r\n");
1546
1547         ast_sip_for_each_aor(endpoint->aors,
1548                              sip_endpoints_aors_ami, &buf);
1549
1550         ast_str_append(&buf, 0, "DeviceState: %s\r\n",
1551                        ast_sip_get_device_state(endpoint));
1552
1553         ast_str_append(&buf, 0, "ActiveChannels: ");
1554         active_channels_to_str(endpoint, &buf);
1555         ast_str_append(&buf, 0, "\r\n");
1556
1557         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1558         return 0;
1559 }
1560
1561 static int ami_show_endpoints(struct mansession *s, const struct message *m)
1562 {
1563         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
1564         RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
1565         int num;
1566
1567         endpoints = ast_sip_get_endpoints();
1568         if (!endpoints) {
1569                 astman_send_error(s, m, "Could not get endpoints\n");
1570                 return 0;
1571         }
1572
1573         if (!(num = ao2_container_count(endpoints))) {
1574                 astman_send_error(s, m, "No endpoints found\n");
1575                 return 0;
1576         }
1577
1578         astman_send_listack(s, m, "A listing of Endpoints follows, presented as EndpointList events",
1579                 "start");
1580
1581         ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
1582
1583         astman_send_list_complete_start(s, m, "EndpointListComplete", num);
1584         astman_send_list_complete_end(s);
1585         return 0;
1586 }
1587
1588 static struct ao2_container *cli_endpoint_get_container(const char *regex)
1589 {
1590         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
1591         struct ao2_container *s_container;
1592
1593         container = ast_sorcery_retrieve_by_regex(sip_sorcery, "endpoint", regex);
1594         if (!container) {
1595                 return NULL;
1596         }
1597
1598         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
1599                 (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
1600         if (!s_container) {
1601                 return NULL;
1602         }
1603
1604         if (ao2_container_dup(s_container, container, 0)) {
1605                 ao2_ref(s_container, -1);
1606                 return NULL;
1607         }
1608
1609         return s_container;
1610 }
1611
1612 static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
1613 {
1614         ao2_callback(obj, OBJ_NODATA, callback, args);
1615
1616         return 0;
1617 }
1618
1619 static void *cli_endpoint_retrieve_by_id(const char *id)
1620 {
1621         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
1622 }
1623
1624 static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
1625 {
1626         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1627
1628         formatter_entry = ast_sip_lookup_cli_formatter(type);
1629         if (formatter_entry) {
1630                 formatter_entry->print_header(NULL, context, 0);
1631         }
1632 }
1633
1634 static int cli_endpoint_print_header(void *obj, void *arg, int flags)
1635 {
1636         struct ast_sip_cli_context *context = arg;
1637
1638         ast_assert(context->output_buffer != NULL);
1639
1640         ast_str_append(&context->output_buffer, 0,
1641                         " Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>\n");
1642
1643         if (context->recurse) {
1644                 context->indent_level++;
1645                 cli_endpoint_print_child_header("auth", context);
1646                 cli_endpoint_print_child_header("aor", context);
1647                 cli_endpoint_print_child_header("transport", context);
1648                 cli_endpoint_print_child_header("identify", context);
1649                 cli_endpoint_print_child_header("channel", context);
1650                 context->indent_level--;
1651         }
1652
1653         return 0;
1654 }
1655
1656 static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
1657 {
1658         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1659
1660         formatter_entry = ast_sip_lookup_cli_formatter(type);
1661         if (formatter_entry) {
1662                 formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
1663         }
1664 }
1665
1666 static int cli_endpoint_print_body(void *obj, void *arg, int flags)
1667 {
1668         struct ast_sip_endpoint *endpoint = obj;
1669         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1670         struct ast_sip_cli_context *context = arg;
1671         const char *id = ast_sorcery_object_get_id(endpoint);
1672         char *print_name = NULL;
1673         int print_name_len;
1674         char *number = S_COR(endpoint->id.self.number.valid,
1675                 endpoint->id.self.number.str, NULL);
1676         int indent;
1677         int flexwidth;
1678
1679         ast_assert(context->output_buffer != NULL);
1680
1681         if (number) {
1682                 print_name_len = strlen(id) + strlen(number) + 2;
1683                 print_name = ast_alloca(print_name_len);
1684                 snprintf(print_name, print_name_len, "%s/%s", id, number);
1685         }
1686
1687         indent = CLI_INDENT_TO_SPACES(context->indent_level);
1688         flexwidth = CLI_LAST_TABSTOP - indent - 2;
1689
1690         ast_str_append(&context->output_buffer, 0, "%*s:  %-*.*s  %-12.12s  %d of %.0f\n",
1691                 indent, "Endpoint",
1692                 flexwidth, flexwidth, print_name ? print_name : id,
1693                 ast_sip_get_device_state(endpoint),
1694                 endpoint_snapshot->num_channels,
1695                 (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
1696                                                                                                                 INFINITY
1697                                                                                                                 );
1698
1699         if (context->recurse) {
1700                 context->indent_level++;
1701
1702                 context->auth_direction = "Out";
1703                 cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
1704                 context->auth_direction = "In";
1705                 cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
1706
1707                 cli_endpoint_print_child_body("aor", endpoint->aors, context);
1708                 cli_endpoint_print_child_body("transport", endpoint, context);
1709                 cli_endpoint_print_child_body("identify", endpoint, context);
1710                 cli_endpoint_print_child_body("channel", endpoint, context);
1711
1712                 context->indent_level--;
1713
1714                 if (context->indent_level == 0) {
1715                         ast_str_append(&context->output_buffer, 0, "\n");
1716                 }
1717         }
1718
1719         if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1720                 ast_str_append(&context->output_buffer, 0, "\n");
1721                 ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
1722         }
1723
1724         return 0;
1725 }
1726
1727 static struct ast_cli_entry cli_commands[] = {
1728         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
1729                 .command = "pjsip list endpoints",
1730                 .usage = "Usage: pjsip list endpoints [ like <pattern> ]\n"
1731                                 "       List the configured PJSIP endpoints\n"
1732                                 "       Optional regular expression pattern is used to filter the list.\n"),
1733         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
1734                 .command = "pjsip show endpoints",
1735                 .usage = "Usage: pjsip show endpoints [ like <pattern> ]\n"
1736                                 "       List(detailed) the configured PJSIP endpoints\n"
1737                                 "       Optional regular expression pattern is used to filter the list.\n"),
1738         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
1739                 .command = "pjsip show endpoint",
1740                 .usage = "Usage: pjsip show endpoint <id>\n"
1741                                  "       Show the configured PJSIP endpoint\n"),
1742 };
1743
1744 struct ast_sip_cli_formatter_entry *channel_formatter;
1745 struct ast_sip_cli_formatter_entry *endpoint_formatter;
1746
1747 static void load_all_endpoints(void)
1748 {
1749         struct ao2_container *endpoints;
1750
1751         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1752         ao2_cleanup(endpoints);
1753 }
1754
1755 int ast_res_pjsip_initialize_configuration(void)
1756 {
1757         if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
1758             ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
1759                 return -1;
1760         }
1761
1762         persistent_endpoints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1763                 PERSISTENT_BUCKETS, persistent_endpoint_hash, NULL, persistent_endpoint_cmp);
1764         if (!persistent_endpoints) {
1765                 return -1;
1766         }
1767
1768         if (!(sip_sorcery = ast_sorcery_open())) {
1769                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
1770                 return -1;
1771         }
1772
1773         ast_sip_initialize_cli();
1774
1775         if (ast_sip_initialize_sorcery_auth()) {
1776                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
1777                 ast_sorcery_unref(sip_sorcery);
1778                 sip_sorcery = NULL;
1779                 return -1;
1780         }
1781
1782         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
1783         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
1784
1785         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
1786                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
1787                 ast_sorcery_unref(sip_sorcery);
1788                 sip_sorcery = NULL;
1789                 return -1;
1790         }
1791
1792         if (ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL)) {
1793                 ast_log(LOG_ERROR, "Failed to register nat_hook\n");
1794         }
1795
1796         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
1797         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
1798         ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
1799         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
1800         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
1801         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
1802         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
1803         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
1804         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
1805         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
1806         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
1807         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
1808         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
1809         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
1810         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
1811         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
1812         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));
1813         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));
1814         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
1815         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
1816         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
1817         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
1818         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));
1819         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username,ip", ident_handler, ident_to_str, NULL, 0, 0);
1820         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
1821         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);
1822         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_connected_line", "yes", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_connected_line));
1823         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_connected_line", "yes", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, id.send_connected_line));
1824         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);
1825         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);
1826         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));
1827         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
1828         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);
1829         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
1830         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
1831         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
1832         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
1833         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
1834         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rpid_immediate", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.rpid_immediate));
1835         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
1836         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
1837         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
1838         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
1839         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_subscribe_replaces_unsolicited", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.subscribe_replaces_unsolicited));
1840         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
1841         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
1842         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
1843         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));
1844         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_keepalive", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.keepalive));
1845         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout));
1846         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));
1847         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
1848         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
1849         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
1850         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
1851         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
1852         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
1853         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));
1854         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
1855         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
1856         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
1857         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
1858         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, faxdetect_timeout));
1859         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
1860         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
1861         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
1862         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
1863         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
1864         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
1865         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
1866         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "user_eq_phone", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, usereqphone));
1867         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_passthrough", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, moh_passthrough));
1868         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
1869         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
1870         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
1871         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
1872         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
1873         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
1874         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
1875         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
1876         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "from_user", "", from_user_handler, from_user_to_str, NULL, 0, 0);
1877         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
1878         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
1879         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
1880         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "no", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
1881         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "0", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
1882         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_auto_generate_cert", "no", dtls_handler, dtlsautogeneratecert_to_str, NULL, 0, 0);
1883         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
1884         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
1885         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
1886         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
1887         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
1888         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
1889         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_fingerprint", "", dtls_handler, dtlsfingerprint_to_str, NULL, 0, 0);
1890         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));
1891         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));
1892         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));
1893         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, NULL, 0, 0);
1894         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
1895         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, message_context));
1896         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, accountcode));
1897         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1898         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1899         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "acl", "", endpoint_acl_handler, acl_to_str, NULL, 0, 0);
1900         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1901         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1902         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_acl", "", endpoint_acl_handler, contact_acl_to_str, NULL, 0, 0);
1903         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subscribe_context", "", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct ast_sip_endpoint, subscription.context));
1904         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_user", "", contact_user_handler, contact_user_to_str, NULL, 0, 0);
1905         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "preferred_codec_only", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, preferred_codec_only));
1906         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "asymmetric_rtp_codec", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, asymmetric_rtp_codec));
1907         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtcp_mux", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtcp_mux));
1908         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_overlap", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allow_overlap));
1909         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "refer_blind_progress", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, refer_blind_progress));
1910         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));
1911         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));
1912         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));
1913         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "bundle", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.bundle));
1914         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "webrtc", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, media.webrtc));
1915         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "incoming_mwi_mailbox", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, incoming_mwi_mailbox));
1916         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));
1917         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));
1918         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "suppress_q850_reason_headers", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, suppress_q850_reason_headers));
1919
1920         if (ast_sip_initialize_sorcery_transport()) {
1921                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
1922                 ast_sorcery_unref(sip_sorcery);
1923                 sip_sorcery = NULL;
1924                 return -1;
1925         }
1926
1927         if (ast_sip_initialize_sorcery_location()) {
1928                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
1929                 ast_sorcery_unref(sip_sorcery);
1930                 sip_sorcery = NULL;
1931                 return -1;
1932         }
1933
1934         ast_sorcery_observer_add(sip_sorcery, "endpoint", &endpoint_observers);
1935
1936         if (ast_sip_initialize_sorcery_domain_alias()) {
1937                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
1938                 ast_sorcery_unref(sip_sorcery);
1939                 sip_sorcery = NULL;
1940                 return -1;
1941         }
1942
1943         if (ast_sip_initialize_sorcery_global()) {
1944                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
1945                 ast_sorcery_unref(sip_sorcery);
1946                 sip_sorcery = NULL;
1947                 return -1;
1948         }
1949
1950         endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1951         if (!endpoint_formatter) {
1952                 ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
1953                 ast_sorcery_unref(sip_sorcery);
1954                 sip_sorcery = NULL;
1955                 return -1;
1956         }
1957         endpoint_formatter->name = "endpoint";
1958         endpoint_formatter->print_header = cli_endpoint_print_header;
1959         endpoint_formatter->print_body = cli_endpoint_print_body;
1960         endpoint_formatter->get_container = cli_endpoint_get_container;
1961         endpoint_formatter->iterate = cli_endpoint_iterate;
1962         endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
1963         endpoint_formatter->get_id = ast_sorcery_object_get_id;
1964
1965         ast_sip_register_cli_formatter(endpoint_formatter);
1966         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1967
1968         ast_sorcery_load(sip_sorcery);
1969
1970         load_all_endpoints();
1971
1972         ast_sip_location_prune_boot_contacts();
1973
1974         return 0;
1975 }
1976
1977 void ast_res_pjsip_destroy_configuration(void)
1978 {
1979         if (!sip_sorcery) {
1980                 return;
1981         }
1982
1983         ast_sip_destroy_sorcery_global();
1984         ast_sip_destroy_sorcery_location();
1985         ast_sip_destroy_sorcery_auth();
1986         ast_sip_destroy_sorcery_transport();
1987         ast_sorcery_unref(sip_sorcery);
1988         sip_sorcery = NULL;
1989         ast_manager_unregister(AMI_SHOW_ENDPOINT);
1990         ast_manager_unregister(AMI_SHOW_ENDPOINTS);
1991         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1992         ast_sip_unregister_cli_formatter(endpoint_formatter);
1993         ast_sip_destroy_cli();
1994         ao2_cleanup(persistent_endpoints);
1995         persistent_endpoints = NULL;
1996 }
1997
1998 int ast_res_pjsip_reload_configuration(void)
1999 {
2000         if (sip_sorcery) {
2001                 ast_sorcery_reload(sip_sorcery);
2002         }
2003         return 0;
2004 }
2005
2006 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
2007 {
2008         ast_string_field_free_memory(&subscription->mwi);
2009         ast_free(subscription->mwi.voicemail_extension);
2010 }
2011
2012 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
2013 {
2014         ast_string_field_free_memory(&info->recording);
2015 }
2016
2017 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
2018 {
2019         ast_rtp_dtls_cfg_free(&media->rtp.dtls_cfg);
2020         ast_string_field_free_memory(&media->rtp);
2021         ast_string_field_free_memory(media);
2022 }
2023
2024 static void endpoint_destructor(void* obj)
2025 {
2026         struct ast_sip_endpoint *endpoint = obj;
2027
2028         ast_string_field_free_memory(endpoint);
2029
2030         ao2_cleanup(endpoint->media.codecs);
2031         ast_stream_topology_free(endpoint->media.topology);
2032         subscription_configuration_destroy(&endpoint->subscription);
2033         info_configuration_destroy(&endpoint->info);
2034         media_configuration_destroy(&endpoint->media);
2035         ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
2036         ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
2037         ast_party_id_free(&endpoint->id.self);
2038         endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
2039         endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
2040         ao2_cleanup(endpoint->persistent);
2041         ast_variables_destroy(endpoint->channel_vars);
2042         AST_VECTOR_FREE(&endpoint->ident_method_order);
2043         ast_free(endpoint->contact_user);
2044         ast_free_acl_list(endpoint->contact_acl);
2045         ast_free_acl_list(endpoint->acl);
2046 }
2047
2048 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
2049 {
2050         return ast_string_field_init(&subscription->mwi, 64);
2051 }
2052
2053 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
2054 {
2055         return ast_string_field_init(&info->recording, 32);
2056 }
2057
2058 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
2059 {
2060         return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
2061 }
2062
2063 void *ast_sip_endpoint_alloc(const char *name)
2064 {
2065         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
2066         if (!endpoint) {
2067                 return NULL;
2068         }
2069         if (ast_string_field_init(endpoint, 64)) {
2070                 ao2_cleanup(endpoint);
2071                 return NULL;
2072         }
2073
2074         if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2075                 ao2_cleanup(endpoint);
2076                 return NULL;
2077         }
2078         if (init_subscription_configuration(&endpoint->subscription)) {
2079                 ao2_cleanup(endpoint);
2080                 return NULL;
2081         }
2082         if (init_info_configuration(&endpoint->info)) {
2083                 ao2_cleanup(endpoint);
2084                 return NULL;
2085         }
2086         if (init_media_configuration(&endpoint->media)) {
2087                 ao2_cleanup(endpoint);
2088                 return NULL;
2089         }
2090
2091         ast_party_id_init(&endpoint->id.self);
2092         endpoint->id.self.tag = ast_strdup("");
2093
2094         if (AST_VECTOR_INIT(&endpoint->ident_method_order, 1)) {
2095                 return NULL;
2096         }
2097
2098         return endpoint;
2099 }
2100
2101 struct ao2_container *ast_sip_get_endpoints(void)
2102 {
2103         struct ao2_container *endpoints;
2104
2105         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2106
2107         return endpoints;
2108 }
2109
2110 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
2111 {
2112         RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
2113         return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
2114                 sip_sorcery, "endpoint", name);
2115 }
2116
2117 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
2118 {
2119         int i;
2120
2121         for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
2122                 /* Using AST_VECTOR_GET is safe since the vector is immutable */
2123                 const char *name = AST_VECTOR_GET(auths, i);
2124                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
2125                 if (!out[i]) {
2126                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
2127                         return -1;
2128                 }
2129         }
2130
2131         return 0;
2132 }
2133
2134 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
2135 {
2136         int i;
2137         for (i = 0; i < num_auths; ++i) {
2138                 ao2_cleanup(auths[i]);
2139         }
2140 }
2141
2142 struct ast_sorcery *ast_sip_get_sorcery(void)
2143 {
2144         return sip_sorcery;
2145 }