d382fefaf941befcdab3dbf4ec0f0364b5303e91
[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/astobj2.h"
17 #include "asterisk/utils.h"
18 #include "asterisk/sorcery.h"
19 #include "asterisk/callerid.h"
20 #include "asterisk/stasis_endpoints.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 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 prack_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
246 {
247         struct ast_sip_endpoint *endpoint = obj;
248
249         if (ast_true(var->value)) {
250                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_100REL;
251         } else if (ast_false(var->value)) {
252                 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_100REL;
253         } else if (!strcasecmp(var->value, "required")) {
254                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_100REL;
255         } else {
256                 return -1;
257         }
258
259         return 0;
260 }
261
262 static int timers_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
263 {
264         struct ast_sip_endpoint *endpoint = obj;
265
266         if (ast_true(var->value)) {
267                 endpoint->extensions.flags |= PJSIP_INV_SUPPORT_TIMER;
268         } else if (ast_false(var->value)) {
269                 endpoint->extensions.flags &= PJSIP_INV_SUPPORT_TIMER;
270         } else if (!strcasecmp(var->value, "required")) {
271                 endpoint->extensions.flags |= PJSIP_INV_REQUIRE_TIMER;
272         } else if (!strcasecmp(var->value, "always")) {
273                 endpoint->extensions.flags |= PJSIP_INV_ALWAYS_USE_TIMER;
274         } else {
275                 return -1;
276         }
277
278         return 0;
279 }
280
281 void ast_sip_auth_array_destroy(struct ast_sip_auth_array *auths)
282 {
283         int i;
284
285         if (!auths) {
286                 return;
287         }
288
289         for (i = 0; i < auths->num; ++i) {
290                 ast_free((char *) auths->names[i]);
291         }
292         ast_free(auths->names);
293         auths->names = NULL;
294         auths->num = 0;
295 }
296
297 #define AUTH_INCREMENT 4
298
299 int ast_sip_auth_array_init(struct ast_sip_auth_array *auths, const char *value)
300 {
301         char *auth_names = ast_strdupa(value);
302         char *val;
303         int num_alloced = 0;
304         const char **alloced_auths;
305
306         ast_assert(auths != NULL);
307         ast_assert(auths->names == NULL);
308         ast_assert(!auths->num);
309
310         while ((val = strsep(&auth_names, ","))) {
311                 if (auths->num >= num_alloced) {
312                         num_alloced += AUTH_INCREMENT;
313                         alloced_auths = ast_realloc(auths->names, num_alloced * sizeof(char *));
314                         if (!alloced_auths) {
315                                 goto failure;
316                         }
317                         auths->names = alloced_auths;
318                 }
319                 val = ast_strdup(val);
320                 if (!val) {
321                         goto failure;
322                 }
323                 auths->names[auths->num] = val;
324                 ++auths->num;
325         }
326         return 0;
327
328 failure:
329         ast_sip_auth_array_destroy(auths);
330         return -1;
331 }
332
333 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
334 {
335         struct ast_sip_endpoint *endpoint = obj;
336
337         return ast_sip_auth_array_init(&endpoint->inbound_auths, var->value);
338 }
339
340 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
341 {
342         struct ast_sip_endpoint *endpoint = obj;
343
344         return ast_sip_auth_array_init(&endpoint->outbound_auths, var->value);
345 }
346
347 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
348 {
349         struct ast_sip_endpoint *endpoint = obj;
350         char *idents = ast_strdupa(var->value);
351         char *val;
352
353         while ((val = strsep(&idents, ","))) {
354                 if (!strcasecmp(val, "username")) {
355                         endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
356                 } else {
357                         ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
358                                         val, ast_sorcery_object_get_id(endpoint));
359                         return -1;
360                 }
361         }
362         return 0;
363 }
364
365 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
366 {
367         struct ast_sip_endpoint *endpoint = obj;
368
369         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
370                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
371         } else if (!strcasecmp(var->value, "update")) {
372                 endpoint->media.direct_media.method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
373         } else {
374                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
375                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
376                 return -1;
377         }
378         return 0;
379 }
380
381 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
382 {
383         struct ast_sip_endpoint *endpoint = obj;
384
385         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
386                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
387         } else if (!strcasecmp(var->value, "update")) {
388                 endpoint->id.refresh_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
389         } else {
390                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
391                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
392                 return -1;
393         }
394         return 0;
395 }
396
397 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
398 {
399         struct ast_sip_endpoint *endpoint = obj;
400
401         if (!strcasecmp(var->value, "none")) {
402                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
403         } else if (!strcasecmp(var->value, "outgoing")) {
404                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
405         } else if (!strcasecmp(var->value, "incoming")) {
406                 endpoint->media.direct_media.glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
407         } else {
408                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
409                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
410                 return -1;
411         }
412
413         return 0;
414 }
415
416 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
417 {
418         struct ast_sip_endpoint *endpoint = obj;
419         char cid_name[80] = { '\0' };
420         char cid_num[80] = { '\0' };
421
422         ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
423         if (!ast_strlen_zero(cid_name)) {
424                 endpoint->id.self.name.str = ast_strdup(cid_name);
425                 if (!endpoint->id.self.name.str) {
426                         return -1;
427                 }
428                 endpoint->id.self.name.valid = 1;
429         }
430         if (!ast_strlen_zero(cid_num)) {
431                 endpoint->id.self.number.str = ast_strdup(cid_num);
432                 if (!endpoint->id.self.number.str) {
433                         return -1;
434                 }
435                 endpoint->id.self.number.valid = 1;
436         }
437         return 0;
438 }
439
440 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
441 {
442         struct ast_sip_endpoint *endpoint = obj;
443         int callingpres = ast_parse_caller_presentation(var->value);
444         if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
445                 return -1;
446         }
447         endpoint->id.self.number.presentation = callingpres;
448         endpoint->id.self.name.presentation = callingpres;
449         return 0;
450 }
451
452 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
453 {
454         struct ast_sip_endpoint *endpoint = obj;
455         endpoint->id.self.tag = ast_strdup(var->value);
456         return endpoint->id.self.tag ? 0 : -1;
457 }
458
459 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
460 {
461         struct ast_sip_endpoint *endpoint = obj;
462
463         if (!strcasecmp("no", var->value)) {
464                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
465         } else if (!strcasecmp("sdes", var->value)) {
466                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
467         } else if (!strcasecmp("dtls", var->value)) {
468                 endpoint->media.rtp.encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
469                 ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, "dtlsenable", "yes");
470         } else {
471                 return -1;
472         }
473
474         return 0;
475 }
476
477 static int group_handler(const struct aco_option *opt,
478                          struct ast_variable *var, void *obj)
479 {
480         struct ast_sip_endpoint *endpoint = obj;
481
482         if (!strncmp(var->name, "callgroup", 9)) {
483                 if (!(endpoint->pickup.callgroup = ast_get_group(var->value))) {
484                         return -1;
485                 }
486         } else if (!strncmp(var->name, "pickupgroup", 11)) {
487                 if (!(endpoint->pickup.pickupgroup = ast_get_group(var->value))) {
488                         return -1;
489                 }
490         } else {
491                 return -1;
492         }
493
494         return 0;
495 }
496
497 static int named_groups_handler(const struct aco_option *opt,
498                                 struct ast_variable *var, void *obj)
499 {
500         struct ast_sip_endpoint *endpoint = obj;
501
502         if (!strncmp(var->name, "namedcallgroup", 14)) {
503                 if (!(endpoint->pickup.named_callgroups =
504                       ast_get_namedgroups(var->value))) {
505                         return -1;
506                 }
507         } else if (!strncmp(var->name, "namedpickupgroup", 16)) {
508                 if (!(endpoint->pickup.named_pickupgroups =
509                       ast_get_namedgroups(var->value))) {
510                         return -1;
511                 }
512         } else {
513                 return -1;
514         }
515
516         return 0;
517 }
518
519 static int dtls_handler(const struct aco_option *opt,
520                          struct ast_variable *var, void *obj)
521 {
522         struct ast_sip_endpoint *endpoint = obj;
523
524         return ast_rtp_dtls_cfg_parse(&endpoint->media.rtp.dtls_cfg, var->name, var->value);
525 }
526
527 static int t38udptl_ec_handler(const struct aco_option *opt,
528         struct ast_variable *var, void *obj)
529 {
530         struct ast_sip_endpoint *endpoint = obj;
531
532         if (!strcmp(var->value, "none")) {
533                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_NONE;
534         } else if (!strcmp(var->value, "fec")) {
535                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_FEC;
536         } else if (!strcmp(var->value, "redundancy")) {
537                 endpoint->media.t38.error_correction = UDPTL_ERROR_CORRECTION_REDUNDANCY;
538         } else {
539                 return -1;
540         }
541
542         return 0;
543 }
544
545 static void *sip_nat_hook_alloc(const char *name)
546 {
547         return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
548 }
549
550 /*! \brief Destructor function for persistent endpoint information */
551 static void persistent_endpoint_destroy(void *obj)
552 {
553         struct sip_persistent_endpoint *persistent = obj;
554
555         ast_endpoint_shutdown(persistent->endpoint);
556         ast_free(persistent->aors);
557 }
558
559 /*! \brief Internal function which finds (or creates) persistent endpoint information */
560 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
561 {
562         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
563         SCOPED_AO2LOCK(lock, persistent_endpoints);
564
565         if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
566                 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
567                         return NULL;
568                 }
569
570                 if (!(persistent->endpoint = ast_endpoint_create("PJSIP", ast_sorcery_object_get_id(endpoint)))) {
571                         return NULL;
572                 }
573
574                 persistent->aors = ast_strdup(endpoint->aors);
575
576                 if (ast_strlen_zero(persistent->aors)) {
577                         ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
578                 } else {
579                         persistent_endpoint_update_state(persistent, NULL, 0);
580                 }
581
582                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
583         }
584
585         ao2_ref(persistent->endpoint, +1);
586         return persistent->endpoint;
587 }
588
589 /*! \brief Callback function for when an object is finalized */
590 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
591 {
592         struct ast_sip_endpoint *endpoint = obj;
593
594         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
595                 return -1;
596         }
597
598         if (!ast_strlen_zero(endpoint->outbound_proxy)) {
599                 pj_pool_t *pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound Proxy Validation", 256, 256);
600                 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
601                 pj_str_t tmp;
602
603                 if (!pool) {
604                         ast_log(LOG_ERROR, "Could not allocate pool for outbound proxy validation on '%s'\n",
605                                 ast_sorcery_object_get_id(endpoint));
606                         return -1;
607                 }
608
609                 pj_strdup2_with_null(pool, &tmp, endpoint->outbound_proxy);
610                 if (!pjsip_parse_hdr(pool, &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL)) {
611                         ast_log(LOG_ERROR, "Invalid outbound proxy '%s' specified on endpoint '%s'\n",
612                                 endpoint->outbound_proxy, ast_sorcery_object_get_id(endpoint));
613                         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
614                         return -1;
615                 }
616
617                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
618         }
619
620         return 0;
621 }
622
623 int ast_res_pjsip_initialize_configuration(void)
624 {
625         if (ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands))) {
626                 return -1;
627         }
628
629         if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
630                 return -1;
631         }
632
633         if (!(sip_sorcery = ast_sorcery_open())) {
634                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
635                 return -1;
636         }
637
638         ast_sorcery_apply_config(sip_sorcery, "res_pjsip");
639
640         if (ast_sip_initialize_sorcery_auth(sip_sorcery)) {
641                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
642                 ast_sorcery_unref(sip_sorcery);
643                 sip_sorcery = NULL;
644                 return -1;
645         }
646
647         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "pjsip.conf,criteria=type=endpoint");
648
649         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
650
651         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
652                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
653                 ast_sorcery_unref(sip_sorcery);
654                 sip_sorcery = NULL;
655                 return -1;
656         }
657
658         ast_sorcery_internal_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
659
660         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
661         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
662         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
663         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, media.prefs, media.codecs));
664         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmf_mode", "rfc4733", dtmf_handler, NULL, 0, 0);
665         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ipv6));
666         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.symmetric));
667         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.ice_support));
668         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_ptime));
669         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.force_rport));
670         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, nat.rewrite_contact));
671         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
672         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
673         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "moh_suggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
674         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, NULL, 0, 0);
675         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, NULL, 0, 0);
676         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));
677         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));
678         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, NULL, 0, 0);
679         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, NULL, 0, 0);
680         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
681         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.address));
682         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username", ident_handler, NULL, 0, 0);
683         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.direct_media.enabled));
684         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, NULL, 0, 0);
685         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, NULL, 0, 0);
686         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, NULL, 0, 0);
687         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));
688         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, NULL, 0, 0);
689         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "", caller_id_privacy_handler, NULL, 0, 0);
690         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, NULL, 0, 0);
691         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_inbound));
692         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.trust_outbound));
693         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_pai));
694         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_rpid));
695         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, id.send_diversion));
696         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.mailboxes));
697         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.mwi.aggregate));
698         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, NULL, 0, 0);
699         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.rtp.use_avpf));
700         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, info.recording.enabled));
701         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
702         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "call_group", "", group_handler, NULL, 0, 0);
703         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickup_group", "", group_handler, NULL, 0, 0);
704         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_call_group", "", named_groups_handler, NULL, 0, 0);
705         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "named_pickup_group", "", named_groups_handler, NULL, 0, 0);
706         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));
707         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.enabled));
708         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "t38_udptl_ec", "none", t38udptl_ec_handler, NULL, 0, 0);
709         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_maxdatagram", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.t38.maxdatagram));
710         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fax_detect", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, faxdetect));
711         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.nat));
712         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "t38_udptl_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, media.t38.ipv6));
713         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tone_zone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
714         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
715         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_on_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.onfeature));
716         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "record_off_feature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, info.recording.offfeature));
717         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_transfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
718         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_owner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpowner));
719         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdp_session", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.sdpsession));
720         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_audio));
721         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.tos_video));
722         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_audio));
723         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, media.cos_video));
724         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_subscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, subscription.allow));
725         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sub_min_expiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subscription.minexpiry));
726         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
727         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "from_domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
728         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwi_from_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, subscription.mwi.fromuser));
729         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_engine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, media.rtp.engine));
730         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_verify", "", dtls_handler, NULL, 0, 0);
731         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_rekey", "", dtls_handler, NULL, 0, 0);
732         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cert_file", "", dtls_handler, NULL, 0, 0);
733         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_private_key", "", dtls_handler, NULL, 0, 0);
734         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_cipher", "", dtls_handler, NULL, 0, 0);
735         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_file", "", dtls_handler, NULL, 0, 0);
736         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_ca_path", "", dtls_handler, NULL, 0, 0);
737         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtls_setup", "", dtls_handler, NULL, 0, 0);
738         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));
739
740         if (ast_sip_initialize_sorcery_transport(sip_sorcery)) {
741                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
742                 ast_sorcery_unref(sip_sorcery);
743                 sip_sorcery = NULL;
744                 return -1;
745         }
746
747         if (ast_sip_initialize_sorcery_location(sip_sorcery)) {
748                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
749                 ast_sorcery_unref(sip_sorcery);
750                 sip_sorcery = NULL;
751                 return -1;
752         }
753
754         if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) {
755                 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
756                 ast_sorcery_unref(sip_sorcery);
757                 sip_sorcery = NULL;
758                 return -1;
759         }
760
761         ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
762
763         if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) {
764                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
765                 ast_sorcery_unref(sip_sorcery);
766                 sip_sorcery = NULL;
767                 return -1;
768         }
769
770         if (ast_sip_initialize_sorcery_global(sip_sorcery)) {
771                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
772                 ast_sorcery_unref(sip_sorcery);
773                 sip_sorcery = NULL;
774                 return -1;
775         }
776
777         ast_sorcery_load(sip_sorcery);
778
779         return 0;
780 }
781
782 void ast_res_pjsip_destroy_configuration(void)
783 {
784         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
785         ast_sorcery_unref(sip_sorcery);
786 }
787
788 int ast_res_pjsip_reload_configuration(void)
789 {
790         if (sip_sorcery) {
791                 ast_sorcery_reload(sip_sorcery);
792         }
793         return 0;
794 }
795
796 static void subscription_configuration_destroy(struct ast_sip_endpoint_subscription_configuration *subscription)
797 {
798         ast_string_field_free_memory(&subscription->mwi);
799 }
800
801 static void info_configuration_destroy(struct ast_sip_endpoint_info_configuration *info)
802 {
803         ast_string_field_free_memory(&info->recording);
804 }
805
806 static void media_configuration_destroy(struct ast_sip_endpoint_media_configuration *media)
807 {
808         ast_string_field_free_memory(&media->rtp);
809         ast_string_field_free_memory(media);
810 }
811
812 static void endpoint_destructor(void* obj)
813 {
814         struct ast_sip_endpoint *endpoint = obj;
815
816         ast_string_field_free_memory(endpoint);
817
818         if (endpoint->media.codecs) {
819                 ast_format_cap_destroy(endpoint->media.codecs);
820         }
821         subscription_configuration_destroy(&endpoint->subscription);
822         info_configuration_destroy(&endpoint->info);
823         media_configuration_destroy(&endpoint->media);
824         ast_sip_auth_array_destroy(&endpoint->inbound_auths);
825         ast_sip_auth_array_destroy(&endpoint->outbound_auths);
826         ast_party_id_free(&endpoint->id.self);
827         endpoint->pickup.named_callgroups = ast_unref_namedgroups(endpoint->pickup.named_callgroups);
828         endpoint->pickup.named_pickupgroups = ast_unref_namedgroups(endpoint->pickup.named_pickupgroups);
829         ao2_cleanup(endpoint->persistent);
830 }
831
832 static int init_subscription_configuration(struct ast_sip_endpoint_subscription_configuration *subscription)
833 {
834         return ast_string_field_init(&subscription->mwi, 64);
835 }
836
837 static int init_info_configuration(struct ast_sip_endpoint_info_configuration *info)
838 {
839         return ast_string_field_init(&info->recording, 32);
840 }
841
842 static int init_media_configuration(struct ast_sip_endpoint_media_configuration *media)
843 {
844         return ast_string_field_init(media, 64) || ast_string_field_init(&media->rtp, 32);
845 }
846
847 void *ast_sip_endpoint_alloc(const char *name)
848 {
849         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
850         if (!endpoint) {
851                 return NULL;
852         }
853         if (ast_string_field_init(endpoint, 64)) {
854                 ao2_cleanup(endpoint);
855                 return NULL;
856         }
857         if (!(endpoint->media.codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK))) {
858                 ao2_cleanup(endpoint);
859                 return NULL;
860         }
861         if (init_subscription_configuration(&endpoint->subscription)) {
862                 ao2_cleanup(endpoint);
863                 return NULL;
864         }
865         if (init_info_configuration(&endpoint->info)) {
866                 ao2_cleanup(endpoint);
867                 return NULL;
868         }
869         if (init_media_configuration(&endpoint->media)) {
870                 ao2_cleanup(endpoint);
871                 return NULL;
872         }
873         ast_party_id_init(&endpoint->id.self);
874         return endpoint;
875 }
876
877 struct ao2_container *ast_sip_get_endpoints(void)
878 {
879         struct ao2_container *endpoints;
880
881         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
882
883         return endpoints;
884 }
885
886 int ast_sip_retrieve_auths(const struct ast_sip_auth_array *auths, struct ast_sip_auth **out)
887 {
888         int i;
889
890         for (i = 0; i < auths->num; ++i) {
891                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, auths->names[i]);
892                 if (!out[i]) {
893                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", auths->names[i]);
894                         return -1;
895                 }
896         }
897
898         return 0;
899 }
900
901 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
902 {
903         int i;
904         for (i = 0; i < num_auths; ++i) {
905                 ao2_cleanup(auths[i]);
906         }
907 }
908
909 struct ast_sorcery *ast_sip_get_sorcery(void)
910 {
911         return sip_sorcery;
912 }