res_pjsip_messaging: send message to a default outbound endpoint
[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/cli.h"
16 #include "asterisk/manager.h"
17 #include "asterisk/astobj2.h"
18 #include "asterisk/utils.h"
19 #include "asterisk/sorcery.h"
20 #include "asterisk/callerid.h"
21
22 /*! \brief Number of buckets for persistent endpoint information */
23 #define PERSISTENT_BUCKETS 53
24
25 /*! \brief Persistent endpoint information */
26 struct sip_persistent_endpoint {
27         /*! \brief Asterisk endpoint itself */
28         struct ast_endpoint *endpoint;
29         /*! \brief AORs that we should react to */
30         char *aors;
31 };
32
33 /*! \brief Container for persistent endpoint information */
34 static struct ao2_container *persistent_endpoints;
35
36 static struct ast_sorcery *sip_sorcery;
37
38 /*! \brief Hashing function for persistent endpoint information */
39 static int persistent_endpoint_hash(const void *obj, const int flags)
40 {
41         const struct sip_persistent_endpoint *persistent = obj;
42         const char *id = (flags & OBJ_KEY ? obj : ast_endpoint_get_resource(persistent->endpoint));
43
44         return ast_str_hash(id);
45 }
46
47 /*! \brief Comparison function for persistent endpoint information */
48 static int persistent_endpoint_cmp(void *obj, void *arg, int flags)
49 {
50         const struct sip_persistent_endpoint *persistent1 = obj;
51         const struct sip_persistent_endpoint *persistent2 = arg;
52         const char *id = (flags & OBJ_KEY ? arg : ast_endpoint_get_resource(persistent2->endpoint));
53
54         return !strcmp(ast_endpoint_get_resource(persistent1->endpoint), id) ? CMP_MATCH | CMP_STOP : 0;
55 }
56
57 /*! \brief Callback function for changing the state of an endpoint */
58 static int persistent_endpoint_update_state(void *obj, void *arg, int flags)
59 {
60         struct sip_persistent_endpoint *persistent = obj;
61         char *aor = arg;
62         RAII_VAR(struct ast_sip_contact *, contact, NULL, ao2_cleanup);
63         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
64
65         if (!ast_strlen_zero(aor) && !strstr(persistent->aors, aor)) {
66                 return 0;
67         }
68
69         if ((contact = ast_sip_location_retrieve_contact_from_aor_list(persistent->aors))) {
70                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_ONLINE);
71                 blob = ast_json_pack("{s: s}", "peer_status", "Reachable");
72         } else {
73                 ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_OFFLINE);
74                 blob = ast_json_pack("{s: s}", "peer_status", "Unreachable");
75         }
76
77         ast_endpoint_blob_publish(persistent->endpoint, ast_endpoint_state_type(), blob);
78
79         ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_endpoint_get_resource(persistent->endpoint));
80
81         return 0;
82 }
83
84 /*! \brief Function called when stuff relating to a contact happens (created/deleted) */
85 static void persistent_endpoint_contact_observer(const void *object)
86 {
87         char *id = ast_strdupa(ast_sorcery_object_get_id(object)), *aor = NULL;
88
89         aor = strsep(&id, ";@");
90
91         ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, aor);
92 }
93
94 /*! \brief Observer for contacts so state can be updated on respective endpoints */
95 static const struct ast_sorcery_observer state_contact_observer = {
96         .created = persistent_endpoint_contact_observer,
97         .deleted = persistent_endpoint_contact_observer,
98 };
99
100 static char *handle_cli_show_endpoints(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
101 {
102         RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
103         struct ao2_iterator it_endpoints;
104         struct ast_sip_endpoint *endpoint;
105
106         switch (cmd) {
107         case CLI_INIT:
108                 e->command = "pjsip show endpoints";
109                 e->usage =
110                         "Usage: pjsip show endpoints\n"
111                         "       Show the registered PJSIP endpoints\n";
112                 return NULL;
113         case CLI_GENERATE:
114                 return NULL;
115         }
116
117         endpoints = ast_sip_get_endpoints();
118         if (!endpoints) {
119                 return CLI_FAILURE;
120         }
121
122         if (!ao2_container_count(endpoints)) {
123                 ast_cli(a->fd, "No endpoints found\n");
124                 return CLI_SUCCESS;
125         }
126
127         ast_cli(a->fd, "Endpoints:\n");
128         it_endpoints = ao2_iterator_init(endpoints, 0);
129         while ((endpoint = ao2_iterator_next(&it_endpoints))) {
130                 ast_cli(a->fd, "%s\n", ast_sorcery_object_get_id(endpoint));
131                 ao2_ref(endpoint, -1);
132         }
133         ao2_iterator_destroy(&it_endpoints);
134         return CLI_SUCCESS;
135 }
136
137 static int show_contact(void *obj, void *arg, int flags)
138 {
139         struct ast_sip_contact *contact = obj;
140         struct ast_cli_args *a = arg;
141         RAII_VAR(struct ast_sip_contact_status *, status, ast_sorcery_retrieve_by_id(
142                          ast_sip_get_sorcery(), CONTACT_STATUS,
143                          ast_sorcery_object_get_id(contact)), ao2_cleanup);
144
145         ast_cli(a->fd, "\tContact %s:\n", contact->uri);
146
147         if (!status) {
148                 ast_cli(a->fd, "\tStatus not found!\n");
149                 return 0;
150         }
151
152         ast_cli(a->fd, "\t\tavailable = %s\n", status->status ? "yes" : "no");
153
154         if (status->status) {
155                 ast_cli(a->fd, "\t\tRTT = %lld microseconds\n", (long long)status->rtt);
156         }
157
158         return 0;
159 }
160
161 static void show_endpoint(struct ast_sip_endpoint *endpoint, struct ast_cli_args *a)
162 {
163         char *aor_name, *aors;
164
165         if (ast_strlen_zero(endpoint->aors)) {
166                 return;
167         }
168
169         aors = ast_strdupa(endpoint->aors);
170
171         while ((aor_name = strsep(&aors, ","))) {
172                 RAII_VAR(struct ast_sip_aor *, aor,
173                          ast_sip_location_retrieve_aor(aor_name), ao2_cleanup);
174                 RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup);
175
176                 if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) {
177                         continue;
178                 }
179
180                 ast_cli(a->fd, "AOR %s:\n", ast_sorcery_object_get_id(aor));
181                 ao2_callback(contacts, OBJ_NODATA, show_contact, a);
182         }
183
184         return;
185 }
186
187 static char *cli_show_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
188 {
189         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
190         const char *endpoint_name;
191
192         switch (cmd) {
193         case CLI_INIT:
194                 e->command = "pjsip show endpoint";
195                 e->usage =
196                         "Usage: pjsip show endpoint <endpoint>\n"
197                         "       Show the given PJSIP endpoint.\n";
198                 return NULL;
199         case CLI_GENERATE:
200                 return NULL;
201         }
202
203         if (a->argc != 4) {
204                 return CLI_SHOWUSAGE;
205         }
206
207         endpoint_name = a->argv[3];
208
209         if (!(endpoint = ast_sorcery_retrieve_by_id(
210                       ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
211                 ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name);
212                 return CLI_FAILURE;
213         }
214
215         ast_cli(a->fd, "Endpoint %s:\n", endpoint_name);
216         show_endpoint(endpoint, a);
217
218         return CLI_SUCCESS;
219 }
220
221 static struct ast_cli_entry cli_commands[] = {
222         AST_CLI_DEFINE(handle_cli_show_endpoints, "Show PJSIP Endpoints"),
223         AST_CLI_DEFINE(cli_show_endpoint, "Show PJSIP Endpoint")
224 };
225
226 static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
227 {
228         struct ast_sip_endpoint *endpoint = obj;
229
230         if (!strcasecmp(var->value, "rfc4733")) {
231                 endpoint->dtmf = AST_SIP_DTMF_RFC_4733;
232         } else if (!strcasecmp(var->value, "inband")) {
233                 endpoint->dtmf = AST_SIP_DTMF_INBAND;
234         } else if (!strcasecmp(var->value, "info")) {
235                 endpoint->dtmf = AST_SIP_DTMF_INFO;
236         } else if (!strcasecmp(var->value, "none")) {
237                 endpoint->dtmf = AST_SIP_DTMF_NONE;
238         } else {
239                 return -1;
240         }
241
242         return 0;
243 }
244
245 static int dtmf_to_str(const void *obj, const intptr_t *args, char **buf)
246 {
247         const struct ast_sip_endpoint *endpoint = obj;
248
249         switch (endpoint->dtmf) {
250         case AST_SIP_DTMF_RFC_4733 :
251                 *buf = "rfc4733"; break;
252         case AST_SIP_DTMF_INBAND :
253                 *buf = "inband"; break;
254         case AST_SIP_DTMF_INFO :
255                 *buf = "info"; break;
256         default:
257                 *buf = "none";
258         }
259
260         *buf = ast_strdup(*buf);
261         return 0;
262 }
263
264 static int prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
265 {
266         struct ast_sip_endpoint *endpoint = obj;
267
268         if (ast_true(var->value)) {
269                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
270         } else if (ast_false(var->value)) {
271                 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_100REL;
272         } else if (!strcasecmp(var->value, "required")) {
273                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
274         } else {
275                 return -1;
276         }
277
278         return 0;
279 }
280
281 static int prack_to_str(const void *obj, const intptr_t *args, char **buf)
282 {
283         const struct ast_sip_endpoint *endpoint = obj;
284
285         if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_100REL) {
286                 *buf = "required";
287         } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_100REL) {
288                 *buf = "yes";
289         } else {
290                 *buf = "no";
291         }
292
293         *buf = ast_strdup(*buf);
294         return 0;
295 }
296
297 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
298 {
299         struct ast_sip_endpoint *endpoint = obj;
300
301         if (ast_true(var->value)) {
302                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
303         } else if (ast_false(var->value)) {
304                 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_TIMER;
305         } else if (!strcasecmp(var->value, "required")) {
306                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
307         } else if (!strcasecmp(var->value, "always")) {
308                 endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
309         } else {
310                 return -1;
311         }
312
313         return 0;
314 }
315
316 static int timers_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_ALWAYS_USE_TIMER) {
321                 *buf = "always";
322         } else if (endpoint->extensions.flags & PJSIP_INV_REQUIRE_TIMER) {
323                 *buf = "required";
324         } else if (endpoint->extensions.flags & PJSIP_INV_SUPPORT_TIMER) {
325                 *buf = "yes";
326         } else {
327                 *buf = "no";
328         }
329
330         *buf = ast_strdup(*buf);
331         return 0;
332 }
333
334 void ast_sip_auth_vector_destroy(struct ast_sip_auth_vector *auths)
335 {
336         int i;
337         size_t size;
338
339         if (!auths) {
340                 return;
341         }
342
343         size = AST_VECTOR_SIZE(auths);
344
345         for (i = 0; i < size; ++i) {
346                 const char *name = AST_VECTOR_REMOVE_UNORDERED(auths, 0);
347                 ast_free((char *) name);
348         }
349         AST_VECTOR_FREE(auths);
350 }
351
352 int ast_sip_auth_vector_init(struct ast_sip_auth_vector *auths, const char *value)
353 {
354         char *auth_names = ast_strdupa(value);
355         char *val;
356
357         ast_assert(auths != NULL);
358         ast_assert(AST_VECTOR_SIZE(auths) == 0);
359
360         AST_VECTOR_INIT(auths, 1);
361
362         while ((val = strsep(&auth_names, ","))) {
363                 val = ast_strdup(val);
364                 if (!val) {
365                         goto failure;
366                 }
367                 if (AST_VECTOR_APPEND(auths, val)) {
368                         goto failure;
369                 }
370         }
371         return 0;
372
373 failure:
374         ast_sip_auth_vector_destroy(auths);
375         return -1;
376 }
377
378 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
379 {
380         struct ast_sip_endpoint *endpoint = obj;
381
382         return ast_sip_auth_vector_init(&endpoint->inbound_auths, var->value);
383 }
384
385 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
386 {
387         struct ast_sip_endpoint *endpoint = obj;
388
389         return ast_sip_auth_vector_init(&endpoint->outbound_auths, var->value);
390 }
391
392 int ast_sip_auths_to_str(const struct ast_sip_auth_vector *auths, char **buf)
393 {
394         if (!auths || !AST_VECTOR_SIZE(auths)) {
395                 return 0;
396         }
397
398         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
399                 return -1;
400         }
401
402         /* I feel like accessing the vector's elem array directly is cheating...*/
403         ast_join_delim(*buf, MAX_OBJECT_FIELD, auths->elems, AST_VECTOR_SIZE(auths), ',');
404         return 0;
405 }
406
407 static int inbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
408 {
409         const struct ast_sip_endpoint *endpoint = obj;
410         return ast_sip_auths_to_str(&endpoint->inbound_auths, buf);
411 }
412
413 static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
414 {
415         const struct ast_sip_endpoint *endpoint = obj;
416         return ast_sip_auths_to_str(&endpoint->outbound_auths, buf);
417 }
418
419 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
420 {
421         struct ast_sip_endpoint *endpoint = obj;
422         char *idents = ast_strdupa(var->value);
423         char *val;
424
425         while ((val = strsep(&idents, ","))) {
426                 if (!strcasecmp(val, "username")) {
427                         endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
428                 } else {
429                         ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
430                                         val, ast_sorcery_object_get_id(endpoint));
431                         return -1;
432                 }
433         }
434         return 0;
435 }
436
437 static int ident_to_str(const void *obj, const intptr_t *args, char **buf)
438 {
439         const struct ast_sip_endpoint *endpoint = obj;
440         switch (endpoint->ident_method) {
441         case AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME :
442                 *buf = "username"; break;
443         default:
444                 return 0;
445         }
446
447         *buf = ast_strdup(*buf);
448         return 0;
449 }
450
451 static int redirect_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
452 {
453         struct ast_sip_endpoint *endpoint = obj;
454
455         if (!strcasecmp(var->value, "user")) {
456                 endpoint->redirect_method = AST_SIP_REDIRECT_USER;
457         } else if (!strcasecmp(var->value, "uri_core")) {
458                 endpoint->redirect_method = AST_SIP_REDIRECT_URI_CORE;
459         } else if (!strcasecmp(var->value, "uri_pjsip")) {
460                 endpoint->redirect_method = AST_SIP_REDIRECT_URI_PJSIP;
461         } else {
462                 ast_log(LOG_ERROR, "Unrecognized redirect method %s specified for endpoint %s\n",
463                         var->value, ast_sorcery_object_get_id(endpoint));
464                 return -1;
465         }
466
467         return 0;
468 }
469
470 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
471 {
472         struct ast_sip_endpoint *endpoint = obj;
473
474         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
475                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
476         } else if (!strcasecmp(var->value, "update")) {
477                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
478         } else {
479                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
480                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
481                 return -1;
482         }
483         return 0;
484 }
485
486 static const char *id_configuration_refresh_methods[] = {
487         [AST_SIP_SESSION_REFRESH_METHOD_INVITE] = "invite",
488         [AST_SIP_SESSION_REFRESH_METHOD_UPDATE] = "update"
489 };
490
491 static int direct_media_method_to_str(const void *obj, const intptr_t *args, char **buf)
492 {
493         const struct ast_sip_endpoint *endpoint = obj;
494         if (ARRAY_IN_BOUNDS(endpoint->id.refresh_method, id_configuration_refresh_methods)) {
495                 *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
496         }
497         return 0;
498 }
499
500 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
501 {
502         struct ast_sip_endpoint *endpoint = obj;
503
504         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
505                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
506         } else if (!strcasecmp(var->value, "update")) {
507                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
508         } else {
509                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
510                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
511                 return -1;
512         }
513         return 0;
514 }
515
516 static int connected_line_method_to_str(const void *obj, const intptr_t *args, char **buf)
517 {
518         const struct ast_sip_endpoint *endpoint = obj;
519         *buf = ast_strdup(id_configuration_refresh_methods[endpoint->id.refresh_method]);
520         return 0;
521 }
522
523 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
524 {
525         struct ast_sip_endpoint *endpoint = obj;
526
527         if (!strcasecmp(var->value, "none")) {
528                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
529         } else if (!strcasecmp(var->value, "outgoing")) {
530                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
531         } else if (!strcasecmp(var->value, "incoming")) {
532                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
533         } else {
534                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
535                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
536                 return -1;
537         }
538
539         return 0;
540 }
541
542 static const char *direct_media_glare_mitigation_map[] = {
543         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE] = "none",
544         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING] = "outgoing",
545         [AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING] = "incoming"
546 };
547
548 static int direct_media_glare_mitigation_to_str(const void *obj, const intptr_t *args, char **buf)
549 {
550         const struct ast_sip_endpoint *endpoint = obj;
551         if (ARRAY_IN_BOUNDS(endpoint->media.direct_media.glare_mitigation, direct_media_glare_mitigation_map)) {
552                 *buf = ast_strdup(direct_media_glare_mitigation_map[endpoint->media.direct_media.glare_mitigation]);
553         }
554
555         return 0;
556 }
557
558 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
559 {
560         struct ast_sip_endpoint *endpoint = obj;
561         char cid_name[80] = { '\0' };
562         char cid_num[80] = { '\0' };
563
564         ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
565         if (!ast_strlen_zero(cid_name)) {
566                 endpoint->id.self.name.str = ast_strdup(cid_name);
567                 if (!endpoint->id.self.name.str) {
568                         return -1;
569                 }
570                 endpoint->id.self.name.valid = 1;
571         }
572         if (!ast_strlen_zero(cid_num)) {
573                 endpoint->id.self.number.str = ast_strdup(cid_num);
574                 if (!endpoint->id.self.number.str) {
575                         return -1;
576                 }
577                 endpoint->id.self.number.valid = 1;
578         }
579         return 0;
580 }
581
582 static int caller_id_to_str(const void *obj, const intptr_t *args, char **buf)
583 {
584         const struct ast_sip_endpoint *endpoint = obj;
585         const char *name = S_COR(endpoint->id.self.name.valid,
586                                  endpoint->id.self.name.str, NULL);
587         const char *number = S_COR(endpoint->id.self.number.valid,
588                                    endpoint->id.self.number.str, NULL);
589
590         /* make sure size is at least 10 - that should cover the "<unknown>"
591            case as well as any additional formatting characters added in
592            the name and/or number case. */
593         int size = 10;
594         size += name ? strlen(name) : 0;
595         size += number ? strlen(number) : 0;
596
597         if (!(*buf = ast_calloc(size + 1, sizeof(char)))) {
598                 return -1;
599         }
600
601         ast_callerid_merge(*buf, size + 1, name, number, NULL);
602         return 0;
603 }
604
605 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
606 {
607         struct ast_sip_endpoint *endpoint = obj;
608         int callingpres = ast_parse_caller_presentation(var->value);
609         if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
610                 return -1;
611         }
612         endpoint->id.self.number.presentation = callingpres;
613         endpoint->id.self.name.presentation = callingpres;
614         return 0;
615 }
616
617 static int caller_id_privacy_to_str(const void *obj, const intptr_t *args, char **buf)
618 {
619         const struct ast_sip_endpoint *endpoint = obj;
620         const char *presentation = ast_named_caller_presentation(
621                 endpoint->id.self.name.presentation);
622
623         *buf = ast_strdup(presentation);
624         return 0;
625 }
626
627 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
628 {
629         struct ast_sip_endpoint *endpoint = obj;
630         endpoint->id.self.tag = ast_strdup(var->value);
631         return endpoint->id.self.tag ? 0 : -1;
632 }
633
634 static int caller_id_tag_to_str(const void *obj, const intptr_t *args, char **buf)
635 {
636         const struct ast_sip_endpoint *endpoint = obj;
637         *buf = ast_strdup(endpoint->id.self.tag);
638         return 0;
639 }
640
641 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
642 {
643         struct ast_sip_endpoint *endpoint = obj;
644
645         if (!strcasecmp("no", var->value)) {
646                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
647         } else if (!strcasecmp("sdes", var->value)) {
648                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
649         } else if (!strcasecmp("dtls", var->value)) {
650                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
651                 ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
652         } else {
653                 return -1;
654         }
655
656         return 0;
657 }
658
659 static const char *media_encryption_map[] = {
660         [AST_SIP_MEDIA_TRANSPORT_INVALID] = "invalid",
661         [AST_SIP_MEDIA_ENCRYPT_NONE] = "none",
662         [AST_SIP_MEDIA_ENCRYPT_SDES] = "sdes",
663         [AST_SIP_MEDIA_ENCRYPT_DTLS] = "dtls",
664 };
665
666 static int media_encryption_to_str(const void *obj, const intptr_t *args, char **buf)
667 {
668         const struct ast_sip_endpoint *endpoint = obj;
669         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.encryption, media_encryption_map)) {
670                 *buf = ast_strdup(media_encryption_map[
671                                           endpoint->media.rtp.encryption]);
672         }
673         return 0;
674 }
675
676 static int group_handler(const struct aco_option *opt,
677                          struct ast_variable *var, void *obj)
678 {
679         struct ast_sip_endpoint *endpoint = obj;
680
681         if (!strncmp(var->name, "call_group", 10)) {
682                 if (!(endpoint->pickup.callgroup = ast_get_group(var->value))) {
683                         return -1;
684                 }
685         } else if (!strncmp(var->name, "pickup_group", 12)) {
686                 if (!(endpoint->pickup.pickupgroup = ast_get_group(var->value))) {
687                         return -1;
688                 }
689         } else {
690                 return -1;
691         }
692
693         return 0;
694 }
695
696 static int callgroup_to_str(const void *obj, const intptr_t *args, char **buf)
697 {
698         const struct ast_sip_endpoint *endpoint = obj;
699
700         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
701                 return -1;
702         }
703
704         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.callgroup);
705         return 0;
706 }
707
708 static int pickupgroup_to_str(const void *obj, const intptr_t *args, char **buf)
709 {
710         const struct ast_sip_endpoint *endpoint = obj;
711
712         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
713                 return -1;
714         }
715
716         ast_print_group(*buf, MAX_OBJECT_FIELD, endpoint->pickup.pickupgroup);
717         return 0;
718 }
719
720 static int named_groups_handler(const struct aco_option *opt,
721                                 struct ast_variable *var, void *obj)
722 {
723         struct ast_sip_endpoint *endpoint = obj;
724
725         if (!strncmp(var->name, "named_call_group", 16)) {
726                 if (!(endpoint->pickup.named_callgroups =
727                       ast_get_namedgroups(var->value))) {
728                         return -1;
729                 }
730         } else if (!strncmp(var->name, "named_pickup_group", 18)) {
731                 if (!(endpoint->pickup.named_pickupgroups =
732                       ast_get_namedgroups(var->value))) {
733                         return -1;
734                 }
735         } else {
736                 return -1;
737         }
738
739         return 0;
740 }
741
742 static int named_callgroups_to_str(const void *obj, const intptr_t *args, char **buf)
743 {
744         const struct ast_sip_endpoint *endpoint = obj;
745         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
746
747         ast_print_namedgroups(&str, endpoint->pickup.named_callgroups);
748         *buf = ast_strdup(ast_str_buffer(str));
749         return 0;
750 }
751
752 static int named_pickupgroups_to_str(const void *obj, const intptr_t *args, char **buf)
753 {
754         const struct ast_sip_endpoint *endpoint = obj;
755         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
756
757         ast_print_namedgroups(&str, endpoint->pickup.named_pickupgroups);
758         *buf = ast_strdup(ast_str_buffer(str));
759         return 0;
760 }
761
762 static int dtls_handler(const struct aco_option *opt,
763                          struct ast_variable *var, void *obj)
764 {
765         struct ast_sip_endpoint *endpoint = obj;
766         char *name = ast_strdupa(var->name);
767         char *front, *buf = name;
768
769         /* strip out underscores in the name */
770         front = strtok(buf, "_");
771         while (front) {
772                 int size = strlen(front);
773                 ast_copy_string(buf, front, size + 1);
774                 buf += size;
775                 front = strtok(NULL, "_");
776         }
777
778         return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, name, var->value);
779 }
780
781 static int dtlsverify_to_str(const void *obj, const intptr_t *args, char **buf)
782 {
783         const struct ast_sip_endpoint *endpoint = obj;
784         *buf = ast_strdup(AST_YESNO(endpoint->media.rtp.dtls_cfg.verify));
785         return 0;
786 }
787
788 static int dtlsrekey_to_str(const void *obj, const intptr_t *args, char **buf)
789 {
790         const struct ast_sip_endpoint *endpoint = obj;
791
792         return ast_asprintf(
793                 buf, "%d", endpoint->media.rtp.dtls_cfg.rekey) >=0 ? 0 : -1;
794 }
795
796 static int dtlscertfile_to_str(const void *obj, const intptr_t *args, char **buf)
797 {
798         const struct ast_sip_endpoint *endpoint = obj;
799         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.certfile);
800         return 0;
801 }
802
803 static int dtlsprivatekey_to_str(const void *obj, const intptr_t *args, char **buf)
804 {
805         const struct ast_sip_endpoint *endpoint = obj;
806         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.pvtfile);
807         return 0;
808 }
809
810 static int dtlscipher_to_str(const void *obj, const intptr_t *args, char **buf)
811 {
812         const struct ast_sip_endpoint *endpoint = obj;
813         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cipher);
814         return 0;
815 }
816
817 static int dtlscafile_to_str(const void *obj, const intptr_t *args, char **buf)
818 {
819         const struct ast_sip_endpoint *endpoint = obj;
820         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.cafile);
821         return 0;
822 }
823
824 static int dtlscapath_to_str(const void *obj, const intptr_t *args, char **buf)
825 {
826         const struct ast_sip_endpoint *endpoint = obj;
827         *buf = ast_strdup(endpoint->media.rtp.dtls_cfg.capath);
828         return 0;
829 }
830
831 static const char *ast_rtp_dtls_setup_map[] = {
832         [AST_RTP_DTLS_SETUP_ACTIVE] = "active",
833         [AST_RTP_DTLS_SETUP_PASSIVE] = "passive",
834         [AST_RTP_DTLS_SETUP_ACTPASS] = "actpass",
835         [AST_RTP_DTLS_SETUP_HOLDCONN] = "holdconn",
836 };
837
838 static int dtlssetup_to_str(const void *obj, const intptr_t *args, char **buf)
839 {
840         const struct ast_sip_endpoint *endpoint = obj;
841         if (ARRAY_IN_BOUNDS(endpoint->media.rtp.dtls_cfg.default_setup, ast_rtp_dtls_setup_map)) {
842                 *buf = ast_strdup(ast_rtp_dtls_setup_map[endpoint->media.rtp.dtls_cfg.default_setup]);
843         }
844         return 0;
845 }
846
847 static int t38udptl_ec_handler(const struct aco_option *opt,
848         struct ast_variable *var, void *obj)
849 {
850         struct ast_sip_endpoint *endpoint = obj;
851
852         if (!strcmp(var->value, "none")) {
853                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
854         } else if (!strcmp(var->value, "fec")) {
855                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
856         } else if (!strcmp(var->value, "redundancy")) {
857                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
858         } else {
859                 return -1;
860         }
861
862         return 0;
863 }
864
865 static const char *ast_t38_ec_modes_map[] = {
866         [UDPTL_ERROR_CORRECTION_NONE] = "none",
867         [UDPTL_ERROR_CORRECTION_FEC] = "fec",
868         [UDPTL_ERROR_CORRECTION_REDUNDANCY] = "redundancy"
869 };
870
871 static int t38udptl_ec_to_str(const void *obj, const intptr_t *args, char **buf)
872 {
873         const struct ast_sip_endpoint *endpoint = obj;
874         if (ARRAY_IN_BOUNDS(endpoint->media.t38.error_correction, ast_t38_ec_modes_map)) {
875                 *buf = ast_strdup(ast_t38_ec_modes_map[
876                                           endpoint->media.t38.error_correction]);
877         }
878         return 0;
879 }
880
881 static void *sip_nat_hook_alloc(const char *name)
882 {
883         return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
884 }
885
886 /*! \brief Destructor function for persistent endpoint information */
887 static void persistent_endpoint_destroy(void *obj)
888 {
889         struct sip_persistent_endpoint *persistent = obj;
890
891         ast_endpoint_shutdown(persistent->endpoint);
892         ast_free(persistent->aors);
893 }
894
895 /*! \brief Internal function which finds (or creates) persistent endpoint information */
896 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
897 {
898         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
899         SCOPED_AO2LOCK(lock, persistent_endpoints);
900
901         if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
902                 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
903                         return NULL;
904                 }
905
906                 if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
907                         return NULL;
908                 }
909
910                 persistent->aors = ast_strdup(endpoint->aors);
911
912                 if (ast_strlen_zero(persistent->aors)) {
913                         ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
914                 } else {
915                         persistent_endpoint_update_state(persistent, NULL, 0);
916                 }
917
918                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
919         }
920
921         ao2_ref(persistent->endpoint, +1);
922         return persistent->endpoint;
923 }
924
925 /*! \brief Callback function for when an object is finalized */
926 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
927 {
928         struct ast_sip_endpoint *endpoint = obj;
929
930         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
931                 return -1;
932         }
933
934         if (!ast_strlen_zero(endpoint->outbound_proxy)) {
935                 pj_pool_t *pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound Proxy Validation", 256, 256);
936                 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
937                 pj_str_t tmp;
938
939                 if (!pool) {
940                         ast_log(LOG_ERROR, "Could not allocate pool for outbound proxy validation on '%s'\n",
941                                 ast_sorcery_object_get_id(endpoint));
942                         return -1;
943                 }
944
945                 pj_strdup2_with_null(pool, &tmp, endpoint->outbound_proxy);
946                 if (!pjsip_parse_hdr(pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL)) {
947                         ast_log(LOG_ERROR, "Invalid outbound proxy '%s' specified on endpoint '%s'\n",
948                                 endpoint->outbound_proxy, ast_sorcery_object_get_id(endpoint));
949                         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
950                         return -1;
951                 }
952
953                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
954         }
955
956         return 0;
957 }
958
959 static const char *get_device_state(const struct ast_sip_endpoint *endpoint)
960 {
961         char device[MAX_OBJECT_FIELD];
962
963         snprintf(device, MAX_OBJECT_FIELD, "PJSIP/%s", ast_sorcery_object_get_id(endpoint));
964         return ast_devstate2str(ast_device_state(device));
965 }
966
967 static struct ast_endpoint_snapshot *sip_get_endpoint_snapshot(
968         const struct ast_sip_endpoint *endpoint)
969 {
970         return ast_endpoint_latest_snapshot(
971                 ast_endpoint_get_tech(endpoint->persistent),
972                 ast_endpoint_get_resource(endpoint->persistent));
973 }
974
975 int ast_sip_for_each_channel_snapshot(
976         const struct ast_endpoint_snapshot *endpoint_snapshot,
977         on_channel_snapshot_t on_channel_snapshot, void *arg)
978 {
979         int num, num_channels = endpoint_snapshot->num_channels;
980         RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
981
982         if (!on_channel_snapshot || !num_channels ||
983             !(cache = ast_channel_cache())) {
984                 return 0;
985         }
986
987         ao2_ref(cache, +1);
988
989         for (num = 0; num < num_channels; ++num) {
990                 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
991                 struct ast_channel_snapshot *snapshot;
992
993                 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
994                         endpoint_snapshot->channel_ids[num]);
995
996                 if (!(snapshot = stasis_message_data(msg))) {
997                         continue;
998                 }
999
1000                 if (on_channel_snapshot(
1001                             snapshot, num == (num_channels - 1), arg)) {
1002                         return -1;
1003                 }
1004         }
1005         return 0;
1006 }
1007
1008 static int active_channels_to_str_cb(const struct ast_channel_snapshot *snapshot,
1009                                      int last, void *arg)
1010 {
1011         struct ast_str **buf = arg;
1012         if (last) {
1013                 ast_str_append(buf, 0, "%s", snapshot->name);
1014         } else {
1015                 ast_str_append(buf, 0, "%s,", snapshot->name);
1016         }
1017         return 0;
1018 }
1019
1020 static void active_channels_to_str(const struct ast_sip_endpoint *endpoint,
1021                                    struct ast_str **str)
1022 {
1023
1024         RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot,
1025                  sip_get_endpoint_snapshot(endpoint), ao2_cleanup);
1026
1027         if (endpoint_snapshot) {
1028                 return;
1029         }
1030
1031         ast_sip_for_each_channel_snapshot(endpoint_snapshot,
1032                                           active_channels_to_str_cb, str);
1033 }
1034
1035 #define AMI_DEFAULT_STR_SIZE 512
1036
1037 struct ast_str *ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
1038 {
1039         struct ast_str *buf = ast_str_create(AMI_DEFAULT_STR_SIZE);
1040
1041         if (!(buf)) {
1042                 astman_send_error_va(ami->s, ami->m, "Unable create event "
1043                                      "for %s\n", event);
1044                 return NULL;
1045         }
1046
1047         ast_str_set(&buf, 0, "Event: %s\r\n", event);
1048         return buf;
1049 }
1050
1051 static void sip_sorcery_object_ami_set_type_name(const void *obj, struct ast_str **buf)
1052 {
1053         ast_str_append(buf, 0, "ObjectType: %s\r\n",
1054                        ast_sorcery_object_get_type(obj));
1055         ast_str_append(buf, 0, "ObjectName: %s\r\n",
1056                        ast_sorcery_object_get_id(obj));
1057 }
1058
1059 int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
1060 {
1061         RAII_VAR(struct ast_variable *, objset, ast_sorcery_objectset_create(
1062                          ast_sip_get_sorcery(), obj), ast_variables_destroy);
1063         struct ast_variable *i;
1064
1065         if (!objset) {
1066                 return -1;
1067         }
1068
1069         sip_sorcery_object_ami_set_type_name(obj, buf);
1070
1071         for (i = objset; i; i = i->next) {
1072                 RAII_VAR(char *, camel, ast_to_camel_case(i->name), ast_free);
1073                 ast_str_append(buf, 0, "%s: %s\r\n", camel, i->value);
1074         }
1075
1076         return 0;
1077 }
1078
1079 static int sip_endpoints_aors_ami(void *obj, void *arg, int flags)
1080 {
1081         const struct ast_sip_aor *aor = obj;
1082         struct ast_str **buf = arg;
1083
1084         ast_str_append(buf, 0, "Contacts: ");
1085         ast_sip_for_each_contact(aor, ast_sip_contact_to_str, arg);
1086         ast_str_append(buf, 0, "\r\n");
1087
1088         return 0;
1089 }
1090
1091 static int sip_endpoint_to_ami(const struct ast_sip_endpoint *endpoint,
1092                                struct ast_str **buf)
1093 {
1094         if (ast_sip_sorcery_object_to_ami(endpoint, buf)) {
1095                 return -1;
1096         }
1097
1098         ast_str_append(buf, 0, "DeviceState: %s\r\n",
1099                        get_device_state(endpoint));
1100
1101         ast_str_append(buf, 0, "ActiveChannels: ");
1102         active_channels_to_str(endpoint, buf);
1103         ast_str_append(buf, 0, "\r\n");
1104
1105         return 0;
1106 }
1107
1108 static int format_ami_endpoint(const struct ast_sip_endpoint *endpoint,
1109                                struct ast_sip_ami *ami)
1110 {
1111         RAII_VAR(struct ast_str *, buf,
1112                  ast_sip_create_ami_event("EndpointDetail", ami), ast_free);
1113
1114         if (!buf) {
1115                 return -1;
1116         }
1117
1118         sip_endpoint_to_ami(endpoint, &buf);
1119         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1120         return 0;
1121 }
1122
1123 #define AMI_SHOW_ENDPOINTS "PJSIPShowEndpoints"
1124 #define AMI_SHOW_ENDPOINT "PJSIPShowEndpoint"
1125
1126 static int ami_show_endpoint(struct mansession *s, const struct message *m)
1127 {
1128         struct ast_sip_ami ami = { .s = s, .m = m };
1129         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1130         const char *endpoint_name = astman_get_header(m, "Endpoint");
1131         int count = 0;
1132
1133         if (ast_strlen_zero(endpoint_name)) {
1134                 astman_send_error_va(s, m, "%s requires an endpoint name\n",
1135                         AMI_SHOW_ENDPOINT);
1136                 return 0;
1137         }
1138
1139         if (!strncasecmp(endpoint_name, "pjsip/", 6)) {
1140                 endpoint_name += 6;
1141         }
1142
1143         if (!(endpoint = ast_sorcery_retrieve_by_id(
1144                       ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1145                 astman_send_error_va(s, m, "Unable to retrieve endpoint %s\n",
1146                         endpoint_name);
1147                 return -1;
1148         }
1149
1150         astman_send_listack(s, m, "Following are Events for each object "
1151                             "associated with the the Endpoint", "start");
1152
1153         /* the endpoint detail needs to always come first so apply as such */
1154         if (format_ami_endpoint(endpoint, &ami) ||
1155             ast_sip_format_endpoint_ami(endpoint, &ami, &count)) {
1156                 astman_send_error_va(s, m, "Unable to format endpoint %s\n",
1157                         endpoint_name);
1158         }
1159
1160         astman_append(s,
1161                       "Event: EndpointDetailComplete\r\n"
1162                       "EventList: Complete\r\n"
1163                       "ListItems: %d\r\n\r\n", count + 1);
1164         return 0;
1165 }
1166
1167 static int format_str_append_auth(const struct ast_sip_auth_vector *auths,
1168                                   struct ast_str **buf)
1169 {
1170         char *str = NULL;
1171         if (ast_sip_auths_to_str(auths, &str)) {
1172                 return -1;
1173         }
1174         ast_str_append(buf, 0, "%s", str ? str : "");
1175         ast_free(str);
1176         return 0;
1177 }
1178
1179 static int format_ami_endpoints(void *obj, void *arg, int flags)
1180 {
1181
1182         struct ast_sip_endpoint *endpoint = obj;
1183         struct ast_sip_ami *ami = arg;
1184         RAII_VAR(struct ast_str *, buf,
1185                  ast_sip_create_ami_event("EndpointList", ami), ast_free);
1186
1187         if (!buf) {
1188                 return -1;
1189         }
1190
1191         sip_sorcery_object_ami_set_type_name(endpoint, &buf);
1192         ast_str_append(&buf, 0, "Transport: %s\r\n",
1193                        endpoint->transport);
1194         ast_str_append(&buf, 0, "Aor: %s\r\n",
1195                        endpoint->aors);
1196
1197         ast_str_append(&buf, 0, "Auths: ");
1198         format_str_append_auth(&endpoint->inbound_auths, &buf);
1199         ast_str_append(&buf, 0, "\r\n");
1200
1201         ast_str_append(&buf, 0, "OutboundAuths: ");
1202         format_str_append_auth(&endpoint->outbound_auths, &buf);
1203         ast_str_append(&buf, 0, "\r\n");
1204
1205         ast_sip_for_each_aor(endpoint->aors,
1206                              sip_endpoints_aors_ami, &buf);
1207
1208         ast_str_append(&buf, 0, "DeviceState: %s\r\n",
1209                        get_device_state(endpoint));
1210
1211         ast_str_append(&buf, 0, "ActiveChannels: ");
1212         active_channels_to_str(endpoint, &buf);
1213         ast_str_append(&buf, 0, "\r\n");
1214
1215         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
1216         return 0;
1217 }
1218
1219 static int ami_show_endpoints(struct mansession *s, const struct message *m)
1220 {
1221         struct ast_sip_ami ami = { .s = s, .m = m };
1222         RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup);
1223         int num;
1224
1225         endpoints = ast_sip_get_endpoints();
1226         if (!endpoints) {
1227                 return -1;
1228         }
1229
1230         if (!(num = ao2_container_count(endpoints))) {
1231                 astman_send_error(s, m, "No endpoints found\n");
1232                 return 0;
1233         }
1234
1235         astman_send_listack(s, m, "A listing of Endpoints follows, "
1236                             "presented as EndpointList events", "start");
1237
1238         ao2_callback(endpoints, OBJ_NODATA, format_ami_endpoints, &ami);
1239
1240         astman_append(s,
1241                       "Event: EndpointListComplete\r\n"
1242                       "EventList: Complete\r\n"
1243                       "ListItems: %d\r\n\r\n", num);
1244         return 0;
1245 }
1246
1247 int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_module_info)
1248 {
1249         if (ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands))) {
1250                 return -1;
1251         }
1252
1253         if (ast_manager_register_xml(AMI_SHOW_ENDPOINTS, EVENT_FLAG_SYSTEM, ami_show_endpoints) ||
1254             ast_manager_register_xml(AMI_SHOW_ENDPOINT, EVENT_FLAG_SYSTEM, ami_show_endpoint)) {
1255                 return -1;
1256         }
1257
1258         if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
1259                 return -1;
1260         }
1261
1262         if (!(sip_sorcery = ast_sorcery_open())) {
1263                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
1264                 return -1;
1265         }
1266
1267         ast_sorcery_apply_config(sip_sorcery, "res_pjsip");
1268
1269         if (ast_sip_initialize_sorcery_auth(sip_sorcery)) {
1270                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
1271                 ast_sorcery_unref(sip_sorcery);
1272                 sip_sorcery = NULL;
1273                 return -1;
1274         }
1275
1276         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
1277
1278         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
1279
1280         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
1281                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
1282                 ast_sorcery_unref(sip_sorcery);
1283                 sip_sorcery = NULL;
1284                 return -1;
1285         }
1286
1287         ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
1288
1289         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
1290         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
1291         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
1292         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
1293         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, dtmf_to_str, 0, 0);
1294         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
1295         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
1296         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
1297         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
1298         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
1299         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
1300         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
1301         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
1302         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
1303         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, prack_to_str, 0, 0);
1304         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, timers_to_str, 0, 0);
1305         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));
1306         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));
1307         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, inbound_auths_to_str, 0, 0);
1308         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, 0, 0);
1309         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
1310         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
1311         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, ident_to_str, 0, 0);
1312         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
1313         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, direct_media_method_to_str, 0, 0);
1314         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, connected_line_method_to_str, 0, 0);
1315         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, 0, 0);
1316         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));
1317         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, caller_id_to_str, 0, 0);
1318         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "", caller_id_privacy_handler, caller_id_privacy_to_str, 0, 0);
1319         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, caller_id_tag_to_str, 0, 0);
1320         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
1321         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
1322         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
1323         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
1324         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
1325         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
1326         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
1327         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, media_encryption_to_str, 0, 0);
1328         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
1329         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
1330         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
1331         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, callgroup_to_str, 0, 0);
1332         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, pickupgroup_to_str, 0, 0);
1333         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, named_callgroups_to_str, 0, 0);
1334         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, named_pickupgroups_to_str, 0, 0);
1335         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));
1336         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
1337         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, t38udptl_ec_to_str, 0, 0);
1338         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
1339         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
1340         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
1341         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
1342         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
1343         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
1344         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
1345         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
1346         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
1347         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
1348         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
1349         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_audio));
1350         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_video));
1351         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
1352         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
1353         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
1354         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
1355         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
1356         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
1357         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
1358         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
1359         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "", dtls_handler, dtlsverify_to_str, 0, 0);
1360         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "", dtls_handler, dtlsrekey_to_str, 0, 0);
1361         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, dtlscertfile_to_str, 0, 0);
1362         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, dtlsprivatekey_to_str, 0, 0);
1363         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, dtlscipher_to_str, 0, 0);
1364         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, dtlscafile_to_str, 0, 0);
1365         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, dtlscapath_to_str, 0, 0);
1366         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, dtlssetup_to_str, 0, 0);
1367         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));
1368         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "redirect_method", "user", redirect_handler, NULL, 0, 0);
1369
1370         if (ast_sip_initialize_sorcery_transport(sip_sorcery)) {
1371                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
1372                 ast_sorcery_unref(sip_sorcery);
1373                 sip_sorcery = NULL;
1374                 return -1;
1375         }
1376
1377         if (ast_sip_initialize_sorcery_location(sip_sorcery)) {
1378                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
1379                 ast_sorcery_unref(sip_sorcery);
1380                 sip_sorcery = NULL;
1381                 return -1;
1382         }
1383
1384         if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) {
1385                 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
1386                 ast_sorcery_unref(sip_sorcery);
1387                 sip_sorcery = NULL;
1388                 return -1;
1389         }
1390
1391         ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
1392
1393         if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) {
1394                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
1395                 ast_sorcery_unref(sip_sorcery);
1396                 sip_sorcery = NULL;
1397                 return -1;
1398         }
1399
1400         if (ast_sip_initialize_sorcery_global(sip_sorcery)) {
1401                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
1402                 ast_sorcery_unref(sip_sorcery);
1403                 sip_sorcery = NULL;
1404                 return -1;
1405         }
1406
1407         ast_sorcery_load(sip_sorcery);
1408
1409         return 0;
1410 }
1411
1412 void ast_res_pjsip_destroy_configuration(void)
1413 {
1414         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1415         ast_manager_unregister(AMI_SHOW_ENDPOINT);
1416         ast_manager_unregister(AMI_SHOW_ENDPOINTS);
1417         ast_sorcery_unref(sip_sorcery);
1418 }
1419
1420 /*!
1421  * \internal
1422  * \brief Reload configuration within a PJSIP thread
1423  */
1424 static int reload_configuration_task(void *obj)
1425 {
1426         if (sip_sorcery) {
1427                 ast_sorcery_reload(sip_sorcery);
1428         }
1429         return 0;
1430 }
1431
1432 int ast_res_pjsip_reload_configuration(void)
1433 {
1434         if (ast_sip_push_task(NULL, reload_configuration_task, NULL)) {
1435                 ast_log(LOG_WARNING, "Failed to reload PJSIP configuration\n");
1436         }
1437
1438         return 0;
1439 }
1440
1441 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
1442 {
1443         ast_string_field_free_memory(&subscription->mwi);
1444 }
1445
1446 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
1447 {
1448         ast_string_field_free_memory(&info->recording);
1449 }
1450
1451 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
1452 {
1453         ast_string_field_free_memory(&media->rtp);
1454         ast_string_field_free_memory(media);
1455 }
1456
1457 static void endpoint_destructor(void* obj)
1458 {
1459         struct ast_sip_endpoint *endpoint = obj;
1460
1461         ast_string_field_free_memory(endpoint);
1462
1463         if (endpoint->media.codecs) {
1464                 ast_format_cap_destroy(endpoint->media.codecs);
1465         }
1466         subscription_configuration_destroy(&endpoint->subscription);
1467         info_configuration_destroy(&endpoint->info);
1468         media_configuration_destroy(&endpoint->media);
1469         ast_sip_auth_vector_destroy(&endpoint->inbound_auths);
1470         ast_sip_auth_vector_destroy(&endpoint->outbound_auths);
1471         ast_party_id_free(&endpoint->id.self);
1472         endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
1473         endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
1474         ao2_cleanup(endpoint->persistent);
1475 }
1476
1477 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
1478 {
1479         return ast_string_field_init(&subscription->mwi, 64);
1480 }
1481
1482 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
1483 {
1484         return ast_string_field_init(&info->recording, 32);
1485 }
1486
1487 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
1488 {
1489         return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
1490 }
1491
1492 void *ast_sip_endpoint_alloc(const char *name)
1493 {
1494         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
1495         if (!endpoint) {
1496                 return NULL;
1497         }
1498         if (ast_string_field_init(endpoint, 64)) {
1499                 ao2_cleanup(endpoint);
1500                 return NULL;
1501         }
1502         if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
1503                 ao2_cleanup(endpoint);
1504                 return NULL;
1505         }
1506         if (init_subscription_configuration(&endpoint->subscription)) {
1507                 ao2_cleanup(endpoint);
1508                 return NULL;
1509         }
1510         if (init_info_configuration(&endpoint->info)) {
1511                 ao2_cleanup(endpoint);
1512                 return NULL;
1513         }
1514         if (init_media_configuration(&endpoint->media)) {
1515                 ao2_cleanup(endpoint);
1516                 return NULL;
1517         }
1518         ast_party_id_init(&endpoint->id.self);
1519         return endpoint;
1520 }
1521
1522 struct ao2_container *ast_sip_get_endpoints(void)
1523 {
1524         struct ao2_container *endpoints;
1525
1526         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1527
1528         return endpoints;
1529 }
1530
1531 struct ast_sip_endpoint *ast_sip_default_outbound_endpoint(void)
1532 {
1533         RAII_VAR(char *, name, ast_sip_global_default_outbound_endpoint(), ast_free);
1534         return ast_strlen_zero(name) ? NULL : ast_sorcery_retrieve_by_id(
1535                 sip_sorcery, "endpoint", name);
1536 }
1537
1538 int ast_sip_retrieve_auths(const struct ast_sip_auth_vector *auths, struct ast_sip_auth **out)
1539 {
1540         int i;
1541
1542         for (i = 0; i < AST_VECTOR_SIZE(auths); ++i) {
1543                 /* Using AST_VECTOR_GET is safe since the vector is immutable */
1544                 const char *name = AST_VECTOR_GET(auths, i);
1545                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, name);
1546                 if (!out[i]) {
1547                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", name);
1548                         return -1;
1549                 }
1550         }
1551
1552         return 0;
1553 }
1554
1555 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
1556 {
1557         int i;
1558         for (i = 0; i < num_auths; ++i) {
1559                 ao2_cleanup(auths[i]);
1560         }
1561 }
1562
1563 struct ast_sorcery *ast_sip_get_sorcery(void)
1564 {
1565         return sip_sorcery;
1566 }