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