res_pjsip: ami: Fix error in AMI output when an endpoint has no transport
[asterisk/asterisk.git] / res / res_pjsip / config_transport.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib.h>
23
24 #include "asterisk/res_pjsip.h"
25 #include "asterisk/res_pjsip_cli.h"
26 #include "asterisk/logger.h"
27 #include "asterisk/astobj2.h"
28 #include "asterisk/sorcery.h"
29 #include "asterisk/acl.h"
30 #include "include/res_pjsip_private.h"
31 #include "asterisk/http_websocket.h"
32
33 static int sip_transport_to_ami(const struct ast_sip_transport *transport,
34                                 struct ast_str **buf)
35 {
36         return ast_sip_sorcery_object_to_ami(transport, buf);
37 }
38
39 static int format_ami_endpoint_transport(const struct ast_sip_endpoint *endpoint,
40                                          struct ast_sip_ami *ami)
41 {
42         RAII_VAR(struct ast_str *, buf, NULL, ast_free);
43         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
44
45         if (ast_strlen_zero(endpoint->transport)) {
46                 return 0;
47         }
48
49         buf = ast_sip_create_ami_event("TransportDetail", ami);
50         if (!buf) {
51                 return -1;
52         }
53
54         transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
55                 endpoint->transport);
56         if (!transport) {
57                 astman_send_error_va(ami->s, ami->m, "Unable to retrieve "
58                                      "transport %s\n", endpoint->transport);
59                 return -1;
60         }
61
62         sip_transport_to_ami(transport, &buf);
63
64         ast_str_append(&buf, 0, "EndpointName: %s\r\n",
65                        ast_sorcery_object_get_id(endpoint));
66
67         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
68         ami->count++;
69
70         return 0;
71 }
72
73 struct ast_sip_endpoint_formatter endpoint_transport_formatter = {
74         .format_ami = format_ami_endpoint_transport
75 };
76
77 static int destroy_transport_state(void *data)
78 {
79         pjsip_transport *transport = data;
80         pjsip_transport_shutdown(transport);
81         return 0;
82 }
83
84 /*! \brief Destructor for transport state information */
85 static void transport_state_destroy(void *obj)
86 {
87         struct ast_sip_transport_state *state = obj;
88
89         if (state->transport) {
90                 ast_sip_push_task_synchronous(NULL, destroy_transport_state, state->transport);
91         }
92 }
93
94 /*! \brief Destructor for transport */
95 static void transport_destroy(void *obj)
96 {
97         struct ast_sip_transport *transport = obj;
98
99         ast_string_field_free_memory(transport);
100         ast_free_ha(transport->localnet);
101
102         if (transport->external_address_refresher) {
103                 ast_dnsmgr_release(transport->external_address_refresher);
104         }
105
106         ao2_cleanup(transport->state);
107 }
108
109 /*! \brief Allocator for transport */
110 static void *transport_alloc(const char *name)
111 {
112         struct ast_sip_transport *transport = ast_sorcery_generic_alloc(sizeof(*transport), transport_destroy);
113
114         if (!transport) {
115                 return NULL;
116         }
117
118         if (ast_string_field_init(transport, 256)) {
119                 ao2_cleanup(transport);
120                 return NULL;
121         }
122
123         pjsip_tls_setting_default(&transport->tls);
124         transport->tls.ciphers = transport->ciphers;
125
126         return transport;
127 }
128
129 static void set_qos(struct ast_sip_transport *transport, pj_qos_params *qos)
130 {
131         int tos_as_dscp = transport->tos >> 2;
132
133         if (transport->tos) {
134                 qos->flags |= PJ_QOS_PARAM_HAS_DSCP;
135                 qos->dscp_val = tos_as_dscp;
136         }
137         if (transport->cos) {
138                 qos->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
139                 qos->so_prio = transport->cos;
140         }
141 }
142
143 /*! \brief Apply handler for transports */
144 static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
145 {
146         struct ast_sip_transport *transport = obj;
147         RAII_VAR(struct ast_sip_transport *, existing, ast_sorcery_retrieve_by_id(sorcery, "transport", ast_sorcery_object_get_id(obj)), ao2_cleanup);
148         pj_status_t res = -1;
149
150         if (!existing || !existing->state) {
151                 if (!(transport->state = ao2_alloc(sizeof(*transport->state), transport_state_destroy))) {
152                         ast_log(LOG_ERROR, "Transport state for '%s' could not be allocated\n", ast_sorcery_object_get_id(obj));
153                         return -1;
154                 }
155         } else {
156                 transport->state = existing->state;
157                 ao2_ref(transport->state, +1);
158         }
159
160         /* Once active a transport can not be reconfigured */
161         if (transport->state->transport || transport->state->factory) {
162                 return -1;
163         }
164
165         if (transport->host.addr.sa_family != PJ_AF_INET && transport->host.addr.sa_family != PJ_AF_INET6) {
166                 ast_log(LOG_ERROR, "Transport '%s' could not be started as binding not specified\n", ast_sorcery_object_get_id(obj));
167                 return -1;
168         }
169
170         /* Set default port if not present */
171         if (!pj_sockaddr_get_port(&transport->host)) {
172                 pj_sockaddr_set_port(&transport->host, (transport->type == AST_TRANSPORT_TLS) ? 5061 : 5060);
173         }
174
175         /* Now that we know what address family we can set up a dnsmgr refresh for the external media address if present */
176         if (!ast_strlen_zero(transport->external_signaling_address)) {
177                 if (transport->host.addr.sa_family == pj_AF_INET()) {
178                         transport->external_address.ss.ss_family = AF_INET;
179                 } else if (transport->host.addr.sa_family == pj_AF_INET6()) {
180                         transport->external_address.ss.ss_family = AF_INET6;
181                 } else {
182                         ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external signaling address\n",
183                                         ast_sorcery_object_get_id(obj));
184                         return -1;
185                 }
186
187                 if (ast_dnsmgr_lookup(transport->external_signaling_address, &transport->external_address, &transport->external_address_refresher, NULL) < 0) {
188                         ast_log(LOG_ERROR, "Could not create dnsmgr for external signaling address on '%s'\n", ast_sorcery_object_get_id(obj));
189                         return -1;
190                 }
191         }
192
193         if (transport->type == AST_TRANSPORT_UDP) {
194                 if (transport->host.addr.sa_family == pj_AF_INET()) {
195                         res = pjsip_udp_transport_start(ast_sip_get_pjsip_endpoint(), &transport->host.ipv4, NULL, transport->async_operations, &transport->state->transport);
196                 } else if (transport->host.addr.sa_family == pj_AF_INET6()) {
197                         res = pjsip_udp_transport_start6(ast_sip_get_pjsip_endpoint(), &transport->host.ipv6, NULL, transport->async_operations, &transport->state->transport);
198                 }
199
200                 if (res == PJ_SUCCESS && (transport->tos || transport->cos)) {
201                         pj_sock_t sock;
202                         pj_qos_params qos_params;
203
204                         sock = pjsip_udp_transport_get_socket(transport->state->transport);
205                         pj_sock_get_qos_params(sock, &qos_params);
206                         set_qos(transport, &qos_params);
207                         pj_sock_set_qos_params(sock, &qos_params);
208                 }
209         } else if (transport->type == AST_TRANSPORT_TCP) {
210                 pjsip_tcp_transport_cfg cfg;
211
212                 pjsip_tcp_transport_cfg_default(&cfg, transport->host.addr.sa_family);
213                 cfg.bind_addr = transport->host;
214                 cfg.async_cnt = transport->async_operations;
215                 set_qos(transport, &cfg.qos_params);
216
217                 res = pjsip_tcp_transport_start3(ast_sip_get_pjsip_endpoint(), &cfg, &transport->state->factory);
218         } else if (transport->type == AST_TRANSPORT_TLS) {
219                 transport->tls.ca_list_file = pj_str((char*)transport->ca_list_file);
220                 transport->tls.cert_file = pj_str((char*)transport->cert_file);
221                 transport->tls.privkey_file = pj_str((char*)transport->privkey_file);
222                 transport->tls.password = pj_str((char*)transport->password);
223                 set_qos(transport, &transport->tls.qos_params);
224
225                 res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host, NULL, transport->async_operations, &transport->state->factory);
226         } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) {
227                 if (transport->cos || transport->tos) {
228                         ast_log(LOG_WARNING, "TOS and COS values ignored for websocket transport\n");
229                 }
230                 res = PJ_SUCCESS;
231         }
232
233         if (res != PJ_SUCCESS) {
234                 char msg[PJ_ERR_MSG_SIZE];
235
236                 pj_strerror(res, msg, sizeof(msg));
237                 ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
238                 return -1;
239         }
240         return 0;
241 }
242
243 /*! \brief Custom handler for turning a string protocol into an enum */
244 static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
245 {
246         struct ast_sip_transport *transport = obj;
247
248         if (!strcasecmp(var->value, "udp")) {
249                 transport->type = AST_TRANSPORT_UDP;
250         } else if (!strcasecmp(var->value, "tcp")) {
251                 transport->type = AST_TRANSPORT_TCP;
252         } else if (!strcasecmp(var->value, "tls")) {
253                 transport->type = AST_TRANSPORT_TLS;
254         } else if (!strcasecmp(var->value, "ws")) {
255                 transport->type = AST_TRANSPORT_WS;
256         } else if (!strcasecmp(var->value, "wss")) {
257                 transport->type = AST_TRANSPORT_WSS;
258         } else {
259                 return -1;
260         }
261
262         return 0;
263 }
264
265 static const char *transport_types[] = {
266         [AST_TRANSPORT_UDP] = "udp",
267         [AST_TRANSPORT_TCP] = "tcp",
268         [AST_TRANSPORT_TLS] = "tls",
269         [AST_TRANSPORT_WS] = "ws",
270         [AST_TRANSPORT_WSS] = "wss"
271 };
272
273 static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
274 {
275         const struct ast_sip_transport *transport = obj;
276
277         if (ARRAY_IN_BOUNDS(transport->type, transport_types)) {
278                 *buf = ast_strdup(transport_types[transport->type]);
279         }
280
281         return 0;
282 }
283
284 /*! \brief Custom handler for turning a string bind into a pj_sockaddr */
285 static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
286 {
287         struct ast_sip_transport *transport = obj;
288         pj_str_t buf;
289         int rc = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host);
290
291         return rc != PJ_SUCCESS ? -1 : 0;
292 }
293
294 static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
295 {
296         const struct ast_sip_transport *transport = obj;
297
298         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
299                 return -1;
300         }
301
302         /* include port as well as brackets if IPv6 */
303         pj_sockaddr_print(&transport->host, *buf, MAX_OBJECT_FIELD, 1 | 2);
304
305         return 0;
306 }
307
308 /*! \brief Custom handler for TLS boolean settings */
309 static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
310 {
311         struct ast_sip_transport *transport = obj;
312
313         if (!strcasecmp(var->name, "verify_server")) {
314                 transport->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
315         } else if (!strcasecmp(var->name, "verify_client")) {
316                 transport->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
317         } else if (!strcasecmp(var->name, "require_client_cert")) {
318                 transport->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
319         } else {
320                 return -1;
321         }
322
323         return 0;
324 }
325
326 static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
327 {
328         const struct ast_sip_transport *transport = obj;
329         *buf = ast_strdup(AST_YESNO(transport->tls.verify_server));
330         return 0;
331 }
332
333 static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
334 {
335         const struct ast_sip_transport *transport = obj;
336         *buf = ast_strdup(AST_YESNO(transport->tls.verify_client));
337         return 0;
338 }
339
340 static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
341 {
342         const struct ast_sip_transport *transport = obj;
343         *buf = ast_strdup(AST_YESNO(transport->tls.require_client_cert));
344         return 0;
345 }
346
347 /*! \brief Custom handler for TLS method setting */
348 static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
349 {
350         struct ast_sip_transport *transport = obj;
351
352         if (!strcasecmp(var->value, "default")) {
353                 transport->tls.method = PJSIP_SSL_DEFAULT_METHOD;
354         } else if (!strcasecmp(var->value, "unspecified")) {
355                 transport->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
356         } else if (!strcasecmp(var->value, "tlsv1")) {
357                 transport->tls.method = PJSIP_TLSV1_METHOD;
358         } else if (!strcasecmp(var->value, "sslv2")) {
359                 transport->tls.method = PJSIP_SSLV2_METHOD;
360         } else if (!strcasecmp(var->value, "sslv3")) {
361                 transport->tls.method = PJSIP_SSLV3_METHOD;
362         } else if (!strcasecmp(var->value, "sslv23")) {
363                 transport->tls.method = PJSIP_SSLV23_METHOD;
364         } else {
365                 return -1;
366         }
367
368         return 0;
369 }
370
371 static const char *tls_method_map[] = {
372         [PJSIP_SSL_DEFAULT_METHOD] = "default",
373         [PJSIP_SSL_UNSPECIFIED_METHOD] = "unspecified",
374         [PJSIP_TLSV1_METHOD] = "tlsv1",
375         [PJSIP_SSLV2_METHOD] = "sslv2",
376         [PJSIP_SSLV3_METHOD] = "sslv3",
377         [PJSIP_SSLV23_METHOD] = "sslv23",
378 };
379
380 static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
381 {
382         const struct ast_sip_transport *transport = obj;
383         if (ARRAY_IN_BOUNDS(transport->tls.method, tls_method_map)) {
384                 *buf = ast_strdup(tls_method_map[transport->tls.method]);
385         }
386         return 0;
387 }
388
389 /*! \brief Helper function which turns a cipher name into an identifier */
390 static pj_ssl_cipher cipher_name_to_id(const char *name)
391 {
392         pj_ssl_cipher ciphers[100], id = 0;
393         unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
394         int pos;
395
396         if (pj_ssl_cipher_get_availables(ciphers, &cipher_num)) {
397                 return 0;
398         }
399
400         for (pos = 0; pos < cipher_num; ++pos) {
401                 if (!pj_ssl_cipher_name(ciphers[pos]) ||
402                         strcmp(pj_ssl_cipher_name(ciphers[pos]), name)) {
403                         continue;
404                 }
405
406                 id = ciphers[pos];
407                 break;
408         }
409
410         return id;
411 }
412
413 /*! \brief Custom handler for TLS cipher setting */
414 static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
415 {
416         struct ast_sip_transport *transport = obj;
417         pj_ssl_cipher cipher;
418
419         if (transport->tls.ciphers_num == (SIP_TLS_MAX_CIPHERS - 1)) {
420                 return -1;
421         }
422
423         cipher = cipher_name_to_id(var->value);
424
425         if (!cipher) {
426                 /* TODO: Check this over/tweak - it's taken from pjsua for now */
427                 if (!strnicmp(var->value, "0x", 2)) {
428                         pj_str_t cipher_st = pj_str((char*)var->value + 2);
429                         cipher = pj_strtoul2(&cipher_st, NULL, 16);
430                 } else {
431                         cipher = atoi(var->value);
432                 }
433         }
434
435         if (pj_ssl_cipher_is_supported(cipher)) {
436                 transport->ciphers[transport->tls.ciphers_num++] = cipher;
437                 return 0;
438         } else {
439                 ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", var->value);
440                 return -1;
441         }
442 }
443
444 static int transport_tls_cipher_to_str(const void *obj, const intptr_t *args, char **buf)
445 {
446         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
447         const struct ast_sip_transport *transport = obj;
448         int i;
449
450         if (!str) {
451                 return -1;
452         }
453
454         for (i = 0; i < transport->tls.ciphers_num; ++i) {
455                 ast_str_append(&str, 0, "%s", pj_ssl_cipher_name(transport->ciphers[i]));
456                 if (i < transport->tls.ciphers_num - 1) {
457                         ast_str_append(&str, 0, ",");
458                 }
459         }
460
461         *buf = ast_strdup(ast_str_buffer(str));
462         return 0;
463 }
464
465 /*! \brief Custom handler for localnet setting */
466 static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
467 {
468         struct ast_sip_transport *transport = obj;
469         int error = 0;
470
471         if (!(transport->localnet = ast_append_ha("d", var->value, transport->localnet, &error))) {
472                 return -1;
473         }
474
475         return error;
476 }
477
478 static int localnet_to_vl(const void *obj, struct ast_variable **fields)
479 {
480         const struct ast_sip_transport *transport = obj;
481
482         char str[MAX_OBJECT_FIELD];
483         struct ast_variable *head = NULL;
484         struct ast_ha *ha = transport->localnet;
485
486         for (; ha; ha = ha->next) {
487                 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
488                 snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
489                         addr, ast_sockaddr_stringify_addr(&ha->netmask));
490
491                 ast_variable_list_append(&head, ast_variable_new("local_net", str, ""));
492         }
493
494         if (head) {
495                 *fields = head;
496         }
497
498         return 0;
499 }
500
501 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
502 {
503         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
504         const struct ast_sip_transport *transport = obj;
505
506         ast_ha_join(transport->localnet, &str);
507         *buf = ast_strdup(ast_str_buffer(str));
508         return 0;
509 }
510
511 /*! \brief Custom handler for TOS setting */
512 static int transport_tos_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
513 {
514         struct ast_sip_transport *transport = obj;
515         unsigned int value;
516
517         if (ast_str2tos(var->value, &value)) {
518                 ast_log(LOG_ERROR, "Error configuring transport '%s' - Could not "
519                         "interpret 'tos' value '%s'\n",
520                         ast_sorcery_object_get_id(transport), var->value);
521                 return -1;
522         }
523
524         if (value % 4) {
525                 value = value >> 2;
526                 value = value << 2;
527                 ast_log(LOG_WARNING,
528                         "transport '%s' - 'tos' value '%s' uses bits that are "
529                         "discarded when converted to DSCP. Using equivalent %u instead.\n",
530                         ast_sorcery_object_get_id(transport), var->value, value);
531         }
532
533         transport->tos = value;
534         return 0;
535 }
536
537 static int tos_to_str(const void *obj, const intptr_t *args, char **buf)
538 {
539         const struct ast_sip_transport *transport = obj;
540
541         if (ast_asprintf(buf, "%u", transport->tos) == -1) {
542                 return -1;
543         }
544         return 0;
545 }
546
547 static struct ao2_container *cli_get_container(void)
548 {
549         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
550         struct ao2_container *s_container;
551
552         container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport",
553                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
554         if (!container) {
555                 return NULL;
556         }
557
558         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
559                 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
560         if (!s_container) {
561                 return NULL;
562         }
563
564         if (ao2_container_dup(s_container, container, 0)) {
565                 ao2_ref(s_container, -1);
566                 return NULL;
567         }
568
569         return s_container;
570 }
571
572 static int cli_iterate(void *container, ao2_callback_fn callback, void *args)
573 {
574         const struct ast_sip_endpoint *endpoint = container;
575         struct ast_sip_transport *transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
576                 "transport", endpoint->transport);
577
578         if (!transport) {
579                 return -1;
580         }
581
582         return callback(transport, args, 0);
583 }
584
585 static void *cli_retrieve_by_id(const char *id)
586 {
587         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", id);
588 }
589
590 static int cli_print_header(void *obj, void *arg, int flags)
591 {
592         struct ast_sip_cli_context *context = arg;
593         int indent = CLI_INDENT_TO_SPACES(context->indent_level);
594         int filler = CLI_MAX_WIDTH - indent - 61;
595
596         ast_assert(context->output_buffer != NULL);
597
598         ast_str_append(&context->output_buffer, 0,
599                 "%*s:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress%*.*s>\n",
600                 indent, "Transport", filler, filler, CLI_HEADER_FILLER);
601
602         return 0;
603 }
604
605 static int cli_print_body(void *obj, void *arg, int flags)
606 {
607         struct ast_sip_transport *transport = obj;
608         struct ast_sip_cli_context *context = arg;
609         char hoststr[PJ_INET6_ADDRSTRLEN];
610
611         ast_assert(context->output_buffer != NULL);
612
613         pj_sockaddr_print(&transport->host, hoststr, sizeof(hoststr), 3);
614
615         ast_str_append(&context->output_buffer, 0, "%*s:  %-21s  %6s  %5u  %5u  %s\n",
616                 CLI_INDENT_TO_SPACES(context->indent_level), "Transport",
617                 ast_sorcery_object_get_id(transport),
618                 ARRAY_IN_BOUNDS(transport->type, transport_types) ? transport_types[transport->type] : "Unknown",
619                 transport->cos, transport->tos, hoststr);
620
621         if (context->show_details
622                 || (context->show_details_only_level_0 && context->indent_level == 0)) {
623                 ast_str_append(&context->output_buffer, 0, "\n");
624                 ast_sip_cli_print_sorcery_objectset(transport, context, 0);
625         }
626
627         return 0;
628 }
629
630 static struct ast_cli_entry cli_commands[] = {
631         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Transports",
632                 .command = "pjsip list transports",
633                 .usage = "Usage: pjsip list transports\n"
634                                  "       List the configured PJSIP Transports\n"),
635         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transports",
636                 .command = "pjsip show transports",
637                 .usage = "Usage: pjsip show transports\n"
638                                  "       Show the configured PJSIP Transport\n"),
639         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transport",
640                 .command = "pjsip show transport",
641                 .usage = "Usage: pjsip show transport <id>\n"
642                                  "       Show the configured PJSIP Transport\n"),
643 };
644
645 static struct ast_sip_cli_formatter_entry *cli_formatter;
646
647 /*! \brief Initialize sorcery with transport support */
648 int ast_sip_initialize_sorcery_transport(void)
649 {
650         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
651
652         ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport");
653
654         if (ast_sorcery_object_register_no_reload(sorcery, "transport", transport_alloc, NULL, transport_apply)) {
655                 return -1;
656         }
657
658         ast_sorcery_object_field_register(sorcery, "transport", "type", "", OPT_NOOP_T, 0, 0);
659         ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, NULL, 0, 0);
660         ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, NULL, 0, 0);
661         ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
662         ast_sorcery_object_field_register(sorcery, "transport", "ca_list_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_file));
663         ast_sorcery_object_field_register(sorcery, "transport", "cert_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, cert_file));
664         ast_sorcery_object_field_register(sorcery, "transport", "priv_key_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, privkey_file));
665         ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
666         ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_signaling_address));
667         ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_port", "0", OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_transport, external_signaling_port), 0, 65535);
668         ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
669         ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
670         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, NULL, 0, 0);
671         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, NULL, 0, 0);
672         ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, NULL, 0, 0);
673         ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, NULL, 0, 0);
674         ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, NULL, 0, 0);
675         ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, localnet_to_vl, 0, 0);
676         ast_sorcery_object_field_register_custom(sorcery, "transport", "tos", "0", transport_tos_handler, tos_to_str, NULL, 0, 0);
677         ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
678         ast_sorcery_object_field_register(sorcery, "transport", "websocket_write_timeout", AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR, OPT_INT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_transport, write_timeout), 1, INT_MAX);
679
680         ast_sip_register_endpoint_formatter(&endpoint_transport_formatter);
681
682         cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
683         if (!cli_formatter) {
684                 ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
685                 return -1;
686         }
687         cli_formatter->name = "transport";
688         cli_formatter->print_header = cli_print_header;
689         cli_formatter->print_body = cli_print_body;
690         cli_formatter->get_container = cli_get_container;
691         cli_formatter->iterate = cli_iterate;
692         cli_formatter->get_id = ast_sorcery_object_get_id;
693         cli_formatter->retrieve_by_id = cli_retrieve_by_id;
694
695         ast_sip_register_cli_formatter(cli_formatter);
696         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
697
698         return 0;
699 }
700
701 int ast_sip_destroy_sorcery_transport(void)
702 {
703         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
704         ast_sip_unregister_cli_formatter(cli_formatter);
705
706         return 0;
707 }