Debian Codegraph @

res/res_pjsip/pjsip_configuration.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Matt Jordan <[email protected]>
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_geolocation.h"
25 #include "asterisk/res_pjsip.h"
26 #include "include/res_pjsip_private.h"
27 #include "asterisk/res_pjsip_cli.h"
28 #include "asterisk/acl.h"
29 #include "asterisk/manager.h"
30 #include "asterisk/astobj2.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/sorcery.h"
33 #include "asterisk/callerid.h"
34 #include "asterisk/test.h"
35 #include "asterisk/statsd.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/stream.h"
38 #include "asterisk/stasis.h"
39 #include "asterisk/security_events.h"
40 #include "asterisk/res_stir_shaken.h"
41
42 /*! \brief Number of buckets for persistent endpoint information */
43 #define PERSISTENT_BUCKETS 53
44
45 /*! \brief Persistent endpoint information */
46 struct sip_persistent_endpoint {
47 /*! \brief Asterisk endpoint itself */
48 struct ast_endpoint *endpoint;
49 };
50
51 /*! \brief Container for persistent endpoint information */
52 static struct ao2_container *persistent_endpoints;
53
54 static struct ast_sorcery *sip_sorcery;
55
56 static struct stasis_subscription *acl_change_sub;
57
58 /*! \brief Hashing function for persistent endpoint information */
59 static int persistent_endpoint_hash(const void *obj, const int flags)
60 {
61 const struct sip_persistent_endpoint *persistent = obj;
62 const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
63
64 return ast_str_hash(id);
65 }
66
67 /*! \brief Comparison function for persistent endpoint information */
68 static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
69 {
70 const struct sip_persistent_endpoint *persistent1 = obj;
71 const struct sip_persistent_endpoint *persistent2 = arg;
72 const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
73
74 return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
75 }
76
77 static void endpoint_deleted_observer(const void *object)
78 {
79 const struct ast_sip_endpoint *endpoint = object;
80
81 ao2_find(persistent_endpoints, ast_endpoint_get_resource(endpoint->persistent),
82 OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
83 }
84
85 static const struct ast_sorcery_observer endpoint_observers = {
86 .deleted = endpoint_deleted_observer,
87 };
88
89 static int endpoint_acl_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
90 {
91 struct ast_sip_endpoint *endpoint = obj;
92 int error = 0;
93 int ignore;
94
95 if (ast_strlen_zero(var->value)) return 0;
96
97 if (!strncmp(var->name, "contact_", 8)) {
98 ast_append_acl(var->name + 8, var->value, &endpoint->contact_acl, &error, &ignore);
99 } else {
100 ast_append_acl(var->name, var->value, &endpoint->acl, &error, &ignore);
101 }
102
103 return error;
104 }
105
106 static int acl_to_str(const void *obj, const intptr_t *args, char **buf)
107 {
108 const struct ast_sip_endpoint *endpoint = obj;
109 struct ast_acl_list *acl_list;
110 struct ast_acl *first_acl;
111
112 if (endpoint && !ast_acl_list_is_empty(acl_list=endpoint->acl)) {
113 AST_LIST_LOCK(acl_list);
114 first_acl = AST_LIST_FIRST(acl_list);
115 if (ast_strlen_zero(first_acl->name)) {
116 *buf = "deny/permit";
117 } else {
118 *buf = first_acl->name;
119 }
120 AST_LIST_UNLOCK(acl_list);
121 }
122
123 *buf = ast_strdup(*buf);
124 return 0;
125 }
126
127 static int contact_acl_to_str(const void *obj, const intptr_t *args, char **buf)
128 {
129 const struct ast_sip_endpoint *endpoint = obj;
130 struct ast_acl_list *acl_list;
131 struct ast_acl *first_acl;
132
133 if (endpoint && !ast_acl_list_is_empty(acl_list=endpoint->contact_acl)) {
134 AST_LIST_LOCK(acl_list);
135 first_acl = AST_LIST_FIRST(acl_list);
136 if (ast_strlen_zero(first_acl->name)) {
137 *buf = "deny/permit";
138 } else {
139 *buf = first_acl->name;
140 }
141 AST_LIST_UNLOCK(acl_list);
142 }
143
144 *buf = ast_strdup(*buf);
145 return 0;
146 }
147
148 static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
149 {
150 struct ast_sip_endpoint *endpoint = obj;
151 int dtmf = ast_sip_str_to_dtmf(var->value);
152
153 if (dtmf == -1) {
154 return -1;
155 }
156
157 endpoint->dtmf = dtmf;
158 return 0;
159 }
160
161 static int dtmf_to_str(const void *obj, const intptr_t *args, char **buf)
162 {
163 const struct ast_sip_endpoint *endpoint = obj;
164 char dtmf_str[20];
165 int result = -1;
166
167 result = ast_sip_dtmf_to_str(endpoint->dtmf, dtmf_str, sizeof(dtmf_str));
168
169 if (result == 0) {
170 *buf = ast_strdup(dtmf_str);
171 } else {
172 *buf = ast_strdup("none");
173 }
174 return 0;
175 }
176
177 static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
178 {
179 struct ast_sip_endpoint *endpoint = obj;
180
181 /* clear all */
182 endpoint->extensions.flags &= ~(PJSIP_INV_SUPPORT_100REL | PJSIP_INV_REQUIRE_100REL);
183
184 if (ast_true(var->value)) {
185 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
186 endpoint->rel100 = AST_SIP_100REL_SUPPORTED;
187 } else if (!strcasecmp(var->value, "peer_supported")) {
188 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
189 endpoint->rel100 = AST_SIP_100REL_PEER_SUPPORTED;
190 } else if (!strcasecmp(var->value, "required")) {
191 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
192 endpoint->rel100 = AST_SIP_100REL_REQUIRED;
193 } else if (ast_false(var->value)) {
194 endpoint->rel100 = AST_SIP_100REL_UNSUPPORTED;
195 } else {
196 return -1;
197 }
198
199 return 0;
200 }
201
202 static int prack_to_str(const void *obj, const intptr_t *args, char **buf)
203 {
204 const struct ast_sip_endpoint *endpoint = obj;
205
206 if (endpoint->rel100 == AST_SIP_100REL_SUPPORTED) {
207 *buf = "yes";
208 } else if (endpoint->rel100 == AST_SIP_100REL_PEER_SUPPORTED) {
209 *buf = "peer_supported";
210 } else if (endpoint->rel100 == AST_SIP_100REL_REQUIRED) {
211 *buf = "required";
212 } else {
213 *buf = "no";
214 }
215
216 *buf = ast_strdup(*buf);
217 return 0;
218 }
219
220 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
221 {
222 struct ast_sip_endpoint *endpoint = obj;
223
224 /* clear all */
225 endpoint->extensions.flags &= ~(PJSIP_INV_SUPPORT_TIMER | PJSIP_INV_REQUIRE_TIMER
226 | PJSIP_INV_ALWAYS_USE_TIMER);
227
228 /* set only the specified flag and let pjsip normalize if needed */
229 if (ast_true(var->value)) {
230 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
231 } else if (!strcasecmp(var->value, "required")) {
232 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
233 } else if (!strcasecmp(var->value, "always") || !strcasecmp(var->value, "forced")) {
234 endpoint->extensions.flags |= (PJSIP_INV_SUPPORT_TIMER | PJSIP_INV_ALWAYS_USE_TIMER);
235 } else if (!ast_false(var->value)) {
236 return -1;
237 }
238
239 return 0;
240 }
241
242 static int timers_to_str(const void *obj, const intptr_t *args, char **buf)
243 {
244 const struct ast_sip_endpoint *endpoint = obj;
245
246 if (endpoint->extensions.flags & PJSIP_INV_ALWAYS_USE_TIMER) {
247 *buf = "always";
248 } else if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_TIMER) {
249 *buf = "required";
250 } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_TIMER) {
251 *buf = "yes";
252 } else {
253 *buf = "no";
254 }
255
256 *buf = ast_strdup(*buf);
257 return 0;
258 }
259
260 static int security_mechanism_to_str(const void *obj, const intptr_t *args, char **buf)
261 {
262 const struct ast_sip_endpoint *endpoint = obj;
263
264 return ast_sip_security_mechanisms_to_str(&endpoint->security_mechanisms, 0, buf);
265 }
266
267 static int security_mechanism_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
268 {
269 struct ast_sip_endpoint *endpoint = obj;
270
271 return ast_sip_security_mechanism_vector_init(&endpoint->security_mechanisms, var->value);
272 }
273
274 static const char *security_negotiation_map[] = {
275 [AST_SIP_SECURITY_NEG_NONE] = "no",
276 [AST_SIP_SECURITY_NEG_MEDIASEC] = "mediasec",
277 };
278
279 static int security_negotiation_to_str(const void *obj, const intptr_t *args, char **buf)
280 {
281 const struct ast_sip_endpoint *endpoint = obj;
282 if (ARRAY_IN_BOUNDS(endpoint->security_negotiation, security_negotiation_map)) {
283 *buf = ast_strdup(security_negotiation_map[endpoint->security_negotiation]);
284 }
285 return 0;
286 }
287
288 int ast_sip_set_security_negotiation(enum ast_sip_security_negotiation *security_negotiation, const char *val) {
289 if (!strcasecmp("no", val)) {
290 *security_negotiation = AST_SIP_SECURITY_NEG_NONE;
291 } else if (!strcasecmp("mediasec", val)) {
292 *security_negotiation = AST_SIP_SECURITY_NEG_MEDIASEC;
293 } else {
294 return -1;
295 }
296 return 0;
297 }
298
299 static int security_negotiation_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_set_security_negotiation(&endpoint->security_negotiation, var->value);
304 }
305
306 void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
307 {
308 int i;
309 size_t size;
310
311 if (!auths) {
312 return;
313 }
314
315 size = AST_VECTOR_SIZE(auths);
316
317 for (i = 0; i < size; ++i) {
318 const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
319 ast_free((char *) name);
320 }
321 AST_VECTOR_FREE(auths);
322 }
323
324 int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
325 {
326 char *auth_names = ast_strdupa(value);
327 char *val;
328
329 ast_assert(auths != NULL);
330
331 if (AST_VECTOR_SIZE(auths)) {
332 ast_sip_auth_vector_destroy(auths);
333 }
334 if (AST_VECTOR_INIT(auths, 1)) {
335 return -1;
336 }
337
338 while ((val = ast_strip(strsep(&auth_names, ",")))) {
339 if (ast_strlen_zero(val)) {
340 continue;
341 }
342
343 val = ast_strdup(val);
344 if (!val) {
345 goto failure;
346 }
347 if (AST_VECTOR_APPEND(auths, val)) {
348 ast_free(val);
349
350 goto failure;
351 }
352 }
353 return 0;
354
355 failure:
356 ast_sip_auth_vector_destroy(auths);
357 return -1;
358 }
359
360 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
361 {
362 struct ast_sip_endpoint *endpoint = obj;
363
364 return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
365 }
366
367 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
368 {
369 struct ast_sip_endpoint *endpoint = obj;
370
371 return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
372 }
373
374 int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
375 {
376 if (!auths || !AST_VECTOR_SIZE(auths)) {
377 return 0;
378 }
379
380 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
381 return -1;
382 }
383
384 /* I feel like accessing the vector's elem array directly is cheating...*/
385 ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
386 return 0;
387 }
388
389 static int inbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
390 {
391 const struct ast_sip_endpoint *endpoint = obj;
392 return ast_sip_auths_to_str(&endpoint->inbound_auths, buf);
393 }
394
395 static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
396 {
397 const struct ast_sip_endpoint *endpoint = obj;
398 return ast_sip_auths_to_str(&endpoint->outbound_auths, buf);
399 }
400
401 /*!
402 * \internal
403 * \brief Convert identify_by method to string.
404 *
405 * \param method Method value to convert to string
406 *
407 * \return String representation.
408 */
409 static const char *sip_endpoint_identifier_type2str(enum ast_sip_endpoint_identifier_type method)
410 {
411 const char *str = "<unknown>";
412
413 switch (method) {
414 case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME:
415 str = "username";
416 break;
417 case AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME:
418 str = "auth_username";
419 break;
420 case AST_SIP_ENDPOINT_IDENTIFY_BY_IP:
421 str = "ip";
422 break;
423 case AST_SIP_ENDPOINT_IDENTIFY_BY_HEADER:
424 str = "header";
425 break;
426 case AST_SIP_ENDPOINT_IDENTIFY_BY_REQUEST_URI:
427 str = "request_uri";
428 break;
429 }
430 return str;
431 }
432
433 /*!
434 * \internal
435 * \brief Convert string to an endpoint identifier token.
436 *
437 * \param str String to convert
438 *
439 * \retval enum ast_sip_endpoint_identifier_type token value on success.
440 * \retval -1 on failure.
441 */
442 static int sip_endpoint_identifier_str2type(const char *str)
443 {
444 int method;
445
446 if (!strcasecmp(str, "username")) {
447 method = AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
448 } else if (!strcasecmp(str, "auth_username")) {
449 method = AST_SIP_ENDPOINT_IDENTIFY_BY_AUTH_USERNAME;
450 } else if (!strcasecmp(str, "ip")) {
451 method = AST_SIP_ENDPOINT_IDENTIFY_BY_IP;
452 } else if (!strcasecmp(str, "header")) {
453 method = AST_SIP_ENDPOINT_IDENTIFY_BY_HEADER;
454 } else if (!strcasecmp(str, "request_uri")) {
455 method = AST_SIP_ENDPOINT_IDENTIFY_BY_REQUEST_URI;
456 } else {
457 method = -1;
458 }
459 return method;
460 }
461
462 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
463 {
464 struct ast_sip_endpoint *endpoint = obj;
465 char *idents = ast_strdupa(var->value);
466 char *val;
467 int method;
468
469 /*
470 * If there's already something in the vector when we get here,
471 * it's the default value so we need to clean it out.
472 */
473 if (AST_VECTOR_SIZE(&endpoint->ident_method_order)) {
474 AST_VECTOR_RESET(&endpoint->ident_method_order, AST_VECTOR_ELEM_CLEANUP_NOOP);
475 endpoint->ident_method = 0;
476 }
477
478 while ((val = ast_strip(strsep(&idents, ",")))) {
479 if (ast_strlen_zero(val)) {
480 continue;
481 }
482
483 method = sip_endpoint_identifier_str2type(val);
484 if (method == -1) {
485 ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
486 val, ast_sorcery_object_get_id(endpoint));
487 AST_VECTOR_RESET(&endpoint->ident_method_order, AST_VECTOR_ELEM_CLEANUP_NOOP);
488 endpoint->ident_method = 0;
489 return -1;
490 }
491 if (endpoint->ident_method & method) {
492 /* We are already identifying by this method. No need to do it again. */
493 continue;
494 }
495
496 endpoint->ident_method |= method;
497 AST_VECTOR_APPEND(&endpoint->ident_method_order, method);
498 }
499
500 return 0;
501 }
502
503 static int ident_to_str(const void *obj, const intptr_t *args, char **buf)
504 {
505 const struct ast_sip_endpoint *endpoint = obj;
506 int methods;
507 int idx;
508 int buf_used = 0;
509 int buf_size = MAX_OBJECT_FIELD;
510
511 methods = AST_VECTOR_SIZE(&endpoint->ident_method_order);
512 if (!methods) {
513 return 0;
514 }
515
516 *buf = ast_malloc(buf_size);
517 if (!*buf) {
518 return -1;
519 }
520
521 for (idx = 0; idx < methods; ++idx) {
522 enum ast_sip_endpoint_identifier_type method;
523 const char *method_str;
524
525 method = AST_VECTOR_GET(&endpoint->ident_method_order, idx);
526 method_str = sip_endpoint_identifier_type2str(method);
527
528 /* Should never have an "<unknown>" method string */
529 ast_assert(strcmp(method_str, "<unknown>"));
530 if (!strcmp(method_str, "<unknown>")) {
531 continue;
532 }
533
534 buf_used += snprintf(*buf + buf_used, buf_size - buf_used, "%s%s",
535 method_str, idx < methods - 1 ? "," : "");
536 if (buf_size <= buf_used) {
537 /* Need more room than available, truncating. */
538 *(*buf + (buf_size - 1)) = '\0';
539 ast_log(LOG_WARNING, "Truncated identify_by string: %s\n", *buf);
540 break;
541 }
542 }
543
544 return 0;
545 }
546
547 static int media_address_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
548 {
549 struct ast_sip_endpoint *endpoint = obj;
550 struct ast_sockaddr addr;
551
552 if (ast_sockaddr_parse(&addr, var->value, 0)) {
553 /* If we're able to parse as an IP, ensure it's formatted correctly for later */
554 ast_string_field_set(endpoint, media.address, ast_sockaddr_stringify_addr_remote(&addr));
555 } else {
556 /* If we weren't able to parse it as an IP, just assume it's a hostname */
557 ast_string_field_set(endpoint, media.address, var->value);
558 }
559
560 return 0;
561 }
562
563 static int media_address_to_str(const void *obj, const intptr_t *args, char **buf)
564 {
565 const struct ast_sip_endpoint *endpoint = obj;
566 *buf = ast_strdup(endpoint->media.address);
567 return 0;
568 }
569
570 static int redirect_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
571 {
572 struct ast_sip_endpoint *endpoint = obj;
573
574 if (!strcasecmp(var->value, "user")) {
575 endpoint->redirect_method = AST_SIP_REDIRECT_USER;
576 } else if (!strcasecmp(var->value, "uri_core")) {
577 endpoint->redirect_method = AST_SIP_REDIRECT_URI_CORE;
578 } else if (!strcasecmp(var->value, "uri_pjsip")) {
579 endpoint->redirect_method = AST_SIP_REDIRECT_URI_PJSIP;
580 } else {
581 ast_log(LOG_ERROR, "Unrecognized redirect method %s specified for endpoint %s\n",
582 var->value, ast_sorcery_object_get_id(endpoint));
583 return -1;
584 }
585
586 return 0;
587 }
588
589 static const char *redirect_method_map[] = {
590 [AST_SIP_REDIRECT_USER] = "user",
591 [AST_SIP_REDIRECT_URI_CORE] = "uri_core",
592 [AST_SIP_REDIRECT_URI_PJSIP] = "uri_pjsip",
593 };
594
595 static int redirect_method_to_str(const void *obj, const intptr_t *args, char **buf)
596 {
597 const struct ast_sip_endpoint *endpoint = obj;
598 if (ARRAY_IN_BOUNDS(endpoint->redirect_method, redirect_method_map)) {
599 *buf = ast_strdup(redirect_method_map[endpoint->redirect_method]);
600 }
601 return 0;
602 }
603
604 /*!
605 * \brief Mapping of SIP method names to their corresponding redirect flags
606 */
607 struct redirect_method_map {
608 const char *method_name;
609 enum ast_sip_redirect_method flag;
610 };
611
612 static const struct redirect_method_map redirect_method_mappings[] = {
613 { "message", AST_SIP_REDIRECT_METHOD_MESSAGE },
614 };
615
616 static int follow_redirect_methods_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
617 {
618 struct ast_sip_endpoint *endpoint = obj;
619 char *methods;
620 char *method;
621 int i;
622
623 /* Clear any existing flags */
624 ast_clear_flag(&endpoint->follow_redirect_methods, ~0);
625
626 if (ast_strlen_zero(var->value)) {
627 return 0;
628 }
629
630 methods = ast_strdupa(var->value);
631 while ((method = ast_strsep(&methods, ',', AST_STRSEP_TRIM))) {
632 int found = 0;
633
634 /* Look up the method in our mapping table */
635 for (i = 0; i < ARRAY_LEN(redirect_method_mappings); i++) {
636 if (!strcasecmp(method, redirect_method_mappings[i].method_name)) {
637 ast_set_flag(&endpoint->follow_redirect_methods, redirect_method_mappings[i].flag);
638 found = 1;
639 break;
640 }
641 }
642
643 if (!found) {
644 ast_log(LOG_ERROR, "Unrecognized SIP method '%s' for follow_redirect_methods on endpoint %s\n",
645 method, ast_sorcery_object_get_id(endpoint));
646 return -1;
647 }
648 }
649
650 return 0;
651 }
652
653 static int follow_redirect_methods_to_str(const void *obj, const intptr_t *args, char **buf)
654 {
655 const struct ast_sip_endpoint *endpoint = obj;
656 struct ast_str *str = ast_str_create(64);
657 int first = 1;
658 int i;
659
660 if (!str) {
661 return -1;
662 }
663
664 /* Iterate through all supported methods and append if flag is set */
665 for (i = 0; i < ARRAY_LEN(redirect_method_mappings); i++) {
666 if (ast_test_flag(&endpoint->follow_redirect_methods, redirect_method_mappings[i].flag)) {
667 ast_str_append(&str, 0, "%s%s", first ? "" : ",", redirect_method_mappings[i].method_name);
668 first = 0;
669 }
670 }
671
672 *buf = ast_strdup(ast_str_buffer(str));
673 ast_free(str);
674
675 return 0;
676 }
677
678 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
679 {
680 struct ast_sip_endpoint *endpoint = obj;
681
682 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
683 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
684 } else if (!strcasecmp(var->value, "update")) {
685 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
686 } else {
687 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
688 var->value, var->name, ast_sorcery_object_get_id(endpoint));
689 return -1;
690 }
691 return 0;
692 }
693
694 static const char *id_configuration_refresh_methods[] = {
695 [AST_SIP_SESSION_REFRESH_METHOD_INVITE] = "invite",
696 [AST_SIP_SESSION_REFRESH_METHOD_UPDATE] = "update"
697 };
698
699 static int direct_media_method_to_str(const void *obj, const intptr_t *args, char **buf)
700 {
701 const struct ast_sip_endpoint *endpoint = obj;
702 if (ARRAY_IN_BOUNDS(endpoint->id.refresh_method, id_configuration_refresh_methods)) {
703 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
704 }
705 return 0;
706 }
707
708 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
709 {
710 struct ast_sip_endpoint *endpoint = obj;
711
712 if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
713 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
714 } else if (!strcasecmp(var->value, "update")) {
715 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
716 } else {
717 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
718 var->value, var->name, ast_sorcery_object_get_id(endpoint));
719 return -1;
720 }
721 return 0;
722 }
723
724 static int connected_line_method_to_str(const void *obj, const intptr_t *args, char **buf)
725 {
726 const struct ast_sip_endpoint *endpoint = obj;
727 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
728 return 0;
729 }
730
731 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
732 {
733 struct ast_sip_endpoint *endpoint = obj;
734
735 if (!strcasecmp(var->value, "none")) {
736 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
737 } else if (!strcasecmp(var->value, "outgoing")) {
738 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
739 } else if (!strcasecmp(var->value, "incoming")) {
740 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
741 } else {
742 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
743 var->value, var->name, ast_sorcery_object_get_id(endpoint));
744 return -1;
745 }
746
747 return 0;
748 }
749
750 static const char *direct_media_glare_mitigation_map[] = {
751 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE] = "none",
752 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING] = "outgoing",
753 [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING] = "incoming"
754 };
755
756 static int direct_media_glare_mitigation_to_str(const void *obj, const intptr_t *args, char **buf)
757 {
758 const struct ast_sip_endpoint *endpoint = obj;
759 if (ARRAY_IN_BOUNDS(endpoint->media.direct_media.glare_mitigation, direct_media_glare_mitigation_map)) {
760 *buf = ast_strdup(direct_media_glare_mitigation_map[endpoint->media.direct_media.glare_mitigation]);
761 }
762
763 return 0;
764 }
765
766 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
767 {
768 struct ast_sip_endpoint *endpoint = obj;
769 char cid_name[80] = { '\0' };
770 char cid_num[80] = { '\0' };
771
772 ast_free(endpoint->id.self.name.str);
773 endpoint->id.self.name.str = NULL;
774 endpoint->id.self.name.valid = 0;
775 ast_free(endpoint->id.self.number.str);
776 endpoint->id.self.number.str = NULL;
777 endpoint->id.self.number.valid = 0;
778
779 ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
780 if (!ast_strlen_zero(cid_name)) {
781 endpoint->id.self.name.str = ast_strdup(cid_name);
782 if (!endpoint->id.self.name.str) {
783 return -1;
784 }
785 endpoint->id.self.name.valid = 1;
786 }
787 if (!ast_strlen_zero(cid_num)) {
788 endpoint->id.self.number.str = ast_strdup(cid_num);
789 if (!endpoint->id.self.number.str) {
790 return -1;
791 }
792 endpoint->id.self.number.valid = 1;
793 }
794 return 0;
795 }
796
797 static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
798 {
799 const struct ast_sip_endpoint *endpoint = obj;
800 const char *name = S_COR(endpoint->id.self.name.valid,
801 endpoint->id.self.name.str, NULL);
802 const char *number = S_COR(endpoint->id.self.number.valid,
803 endpoint->id.self.number.str, NULL);
804
805 /* make sure size is at least 10 - that should cover the "<unknown>"
806 case as well as any additional formatting characters added in
807 the name and/or number case. */
808 int size = 10;
809 size += name ? strlen(name) : 0;
810 size += number ? strlen(number) : 0;
811
812 if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
813 return -1;
814 }
815
816 ast_callerid_merge(*buf, size + 1, name, number, NULL);
817 return 0;
818 }
819
820 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
821 {
822 struct ast_sip_endpoint *endpoint = obj;
823 int callingpres = ast_parse_caller_presentation(var->value);
824 if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
825 return -1;
826 }
827 endpoint->id.self.number.presentation = callingpres;
828 endpoint->id.self.name.presentation = callingpres;
829 return 0;
830 }
831
832 static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
833 {
834 const struct ast_sip_endpoint *endpoint = obj;
835 const char *presentation = ast_named_caller_presentation(
836 endpoint->id.self.name.presentation);
837
838 *buf = ast_strdup(presentation);
839 return 0;
840 }
841
842 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
843 {
844 struct ast_sip_endpoint *endpoint = obj;
845
846 ast_free(endpoint->id.self.tag);
847 endpoint->id.self.tag = ast_strdup(var->value);
848
849 return endpoint->id.self.tag ? 0 : -1;
850 }
851
852 static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
853 {
854 const struct ast_sip_endpoint *endpoint = obj;
855 *buf = ast_strdup(endpoint->id.self.tag);
856 return 0;
857 }
858
859 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
860 {
861 struct ast_sip_endpoint *endpoint = obj;
862
863 if (!strcasecmp("no", var->value)) {
864 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
865 } else if (!strcasecmp("sdes", var->value)) {
866 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
867 } else if (!strcasecmp("dtls", var->value)) {
868 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
869 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
870 } else {
871 return -1;
872 }
873
874 return 0;
875 }
876
877 static const char *media_encryption_map[] = {
878 [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
879 [AST_SIP_MEDIA_ENCRYPT_NONE] = "no",
880 [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
881 [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
882 };
883
884 static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
885 {
886 const struct ast_sip_endpoint *endpoint = obj;
887 if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
888 *buf = ast_strdup(media_encryption_map[
889 endpoint->media.rtp.encryption]);
890 }
891 return 0;
892 }
893
894 static int stir_shaken_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
895 {
896 struct ast_sip_endpoint *endpoint = obj;
897
898 ast_log(LOG_WARNING, "Endpoint %s: Option 'stir_shaken' is no longer supported. Use 'stir_shaken_profile' instead.\n",
899 ast_sorcery_object_get_id(endpoint));
900 endpoint->stir_shaken = 0;
901
902 return 0;
903 }
904
905 static int stir_shaken_to_str(const void *obj, const intptr_t *args, char **buf)
906 {
907 *buf = ast_strdup("no");
908
909 return 0;
910 }
911
912 static int group_handler(const struct aco_option *opt,
913 struct ast_variable *var, void *obj)
914 {
915 struct ast_sip_endpoint *endpoint = obj;
916
917 if (!strncmp(var->name, "call_group", 10)) {
918 endpoint->pickup.callgroup = ast_get_group(var->value);
919 } else if (!strncmp(var->name, "pickup_group", 12)) {
920 endpoint->pickup.pickupgroup = ast_get_group(var->value);
921 } else {
922 return -1;
923 }
924
925 return 0;
926 }
927
928 static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
929 {
930 const struct ast_sip_endpoint *endpoint = obj;
931
932 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
933 return -1;
934 }
935
936 ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
937 return 0;
938 }
939
940 static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
941 {
942 const struct ast_sip_endpoint *endpoint = obj;
943
944 if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
945 return -1;
946 }
947
948 ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
949 return 0;
950 }
951
952 static int named_groups_handler(const struct aco_option *opt,
953 struct ast_variable *var, void *obj)
954 {
955 struct ast_sip_endpoint *endpoint = obj;
956
957 if (!strncmp(var->name, "named_call_group", 16)) {
958 if (ast_strlen_zero(var->value)) {
959 endpoint->pickup.named_callgroups =
960 ast_unref_namedgroups(endpoint->pickup.named_callgroups);
961 } else if (!(endpoint->pickup.named_callgroups =
962 ast_get_namedgroups(var->value))) {
963 return -1;
964 }
965 } else if (!strncmp(var->name, "named_pickup_group", 18)) {
966 if (ast_strlen_zero(var->value)) {
967 endpoint->pickup.named_pickupgroups =
968 ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
969 } else if (!(endpoint->pickup.named_pickupgroups =
970 ast_get_namedgroups(var->value))) {
971 return -1;
972 }
973 } else {
974 return -1;
975 }
976
977 return 0;
978 }
979
980 static int named_callgroups_to_str(const void *obj, const intptr_t *args, char **buf)
981 {
982 const struct ast_sip_endpoint *endpoint = obj;
983 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
984
985 ast_print_namedgroups(&str, endpoint->pickup.named_callgroups);
986 *buf = ast_strdup(ast_str_buffer(str));
987 return 0;
988 }
989
990 static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
991 {
992 const struct ast_sip_endpoint *endpoint = obj;
993 RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
994
995 ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
996 *buf = ast_strdup(ast_str_buffer(str));
997 return 0;
998 }
999
1000 static int dtls_handler(const struct aco_option *opt,
1001 struct ast_variable *var, void *obj)
1002 {
1003 struct ast_sip_endpoint *endpoint = obj;
1004 char *name = ast_strdupa(var->name);
1005 char *front = NULL;
1006 char *back = NULL;
1007 char *buf = name;
1008
1009 /* strip out underscores in the name */
1010 front = strtok_r(buf, "_", &back);
1011 while (front) {
1012 int size = strlen(front);
1013 ast_copy_string(buf, front, size + 1);
1014 buf += size;
1015 front = strtok_r(NULL, "_", &back);
1016 }
1017
1018 return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
1019 }
1020
1021 static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
1022 {
1023 const struct ast_sip_endpoint *endpoint = obj;
1024 if (endpoint->media.rtp.dtls_cfg.verify == AST_RTP_DTLS_VERIFY_NONE) {
1025 *buf = ast_strdup("No");
1026 } else if (endpoint->media.rtp.dtls_cfg.verify == AST_RTP_DTLS_VERIFY_FINGERPRINT) {
1027 *buf = ast_strdup("Fingerprint");
1028 } else if (endpoint->media.rtp.dtls_cfg.verify == AST_RTP_DTLS_VERIFY_CERTIFICATE) {
1029 *buf = ast_strdup("Certificate");
1030 } else if (endpoint->media.rtp.dtls_cfg.verify == (AST_RTP_DTLS_VERIFY_FINGERPRINT|AST_RTP_DTLS_VERIFY_CERTIFICATE)) {
1031 *buf = ast_strdup("Yes");
1032 } else {
1033 *buf = NULL;
1034 ast_assert(0);
1035 return 1;
1036 }
1037 return 0;
1038 }
1039
1040 static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
1041 {
1042 const struct ast_sip_endpoint *endpoint = obj;
1043
1044 return ast_asprintf(
1045 buf, "%u", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
1046 }
1047
1048 static int dtlsautogeneratecert_to_str(const void *obj, const intptr_t *args, char **buf)
1049 {
1050 const struct ast_sip_endpoint *endpoint = obj;
1051 *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.ephemeral_cert));
1052 return 0;
1053 }
1054
1055 static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
1056 {
1057 const struct ast_sip_endpoint *endpoint = obj;
1058 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
1059 return 0;
1060 }
1061
1062 static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
1063 {
1064 const struct ast_sip_endpoint *endpoint = obj;
1065 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
1066 return 0;
1067 }
1068
1069 static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
1070 {
1071 const struct ast_sip_endpoint *endpoint = obj;
1072 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
1073 return 0;
1074 }
1075
1076 static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
1077 {
1078 const struct ast_sip_endpoint *endpoint = obj;
1079 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
1080 return 0;
1081 }
1082
1083 static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
1084 {
1085 const struct ast_sip_endpoint *endpoint = obj;
1086 *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
1087 return 0;
1088 }
1089
1090 static const char *ast_rtp_dtls_setup_map[] = {
1091 [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
1092 [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
1093 [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
1094 [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
1095 };
1096
1097 static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
1098 {
1099 const struct ast_sip_endpoint *endpoint = obj;
1100 if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
1101 *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
1102 }
1103 return 0;
1104 }
1105
1106 static const char *ast_rtp_dtls_fingerprint_map[] = {
1107 [AST_RTP_DTLS_HASH_SHA256] = "SHA-256",
1108 [AST_RTP_DTLS_HASH_SHA1] = "SHA-1",
1109 };
1110
1111 static int dtlsfingerprint_to_str(const void *obj, const intptr_t *args, char **buf)
1112 {
1113 const struct ast_sip_endpoint *endpoint = obj;
1114 if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.hash, ast_rtp_dtls_fingerprint_map)) {
1115 *buf = ast_strdup(ast_rtp_dtls_fingerprint_map[endpoint->media.rtp.dtls_cfg.hash]);
1116 }
1117 return 0;
1118 }
1119
1120 static int t38udptl_ec_handler(const struct aco_option *opt,
1121 struct ast_variable *var, void *obj)
1122 {
1123 struct ast_sip_endpoint *endpoint = obj;
1124
1125 if (!strcmp(var->value, "none")) {
1126 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
1127 } else if (!strcmp(var->value, "fec")) {
1128 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
1129 } else if (!strcmp(var->value, "redundancy")) {
1130 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
1131 } else {
1132 return -1;
1133 }
1134
1135 return 0;
1136 }
1137
1138 static const char *ast_t38_ec_modes_map[] = {
1139 [UDPTL_ERROR_CORRECTION_NONE] = "none",
1140 [UDPTL_ERROR_CORRECTION_FEC] = "fec",
1141 [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
1142 };
1143
1144 static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
1145 {
1146 const struct ast_sip_endpoint *endpoint = obj;
1147 if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
1148 *buf = ast_strdup(ast_t38_ec_modes_map[
1149 endpoint->media.t38.error_correction]);
1150 }
1151 return 0;
1152 }
1153
1154 static int tos_handler(const struct aco_option *opt,
1155 struct ast_variable *var, void *obj)
1156 {
1157 struct ast_sip_endpoint *endpoint = obj;
1158 unsigned int value;
1159
1160 if (ast_str2tos(var->value, &value)) {
1161 ast_log(LOG_ERROR, "Error configuring endpoint '%s' - Could not "
1162 "interpret '%s' value '%s'\n",
1163 ast_sorcery_object_get_id(endpoint), var->name, var->value);
1164 return -1;
1165 }
1166
1167 if (!strcmp(var->name, "tos_audio")) {
1168 endpoint->media.tos_audio = value;
1169 } else if (!strcmp(var->name, "tos_video")) {
1170 endpoint->media.tos_video = value;
1171 } else {
1172 /* If we reach this point, someone called the tos_handler when they shouldn't have. */
1173 ast_assert(0);
1174 return -1;
1175 }
1176 return 0;
1177 }
1178
1179 static int tos_audio_to_str(const void *obj, const intptr_t *args, char **buf)
1180 {
1181 const struct ast_sip_endpoint *endpoint = obj;
1182
1183 if (ast_asprintf(buf, "%u", endpoint->media.tos_audio) == -1) {
1184 return -1;
1185 }
1186 return 0;
1187 }
1188
1189 static int tos_video_to_str(const void *obj, const intptr_t *args, char **buf)
1190 {
1191 const struct ast_sip_endpoint *endpoint = obj;
1192
1193 if (ast_asprintf(buf, "%u", endpoint->media.tos_video) == -1) {
1194 return -1;
1195 }
1196 return 0;
1197 }
1198
1199 static int from_user_handler(const struct aco_option *opt,
1200 struct ast_variable *var, void *obj)
1201 {
1202 struct ast_sip_endpoint *endpoint = obj;
1203 /* Valid non-alphanumeric characters for URI */
1204 char *valid_uri_marks = "-._~%!$&'()*+,;=:";
1205 const char *val;
1206
1207 for (val = var->value; *val; val++) {
1208 if (!isalpha(*val) && !isdigit(*val) && !strchr(valid_uri_marks, *val)) {
1209 ast_log(LOG_ERROR, "Error configuring endpoint '%s' - '%s' field "
1210 "contains invalid character '%c'\n",
1211 ast_sorcery_object_get_id(endpoint), var->name, *val);
1212 return -1;
1213 }
1214 }
1215
1216 ast_string_field_set(endpoint, fromuser, var->value);
1217
1218 return 0;
1219 }
1220
1221 static int from_user_to_str(const void *obj, const intptr_t *args, char **buf)
1222 {
1223 const struct ast_sip_endpoint *endpoint = obj;
1224
1225 *buf = ast_strdup(endpoint->fromuser);
1226
1227 return 0;
1228 }
1229
1230 static int set_var_handler(const struct aco_option *opt,
1231 struct ast_variable *var, void *obj)
1232 {
1233 struct ast_sip_endpoint *endpoint = obj;
1234 struct ast_variable *new_var;
1235 char *name;
1236 char *val;
1237
1238 if (ast_strlen_zero(var->value)) {
1239 return 0;
1240 }
1241
1242 name = ast_strdupa(var->value);
1243 val = strchr(name, '=');
1244
1245 if (!val) {
1246 return -1;
1247 }
1248
1249 *val++ = '\0';
1250
1251 if (!(new_var = ast_variable_new(name, val, ""))) {
1252 return -1;
1253 }
1254
1255 if (ast_variable_list_replace(&endpoint->channel_vars, new_var)) {
1256 ast_variable_list_append(&endpoint->channel_vars, new_var);
1257 }
1258
1259 return 0;
1260 }
1261
1262 static int set_var_to_str(const void *obj, const intptr_t *args, char **buf)
1263 {
1264 struct ast_str *str = ast_str_create(MAX_OBJECT_FIELD);
1265 const struct ast_sip_endpoint *endpoint = obj;
1266 struct ast_variable *var;
1267
1268 for (var = endpoint->channel_vars; var; var = var->next) {
1269 ast_str_append(&str, 0, "%s=%s,", var->name, var->value);
1270 }
1271
1272 *buf = ast_strdup(ast_str_truncate(str, -1));
1273 ast_free(str);
1274 return 0;
1275 }
1276
1277 static int set_var_to_vl(const void *obj, struct ast_variable **fields)
1278 {
1279 const struct ast_sip_endpoint *endpoint = obj;
1280 if (endpoint->channel_vars) {
1281 *fields = ast_variables_dup(endpoint->channel_vars);
1282 }
1283 return 0;
1284 }
1285
1286 static int voicemail_extension_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1287 {
1288 struct ast_sip_endpoint *endpoint = obj;
1289
1290 ast_free(endpoint->subscription.mwi.voicemail_extension);
1291 endpoint->subscription.mwi.voicemail_extension = ast_strdup(var->value);
1292
1293 return endpoint->subscription.mwi.voicemail_extension ? 0 : -1;
1294 }
1295
1296 static int voicemail_extension_to_str(const void *obj, const intptr_t *args, char **buf)
1297 {
1298 const struct ast_sip_endpoint *endpoint = obj;
1299
1300 *buf = ast_strdup(endpoint->subscription.mwi.voicemail_extension);
1301
1302 return 0;
1303 }
1304
1305 static int contact_user_handler(const struct aco_option *opt,
1306 struct ast_variable *var, void *obj)
1307 {
1308 struct ast_sip_endpoint *endpoint = obj;
1309
1310 ast_free(endpoint->contact_user);
1311 endpoint->contact_user = ast_strdup(var->value);
1312
1313 return endpoint->contact_user ? 0 : -1;
1314 }
1315
1316 static int contact_user_to_str(const void *obj, const intptr_t *args, char **buf)
1317 {
1318 const struct ast_sip_endpoint *endpoint = obj;
1319
1320 *buf = ast_strdup(endpoint->contact_user);
1321 if (!(*buf)) {
1322 return -1;
1323 }
1324
1325 return 0;
1326 }
1327
1328 static int call_offer_pref_handler(const struct aco_option *opt,
1329 struct ast_variable *var, void *obj)
1330 {
1331 struct ast_sip_endpoint *endpoint = obj;
1332 struct ast_flags pref = { 0, };
1333 int outgoing = strcmp(var->name, "outgoing_call_offer_pref") == 0;
1334
1335 int res = ast_sip_call_codec_str_to_pref(&pref, var->value, outgoing);
1336 if (res != 0) {
1337 return -1;
1338 }
1339
1340 if (outgoing) {
1341 endpoint->media.outgoing_call_offer_pref = pref;
1342 } else {
1343 endpoint->media.incoming_call_offer_pref = pref;
1344 }
1345
1346 return 0;
1347 }
1348
1349 static int incoming_call_offer_pref_to_str(const void *obj, const intptr_t *args, char **buf)
1350 {
1351 const struct ast_sip_endpoint *endpoint = obj;
1352
1353 *buf = ast_strdup(ast_sip_call_codec_pref_to_str(endpoint->media.incoming_call_offer_pref));
1354 if (!(*buf)) {
1355 return -1;
1356 }
1357
1358 return 0;
1359 }
1360
1361 static int outgoing_call_offer_pref_to_str(const void *obj, const intptr_t *args, char **buf)
1362 {
1363 const struct ast_sip_endpoint *endpoint = obj;
1364
1365 *buf = ast_strdup(ast_sip_call_codec_pref_to_str(endpoint->media.outgoing_call_offer_pref));
1366 if (!(*buf)) {
1367 return -1;
1368 }
1369
1370 return 0;
1371 }
1372
1373 static int codec_prefs_handler(const struct aco_option *opt,
1374 struct ast_variable *var, void *obj)
1375 {
1376 struct ast_sip_endpoint *endpoint = obj;
1377 struct ast_stream_codec_negotiation_prefs *option_prefs;
1378 struct ast_stream_codec_negotiation_prefs prefs;
1379 struct ast_str *error_message = ast_str_create(128);
1380 enum ast_stream_codec_negotiation_prefs_prefer_values default_prefer;
1381 enum ast_stream_codec_negotiation_prefs_operation_values default_operation;
1382 int res = 0;
1383
1384 res = ast_stream_codec_prefs_parse(var->value, &prefs, &error_message);
1385 if (res < 0) {
1386 ast_log(LOG_ERROR, "Endpoint '%s': %s for option '%s'\n",
1387 ast_sorcery_object_get_id(endpoint), ast_str_buffer(error_message), var->name);
1388 ast_free(error_message);
1389 return -1;
1390 }
1391 ast_free(error_message);
1392
1393 if (strcmp(var->name, "codec_prefs_incoming_offer") == 0) {
1394 if (prefs.operation == CODEC_NEGOTIATION_OPERATION_UNION) {
1395 ast_log(LOG_ERROR, "Endpoint '%s': Codec preference '%s' has invalid value '%s' for option: '%s'",
1396 ast_sorcery_object_get_id(endpoint),
1397 ast_stream_codec_param_to_str(CODEC_NEGOTIATION_PARAM_OPERATION),
1398 ast_stream_codec_operation_to_str(CODEC_NEGOTIATION_OPERATION_UNION),
1399 var->name);
1400 return -1;
1401 }
1402 option_prefs = &endpoint->media.codec_prefs_incoming_offer;
1403 default_prefer = CODEC_NEGOTIATION_PREFER_PENDING;
1404 default_operation = CODEC_NEGOTIATION_OPERATION_INTERSECT;
1405 } else if (strcmp(var->name, "codec_prefs_outgoing_offer") == 0) {
1406 option_prefs = &endpoint->media.codec_prefs_outgoing_offer;
1407 default_prefer = CODEC_NEGOTIATION_PREFER_PENDING;
1408 default_operation = CODEC_NEGOTIATION_OPERATION_UNION;
1409 } else if (strcmp(var->name, "codec_prefs_incoming_answer") == 0) {
1410 option_prefs = &endpoint->media.codec_prefs_incoming_answer;
1411 default_prefer = CODEC_NEGOTIATION_PREFER_PENDING;
1412 default_operation = CODEC_NEGOTIATION_OPERATION_INTERSECT;
1413 } else if (strcmp(var->name, "codec_prefs_outgoing_answer") == 0) {
1414 option_prefs = &endpoint->media.codec_prefs_outgoing_answer;
1415 default_prefer = CODEC_NEGOTIATION_PREFER_PENDING;
1416 default_operation = CODEC_NEGOTIATION_OPERATION_INTERSECT;
1417 } else {
1418 ast_log(LOG_ERROR, "Endpoint '%s': Unsupported option '%s'\n",
1419 ast_sorcery_object_get_id(endpoint),
1420 var->name);
1421 return -1;
1422 }
1423
1424 if (prefs.prefer == CODEC_NEGOTIATION_PREFER_UNSPECIFIED) {
1425 prefs.prefer = default_prefer;
1426 }
1427
1428 if (prefs.operation == CODEC_NEGOTIATION_OPERATION_UNSPECIFIED) {
1429 prefs.operation = default_operation;
1430 }
1431
1432 if (prefs.keep == CODEC_NEGOTIATION_KEEP_UNSPECIFIED) {
1433 prefs.keep = CODEC_NEGOTIATION_KEEP_ALL;
1434 }
1435
1436 if (prefs.transcode == CODEC_NEGOTIATION_TRANSCODE_UNSPECIFIED) {
1437 prefs.transcode = CODEC_NEGOTIATION_TRANSCODE_ALLOW;
1438 }
1439
1440 /* Now that defaults have been applied as needed we apply the full codec
1441 * preference configuration to the option.
1442 */
1443 *option_prefs = prefs;
1444
1445 return 0;
1446 }
1447
1448 static int codec_prefs_to_str(const struct ast_stream_codec_negotiation_prefs *prefs,
1449 const void *obj, const intptr_t *args, char **buf)
1450 {
1451 struct ast_str *codecs = ast_str_create(AST_STREAM_MAX_CODEC_PREFS_LENGTH);
1452
1453 if (!codecs) {
1454 return -1;
1455 }
1456
1457 *buf = ast_strdup(ast_stream_codec_prefs_to_str(prefs, &codecs));
1458 ast_free(codecs);
1459
1460 return 0;
1461 }
1462
1463 static int incoming_offer_codec_prefs_to_str(const void *obj, const intptr_t *args, char **buf)
1464 {
1465 const struct ast_sip_endpoint *endpoint = obj;
1466 return codec_prefs_to_str(&endpoint->media.codec_prefs_incoming_offer, obj, args, buf);
1467 }
1468
1469 static int outgoing_offer_codec_prefs_to_str(const void *obj, const intptr_t *args, char **buf)
1470 {
1471 const struct ast_sip_endpoint *endpoint = obj;
1472 return codec_prefs_to_str(&endpoint->media.codec_prefs_outgoing_offer, obj, args, buf);
1473 }
1474
1475 static int incoming_answer_codec_prefs_to_str(const void *obj, const intptr_t *args, char **buf)
1476 {
1477 const struct ast_sip_endpoint *endpoint = obj;
1478 return codec_prefs_to_str(&endpoint->media.codec_prefs_incoming_answer, obj, args, buf);
1479 }
1480
1481 static int outgoing_answer_codec_prefs_to_str(const void *obj, const intptr_t *args, char **buf)
1482 {
1483 const struct ast_sip_endpoint *endpoint = obj;
1484 return codec_prefs_to_str(&endpoint->media.codec_prefs_outgoing_answer, obj, args, buf);
1485 }
1486
1487 static void *sip_nat_hook_alloc(const char *name)
1488 {
1489 return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
1490 }
1491
1492 /*! \brief Destructor function for persistent endpoint information */
1493 static void persistent_endpoint_destroy(void *obj)
1494 {
1495 struct sip_persistent_endpoint *persistent = obj;
1496
1497 ast_endpoint_shutdown(persistent->endpoint);
1498 }
1499
1500 static int add_to_regcontext(void *obj, void *arg, int flags)
1501 {
1502 struct sip_persistent_endpoint *persistent = obj;
1503 const char *regcontext = arg;
1504
1505 if (ast_endpoint_get_state(persistent->endpoint) == AST_ENDPOINT_ONLINE) {
1506 if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(
1507 persistent->endpoint), 1, NULL)) {
1508 ast_add_extension(regcontext, 1, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, NULL,
1509 "Noop", ast_strdup(ast_endpoint_get_resource(persistent->endpoint)), ast_free_ptr, "PJSIP");
1510 }
1511 }
1512
1513 return 0;
1514 }
1515
1516 int ast_sip_persistent_endpoint_add_to_regcontext(const char *regcontext)
1517 {
1518 if (ast_strlen_zero(regcontext)) {
1519 return 0;
1520 }
1521
1522 /* Make sure the regcontext exists */
1523 if (!ast_context_find_or_create(NULL, NULL, regcontext, "PJSIP")) {
1524 ast_log(LOG_ERROR, "Failed to create regcontext '%s'\n", regcontext);
1525 return -1;
1526 }
1527
1528 /* Add any online endpoints */
1529 ao2_callback(persistent_endpoints, OBJ_NODATA, add_to_regcontext, (void *)regcontext);
1530 return 0;
1531 }
1532
1533 int ast_sip_persistent_endpoint_update_state(const char *endpoint_name, enum ast_endpoint_state state)
1534 {
1535 struct sip_persistent_endpoint *persistent;
1536 struct ast_json *blob;
1537 char *regcontext;
1538
1539 persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1540 if (!persistent) {
1541 return -1;
1542 }
1543
1544 /* If there was no state change, don't publish anything. */
1545 if (ast_endpoint_get_state(persistent->endpoint) == state) {
1546 ao2_ref(persistent, -1);
1547 return 0;
1548 }
1549
1550 regcontext = ast_sip_get_regcontext();
1551
1552 if (state == AST_ENDPOINT_ONLINE) {
1553 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
1554 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
1555
1556 if (!ast_strlen_zero(regcontext)) {
1557 if (!ast_exists_extension(NULL, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL)) {
1558 ast_add_extension(regcontext, 1, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, NULL,
1559 "Noop", ast_strdup(ast_endpoint_get_resource(persistent->endpoint)), ast_free_ptr, "PJSIP");
1560 }
1561 }
1562
1563 ast_verb(2, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(persistent->endpoint));
1564 } else {
1565 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1566 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
1567
1568 if (!ast_strlen_zero(regcontext)) {
1569 struct pbx_find_info q = { .stacklen = 0 };
1570
1571 if (pbx_find_extension(NULL, NULL, &q, regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL, "", E_MATCH)) {
1572 ast_context_remove_extension(regcontext, ast_endpoint_get_resource(persistent->endpoint), 1, NULL);
1573 }
1574 }
1575
1576 ast_verb(2, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(persistent->endpoint));
1577 }
1578
1579 ast_free(regcontext);
1580
1581 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
1582 ast_json_unref(blob);
1583 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
1584
1585 ao2_ref(persistent, -1);
1586
1587 return 0;
1588 }
1589
1590 void ast_sip_persistent_endpoint_publish_contact_state(const char *endpoint_name, const struct ast_sip_contact_status *contact_status)
1591 {
1592 struct sip_persistent_endpoint *persistent;
1593 struct ast_json *blob;
1594 char rtt[32];
1595
1596 persistent = ao2_find(persistent_endpoints, endpoint_name, OBJ_SEARCH_KEY);
1597 if (!persistent) {
1598 return;
1599 }
1600
1601 snprintf(rtt, sizeof(rtt), "%" PRId64, contact_status->rtt);
1602 blob = ast_json_pack("{s: s, s: s, s: s, s: s, s: s}",
1603 "contact_status", ast_sip_get_contact_status_label(contact_status->status),
1604 "aor", contact_status->aor,
1605 "uri", contact_status->uri,
1606 "roundtrip_usec", rtt,
1607 "endpoint_name", ast_endpoint_get_resource(persistent->endpoint));
1608 if (blob) {
1609 ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_contact_state_type(), blob);
1610 ast_json_unref(blob);
1611 }
1612
1613 ao2_ref(persistent, -1);
1614 }
1615
1616 /*! \brief Internal function which finds (or creates) persistent endpoint information */
1617 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
1618 {
1619 RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
1620 SCOPED_AO2LOCK(lock, persistent_endpoints);
1621
1622 persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint),
1623 OBJ_SEARCH_KEY | OBJ_NOLOCK);
1624 if (!persistent) {
1625 persistent = ao2_alloc_options(sizeof(*persistent), persistent_endpoint_destroy,
1626 AO2_ALLOC_OPT_LOCK_NOLOCK);
1627 if (!persistent) {
1628 return NULL;
1629 }
1630
1631 persistent->endpoint = ast_endpoint_create("PJSIP",
1632 ast_sorcery_object_get_id(endpoint));
1633 if (!persistent->endpoint) {
1634 return NULL;
1635 }
1636
1637 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
1638
1639 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
1640 }
1641
1642 ao2_ref(persistent->endpoint, +1);
1643 return persistent->endpoint;
1644 }
1645
1646 /*! \brief Callback function for when an object is finalized */
1647 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
1648 {
1649 struct ast_sip_endpoint *endpoint = obj;
1650
1651 if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
1652 return -1;
1653 }
1654
1655 if (endpoint->extensions.timer.min_se < 90) {
1656 ast_log(LOG_ERROR, "Session timer minimum expires time must be 90 or greater on endpoint '%s'\n",
1657 ast_sorcery_object_get_id(endpoint));
1658 return -1;
1659 } else if (endpoint->extensions.timer.sess_expires < endpoint->extensions.timer.min_se) {
1660 ast_log(LOG_ERROR, "Session timer expires must be greater than minimum session expires time on endpoint '%s'\n",
1661 ast_sorcery_object_get_id(endpoint));
1662 return -1;
1663 }
1664
1665 if (ast_strlen_zero(endpoint->media.sdpsession) || !*ast_skip_blanks(endpoint->media.sdpsession)) {
1666 ast_log(LOG_WARNING, "SDP session was set to empty on endpoint '%s'. Not permitted as per RFC8866. SDP session set to '-'. \n",
1667 ast_sorcery_object_get_id(endpoint));
1668 ast_string_field_set(endpoint, media.sdpsession, "-");
1669 }
1670
1671 if (ast_strlen_zero(endpoint->media.sdpowner) || *ast_skip_nonblanks(ast_skip_blanks(endpoint->media.sdpowner))) {
1672 ast_log(LOG_WARNING, "SDP origin username on endpoint '%s' is empty or contains spaces ('%s'). Not permitted as per RFC8866. Setting to '-'.\n",
1673 ast_sorcery_object_get_id(endpoint), endpoint->media.sdpowner);
1674 ast_string_field_set(endpoint, media.sdpowner, "-");
1675 }
1676
1677 if (ast_rtp_dtls_cfg_validate(&endpoint->media.rtp.dtls_cfg)) {
1678 return -1;
1679 }
1680
1681 if (endpoint->media.rtp.port_start || endpoint->media.rtp.port_end) {
1682 if (!endpoint->media.rtp.port_start || !endpoint->media.rtp.port_end) {
1683 ast_log(LOG_ERROR, "Endpoint '%s': Both rtp_port_start and rtp_port_end must be set together\n",
1684 ast_sorcery_object_get_id(endpoint));
1685 return -1;
1686 }
1687 if (endpoint->media.rtp.port_start < 1024 || endpoint->media.rtp.port_end < 1024) {
1688 ast_log(LOG_ERROR, "Endpoint '%s': rtp_port_start and rtp_port_end must be at least 1024\n",
1689 ast_sorcery_object_get_id(endpoint));
1690 return -1;
1691 }
1692 if (endpoint->media.rtp.port_end <= endpoint->media.rtp.port_start) {
1693 ast_log(LOG_ERROR, "Endpoint '%s': rtp_port_end (%u) must be greater than rtp_port_start (%u)\n",
1694 ast_sorcery_object_get_id(endpoint),
1695 endpoint->media.rtp.port_end,
1696 endpoint->media.rtp.port_start);
1697 return -1;
1698 }
1699 ast_debug(1, "Endpoint '%s': Using per-endpoint RTP port range %u-%u\n",
1700 ast_sorcery_object_get_id(endpoint),
1701 endpoint->media.rtp.port_start,
1702 endpoint->media.rtp.port_end);
1703 }
1704
1705 if (endpoint->preferred_codec_only) {
1706 if (endpoint->media.incoming_call_offer_pref.flags != (AST_SIP_CALL_CODEC_PREF_LOCAL | AST_SIP_CALL_CODEC_PREF_INTERSECT | AST_SIP_CALL_CODEC_PREF_ALL)) {
1707 ast_log(LOG_ERROR, "Setting both preferred_codec_only and incoming_call_offer_pref is not supported on endpoint '%s'\n",
1708 ast_sorcery_object_get_id(endpoint));
1709 return -1;
1710 }
1711 ast_clear_flag(&endpoint->media.incoming_call_offer_pref, AST_SIP_CALL_CODEC_PREF_ALL);
1712 ast_set_flag(&endpoint->media.incoming_call_offer_pref, AST_SIP_CALL_CODEC_PREF_FIRST);
1713 }
1714
1715 endpoint->media.topology = ast_stream_topology_create_from_format_cap(endpoint->media.codecs);
1716 if (!endpoint->media.topology) {
1717 return -1;
1718 }
1719
1720 endpoint->media.rtcp_mux |= endpoint->media.bundle;
1721
1722 /*
1723 * If webrtc has been enabled then enable those attributes, and default
1724 * some, that are needed in order for webrtc to work.
1725 */
1726 endpoint->media.bundle |= endpoint->media.webrtc;
1727 endpoint->media.rtcp_mux |= endpoint->media.webrtc;
1728 endpoint->media.rtp.use_avpf |= endpoint->media.webrtc;
1729 endpoint->media.rtp.ice_support |= endpoint->media.webrtc;
1730 endpoint->media.rtp.use_received_transport |= endpoint->media.webrtc;
1731
1732 if (endpoint->media.webrtc) {
1733 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
1734 endpoint->media.rtp.dtls_cfg.enabled = 1;
1735 endpoint->media.rtp.dtls_cfg.default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
1736 endpoint->media.rtp.dtls_cfg.verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
1737
1738 /* RFC8827 says: Implementations MUST NOT implement DTLS renegotiation
1739 * and MUST reject it with a "no_renegotiation" alert if offered. */
1740 if (endpoint->media.rtp.dtls_cfg.rekey) {
1741 ast_log(LOG_WARNING, "DTLS renegotiation is not supported with WebRTC. Disabling dtls_rekey.\n");
1742 endpoint->media.rtp.dtls_cfg.rekey = 0;
1743 }
1744
1745 if (ast_strlen_zero(endpoint->media.rtp.dtls_cfg.certfile)) {
1746 /* If no certificate has been specified, try to automatically create one */
1747 endpoint->media.rtp.dtls_cfg.ephemeral_cert = 1;
1748 }
1749 }
1750
1751 if (!ast_strlen_zero(endpoint->geoloc_incoming_call_profile) ||
1752 !ast_strlen_zero(endpoint->geoloc_outgoing_call_profile)) {
1753
1754 if (!ast_geoloc_is_loaded()) {
1755 ast_log(LOG_ERROR, "A geoloc incoming and/or outgoing call_profile was specified on endpoint '%s'"
1756 " but res_geolocation is not loaded.\n", ast_sorcery_object_get_id(endpoint));
1757 return -1;
1758 }
1759
1760 if (!ast_strlen_zero(endpoint->geoloc_incoming_call_profile)) {
1761 struct ast_geoloc_profile *profile = ast_geoloc_get_profile(endpoint->geoloc_incoming_call_profile);
1762 if (!profile) {
1763 ast_log(LOG_ERROR, "geoloc_incoming_call_profile '%s' on endpoint '%s' doesn't exist\n",
1764 endpoint->geoloc_incoming_call_profile, ast_sorcery_object_get_id(endpoint));
1765 return -1;
1766 }
1767 ao2_cleanup(profile);
1768 }
1769
1770 if (!ast_strlen_zero(endpoint->geoloc_outgoing_call_profile)) {
1771 struct ast_geoloc_profile *profile = ast_geoloc_get_profile(endpoint->geoloc_outgoing_call_profile);
1772 if (!profile) {
1773 ast_log(LOG_ERROR, "geoloc_outgoing_call_profile '%s' on endpoint '%s' doesn't exist\n",
1774 endpoint->geoloc_outgoing_call_profile, ast_sorcery_object_get_id(endpoint));
1775 return -1;
1776 }
1777 ao2_cleanup(profile);
1778 }
1779 }
1780
1781 return 0;
1782 }
1783
1784 const char *ast_sip_get_device_state(const struct ast_sip_endpoint *endpoint)
1785 {
1786 char device[MAX_OBJECT_FIELD];
1787
1788 snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
1789 return ast_devstate2str(ast_device_state(device));
1790 }
1791
1792 struct ast_endpoint_snapshot *ast_sip_get_endpoint_snapshot(
1793 const struct ast_sip_endpoint *endpoint)
1794 {
1795 return ast_endpoint_latest_snapshot(
1796 ast_endpoint_get_tech(endpoint->persistent),
1797 ast_endpoint_get_resource(endpoint->persistent));
1798 }
1799
1800 int ast_sip_for_each_channel_snapshot(
1801 const struct ast_endpoint_snapshot *endpoint_snapshot,
1802 ao2_callback_fn on_channel_snapshot, void *arg)
1803 {
1804 int num, num_channels = endpoint_snapshot->num_channels;
1805
1806 if (!on_channel_snapshot || !num_channels) {
1807 return 0;
1808 }
1809
1810 for (num = 0; num < num_channels; ++num) {
1811 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1812 int res;
1813
1814 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1815 if (!snapshot) {
1816 continue;
1817 }
1818
1819 res = on_channel_snapshot(snapshot, arg, 0);
1820 if (res) {
1821 return -1;
1822 }
1823 }
1824 return 0;
1825 }
1826
1827 int ast_sip_for_each_channel(
1828 const struct ast_sip_endpoint *endpoint,
1829 ao2_callback_fn on_channel_snapshot, void *arg)
1830 {
1831 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1832 return ast_sip_for_each_channel_snapshot(endpoint_snapshot, on_channel_snapshot, arg);
1833 }
1834
1835 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
1836 struct ast_str **str)
1837 {
1838
1839 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1840 ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1841
1842 ast_str_append(str, 0, "%d",
1843 endpoint_snapshot ? endpoint_snapshot->num_channels : 0);
1844 }
1845
1846 #define AMI_DEFAULT_STR_SIZE 512
1847
1848 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1849 {
1850 struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1851
1852 if (!(buf)) {
1853 astman_send_error_va(ami->s, ami->m, "Unable create event "
1854 "for %s\n", event);
1855 return NULL;
1856 }
1857
1858 ast_str_set(&buf, 0, "Event: %s\r\n", event);
1859 if (!ast_strlen_zero(ami->action_id)) {
1860 ast_str_append(&buf, 0, "ActionID: %s\r\n", ami->action_id);
1861 }
1862 return buf;
1863 }
1864
1865 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1866 {
1867 ast_str_append(buf, 0, "ObjectType: %s\r\n",
1868 ast_sorcery_object_get_type(obj));
1869 ast_str_append(buf, 0, "ObjectName: %s\r\n",
1870 ast_sorcery_object_get_id(obj));
1871 }
1872
1873 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1874 {
1875 RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create2(
1876 ast_sip_get_sorcery(), obj, AST_HANDLER_ONLY_STRING), ast_variables_destroy);
1877 struct ast_variable *i;
1878
1879 if (!objset) {
1880 return -1;
1881 }
1882
1883 sip_sorcery_object_ami_set_type_name(obj, buf);
1884
1885 for (i = objset; i; i = i->next) {
1886 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1887 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1888 }
1889
1890 return 0;
1891 }
1892
1893 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1894 {
1895 struct ast_sip_aor *aor = obj;
1896 struct ast_str **buf = arg;
1897
1898 ast_str_append(buf, 0, "Contacts: ");
1899 ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1900 ast_str_append(buf, 0, "\r\n");
1901
1902 return 0;
1903 }
1904
1905 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1906 struct ast_str **buf)
1907 {
1908 if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1909 return -1;
1910 }
1911
1912 ast_str_append(buf, 0, "DeviceState: %s\r\n",
1913 ast_sip_get_device_state(endpoint));
1914
1915 ast_str_append(buf, 0, "ActiveChannels: ");
1916 active_channels_to_str(endpoint, buf);
1917 ast_str_append(buf, 0, "\r\n");
1918
1919 return 0;
1920 }
1921
1922 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1923 struct ast_sip_ami *ami)
1924 {
1925 RAII_VAR(struct ast_str *, buf,
1926 ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1927
1928 if (!buf) {
1929 return -1;
1930 }
1931
1932 sip_endpoint_to_ami(endpoint, &buf);
1933 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1934 return 0;
1935 }
1936
1937 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1938 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1939
1940 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1941 {
1942 struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"),
1943 .count = 0, };
1944 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1945 const char *endpoint_name = astman_get_header(m, "Endpoint");
1946 int count = 0;
1947
1948 if (ast_strlen_zero(endpoint_name)) {
1949 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1950 AMI_SHOW_ENDPOINT);
1951 return 0;
1952 }
1953
1954 if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1955 endpoint_name += 6;
1956 }
1957
1958 if (!(endpoint = ast_sorcery_retrieve_by_id(
1959 ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1960 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1961 endpoint_name);
1962 return 0;
1963 }
1964
1965 astman_send_listack(s, m, "Following are Events for each object associated with the Endpoint",
1966 "start");
1967
1968 /* the endpoint detail needs to always come first so apply as such */
1969 if (format_ami_endpoint(endpoint, &ami) ||
1970 ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1971 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1972 endpoint_name);
1973 }
1974
1975 astman_send_list_complete_start(s, m, "EndpointDetailComplete", ami.count + 1);
1976 astman_send_list_complete_end(s);
1977
1978 return 0;
1979 }
1980
1981 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1982 struct ast_str **buf)
1983 {
1984 char *str = NULL;
1985 if (ast_sip_auths_to_str(auths, &str)) {
1986 return -1;
1987 }
1988 ast_str_append(buf, 0, "%s", str ? str : "");
1989 ast_free(str);
1990 return 0;
1991 }
1992
1993 static int format_ami_endpoints(void *obj, void *arg, int flags)
1994 {
1995
1996 struct ast_sip_endpoint *endpoint = obj;
1997 struct ast_sip_ami *ami = arg;
1998 RAII_VAR(struct ast_str *, buf,
1999 ast_sip_create_ami_event("EndpointList", ami), ast_free);
2000
2001 if (!buf) {
2002 return CMP_STOP;
2003 }
2004
2005 sip_sorcery_object_ami_set_type_name(endpoint, &buf);
2006 ast_str_append(&buf, 0, "Transport: %s\r\n",
2007 endpoint->transport);
2008 ast_str_append(&buf, 0, "Aor: %s\r\n",
2009 endpoint->aors);
2010
2011 ast_str_append(&buf, 0, "Auths: ");
2012 format_str_append_auth(&endpoint->inbound_auths, &buf);
2013 ast_str_append(&buf, 0, "\r\n");
2014
2015 ast_str_append(&buf, 0, "OutboundAuths: ");
2016 format_str_append_auth(&endpoint->outbound_auths, &buf);
2017 ast_str_append(&buf, 0, "\r\n");
2018
2019 ast_sip_for_each_aor(endpoint->aors,
2020 sip_endpoints_aors_ami, &buf);
2021
2022 ast_str_append(&buf, 0, "DeviceState: %s\r\n",
2023 ast_sip_get_device_state(endpoint));
2024
2025 ast_str_append(&buf, 0, "ActiveChannels: ");
2026 active_channels_to_str(endpoint, &buf);
2027 ast_str_append(&buf, 0, "\r\n");
2028
2029 astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
2030 return 0;
2031 }
2032
2033 static int ami_show_endpoints(struct mansession *s, const struct message *m)
2034 {
2035 struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
2036 RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
2037 int num;
2038
2039 endpoints = ast_sip_get_endpoints();
2040 if (!endpoints) {
2041 astman_send_error(s, m, "Could not get endpoints\n");
2042 return 0;
2043 }
2044
2045 if (!(num = ao2_container_count(endpoints))) {
2046 astman_send_error(s, m, "No endpoints found\n");
2047 return 0;
2048 }
2049
2050 astman_send_listack(s, m, "A listing of Endpoints follows, presented as EndpointList events",
2051 "start");
2052
2053 ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
2054
2055 astman_send_list_complete_start(s, m, "EndpointListComplete", num);
2056 astman_send_list_complete_end(s);
2057 return 0;
2058 }
2059
2060 static struct ao2_container *cli_endpoint_get_container(const char *regex)
2061 {
2062 RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
2063 struct ao2_container *s_container;
2064
2065 container = ast_sorcery_retrieve_by_regex(sip_sorcery, "endpoint", regex);
2066 if (!container) {
2067 return NULL;
2068 }
2069
2070 s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
2071 (void *)ast_sorcery_object_id_sort, (void *)ast_sorcery_object_id_compare);
2072 if (!s_container) {
2073 return NULL;
2074 }
2075
2076 if (ao2_container_dup(s_container, container, 0)) {
2077 ao2_ref(s_container, -1);
2078 return NULL;
2079 }
2080
2081 return s_container;
2082 }
2083
2084 static int cli_endpoint_iterate(void *obj, ao2_callback_fn callback, void *args)
2085 {
2086 ao2_callback(obj, OBJ_NODATA, callback, args);
2087
2088 return 0;
2089 }
2090
2091 static void *cli_endpoint_retrieve_by_id(const char *id)
2092 {
2093 return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", id);
2094 }
2095
2096 static void cli_endpoint_print_child_header(char *type, struct ast_sip_cli_context *context)
2097 {
2098 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
2099
2100 formatter_entry = ast_sip_lookup_cli_formatter(type);
2101 if (formatter_entry) {
2102 formatter_entry->print_header(NULL, context, 0);
2103 }
2104 }
2105
2106 static int cli_endpoint_print_header(void *obj, void *arg, int flags)
2107 {
2108 struct ast_sip_cli_context *context = arg;
2109
2110 ast_assert(context->output_buffer != NULL);
2111
2112 ast_str_append(&context->output_buffer, 0,
2113 " Endpoint: <Endpoint/CID.....................................> <State.....> <Channels.>\n");
2114
2115 if (context->recurse) {
2116 context->indent_level++;
2117 cli_endpoint_print_child_header("auth", context);
2118 cli_endpoint_print_child_header("aor", context);
2119 cli_endpoint_print_child_header("transport", context);
2120 cli_endpoint_print_child_header("identify", context);
2121 cli_endpoint_print_child_header("channel", context);
2122 context->indent_level--;
2123 }
2124
2125 return 0;
2126 }
2127
2128 static void cli_endpoint_print_child_body(char *type, const void *obj, struct ast_sip_cli_context *context)
2129 {
2130 RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
2131
2132 formatter_entry = ast_sip_lookup_cli_formatter(type);
2133 if (formatter_entry) {
2134 formatter_entry->iterate((void *)obj, formatter_entry->print_body, context);
2135 }
2136 }
2137
2138 static int cli_endpoint_print_body(void *obj, void *arg, int flags)
2139 {
2140 struct ast_sip_endpoint *endpoint = obj;
2141 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, ast_sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
2142 struct ast_sip_cli_context *context = arg;
2143 const char *id = ast_sorcery_object_get_id(endpoint);
2144 char *print_name = NULL;
2145 int print_name_len;
2146 char *number = S_COR(endpoint->id.self.number.valid,
2147 endpoint->id.self.number.str, NULL);
2148 int indent;
2149 int flexwidth;
2150
2151 ast_assert(context->output_buffer != NULL);
2152
2153 if (number) {
2154 print_name_len = strlen(id) + strlen(number) + 2;
2155 print_name = ast_alloca(print_name_len);
2156 snprintf(print_name, print_name_len, "%s/%s", id, number);
2157 }
2158
2159 indent = CLI_INDENT_TO_SPACES(context->indent_level);
2160 flexwidth = CLI_LAST_TABSTOP - indent - 2;
2161
2162 ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %-12.12s %d of %.0f\n",
2163 indent, "Endpoint",
2164 flexwidth, flexwidth, print_name ? print_name : id,
2165 ast_sip_get_device_state(endpoint),
2166 endpoint_snapshot->num_channels,
2167 (double) endpoint->devicestate_busy_at ? endpoint->devicestate_busy_at :
2168 INFINITY
2169 );
2170
2171 if (context->recurse) {
2172 context->indent_level++;
2173
2174 context->auth_direction = "Out";
2175 cli_endpoint_print_child_body("auth", &endpoint->outbound_auths, context);
2176 context->auth_direction = "In";
2177 cli_endpoint_print_child_body("auth", &endpoint->inbound_auths, context);
2178
2179 cli_endpoint_print_child_body("aor", endpoint->aors, context);
2180 cli_endpoint_print_child_body("transport", endpoint, context);
2181 cli_endpoint_print_child_body("identify", endpoint, context);
2182 cli_endpoint_print_child_body("channel", endpoint, context);
2183
2184 context->indent_level--;
2185
2186 if (context->indent_level == 0) {
2187 ast_str_append(&context->output_buffer, 0, "\n");
2188 }
2189 }
2190
2191 if (context->show_details || (context->show_details_only_level_0 && context->indent_level == 0)) {
2192 ast_str_append(&context->output_buffer, 0, "\n");
2193 ast_sip_cli_print_sorcery_objectset(endpoint, context, 0);
2194 }
2195
2196 return 0;
2197 }
2198
2199 static struct ast_cli_entry cli_commands[] = {
2200 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Endpoints",
2201 .command = "pjsip list endpoints",
2202 .usage = "Usage: pjsip list endpoints [ like <pattern> ]\n"
2203 " List the configured PJSIP endpoints\n"
2204 " Optional regular expression pattern is used to filter the list.\n"),
2205 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoints",
2206 .command = "pjsip show endpoints",
2207 .usage = "Usage: pjsip show endpoints [ like <pattern> ]\n"
2208 " List(detailed) the configured PJSIP endpoints\n"
2209 " Optional regular expression pattern is used to filter the list.\n"),
2210 AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Endpoint",
2211 .command = "pjsip show endpoint",
2212 .usage = "Usage: pjsip show endpoint <id>\n"
2213 " Show the configured PJSIP endpoint\n"),
2214 };
2215
2216 struct ast_sip_cli_formatter_entry *channel_formatter;
2217 struct ast_sip_cli_formatter_entry *endpoint_formatter;
2218
2219 static void load_all_endpoints(void)
2220 {
2221 struct ao2_container *endpoints;
2222
2223 endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2224 ao2_cleanup(endpoints);
2225 }
2226
2227 static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
2228 struct stasis_message *message)
2229 {
2230 if (stasis_message_type(message) != ast_named_acl_change_type()) {
2231 return;
2232 }
2233
2234 ast_sorcery_force_reload_object(sip_sorcery, "endpoint");
2235 }
2236
2237 int ast_res_pjsip_initialize_configuration(void)
2238 {
2239 if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
2240 ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
2241 return -1;
2242 }
2243
2244 persistent_endpoints = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
2245 PERSISTENT_BUCKETS, persistent_endpoint_hash, NULL, persistent_endpoint_cmp);
2246 if (!persistent_endpoints) {
2247 return -1;
2248 }
2249
2250 if (!(sip_sorcery = ast_sorcery_open())) {
2251 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
2252 return -1;
2253 }
2254
2255 ast_sip_initialize_cli();
2256
2257 if (ast_sip_initialize_sorcery_auth()) {
2258 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
2259 ast_sorcery_unref(sip_sorcery);
2260 sip_sorcery = NULL;
2261 return -1;
2262 }
2263
2264 ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
2265 ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
2266
2267 if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
2268 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
2269 ast_sorcery_unref(sip_sorcery);
2270 sip_sorcery = NULL;
2271 return -1;
2272 }
2273
2274 if (ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL)) {
2275 ast_log(LOG_ERROR, "Failed to register nat_hook\n");
2276 }
2277
2278 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
2279 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
2280 ast_sorcery_object_field_register_alias(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.codecs));
2281 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.codecs));
2282 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, NULL, 0, 0);
2283 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
2284 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
2285 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
2286 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
2287 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
2288 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
2289 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
2290 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
2291 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
2292 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, NULL, 0, 0);
2293 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, NULL, 0, 0);
2294 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));
2295 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));
2296 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, NULL, 0, 0);
2297 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, NULL, 0, 0);
2298 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
2299 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_address", "", media_address_handler, media_address_to_str, NULL, 0, 0);
2300 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));
2301 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username,ip", ident_handler, ident_to_str, NULL, 0, 0);
2302 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
2303 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);
2304 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));
2305 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));
2306 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);
2307 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);
2308 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));
2309 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, NULL, 0, 0);
2310 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);
2311 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, NULL, 0, 0);
2312 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
2313 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
2314 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
2315 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
2316 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rpid_immediate", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.rpid_immediate));
2317 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
2318 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_history_info", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_history_info));
2319 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
2320 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "voicemail_extension", "", voicemail_extension_handler, voicemail_extension_to_str, NULL, 0, 0);
2321 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
2322 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));
2323 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, NULL, 0, 0);
2324 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
2325 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_avp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.force_avp));
2326 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));
2327 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_keepalive", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.keepalive));
2328 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.rtp.timeout));
2329 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));
2330 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_port_start", "0", OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_endpoint, media.rtp.port_start), 0, 65535);
2331 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_port_end", "0", OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_endpoint, media.rtp.port_end), 0, 65535);
2332 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
2333 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
2334 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, NULL, 0, 0);
2335 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, NULL, 0, 0);
2336 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, NULL, 0, 0);
2337 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, NULL, 0, 0);
2338 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));
2339 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
2340 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, NULL, 0, 0);
2341 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
2342 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
2343 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect_timeout", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, faxdetect_timeout));
2344 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
2345 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
2346 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_bind_udptl_to_media_address", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.bind_udptl_to_media_address));
2347 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
2348 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
2349 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
2350 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
2351 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
2352 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "user_eq_phone", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, usereqphone));
2353 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_passthrough", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, moh_passthrough));
2354 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
2355 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
2356 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_audio", "0", tos_handler, tos_audio_to_str, NULL, 0, 0);
2357 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "tos_video", "0", tos_handler, tos_video_to_str, NULL, 0, 0);
2358 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
2359 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
2360 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
2361 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
2362 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "from_user", "", from_user_handler, from_user_to_str, NULL, 0, 0);
2363 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
2364 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
2365 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
2366 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "no", dtls_handler, dtlsverify_to_str, NULL, 0, 0);
2367 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "0", dtls_handler, dtlsrekey_to_str, NULL, 0, 0);
2368 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_auto_generate_cert", "no", dtls_handler, dtlsautogeneratecert_to_str, NULL, 0, 0);
2369 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, NULL, 0, 0);
2370 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, NULL, 0, 0);
2371 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, NULL, 0, 0);
2372 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, NULL, 0, 0);
2373 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, NULL, 0, 0);
2374 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, NULL, 0, 0);
2375 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_fingerprint", "", dtls_handler, dtlsfingerprint_to_str, NULL, 0, 0);
2376 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));
2377 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));
2378 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));
2379 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_method_handler, redirect_method_to_str, NULL, 0, 0);
2380 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "follow_redirect_methods", "", follow_redirect_methods_handler, follow_redirect_methods_to_str, NULL, 0, 0);
2381 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "set_var", "", set_var_handler, set_var_to_str, set_var_to_vl, 0, 0);
2382 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "message_context", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, message_context));
2383 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "accountcode", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, accountcode));
2384 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
2385 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
2386 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "acl", "", endpoint_acl_handler, acl_to_str, NULL, 0, 0);
2387 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_deny", "", endpoint_acl_handler, NULL, NULL, 0, 0);
2388 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_permit", "", endpoint_acl_handler, NULL, NULL, 0, 0);
2389 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_acl", "", endpoint_acl_handler, contact_acl_to_str, NULL, 0, 0);
2390 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subscribe_context", "", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct ast_sip_endpoint, subscription.context));
2391 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_user", "", contact_user_handler, contact_user_to_str, NULL, 0, 0);
2392 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "preferred_codec_only", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, preferred_codec_only));
2393 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "asymmetric_rtp_codec", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, asymmetric_rtp_codec));
2394 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtcp_mux", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtcp_mux));
2395 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_overlap", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allow_overlap));
2396 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "overlap_context", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, overlap_context));
2397 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "refer_blind_progress", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, refer_blind_progress));
2398 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));
2399 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));
2400 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));
2401 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "bundle", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.bundle));
2402 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "webrtc", "no", OPT_YESNO_T, 1, FLDSET(struct ast_sip_endpoint, media.webrtc));
2403 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "incoming_mwi_mailbox", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, incoming_mwi_mailbox));
2404 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));
2405 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));
2406 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));
2407 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ignore_183_without_sdp", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, ignore_183_without_sdp));
2408 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "incoming_call_offer_pref", "local",
2409 call_offer_pref_handler, incoming_call_offer_pref_to_str, NULL, 0, 0);
2410 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outgoing_call_offer_pref", "remote_merge",
2411 call_offer_pref_handler, outgoing_call_offer_pref_to_str, NULL, 0, 0);
2412 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "codec_prefs_incoming_offer",
2413 "prefer: pending, operation: intersect, keep: all, transcode: allow",
2414 codec_prefs_handler, incoming_offer_codec_prefs_to_str, NULL, 0, 0);
2415 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "codec_prefs_outgoing_offer",
2416 "prefer: pending, operation: union, keep: all, transcode: allow",
2417 codec_prefs_handler, outgoing_offer_codec_prefs_to_str, NULL, 0, 0);
2418 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "codec_prefs_incoming_answer",
2419 "prefer: pending, operation: intersect, keep: all",
2420 codec_prefs_handler, incoming_answer_codec_prefs_to_str, NULL, 0, 0);
2421 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "codec_prefs_outgoing_answer",
2422 "prefer: pending, operation: intersect, keep: all",
2423 codec_prefs_handler, outgoing_answer_codec_prefs_to_str, NULL, 0, 0);
2424 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint",
2425 "stir_shaken", 0, stir_shaken_handler, stir_shaken_to_str, NULL, 0, 0);
2426 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "stir_shaken_profile", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, stir_shaken_profile));
2427 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_unauthenticated_options", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allow_unauthenticated_options));
2428 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "geoloc_incoming_call_profile", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, geoloc_incoming_call_profile));
2429 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "geoloc_outgoing_call_profile", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, geoloc_outgoing_call_profile));
2430 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "security_mechanisms", "", security_mechanism_handler, security_mechanism_to_str, NULL, 0, 0);
2431 ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "security_negotiation", "no", security_negotiation_handler, security_negotiation_to_str, NULL, 0, 0);
2432 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_aoc", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_aoc));
2433 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tenantid", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, tenantid));
2434 ast_sorcery_object_field_register(sip_sorcery, "endpoint", "suppress_moh_on_sendonly",
2435 "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, suppress_moh_on_sendonly));
2436
2437 if (ast_sip_initialize_sorcery_transport()) {
2438 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
2439 ast_sorcery_unref(sip_sorcery);
2440 sip_sorcery = NULL;
2441 return -1;
2442 }
2443
2444 if (ast_sip_initialize_sorcery_location()) {
2445 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
2446 ast_sorcery_unref(sip_sorcery);
2447 sip_sorcery = NULL;
2448 return -1;
2449 }
2450
2451 ast_sorcery_observer_add(sip_sorcery, "endpoint", &endpoint_observers);
2452
2453 if (ast_sip_initialize_sorcery_domain_alias()) {
2454 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
2455 ast_sorcery_unref(sip_sorcery);
2456 sip_sorcery = NULL;
2457 return -1;
2458 }
2459
2460 if (ast_sip_initialize_sorcery_global()) {
2461 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
2462 ast_sorcery_unref(sip_sorcery);
2463 sip_sorcery = NULL;
2464 return -1;
2465 }
2466
2467 endpoint_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
2468 if (!endpoint_formatter) {
2469 ast_log(LOG_ERROR, "Unable to allocate memory for endpoint_formatter\n");
2470 ast_sorcery_unref(sip_sorcery);
2471 sip_sorcery = NULL;
2472 return -1;
2473 }
2474 endpoint_formatter->name = "endpoint";
2475 endpoint_formatter->print_header = cli_endpoint_print_header;
2476 endpoint_formatter->print_body = cli_endpoint_print_body;
2477 endpoint_formatter->get_container = cli_endpoint_get_container;
2478 endpoint_formatter->iterate = cli_endpoint_iterate;
2479 endpoint_formatter->retrieve_by_id = cli_endpoint_retrieve_by_id;
2480 endpoint_formatter->get_id = ast_sorcery_object_get_id;
2481
2482 ast_sip_register_cli_formatter(endpoint_formatter);
2483 ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
2484
2485 ast_sorcery_load(sip_sorcery);
2486
2487 load_all_endpoints();
2488
2489 acl_change_sub = stasis_subscribe(ast_security_topic(), acl_change_stasis_cb, NULL);
2490 stasis_subscription_accept_message_type(acl_change_sub, ast_named_acl_change_type());
2491 stasis_subscription_set_filter(acl_change_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
2492
2493 return 0;
2494 }
2495
2496 void ast_res_pjsip_destroy_configuration(void)
2497 {
2498 if (!sip_sorcery) {
2499 return;
2500 }
2501
2502 acl_change_sub = stasis_unsubscribe_and_join(acl_change_sub);
2503 ast_sip_destroy_sorcery_global();
2504 ast_sip_destroy_sorcery_location();
2505 ast_sip_destroy_sorcery_auth();
2506 ast_sip_destroy_sorcery_transport();
2507 ast_sorcery_unref(sip_sorcery);
2508 sip_sorcery = NULL;
2509 ast_manager_unregister(AMI_SHOW_ENDPOINT);
2510 ast_manager_unregister(AMI_SHOW_ENDPOINTS);
2511 ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
2512 ast_sip_unregister_cli_formatter(endpoint_formatter);
2513 ast_sip_destroy_cli();
2514 ao2_cleanup(persistent_endpoints);
2515 persistent_endpoints = NULL;
2516 }
2517
2518 int ast_res_pjsip_reload_configuration(void)
2519 {
2520 if (sip_sorcery) {
2521 ast_sorcery_reload(sip_sorcery);
2522 }
2523 return 0;
2524 }
2525
2526 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
2527 {
2528 ast_string_field_free_memory(&subscription->mwi);
2529 ast_free(subscription->mwi.voicemail_extension);
2530 }
2531
2532 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
2533 {
2534 ast_string_field_free_memory(&info->recording);
2535 }
2536
2537 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
2538 {
2539 ast_rtp_dtls_cfg_free(&media->rtp.dtls_cfg);
2540 ast_string_field_free_memory(&media->rtp);
2541 ast_string_field_free_memory(media);
2542 }
2543
2544 static void endpoint_destructor(void* obj)
2545 {
2546 struct ast_sip_endpoint *endpoint = obj;
2547
2548 ast_string_field_free_memory(endpoint);
2549
2550 ao2_cleanup(endpoint->media.codecs);
2551 ast_stream_topology_free(endpoint->media.topology);
2552 subscription_configuration_destroy(&endpoint->subscription);
2553 info_configuration_destroy(&endpoint->info);
2554 media_configuration_destroy(&endpoint->media);
2555 ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
2556 ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
2557 ast_party_id_free(&endpoint->id.self);
2558 endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
2559 endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
2560 ao2_cleanup(endpoint->persistent);
2561 ast_variables_destroy(endpoint->channel_vars);
2562 AST_VECTOR_FREE(&endpoint->ident_method_order);
2563 ast_free(endpoint->contact_user);
2564 ast_free_acl_list(endpoint->contact_acl);
2565 ast_free_acl_list(endpoint->acl);
2566 ast_sip_security_mechanisms_vector_destroy(&endpoint->security_mechanisms);
2567 }
2568
2569 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
2570 {
2571 return ast_string_field_init(&subscription->mwi, 64);
2572 }
2573
2574 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
2575 {
2576 return ast_string_field_init(&info->recording, 32);
2577 }
2578
2579 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
2580 {
2581 return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
2582 }
2583
2584 void *ast_sip_endpoint_alloc(const char *name)
2585 {
2586 struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
2587 if (!endpoint) {
2588 return NULL;
2589 }
2590 if (ast_string_field_init(endpoint, 128)) {
2591 ao2_cleanup(endpoint);
2592 return NULL;
2593 }
2594
2595 if (ast_string_field_init_extended(endpoint, geoloc_incoming_call_profile) ||
2596 ast_string_field_init_extended(endpoint, geoloc_outgoing_call_profile)) {
2597 ao2_cleanup(endpoint);
2598 return NULL;
2599 }
2600 if (ast_string_field_init_extended(endpoint, overlap_context)) {
2601 ao2_cleanup(endpoint);
2602 return NULL;
2603 }
2604 if (ast_string_field_init_extended(endpoint, tenantid)) {
2605 ao2_cleanup(endpoint);
2606 return NULL;
2607 }
2608
2609 if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
2610 ao2_cleanup(endpoint);
2611 return NULL;
2612 }
2613 if (init_subscription_configuration(&endpoint->subscription)) {
2614 ao2_cleanup(endpoint);
2615 return NULL;
2616 }
2617 if (init_info_configuration(&endpoint->info)) {
2618 ao2_cleanup(endpoint);
2619 return NULL;
2620 }
2621 if (init_media_configuration(&endpoint->media)) {
2622 ao2_cleanup(endpoint);
2623 return NULL;
2624 }
2625
2626 ast_party_id_init(&endpoint->id.self);
2627 endpoint->id.self.tag = ast_strdup("");
2628
2629 if (AST_VECTOR_INIT(&endpoint->ident_method_order, 1)) {
2630 return NULL;
2631 }
2632
2633 return endpoint;
2634 }
2635
2636 struct ao2_container *ast_sip_get_endpoints(void)
2637 {
2638 struct ao2_container *endpoints;
2639
2640 endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2641
2642 return endpoints;
2643 }
2644
2645 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
2646 {
2647 RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
2648 return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
2649 sip_sorcery, "endpoint", name);
2650 }
2651
2652 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
2653 {
2654 int i;
2655
2656 for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
2657 /* Using AST_VECTOR_GET is safe since the vector is immutable */
2658 const char *name = AST_VECTOR_GET(auths, i);
2659 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
2660 if (!out[i]) {
2661 ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
2662 return -1;
2663 }
2664 }
2665
2666 return 0;
2667 }
2668
2669 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
2670 {
2671 int i;
2672 for (i = 0; i < num_auths; ++i) {
2673 ao2_cleanup(auths[i]);
2674 }
2675 }
2676
2677 int ast_sip_retrieve_auths_vector(const struct ast_sip_auth_vector *auth_ids,
2678 struct ast_sip_auth_objects_vector *auth_objects)
2679 {
2680 int i;
2681
2682 for (i = 0; i < AST_VECTOR_SIZE(auth_ids); ++i) {
2683 /* Using AST_VECTOR_GET is safe since the vector is immutable */
2684 const char *name = AST_VECTOR_GET(auth_ids, i);
2685 struct ast_sip_auth *auth_object = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
2686 if (!auth_object) {
2687 ast_log(LOG_WARNING, "Auth object '%s' could not be found\n", name);
2688 } else {
2689 AST_VECTOR_APPEND(auth_objects, auth_object);
2690 }
2691 }
2692
2693 return AST_VECTOR_SIZE(auth_objects) == AST_VECTOR_SIZE(auth_ids) ? 0 : -1;
2694 }
2695
2696 struct ast_sorcery *ast_sip_get_sorcery(void)
2697 {
2698 return sip_sorcery;
2699 }