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