Change the default value for "allowsubscribe" to yes to match chan_sip.
[asterisk/asterisk.git] / res / res_sip / sip_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_sip.h"
14 #include "include/res_sip_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, "Gulp/%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 = "sip show endpoints";
109                 e->usage =
110                         "Usage: sip show endpoints\n"
111                         "       Show the registered SIP 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 = "sip show endpoint";
195                 e->usage =
196                         "Usage: sip show endpoint <endpoint>\n"
197                         "       Show the given SIP 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 SIP Endpoints"),
223         AST_CLI_DEFINE(cli_show_endpoint, "Show SIP 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 |= PJSIP_INV_SUPPORT_100REL;
251         } else if (ast_false(var->value)) {
252                 endpoint->extensions &= PJSIP_INV_SUPPORT_100REL;
253         } else if (!strcasecmp(var->value, "required")) {
254                 endpoint->extensions |= 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 |= PJSIP_INV_SUPPORT_TIMER;
268         } else if (ast_false(var->value)) {
269                 endpoint->extensions &= PJSIP_INV_SUPPORT_TIMER;
270         } else if (!strcasecmp(var->value, "required")) {
271                 endpoint->extensions |= PJSIP_INV_REQUIRE_TIMER;
272         } else if (!strcasecmp(var->value, "always")) {
273                 endpoint->extensions |= PJSIP_INV_ALWAYS_USE_TIMER;
274         } else {
275                 return -1;
276         }
277
278         return 0;
279 }
280
281 static void destroy_auths(const char **auths, size_t num_auths)
282 {
283         int i;
284
285         if (!auths) {
286                 return;
287         }
288
289         for (i = 0; i < num_auths; ++i) {
290                 ast_free((char *) auths[i]);
291         }
292         ast_free(auths);
293 }
294
295 #define AUTH_INCREMENT 4
296
297 static const char **auth_alloc(const char *value, size_t *num_auths)
298 {
299         char *auths = ast_strdupa(value);
300         char *val;
301         int num_alloced = 0;
302         const char **alloced_auths = NULL;
303
304         while ((val = strsep(&auths, ","))) {
305                 if (*num_auths >= num_alloced) {
306                         size_t size;
307                         num_alloced += AUTH_INCREMENT;
308                         size = num_alloced * sizeof(char *);
309                         alloced_auths = ast_realloc(alloced_auths, size);
310                         if (!alloced_auths) {
311                                 goto failure;
312                         }
313                 }
314                 alloced_auths[*num_auths] = ast_strdup(val);
315                 if (!alloced_auths[*num_auths]) {
316                         goto failure;
317                 }
318                 ++(*num_auths);
319         }
320         return alloced_auths;
321
322 failure:
323         destroy_auths(alloced_auths, *num_auths);
324         return NULL;
325 }
326
327 static int inbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
328 {
329         struct ast_sip_endpoint *endpoint = obj;
330
331         endpoint->sip_inbound_auths = auth_alloc(var->value, &endpoint->num_inbound_auths);
332         if (!endpoint->sip_inbound_auths) {
333                 return -1;
334         }
335         return 0;
336 }
337 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
338 {
339         struct ast_sip_endpoint *endpoint = obj;
340
341         endpoint->sip_outbound_auths = auth_alloc(var->value, &endpoint->num_outbound_auths);
342         if (!endpoint->sip_outbound_auths) {
343                 return -1;
344         }
345         return 0;
346 }
347
348 static int ident_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
349 {
350         struct ast_sip_endpoint *endpoint = obj;
351         char *idents = ast_strdupa(var->value);
352         char *val;
353
354         while ((val = strsep(&idents, ","))) {
355                 if (!strcasecmp(val, "username")) {
356                         endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_USERNAME;
357                 } else if (!strcasecmp(val, "location")) {
358                         endpoint->ident_method |= AST_SIP_ENDPOINT_IDENTIFY_BY_LOCATION;
359                 } else {
360                         ast_log(LOG_ERROR, "Unrecognized identification method %s specified for endpoint %s\n",
361                                         val, ast_sorcery_object_get_id(endpoint));
362                         return -1;
363                 }
364         }
365         return 0;
366 }
367
368 static int direct_media_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
369 {
370         struct ast_sip_endpoint *endpoint = obj;
371
372         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
373                 endpoint->direct_media_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
374         } else if (!strcasecmp(var->value, "update")) {
375                 endpoint->direct_media_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
376         } else {
377                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
378                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
379                 return -1;
380         }
381         return 0;
382 }
383
384 static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
385 {
386         struct ast_sip_endpoint *endpoint = obj;
387
388         if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) {
389                 endpoint->connected_line_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE;
390         } else if (!strcasecmp(var->value, "update")) {
391                 endpoint->connected_line_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
392         } else {
393                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
394                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
395                 return -1;
396         }
397         return 0;
398 }
399
400 static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
401 {
402         struct ast_sip_endpoint *endpoint = obj;
403
404         if (!strcasecmp(var->value, "none")) {
405                 endpoint->direct_media_glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE;
406         } else if (!strcasecmp(var->value, "outgoing")) {
407                 endpoint->direct_media_glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING;
408         } else if (!strcasecmp(var->value, "incoming")) {
409                 endpoint->direct_media_glare_mitigation = AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING;
410         } else {
411                 ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n",
412                                 var->value, var->name, ast_sorcery_object_get_id(endpoint));
413                 return -1;
414         }
415
416         return 0;
417 }
418
419 static int caller_id_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
420 {
421         struct ast_sip_endpoint *endpoint = obj;
422         char cid_name[80] = { '\0' };
423         char cid_num[80] = { '\0' };
424
425         ast_callerid_split(var->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
426         if (!ast_strlen_zero(cid_name)) {
427                 endpoint->id.name.str = ast_strdup(cid_name);
428                 if (!endpoint->id.name.str) {
429                         return -1;
430                 }
431                 endpoint->id.name.valid = 1;
432         }
433         if (!ast_strlen_zero(cid_num)) {
434                 endpoint->id.number.str = ast_strdup(cid_num);
435                 if (!endpoint->id.number.str) {
436                         return -1;
437                 }
438                 endpoint->id.number.valid = 1;
439         }
440         return 0;
441 }
442
443 static int caller_id_privacy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
444 {
445         struct ast_sip_endpoint *endpoint = obj;
446         int callingpres = ast_parse_caller_presentation(var->value);
447         if (callingpres == -1 && sscanf(var->value, "%d", &callingpres) != 1) {
448                 return -1;
449         }
450         endpoint->id.number.presentation = callingpres;
451         endpoint->id.name.presentation = callingpres;
452         return 0;
453 }
454
455 static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
456 {
457         struct ast_sip_endpoint *endpoint = obj;
458         endpoint->id.tag = ast_strdup(var->value);
459         return endpoint->id.tag ? 0 : -1;
460 }
461
462 static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
463 {
464         struct ast_sip_endpoint *endpoint = obj;
465
466         if (!strcasecmp("no", var->value)) {
467                 endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
468         } else if (!strcasecmp("sdes", var->value)) {
469                 endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_SDES;
470         } else if (!strcasecmp("dtls", var->value)) {
471                 endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;
472                 ast_rtp_dtls_cfg_parse(&endpoint->dtls_cfg, "dtlsenable", "yes");
473         } else {
474                 return -1;
475         }
476
477         return 0;
478 }
479
480 static int group_handler(const struct aco_option *opt,
481                          struct ast_variable *var, void *obj)
482 {
483         struct ast_sip_endpoint *endpoint = obj;
484
485         if (!strncmp(var->name, "callgroup", 9)) {
486                 if (!(endpoint->callgroup = ast_get_group(var->value))) {
487                         return -1;
488                 }
489         } else if (!strncmp(var->name, "pickupgroup", 11)) {
490                 if (!(endpoint->pickupgroup = ast_get_group(var->value))) {
491                         return -1;
492                 }
493         } else {
494                 return -1;
495         }
496
497         return 0;
498 }
499
500 static int named_groups_handler(const struct aco_option *opt,
501                                 struct ast_variable *var, void *obj)
502 {
503         struct ast_sip_endpoint *endpoint = obj;
504
505         if (!strncmp(var->name, "namedcallgroup", 14)) {
506                 if (!(endpoint->named_callgroups =
507                       ast_get_namedgroups(var->value))) {
508                         return -1;
509                 }
510         } else if (!strncmp(var->name, "namedpickupgroup", 16)) {
511                 if (!(endpoint->named_pickupgroups =
512                       ast_get_namedgroups(var->value))) {
513                         return -1;
514                 }
515         } else {
516                 return -1;
517         }
518
519         return 0;
520 }
521
522 static int dtls_handler(const struct aco_option *opt,
523                          struct ast_variable *var, void *obj)
524 {
525         struct ast_sip_endpoint *endpoint = obj;
526
527         return ast_rtp_dtls_cfg_parse(&endpoint->dtls_cfg, var->name, var->value);
528 }
529
530 static void *sip_nat_hook_alloc(const char *name)
531 {
532         return ast_sorcery_generic_alloc(sizeof(struct ast_sip_nat_hook), NULL);
533 }
534
535 /*! \brief Destructor function for persistent endpoint information */
536 static void persistent_endpoint_destroy(void *obj)
537 {
538         struct sip_persistent_endpoint *persistent = obj;
539
540         ast_endpoint_shutdown(persistent->endpoint);
541         ast_free(persistent->aors);
542 }
543
544 /*! \brief Internal function which finds (or creates) persistent endpoint information */
545 static struct ast_endpoint *persistent_endpoint_find_or_create(const struct ast_sip_endpoint *endpoint)
546 {
547         RAII_VAR(struct sip_persistent_endpoint *, persistent, NULL, ao2_cleanup);
548         SCOPED_AO2LOCK(lock, persistent_endpoints);
549
550         if (!(persistent = ao2_find(persistent_endpoints, ast_sorcery_object_get_id(endpoint), OBJ_KEY | OBJ_NOLOCK))) {
551                 if (!(persistent = ao2_alloc(sizeof(*persistent), persistent_endpoint_destroy))) {
552                         return NULL;
553                 }
554
555                 if (!(persistent->endpoint = ast_endpoint_create("Gulp", ast_sorcery_object_get_id(endpoint)))) {
556                         return NULL;
557                 }
558
559                 persistent->aors = ast_strdup(endpoint->aors);
560
561                 if (ast_strlen_zero(persistent->aors)) {
562                         ast_endpoint_set_state(persistent->endpoint, AST_ENDPOINT_UNKNOWN);
563                 } else {
564                         persistent_endpoint_update_state(persistent, NULL, 0);
565                 }
566
567                 ao2_link_flags(persistent_endpoints, persistent, OBJ_NOLOCK);
568         }
569
570         ao2_ref(persistent->endpoint, +1);
571         return persistent->endpoint;
572 }
573
574 /*! \brief Callback function for when an object is finalized */
575 static int sip_endpoint_apply_handler(const struct ast_sorcery *sorcery, void *obj)
576 {
577         struct ast_sip_endpoint *endpoint = obj;
578
579         if (!(endpoint->persistent = persistent_endpoint_find_or_create(endpoint))) {
580                 return -1;
581         }
582
583         return 0;
584 }
585
586 int ast_res_sip_initialize_configuration(void)
587 {
588         if (ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands))) {
589                 return -1;
590         }
591
592         if (!(persistent_endpoints = ao2_container_alloc(PERSISTENT_BUCKETS, persistent_endpoint_hash, persistent_endpoint_cmp))) {
593                 return -1;
594         }
595
596         if (!(sip_sorcery = ast_sorcery_open())) {
597                 ast_log(LOG_ERROR, "Failed to open SIP sorcery failed to open\n");
598                 return -1;
599         }
600
601         ast_sorcery_apply_config(sip_sorcery, "res_sip");
602
603         if (ast_sip_initialize_sorcery_auth(sip_sorcery)) {
604                 ast_log(LOG_ERROR, "Failed to register SIP authentication support\n");
605                 ast_sorcery_unref(sip_sorcery);
606                 sip_sorcery = NULL;
607                 return -1;
608         }
609
610         ast_sorcery_apply_default(sip_sorcery, "endpoint", "config", "res_sip.conf,criteria=type=endpoint");
611
612         ast_sorcery_apply_default(sip_sorcery, "nat_hook", "memory", NULL);
613
614         if (ast_sorcery_object_register(sip_sorcery, "endpoint", ast_sip_endpoint_alloc, NULL, sip_endpoint_apply_handler)) {
615                 ast_log(LOG_ERROR, "Failed to register SIP endpoint object with sorcery\n");
616                 ast_sorcery_unref(sip_sorcery);
617                 sip_sorcery = NULL;
618                 return -1;
619         }
620
621         ast_sorcery_object_register(sip_sorcery, "nat_hook", sip_nat_hook_alloc, NULL, NULL);
622
623         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "type", "", OPT_NOOP_T, 0, 0);
624         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context));
625         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, prefs, codecs));
626         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, prefs, codecs));
627         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmfmode", "rfc4733", dtmf_handler, NULL, 0, 0);
628         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rtp_ipv6));
629         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rtp_symmetric));
630         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "ice_support", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, ice_support));
631         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_ptime", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, use_ptime));
632         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "force_rport", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, force_rport));
633         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rewrite_contact", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rewrite_contact));
634         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, transport));
635         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, outbound_proxy));
636         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mohsuggest", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mohsuggest));
637         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "100rel", "yes", prack_handler, NULL, 0, 0);
638         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "timers", "yes", timers_handler, NULL, 0, 0);
639         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_min_se", "90", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, min_se));
640         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "timers_sess_expires", "1800", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, sess_expires));
641         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "auth", "", inbound_auth_handler, NULL, 0, 0);
642         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "outbound_auth", "", outbound_auth_handler, NULL, 0, 0);
643         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aors", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, aors));
644         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, external_media_address));
645         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username,location", ident_handler, NULL, 0, 0);
646         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, direct_media));
647         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, NULL, 0, 0);
648         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, NULL, 0, 0);
649         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, NULL, 0, 0);
650         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disable_direct_media_on_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, disable_direct_media_on_nat));
651         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, NULL, 0, 0);
652         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_privacy", "", caller_id_privacy_handler, NULL, 0, 0);
653         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid_tag", "", caller_id_tag_handler, NULL, 0, 0);
654         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_inbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, trust_id_inbound));
655         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, trust_id_outbound));
656         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_pai));
657         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_rpid));
658         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_diversion));
659         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mailboxes));
660         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, aggregate_mwi));
661         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, NULL, 0, 0);
662         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, use_avpf));
663         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, one_touch_recording));
664         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress));
665         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callgroup", "", group_handler, NULL, 0, 0);
666         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickupgroup", "", group_handler, NULL, 0, 0);
667         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedcallgroup", "", named_groups_handler, NULL, 0, 0);
668         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedpickupgroup", "", named_groups_handler, NULL, 0, 0);
669         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "devicestate_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at));
670         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtpengine", "asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, rtp_engine));
671         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tonezone", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, zone));
672         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "language", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, language));
673         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "recordonfeature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, recordonfeature));
674         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "recordofffeature", "automixmon", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, recordofffeature));
675         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allowtransfer", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowtransfer));
676         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdpowner", "-", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, sdpowner));
677         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "sdpsession", "Asterisk", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, sdpsession));
678         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, tos_audio));
679         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, tos_video));
680         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_audio", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, cos_audio));
681         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "cos_video", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, cos_video));
682         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allowsubscribe", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allowsubscribe));
683         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subminexpiry", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subminexpiry));
684         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subminexpirey", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, subminexpiry));
685         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fromuser", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromuser));
686         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "fromdomain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, fromdomain));
687         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mwifromuser", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mwi_from));
688         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsverify", "", dtls_handler, NULL, 0, 0);
689         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsrekey", "", dtls_handler, NULL, 0, 0);
690         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscertfile", "", dtls_handler, NULL, 0, 0);
691         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlsprivatekey", "", dtls_handler, NULL, 0, 0);
692         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscipher", "", dtls_handler, NULL, 0, 0);
693         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscafile", "", dtls_handler, NULL, 0, 0);
694         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlscapath", "", dtls_handler, NULL, 0, 0);
695         ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtlssetup", "", dtls_handler, NULL, 0, 0);
696         ast_sorcery_object_field_register(sip_sorcery, "endpoint", "srtp_tag_32", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, srtp_tag_32));
697
698         if (ast_sip_initialize_sorcery_transport(sip_sorcery)) {
699                 ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
700                 ast_sorcery_unref(sip_sorcery);
701                 sip_sorcery = NULL;
702                 return -1;
703         }
704
705         if (ast_sip_initialize_sorcery_location(sip_sorcery)) {
706                 ast_log(LOG_ERROR, "Failed to register SIP location support with sorcery\n");
707                 ast_sorcery_unref(sip_sorcery);
708                 sip_sorcery = NULL;
709                 return -1;
710         }
711
712         if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) {
713                 ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n");
714                 ast_sorcery_unref(sip_sorcery);
715                 sip_sorcery = NULL;
716                 return -1;
717         }
718
719         ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer);
720
721         if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) {
722                 ast_log(LOG_ERROR, "Failed to register SIP domain aliases support with sorcery\n");
723                 ast_sorcery_unref(sip_sorcery);
724                 sip_sorcery = NULL;
725                 return -1;
726         }
727
728         if (ast_sip_initialize_sorcery_security(sip_sorcery)) {
729                 ast_log(LOG_ERROR, "Failed to register SIP security support\n");
730                 ast_sorcery_unref(sip_sorcery);
731                 sip_sorcery = NULL;
732                 return -1;
733         }
734
735         if (ast_sip_initialize_sorcery_global(sip_sorcery)) {
736                 ast_log(LOG_ERROR, "Failed to register SIP Global support\n");
737                 ast_sorcery_unref(sip_sorcery);
738                 sip_sorcery = NULL;
739                 return -1;
740         }
741
742         ast_sorcery_load(sip_sorcery);
743
744         return 0;
745 }
746
747 void ast_res_sip_destroy_configuration(void)
748 {
749         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
750         ast_sorcery_unref(sip_sorcery);
751 }
752
753 int ast_res_sip_reload_configuration(void)
754 {
755         if (sip_sorcery) {
756                 ast_sorcery_reload(sip_sorcery);
757         }
758         return 0;
759 }
760
761 static void endpoint_destructor(void* obj)
762 {
763         struct ast_sip_endpoint *endpoint = obj;
764
765         ast_string_field_free_memory(endpoint);
766
767         if (endpoint->codecs) {
768                 ast_format_cap_destroy(endpoint->codecs);
769         }
770         destroy_auths(endpoint->sip_inbound_auths, endpoint->num_inbound_auths);
771         destroy_auths(endpoint->sip_outbound_auths, endpoint->num_outbound_auths);
772         ast_party_id_free(&endpoint->id);
773         endpoint->named_callgroups = ast_unref_namedgroups(endpoint->named_callgroups);
774         endpoint->named_pickupgroups = ast_unref_namedgroups(endpoint->named_pickupgroups);
775         ao2_cleanup(endpoint->persistent);
776 }
777
778 void *ast_sip_endpoint_alloc(const char *name)
779 {
780         struct ast_sip_endpoint *endpoint = ast_sorcery_generic_alloc(sizeof(*endpoint), endpoint_destructor);
781         if (!endpoint) {
782                 return NULL;
783         }
784         if (ast_string_field_init(endpoint, 64)) {
785                 ao2_cleanup(endpoint);
786                 return NULL;
787         }
788         if (!(endpoint->codecs = ast_format_cap_alloc_nolock())) {
789                 ao2_cleanup(endpoint);
790                 return NULL;
791         }
792         ast_party_id_init(&endpoint->id);
793         return endpoint;
794 }
795
796 struct ao2_container *ast_sip_get_endpoints(void)
797 {
798         struct ao2_container *endpoints;
799
800         endpoints = ast_sorcery_retrieve_by_fields(sip_sorcery, "endpoint", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
801
802         return endpoints;
803 }
804
805 int ast_sip_retrieve_auths(const char *auth_names[], size_t num_auths, struct ast_sip_auth **out)
806 {
807         int i;
808
809         for (i = 0; i < num_auths; ++i) {
810                 out[i] = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE, auth_names[i]);
811                 if (!out[i]) {
812                         ast_log(LOG_NOTICE, "Couldn't find auth '%s'. Cannot authenticate\n", auth_names[i]);
813                         return -1;
814                 }
815         }
816
817         return 0;
818 }
819
820 void ast_sip_cleanup_auths(struct ast_sip_auth *auths[], size_t num_auths)
821 {
822         int i;
823         for (i = 0; i < num_auths; ++i) {
824                 ao2_cleanup(auths[i]);
825         }
826 }
827
828 struct ast_sorcery *ast_sip_get_sorcery(void)
829 {
830         return sip_sorcery;
831 }