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