pjsip/config_global: regcontext context not created
[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 static int add_to_regcontext(void *obj, void *arg, int flags)
1132 {
1133         struct sip_persistent_endpoint *persistent = obj;
1134         const char *regcontext = arg;
1135
1136         if (ast_endpoint_get_state(persistent->endpoint) == AST_ENDPOINT_ONLINE) {
1137                 if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(
1138                                 persistent->endpoint), 1, NULL)) {
1139                         ast_add_extension(regcontext, 1, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, NULL,
1140                                 "Noop", ast_strdup(ast_endpoint_get_resource(persistent->endpoint)), ast_free_ptr, "PJSIP");
1141                 }
1142         }
1143
1144         return 0;
1145 }
1146
1147 int ast_sip_persistent_endpoint_add_to_regcontext(const char *regcontext)
1148 {
1149         if (ast_strlen_zero(regcontext)) {
1150                 return 0;
1151         }
1152
1153         /* Make sure the regcontext exists */
1154         if (!ast_context_find_or_create(NULL, NULL, regcontext, "PJSIP")) {
1155                 ast_log(LOG_ERROR, "Failed to create regcontext '%s'\n", regcontext);
1156                 return -1;
1157         }
1158
1159         /* Add any online endpoints */
1160         ao2_callback(persistent_endpoints, OBJ_NODATA, add_to_regcontext, (void *)regcontext);
1161         return 0;
1162 }
1163
1164 int ast_sip_persistent_endpoint_update_state(const char *endpoint_name, enum ast_endpoint_state state)
1165 {
1166         struct sip_persistent_endpoint *persistent;
1167         struct ast_json *blob;
1168         char *regcontext;
1169
1170         persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1171         if (!persistent) {
1172                 return -1;
1173         }
1174
1175         /* If there was no state change, don't publish anything. */
1176         if (ast_endpoint_get_state(persistent->endpoint) == state) {
1177                 ao2_ref(persistent, -1);
1178                 return 0;
1179         }
1180
1181         regcontext = ast_sip_get_regcontext();
1182
1183         if (state == AST_ENDPOINT_ONLINE) {
1184                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
1185                 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
1186
1187                 if (!ast_strlen_zero(regcontext)) {
1188                         if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL)) {
1189                                 ast_add_extension(regcontext, 1, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, NULL,
1190                                         "Noop", ast_strdup(ast_endpoint_get_resource(persistent->endpoint)), ast_free_ptr, "PJSIP");
1191                         }
1192                 }
1193
1194                 ast_verb(2, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(persistent->endpoint));
1195         } else {
1196                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1197                 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
1198
1199                 if (!ast_strlen_zero(regcontext)) {
1200                         struct pbx_find_info q = { .stacklen = 0 };
1201
1202                         if (pbx_find_extension(NULL, NULL, &q, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, "", E_MATCH)) {
1203                                 ast_context_remove_extension(regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL);
1204                         }
1205                 }
1206
1207                 ast_verb(2, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(persistent->endpoint));
1208         }
1209
1210         ast_free(regcontext);
1211
1212         ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
1213         ast_json_unref(blob);
1214         ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
1215
1216         ao2_ref(persistent, -1);
1217
1218         return 0;
1219 }
1220
1221 void ast_sip_persistent_endpoint_publish_contact_state(const char *endpoint_name, const struct ast_sip_contact_status *contact_status)
1222 {
1223         struct sip_persistent_endpoint *persistent;
1224         struct ast_json *blob;
1225         char rtt[32];
1226
1227         persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1228         if (!persistent) {
1229                 return;
1230         }
1231
1232         snprintf(rtt, sizeof(rtt), "%" PRId64, contact_status->rtt);
1233         blob = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
1234                 "contact_status", ast_sip_get_contact_status_label(contact_status->status),
1235                 "aor", contact_status->aor,
1236                 "uri", contact_status->uri,
1237                 "roundtrip_usec", rtt,
1238                 "endpoint_name", ast_endpoint_get_resource(persistent->endpoint));
1239         if (blob) {
1240                 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_contact_state_type(), blob);
1241                 ast_json_unref(blob);
1242         }
1243
1244         ao2_ref(persistent, -1);
1245 }
1246
1247 /*! \brief Internal function which finds (or creates) persistent endpoint information */
1248 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
1249 {
1250         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
1251         SCOPED_AO2LOCK(lock, persistent_endpoints);
1252
1253         persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint),
1254                 OBJ_SEARCH_KEY | OBJ_NOLOCK);
1255         if (!persistent) {
1256                 persistent = ao2_alloc_options(sizeof(*persistent), persistent_endpoint_destroy,
1257                         AO2_ALLOC_OPT_LOCK_NOLOCK);
1258                 if (!persistent) {
1259                         return NULL;
1260                 }
1261
1262                 persistent->endpoint = ast_endpoint_create("PJSIP",
1263                         ast_sorcery_object_get_id(endpoint));
1264                 if (!persistent->endpoint) {
1265                         return NULL;
1266                 }
1267
1268                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1269
1270                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
1271         }
1272
1273         ao2_ref(persistent->endpoint, +1);
1274         return persistent->endpoint;
1275 }
1276
1277 /*! \brief Callback function for when an object is finalized */
1278 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
1279 {
1280         struct ast_sip_endpoint *endpoint = obj;
1281
1282         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
1283                 return -1;
1284         }
1285
1286         if (endpoint->extensions.timer.min_se < 90) {
1287                 ast_log(LOG_ERROR, "Session timer minimum expires time must be 90 or greater on endpoint '%s'\n",
1288                         ast_sorcery_object_get_id(endpoint));
1289                 return -1;
1290         } else if (endpoint->extensions.timer.sess_expires < endpoint->extensions.timer.min_se) {
1291                 ast_log(LOG_ERROR, "Session timer expires must be greater than minimum session expires time on endpoint '%s'\n",
1292                         ast_sorcery_object_get_id(endpoint));
1293                 return -1;
1294         }
1295
1296         if (ast_rtp_dtls_cfg_validate(&endpoint->media.rtp.dtls_cfg)) {
1297                 return -1;
1298         }
1299
1300         endpoint->media.topology = ast_stream_topology_create_from_format_cap(endpoint->media.codecs);
1301         if (!endpoint->media.topology) {
1302                 return -1;
1303         }
1304
1305         endpoint->media.rtcp_mux |= endpoint->media.bundle;
1306
1307         /*
1308          * If webrtc has been enabled then enable those attributes, and default
1309          * some, that are needed in order for webrtc to work.
1310          */
1311         endpoint->media.bundle |= endpoint->media.webrtc;
1312         endpoint->media.rtcp_mux |= endpoint->media.webrtc;
1313         endpoint->media.rtp.use_avpf |= endpoint->media.webrtc;
1314         endpoint->media.rtp.ice_support |= endpoint->media.webrtc;
1315         endpoint->media.rtp.use_received_transport |= endpoint->media.webrtc;
1316
1317         if (endpoint->media.webrtc) {
1318                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
1319                 endpoint->media.rtp.dtls_cfg.enabled = 1;
1320                 endpoint->media.rtp.dtls_cfg.default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
1321                 endpoint->media.rtp.dtls_cfg.verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
1322
1323                 if (ast_strlen_zero(endpoint->media.rtp.dtls_cfg.certfile)) {
1324                         /* If no certificate has been specified, try to automatically create one */
1325                         endpoint->media.rtp.dtls_cfg.ephemeral_cert = 1;
1326                 }
1327         }
1328
1329         return 0;
1330 }
1331
1332 const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
1333 {
1334         char device[MAX_OBJECT_FIELD];
1335
1336         snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
1337         return ast_devstate2str(ast_device_state(device));
1338 }
1339
1340 struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
1341         const struct ast_sip_endpoint *endpoint)
1342 {
1343         return ast_endpoint_latest_snapshot(
1344                 ast_endpoint_get_tech(endpoint->persistent),
1345                 ast_endpoint_get_resource(endpoint->persistent));
1346 }
1347
1348 int ast_sip_for_each_channel_snapshot(
1349         const struct ast_endpoint_snapshot *endpoint_snapshot,
1350         ao2_callback_fn on_channel_snapshot, void *arg)
1351 {
1352         int num, num_channels = endpoint_snapshot->num_channels;
1353
1354         if (!on_channel_snapshot || !num_channels) {
1355                 return 0;
1356         }
1357
1358         for (num = 0; num < num_channels; ++num) {
1359                 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1360                 int res;
1361
1362                 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1363                 if (!snapshot) {
1364                         continue;
1365                 }
1366
1367                 res = on_channel_snapshot(snapshot, arg, 0);
1368                 if (res) {
1369                         return -1;
1370                 }
1371         }
1372         return 0;
1373 }
1374
1375 int ast_sip_for_each_channel(
1376         const struct ast_sip_endpoint *endpoint,
1377         ao2_callback_fn on_channel_snapshot, void *arg)
1378 {
1379         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1380         return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
1381 }
1382
1383 static int active_channels_to_str_cb(void *object, void *arg, int flags)
1384 {
1385         const struct ast_channel_snapshot *snapshot = object;
1386         struct ast_str **buf = arg;
1387         ast_str_append(buf, 0, "%s,", snapshot->base->name);
1388         return 0;
1389 }
1390
1391 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
1392                                    struct ast_str **str)
1393 {
1394
1395         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1396                  ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1397
1398         if (endpoint_snapshot) {
1399                 return;
1400         }
1401
1402         ast_sip_for_each_channel_snapshot(endpoint_snapshot,
1403                                           active_channels_to_str_cb, str);
1404         ast_str_truncate(*str, -1);
1405 }
1406
1407 #define AMI_DEFAULT_STR_SIZE 512
1408
1409 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1410 {
1411         struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1412
1413         if (!(buf)) {
1414                 astman_send_error_va(ami->s, ami->m, "Unable create event "
1415                                      "for %s\n", event);
1416                 return NULL;
1417         }
1418
1419         ast_str_set(&buf, 0, "Event: %s\r\n", event);
1420         if (!ast_strlen_zero(ami->action_id)) {
1421                 ast_str_append(&buf, 0, "ActionID: %s\r\n", ami->action_id);
1422         }
1423         return buf;
1424 }
1425
1426 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1427 {
1428         ast_str_append(buf, 0, "ObjectType: %s\r\n",
1429                        ast_sorcery_object_get_type(obj));
1430         ast_str_append(buf, 0, "ObjectName: %s\r\n",
1431                        ast_sorcery_object_get_id(obj));
1432 }
1433
1434 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1435 {
1436         RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
1437                          ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
1438         struct ast_variable *i;
1439
1440         if (!objset) {
1441                 return -1;
1442         }
1443
1444         sip_sorcery_object_ami_set_type_name(obj, buf);
1445
1446         for (i = objset; i; i = i->next) {
1447                 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1448                 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1449         }
1450
1451         return 0;
1452 }
1453
1454 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1455 {
1456         struct ast_sip_aor *aor = obj;
1457         struct ast_str **buf = arg;
1458
1459         ast_str_append(buf, 0, "Contacts: ");
1460         ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1461         ast_str_append(buf, 0, "\r\n");
1462
1463         return 0;
1464 }
1465
1466 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1467                                struct ast_str **buf)
1468 {
1469         if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1470                 return -1;
1471         }
1472
1473         ast_str_append(buf, 0, "DeviceState: %s\r\n",
1474                        ast_sip_get_device_state(endpoint));
1475
1476         ast_str_append(buf, 0, "ActiveChannels: ");
1477         active_channels_to_str(endpoint, buf);
1478         ast_str_append(buf, 0, "\r\n");
1479
1480         return 0;
1481 }
1482
1483 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1484                                struct ast_sip_ami *ami)
1485 {
1486         RAII_VAR(struct ast_str *, buf,
1487                  ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1488
1489         if (!buf) {
1490                 return -1;
1491         }
1492
1493         sip_endpoint_to_ami(endpoint, &buf);
1494         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1495         return 0;
1496 }
1497
1498 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1499 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1500
1501 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1502 {
1503         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"),
1504                 .count = 0, };
1505         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1506         const char *endpoint_name = astman_get_header(m, "Endpoint");
1507         int count = 0;
1508
1509         if (ast_strlen_zero(endpoint_name)) {
1510                 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1511                         AMI_SHOW_ENDPOINT);
1512                 return 0;
1513         }
1514
1515         if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1516                 endpoint_name += 6;
1517         }
1518
1519         if (!(endpoint = ast_sorcery_retrieve_by_id(
1520                       ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1521                 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1522                         endpoint_name);
1523                 return 0;
1524         }
1525
1526         astman_send_listack(s, m, "Following are Events for each object associated with the Endpoint",
1527                 "start");
1528
1529         /* the endpoint detail needs to always come first so apply as such */
1530         if (format_ami_endpoint(endpoint, &ami) ||
1531             ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1532                 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1533                         endpoint_name);
1534         }
1535
1536         astman_send_list_complete_start(s, m, "EndpointDetailComplete", ami.count + 1);
1537         astman_send_list_complete_end(s);
1538
1539         return 0;
1540 }
1541
1542 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1543                                   struct ast_str **buf)
1544 {
1545         char *str = NULL;
1546         if (ast_sip_auths_to_str(auths, &str)) {
1547                 return -1;
1548         }
1549         ast_str_append(buf, 0, "%s", str ? str : "");
1550         ast_free(str);
1551         return 0;
1552 }
1553
1554 static int format_ami_endpoints(void *obj, void *arg, int flags)
1555 {
1556
1557         struct ast_sip_endpoint *endpoint = obj;
1558         struct ast_sip_ami *ami = arg;
1559         RAII_VAR(struct ast_str *, buf,
1560                  ast_sip_create_ami_event("EndpointList", ami), ast_free);
1561
1562         if (!buf) {
1563                 return CMP_STOP;
1564         }
1565
1566         sip_sorcery_object_ami_set_type_name(endpoint, &buf);
1567         ast_str_append(&buf, 0, "Transport: %s\r\n",
1568                        endpoint->transport);
1569         ast_str_append(&buf, 0, "Aor: %s\r\n",
1570                        endpoint->aors);
1571
1572         ast_str_append(&buf, 0, "Auths: ");
1573         format_str_append_auth(&endpoint->inbound_auths, &buf);
1574         ast_str_append(&buf, 0, "\r\n");
1575
1576         ast_str_append(&buf, 0, "OutboundAuths: ");
1577         format_str_append_auth(&endpoint->outbound_auths, &buf);
1578         ast_str_append(&buf, 0, "\r\n");
1579
1580         ast_sip_for_each_aor(endpoint->aors,
1581                              sip_endpoints_aors_ami, &buf);
1582
1583         ast_str_append(&buf, 0, "DeviceState: %s\r\n",
1584                        ast_sip_get_device_state(endpoint));
1585
1586         ast_str_append(&buf, 0, "ActiveChannels: ");
1587         active_channels_to_str(endpoint, &buf);
1588         ast_str_append(&buf, 0, "\r\n");
1589
1590         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1591         return 0;
1592 }
1593
1594 static int ami_show_endpoints(struct mansession *s, const struct message *m)
1595 {
1596         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
1597         RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
1598         int num;
1599
1600         endpoints = ast_sip_get_endpoints();
1601         if (!endpoints) {
1602                 astman_send_error(s, m, "Could not get endpoints\n");
1603                 return 0;
1604         }
1605
1606         if (!(num = ao2_container_count(endpoints))) {
1607                 astman_send_error(s, m, "No endpoints found\n");
1608                 return 0;
1609         }
1610
1611         astman_send_listack(s, m, "A listing of Endpoints follows, presented as EndpointList events",
1612                 "start");
1613
1614         ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
1615
1616         astman_send_list_complete_start(s, m, "EndpointListComplete", num);
1617         astman_send_list_complete_end(s);
1618         return 0;
1619 }
1620
1621 static struct ao2_container *cli_endpoint_get_container(const char *regex)
1622 {
1623         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
1624         struct ao2_container *s_container;
1625
1626         container = ast_sorcery_retrieve_by_regex(sip_sorcery, "endpoint", regex);
1627         if (!container) {
1628                 return NULL;
1629         }
1630
1631         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
1632                 (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
1633         if (!s_container) {
1634                 return NULL;
1635         }
1636
1637         if (ao2_container_dup(s_container, container, 0)) {
1638                 ao2_ref(s_container, -1);
1639                 return NULL;
1640         }
1641
1642         return s_container;
1643 }
1644
1645 static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
1646 {
1647         ao2_callback(obj, OBJ_NODATA, callback, args);
1648
1649         return 0;
1650 }
1651
1652 static void *cli_endpoint_retrieve_by_id(const char *id)
1653 {
1654         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
1655 }
1656
1657 static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
1658 {
1659         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1660
1661         formatter_entry = ast_sip_lookup_cli_formatter(type);
1662         if (formatter_entry) {
1663                 formatter_entry->print_header(NULL, context, 0);
1664         }
1665 }
1666
1667 static int cli_endpoint_print_header(void *obj, void *arg, int flags)
1668 {
1669         struct ast_sip_cli_context *context = arg;
1670
1671         ast_assert(context->output_buffer != NULL);
1672
1673         ast_str_append(&context->output_buffer, 0,
1674                         " Endpoint:  <Endpoint/CID.....................................>  <State.....>  <Channels.>\n");
1675
1676         if (context->recurse) {
1677                 context->indent_level++;
1678                 cli_endpoint_print_child_header("auth", context);
1679                 cli_endpoint_print_child_header("aor", context);
1680                 cli_endpoint_print_child_header("transport", context);
1681                 cli_endpoint_print_child_header("identify", context);
1682                 cli_endpoint_print_child_header("channel", context);
1683                 context->indent_level--;
1684         }
1685
1686         return 0;
1687 }
1688
1689 static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
1690 {
1691         RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1692
1693         formatter_entry = ast_sip_lookup_cli_formatter(type);
1694         if (formatter_entry) {
1695                 formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
1696         }
1697 }
1698
1699 static int cli_endpoint_print_body(void *obj, void *arg, int flags)
1700 {
1701         struct ast_sip_endpoint *endpoint = obj;
1702         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1703         struct ast_sip_cli_context *context = arg;
1704         const char *id = ast_sorcery_object_get_id(endpoint);
1705         char *print_name = NULL;
1706         int print_name_len;
1707         char *number = S_COR(endpoint->id.self.number.valid,
1708                 endpoint->id.self.number.str, NULL);
1709         int indent;
1710         int flexwidth;
1711
1712         ast_assert(context->output_buffer != NULL);
1713
1714         if (number) {
1715                 print_name_len = strlen(id) + strlen(number) + 2;
1716                 print_name = ast_alloca(print_name_len);
1717                 snprintf(print_name, print_name_len, "%s/%s", id, number);
1718         }
1719
1720         indent = CLI_INDENT_TO_SPACES(context->indent_level);
1721         flexwidth = CLI_LAST_TABSTOP - indent - 2;
1722
1723         ast_str_append(&context->output_buffer, 0, "%*s:  %-*.*s  %-12.12s  %d of %.0f\n",
1724                 indent, "Endpoint",
1725                 flexwidth, flexwidth, print_name ? print_name : id,
1726                 ast_sip_get_device_state(endpoint),
1727                 endpoint_snapshot->num_channels,
1728                 (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
1729                                                                                                                 INFINITY
1730                                                                                                                 );
1731
1732         if (context->recurse) {
1733                 context->indent_level++;
1734
1735                 context->auth_direction = "Out";
1736                 cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
1737                 context->auth_direction = "In";
1738                 cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
1739
1740                 cli_endpoint_print_child_body("aor", endpoint->aors, context);
1741                 cli_endpoint_print_child_body("transport", endpoint, context);
1742                 cli_endpoint_print_child_body("identify", endpoint, context);
1743                 cli_endpoint_print_child_body("channel", endpoint, context);
1744
1745                 context->indent_level--;
1746
1747                 if (context->indent_level == 0) {
1748                         ast_str_append(&context->output_buffer, 0, "\n");
1749                 }
1750         }
1751
1752         if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
1753                 ast_str_append(&context->output_buffer, 0, "\n");
1754                 ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
1755         }
1756
1757         return 0;
1758 }
1759
1760 static struct ast_cli_entry cli_commands[] = {
1761         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
1762                 .command = "pjsip list endpoints",
1763                 .usage = "Usage: pjsip list endpoints [ like <pattern> ]\n"
1764                                 "       List the configured PJSIP endpoints\n"
1765                                 "       Optional regular expression pattern is used to filter the list.\n"),
1766         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
1767                 .command = "pjsip show endpoints",
1768                 .usage = "Usage: pjsip show endpoints [ like <pattern> ]\n"
1769                                 "       List(detailed) the configured PJSIP endpoints\n"
1770                                 "       Optional regular expression pattern is used to filter the list.\n"),
1771         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
1772                 .command = "pjsip show endpoint",
1773                 .usage = "Usage: pjsip show endpoint <id>\n"
1774                                  "       Show the configured PJSIP endpoint\n"),
1775 };
1776
1777 struct ast_sip_cli_formatter_entry *channel_formatter;
1778 struct ast_sip_cli_formatter_entry *endpoint_formatter;
1779
1780 static void load_all_endpoints(void)
1781 {
1782         struct ao2_container *endpoints;
1783
1784         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1785         ao2_cleanup(endpoints);
1786 }
1787
1788 int ast_res_pjsip_initialize_configuration(void)
1789 {
1790         if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
1791             ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
1792                 return -1;
1793         }
1794
1795         persistent_endpoints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1796                 PERSISTENT_BUCKETS, persistent_endpoint_hash, NULL, persistent_endpoint_cmp);
1797         if (!persistent_endpoints) {
1798                 return -1;
1799         }
1800
1801         if (!(sip_sorcery = ast_sorcery_open())) {
1802                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
1803                 return -1;
1804         }
1805
1806         ast_sip_initialize_cli();
1807
1808         if (ast_sip_initialize_sorcery_auth()) {
1809                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
1810                 ast_sorcery_unref(sip_sorcery);
1811                 sip_sorcery = NULL;
1812                 return -1;
1813         }
1814
1815         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
1816         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
1817
1818         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
1819                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
1820                 ast_sorcery_unref(sip_sorcery);
1821                 sip_sorcery = NULL;
1822                 return -1;
1823         }
1824
1825         if (ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL)) {
1826                 ast_log(LOG_ERROR, "Failed to register nat_hook\n");
1827         }
1828
1829         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
1830         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
1831         ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
1832         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
1833         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
1834         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
1835         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
1836         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
1837         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
1838         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
1839         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
1840         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
1841         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
1842         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
1843         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
1844         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
1845         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));
1846         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));
1847         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
1848         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
1849         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
1850         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
1851         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));
1852         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username,ip", ident_handler, ident_to_str, NULL, 0, 0);
1853         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
1854         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);
1855         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));
1856         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));
1857         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);
1858         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);
1859         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));
1860         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
1861         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);
1862         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
1863         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
1864         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
1865         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
1866         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
1867         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rpid_immediate", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.rpid_immediate));
1868         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
1869         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
1870         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
1871         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
1872         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));
1873         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
1874         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
1875         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
1876         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));
1877         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_keepalive", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.keepalive));
1878         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout));
1879         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));
1880         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
1881         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
1882         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
1883         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
1884         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
1885         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
1886         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));
1887         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
1888         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
1889         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
1890         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
1891         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, faxdetect_timeout));
1892         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
1893         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
1894         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
1895         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
1896         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
1897         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
1898         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
1899         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "user_eq_phone", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, usereqphone));
1900         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_passthrough", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, moh_passthrough));
1901         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
1902         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
1903         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
1904         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
1905         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
1906         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
1907         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
1908         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
1909         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "from_user", "", from_user_handler, from_user_to_str, NULL, 0, 0);
1910         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
1911         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
1912         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
1913         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "no", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
1914         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "0", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
1915         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_auto_generate_cert", "no", dtls_handler, dtlsautogeneratecert_to_str, NULL, 0, 0);
1916         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
1917         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
1918         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
1919         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
1920         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
1921         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
1922         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_fingerprint", "", dtls_handler, dtlsfingerprint_to_str, NULL, 0, 0);
1923         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));
1924         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));
1925         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));
1926         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, NULL, 0, 0);
1927         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
1928         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, message_context));
1929         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, accountcode));
1930         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1931         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1932         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "acl", "", endpoint_acl_handler, acl_to_str, NULL, 0, 0);
1933         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1934         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
1935         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_acl", "", endpoint_acl_handler, contact_acl_to_str, NULL, 0, 0);
1936         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subscribe_context", "", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct ast_sip_endpoint, subscription.context));
1937         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_user", "", contact_user_handler, contact_user_to_str, NULL, 0, 0);
1938         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "preferred_codec_only", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, preferred_codec_only));
1939         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "asymmetric_rtp_codec", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, asymmetric_rtp_codec));
1940         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtcp_mux", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtcp_mux));
1941         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_overlap", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allow_overlap));
1942         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "refer_blind_progress", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, refer_blind_progress));
1943         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));
1944         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));
1945         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));
1946         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "bundle", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.bundle));
1947         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "webrtc", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, media.webrtc));
1948         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "incoming_mwi_mailbox", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, incoming_mwi_mailbox));
1949         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));
1950         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));
1951         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));
1952
1953         if (ast_sip_initialize_sorcery_transport()) {
1954                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
1955                 ast_sorcery_unref(sip_sorcery);
1956                 sip_sorcery = NULL;
1957                 return -1;
1958         }
1959
1960         if (ast_sip_initialize_sorcery_location()) {
1961                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
1962                 ast_sorcery_unref(sip_sorcery);
1963                 sip_sorcery = NULL;
1964                 return -1;
1965         }
1966
1967         ast_sorcery_observer_add(sip_sorcery, "endpoint", &endpoint_observers);
1968
1969         if (ast_sip_initialize_sorcery_domain_alias()) {
1970                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
1971                 ast_sorcery_unref(sip_sorcery);
1972                 sip_sorcery = NULL;
1973                 return -1;
1974         }
1975
1976         if (ast_sip_initialize_sorcery_global()) {
1977                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
1978                 ast_sorcery_unref(sip_sorcery);
1979                 sip_sorcery = NULL;
1980                 return -1;
1981         }
1982
1983         endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1984         if (!endpoint_formatter) {
1985                 ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
1986                 ast_sorcery_unref(sip_sorcery);
1987                 sip_sorcery = NULL;
1988                 return -1;
1989         }
1990         endpoint_formatter->name = "endpoint";
1991         endpoint_formatter->print_header = cli_endpoint_print_header;
1992         endpoint_formatter->print_body = cli_endpoint_print_body;
1993         endpoint_formatter->get_container = cli_endpoint_get_container;
1994         endpoint_formatter->iterate = cli_endpoint_iterate;
1995         endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
1996         endpoint_formatter->get_id = ast_sorcery_object_get_id;
1997
1998         ast_sip_register_cli_formatter(endpoint_formatter);
1999         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
2000
2001         ast_sorcery_load(sip_sorcery);
2002
2003         load_all_endpoints();
2004
2005         ast_sip_location_prune_boot_contacts();
2006
2007         return 0;
2008 }
2009
2010 void ast_res_pjsip_destroy_configuration(void)
2011 {
2012         if (!sip_sorcery) {
2013                 return;
2014         }
2015
2016         ast_sip_destroy_sorcery_global();
2017         ast_sip_destroy_sorcery_location();
2018         ast_sip_destroy_sorcery_auth();
2019         ast_sip_destroy_sorcery_transport();
2020         ast_sorcery_unref(sip_sorcery);
2021         sip_sorcery = NULL;
2022         ast_manager_unregister(AMI_SHOW_ENDPOINT);
2023         ast_manager_unregister(AMI_SHOW_ENDPOINTS);
2024         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
2025         ast_sip_unregister_cli_formatter(endpoint_formatter);
2026         ast_sip_destroy_cli();
2027         ao2_cleanup(persistent_endpoints);
2028         persistent_endpoints = NULL;
2029 }
2030
2031 int ast_res_pjsip_reload_configuration(void)
2032 {
2033         if (sip_sorcery) {
2034                 ast_sorcery_reload(sip_sorcery);
2035         }
2036         return 0;
2037 }
2038
2039 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
2040 {
2041         ast_string_field_free_memory(&subscription->mwi);
2042         ast_free(subscription->mwi.voicemail_extension);
2043 }
2044
2045 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
2046 {
2047         ast_string_field_free_memory(&info->recording);
2048 }
2049
2050 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
2051 {
2052         ast_rtp_dtls_cfg_free(&media->rtp.dtls_cfg);
2053         ast_string_field_free_memory(&media->rtp);
2054         ast_string_field_free_memory(media);
2055 }
2056
2057 static void endpoint_destructor(void* obj)
2058 {
2059         struct ast_sip_endpoint *endpoint = obj;
2060
2061         ast_string_field_free_memory(endpoint);
2062
2063         ao2_cleanup(endpoint->media.codecs);
2064         ast_stream_topology_free(endpoint->media.topology);
2065         subscription_configuration_destroy(&endpoint->subscription);
2066         info_configuration_destroy(&endpoint->info);
2067         media_configuration_destroy(&endpoint->media);
2068         ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
2069         ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
2070         ast_party_id_free(&endpoint->id.self);
2071         endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
2072         endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
2073         ao2_cleanup(endpoint->persistent);
2074         ast_variables_destroy(endpoint->channel_vars);
2075         AST_VECTOR_FREE(&endpoint->ident_method_order);
2076         ast_free(endpoint->contact_user);
2077         ast_free_acl_list(endpoint->contact_acl);
2078         ast_free_acl_list(endpoint->acl);
2079 }
2080
2081 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
2082 {
2083         return ast_string_field_init(&subscription->mwi, 64);
2084 }
2085
2086 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
2087 {
2088         return ast_string_field_init(&info->recording, 32);
2089 }
2090
2091 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
2092 {
2093         return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
2094 }
2095
2096 void *ast_sip_endpoint_alloc(const char *name)
2097 {
2098         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
2099         if (!endpoint) {
2100                 return NULL;
2101         }
2102         if (ast_string_field_init(endpoint, 64)) {
2103                 ao2_cleanup(endpoint);
2104                 return NULL;
2105         }
2106
2107         if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2108                 ao2_cleanup(endpoint);
2109                 return NULL;
2110         }
2111         if (init_subscription_configuration(&endpoint->subscription)) {
2112                 ao2_cleanup(endpoint);
2113                 return NULL;
2114         }
2115         if (init_info_configuration(&endpoint->info)) {
2116                 ao2_cleanup(endpoint);
2117                 return NULL;
2118         }
2119         if (init_media_configuration(&endpoint->media)) {
2120                 ao2_cleanup(endpoint);
2121                 return NULL;
2122         }
2123
2124         ast_party_id_init(&endpoint->id.self);
2125         endpoint->id.self.tag = ast_strdup("");
2126
2127         if (AST_VECTOR_INIT(&endpoint->ident_method_order, 1)) {
2128                 return NULL;
2129         }
2130
2131         return endpoint;
2132 }
2133
2134 struct ao2_container *ast_sip_get_endpoints(void)
2135 {
2136         struct ao2_container *endpoints;
2137
2138         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2139
2140         return endpoints;
2141 }
2142
2143 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
2144 {
2145         RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
2146         return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
2147                 sip_sorcery, "endpoint", name);
2148 }
2149
2150 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
2151 {
2152         int i;
2153
2154         for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
2155                 /* Using AST_VECTOR_GET is safe since the vector is immutable */
2156                 const char *name = AST_VECTOR_GET(auths, i);
2157                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
2158                 if (!out[i]) {
2159                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
2160                         return -1;
2161                 }
2162         }
2163
2164         return 0;
2165 }
2166
2167 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
2168 {
2169         int i;
2170         for (i = 0; i < num_auths; ++i) {
2171                 ao2_cleanup(auths[i]);
2172         }
2173 }
2174
2175 struct ast_sorcery *ast_sip_get_sorcery(void)
2176 {
2177         return sip_sorcery;
2178 }