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