Merge "chan_sip: Check sip_pvt pointer in ast_channel_get_t38_state(c)"
[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                 /* The following check is a work-around for ASTERISK-25615.
220                  * When that issue is resolved in upstream pjproject, this check can be removed.
221                  */
222                 if (transport->async_operations > 1) {
223                         ast_log(LOG_ERROR, "Transport: %s: When protocol=tls, async_operations can't be > 1 (ASTERISK-25615)\n",
224                                         ast_sorcery_object_get_id(obj));
225                         return -1;
226                 }
227                 transport->tls.ca_list_file = pj_str((char*)transport->ca_list_file);
228 #ifdef HAVE_PJ_SSL_CERT_LOAD_FROM_FILES2
229                 transport->tls.ca_list_path = pj_str((char*)transport->ca_list_path);
230 #else
231                 if (!ast_strlen_zero(transport->ca_list_path)) {
232                         ast_log(LOG_WARNING, "Asterisk has been built against a version of pjproject that does not "
233                                         "support the 'ca_list_path' option. Please upgrade to version 2.4 or later.\n");
234                 }
235 #endif
236                 transport->tls.cert_file = pj_str((char*)transport->cert_file);
237                 transport->tls.privkey_file = pj_str((char*)transport->privkey_file);
238                 transport->tls.password = pj_str((char*)transport->password);
239                 set_qos(transport, &transport->tls.qos_params);
240
241                 res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host, NULL, transport->async_operations, &transport->state->factory);
242         } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) {
243                 if (transport->cos || transport->tos) {
244                         ast_log(LOG_WARNING, "TOS and COS values ignored for websocket transport\n");
245                 }
246                 res = PJ_SUCCESS;
247         }
248
249         if (res != PJ_SUCCESS) {
250                 char msg[PJ_ERR_MSG_SIZE];
251
252                 pj_strerror(res, msg, sizeof(msg));
253                 ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
254                 return -1;
255         }
256         return 0;
257 }
258
259 /*! \brief Custom handler for turning a string protocol into an enum */
260 static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
261 {
262         struct ast_sip_transport *transport = obj;
263
264         if (!strcasecmp(var->value, "udp")) {
265                 transport->type = AST_TRANSPORT_UDP;
266         } else if (!strcasecmp(var->value, "tcp")) {
267                 transport->type = AST_TRANSPORT_TCP;
268         } else if (!strcasecmp(var->value, "tls")) {
269                 transport->type = AST_TRANSPORT_TLS;
270         } else if (!strcasecmp(var->value, "ws")) {
271                 transport->type = AST_TRANSPORT_WS;
272         } else if (!strcasecmp(var->value, "wss")) {
273                 transport->type = AST_TRANSPORT_WSS;
274         } else {
275                 return -1;
276         }
277
278         return 0;
279 }
280
281 static const char *transport_types[] = {
282         [AST_TRANSPORT_UDP] = "udp",
283         [AST_TRANSPORT_TCP] = "tcp",
284         [AST_TRANSPORT_TLS] = "tls",
285         [AST_TRANSPORT_WS] = "ws",
286         [AST_TRANSPORT_WSS] = "wss"
287 };
288
289 static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
290 {
291         const struct ast_sip_transport *transport = obj;
292
293         if (ARRAY_IN_BOUNDS(transport->type, transport_types)) {
294                 *buf = ast_strdup(transport_types[transport->type]);
295         }
296
297         return 0;
298 }
299
300 /*! \brief Custom handler for turning a string bind into a pj_sockaddr */
301 static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
302 {
303         struct ast_sip_transport *transport = obj;
304         pj_str_t buf;
305         int rc = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host);
306
307         return rc != PJ_SUCCESS ? -1 : 0;
308 }
309
310 static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
311 {
312         const struct ast_sip_transport *transport = obj;
313
314         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
315                 return -1;
316         }
317
318         /* include port as well as brackets if IPv6 */
319         pj_sockaddr_print(&transport->host, *buf, MAX_OBJECT_FIELD, 1 | 2);
320
321         return 0;
322 }
323
324 /*! \brief Custom handler for TLS boolean settings */
325 static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
326 {
327         struct ast_sip_transport *transport = obj;
328
329         if (!strcasecmp(var->name, "verify_server")) {
330                 transport->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
331         } else if (!strcasecmp(var->name, "verify_client")) {
332                 transport->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
333         } else if (!strcasecmp(var->name, "require_client_cert")) {
334                 transport->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
335         } else {
336                 return -1;
337         }
338
339         return 0;
340 }
341
342 static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
343 {
344         const struct ast_sip_transport *transport = obj;
345         *buf = ast_strdup(AST_YESNO(transport->tls.verify_server));
346         return 0;
347 }
348
349 static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
350 {
351         const struct ast_sip_transport *transport = obj;
352         *buf = ast_strdup(AST_YESNO(transport->tls.verify_client));
353         return 0;
354 }
355
356 static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
357 {
358         const struct ast_sip_transport *transport = obj;
359         *buf = ast_strdup(AST_YESNO(transport->tls.require_client_cert));
360         return 0;
361 }
362
363 /*! \brief Custom handler for TLS method setting */
364 static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
365 {
366         struct ast_sip_transport *transport = obj;
367
368         if (ast_strlen_zero(var->value) || !strcasecmp(var->value, "default")) {
369                 transport->tls.method = PJSIP_SSL_DEFAULT_METHOD;
370         } else if (!strcasecmp(var->value, "unspecified")) {
371                 transport->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
372         } else if (!strcasecmp(var->value, "tlsv1")) {
373                 transport->tls.method = PJSIP_TLSV1_METHOD;
374         } else if (!strcasecmp(var->value, "sslv2")) {
375                 transport->tls.method = PJSIP_SSLV2_METHOD;
376         } else if (!strcasecmp(var->value, "sslv3")) {
377                 transport->tls.method = PJSIP_SSLV3_METHOD;
378         } else if (!strcasecmp(var->value, "sslv23")) {
379                 transport->tls.method = PJSIP_SSLV23_METHOD;
380         } else {
381                 return -1;
382         }
383
384         return 0;
385 }
386
387 static const char *tls_method_map[] = {
388         [PJSIP_SSL_UNSPECIFIED_METHOD] = "unspecified",
389         [PJSIP_TLSV1_METHOD] = "tlsv1",
390         [PJSIP_SSLV2_METHOD] = "sslv2",
391         [PJSIP_SSLV3_METHOD] = "sslv3",
392         [PJSIP_SSLV23_METHOD] = "sslv23",
393 };
394
395 static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
396 {
397         const struct ast_sip_transport *transport = obj;
398         if (ARRAY_IN_BOUNDS(transport->tls.method, tls_method_map)) {
399                 *buf = ast_strdup(tls_method_map[transport->tls.method]);
400         }
401         return 0;
402 }
403
404 /*! \brief Helper function which turns a cipher name into an identifier */
405 static pj_ssl_cipher cipher_name_to_id(const char *name)
406 {
407         pj_ssl_cipher ciphers[100];
408         pj_ssl_cipher id = 0;
409         unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
410         int pos;
411         const char *pos_name;
412
413         if (pj_ssl_cipher_get_availables(ciphers, &cipher_num)) {
414                 return 0;
415         }
416
417         for (pos = 0; pos < cipher_num; ++pos) {
418                 pos_name = pj_ssl_cipher_name(ciphers[pos]);
419                 if (!pos_name || strcmp(pos_name, name)) {
420                         continue;
421                 }
422
423                 id = ciphers[pos];
424                 break;
425         }
426
427         return id;
428 }
429
430 /*!
431  * \internal
432  * \brief Add a new cipher to the transport's cipher list array.
433  *
434  * \param transport Which transport to add the cipher to.
435  * \param name Cipher identifier name.
436  *
437  * \retval 0 on success.
438  * \retval -1 on error.
439  */
440 static int transport_cipher_add(struct ast_sip_transport *transport, const char *name)
441 {
442         pj_ssl_cipher cipher;
443         int idx;
444
445         cipher = cipher_name_to_id(name);
446         if (!cipher) {
447                 /* TODO: Check this over/tweak - it's taken from pjsua for now */
448                 if (!strnicmp(name, "0x", 2)) {
449                         pj_str_t cipher_st = pj_str((char *) name + 2);
450                         cipher = pj_strtoul2(&cipher_st, NULL, 16);
451                 } else {
452                         cipher = atoi(name);
453                 }
454         }
455
456         if (pj_ssl_cipher_is_supported(cipher)) {
457                 for (idx = transport->tls.ciphers_num; idx--;) {
458                         if (transport->ciphers[idx] == cipher) {
459                                 /* The cipher is already in the list. */
460                                 return 0;
461                         }
462                 }
463                 transport->ciphers[transport->tls.ciphers_num++] = cipher;
464                 return 0;
465         } else {
466                 ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", name);
467                 return -1;
468         }
469 }
470
471 /*! \brief Custom handler for TLS cipher setting */
472 static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
473 {
474         struct ast_sip_transport *transport = obj;
475         char *parse;
476         char *name;
477         int res = 0;
478
479         parse = ast_strdupa(S_OR(var->value, ""));
480         while ((name = strsep(&parse, ","))) {
481                 name = ast_strip(name);
482                 if (ast_strlen_zero(name)) {
483                         continue;
484                 }
485                 if (ARRAY_LEN(transport->ciphers) <= transport->tls.ciphers_num) {
486                         ast_log(LOG_ERROR, "Too many ciphers specified\n");
487                         res = -1;
488                         break;
489                 }
490                 res |= transport_cipher_add(transport, name);
491         }
492         return res ? -1 : 0;
493 }
494
495 static void cipher_to_str(char **buf, const pj_ssl_cipher *ciphers, unsigned int cipher_num)
496 {
497         struct ast_str *str;
498         int idx;
499
500         str = ast_str_create(128);
501         if (!str) {
502                 *buf = NULL;
503                 return;
504         }
505
506         for (idx = 0; idx < cipher_num; ++idx) {
507                 ast_str_append(&str, 0, "%s", pj_ssl_cipher_name(ciphers[idx]));
508                 if (idx < cipher_num - 1) {
509                         ast_str_append(&str, 0, ", ");
510                 }
511         }
512
513         *buf = ast_strdup(ast_str_buffer(str));
514         ast_free(str);
515 }
516
517 static int transport_tls_cipher_to_str(const void *obj, const intptr_t *args, char **buf)
518 {
519         const struct ast_sip_transport *transport = obj;
520
521         cipher_to_str(buf, transport->ciphers, transport->tls.ciphers_num);
522         return *buf ? 0 : -1;
523 }
524
525 static char *handle_pjsip_list_ciphers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
526 {
527         pj_ssl_cipher ciphers[100];
528         unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
529         char *buf;
530
531         switch (cmd) {
532         case CLI_INIT:
533                 e->command = "pjsip list ciphers";
534                 e->usage = "Usage: pjsip list ciphers\n"
535                         "       List available OpenSSL cipher names.\n";
536                 return NULL;
537         case CLI_GENERATE:
538                 return NULL;
539         }
540
541         if (pj_ssl_cipher_get_availables(ciphers, &cipher_num) || !cipher_num) {
542                 buf = NULL;
543         } else {
544                 cipher_to_str(&buf, ciphers, cipher_num);
545         }
546
547         if (!ast_strlen_zero(buf)) {
548                 ast_cli(a->fd, "Available ciphers: '%s'\n", buf);
549         } else {
550                 ast_cli(a->fd, "No available ciphers\n");
551         }
552         ast_free(buf);
553         return CLI_SUCCESS;
554 }
555
556 /*! \brief Custom handler for localnet setting */
557 static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
558 {
559         struct ast_sip_transport *transport = obj;
560         int error = 0;
561
562         if (ast_strlen_zero(var->value)) {
563                 ast_free_ha(transport->localnet);
564                 transport->localnet = NULL;
565                 return 0;
566         }
567
568         if (!(transport->localnet = ast_append_ha("d", var->value, transport->localnet, &error))) {
569                 return -1;
570         }
571
572         return error;
573 }
574
575 static int localnet_to_vl(const void *obj, struct ast_variable **fields)
576 {
577         const struct ast_sip_transport *transport = obj;
578
579         char str[MAX_OBJECT_FIELD];
580         struct ast_variable *head = NULL;
581         struct ast_ha *ha = transport->localnet;
582
583         for (; ha; ha = ha->next) {
584                 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
585                 snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
586                         addr, ast_sockaddr_stringify_addr(&ha->netmask));
587
588                 ast_variable_list_append(&head, ast_variable_new("local_net", str, ""));
589         }
590
591         if (head) {
592                 *fields = head;
593         }
594
595         return 0;
596 }
597
598 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
599 {
600         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
601         const struct ast_sip_transport *transport = obj;
602
603         ast_ha_join(transport->localnet, &str);
604         *buf = ast_strdup(ast_str_buffer(str));
605         return 0;
606 }
607
608 /*! \brief Custom handler for TOS setting */
609 static int transport_tos_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
610 {
611         struct ast_sip_transport *transport = obj;
612         unsigned int value;
613
614         if (ast_str2tos(var->value, &value)) {
615                 ast_log(LOG_ERROR, "Error configuring transport '%s' - Could not "
616                         "interpret 'tos' value '%s'\n",
617                         ast_sorcery_object_get_id(transport), var->value);
618                 return -1;
619         }
620
621         if (value % 4) {
622                 value = value >> 2;
623                 value = value << 2;
624                 ast_log(LOG_WARNING,
625                         "transport '%s' - 'tos' value '%s' uses bits that are "
626                         "discarded when converted to DSCP. Using equivalent %u instead.\n",
627                         ast_sorcery_object_get_id(transport), var->value, value);
628         }
629
630         transport->tos = value;
631         return 0;
632 }
633
634 static int tos_to_str(const void *obj, const intptr_t *args, char **buf)
635 {
636         const struct ast_sip_transport *transport = obj;
637
638         if (ast_asprintf(buf, "%u", transport->tos) == -1) {
639                 return -1;
640         }
641         return 0;
642 }
643
644 static struct ao2_container *cli_get_container(const char *regex)
645 {
646         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
647         struct ao2_container *s_container;
648
649         container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "transport",
650                 regex);
651         if (!container) {
652                 return NULL;
653         }
654
655         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
656                 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
657         if (!s_container) {
658                 return NULL;
659         }
660
661         if (ao2_container_dup(s_container, container, 0)) {
662                 ao2_ref(s_container, -1);
663                 return NULL;
664         }
665
666         return s_container;
667 }
668
669 static int cli_iterate(void *container, ao2_callback_fn callback, void *args)
670 {
671         const struct ast_sip_endpoint *endpoint = container;
672         struct ast_sip_transport *transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
673                 "transport", endpoint->transport);
674
675         if (!transport) {
676                 return -1;
677         }
678
679         return callback(transport, args, 0);
680 }
681
682 static void *cli_retrieve_by_id(const char *id)
683 {
684         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", id);
685 }
686
687 static int cli_print_header(void *obj, void *arg, int flags)
688 {
689         struct ast_sip_cli_context *context = arg;
690         int indent = CLI_INDENT_TO_SPACES(context->indent_level);
691         int filler = CLI_MAX_WIDTH - indent - 61;
692
693         ast_assert(context->output_buffer != NULL);
694
695         ast_str_append(&context->output_buffer, 0,
696                 "%*s:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress%*.*s>\n",
697                 indent, "Transport", filler, filler, CLI_HEADER_FILLER);
698
699         return 0;
700 }
701
702 static int cli_print_body(void *obj, void *arg, int flags)
703 {
704         struct ast_sip_transport *transport = obj;
705         struct ast_sip_cli_context *context = arg;
706         char hoststr[PJ_INET6_ADDRSTRLEN];
707
708         ast_assert(context->output_buffer != NULL);
709
710         pj_sockaddr_print(&transport->host, hoststr, sizeof(hoststr), 3);
711
712         ast_str_append(&context->output_buffer, 0, "%*s:  %-21s  %6s  %5u  %5u  %s\n",
713                 CLI_INDENT_TO_SPACES(context->indent_level), "Transport",
714                 ast_sorcery_object_get_id(transport),
715                 ARRAY_IN_BOUNDS(transport->type, transport_types) ? transport_types[transport->type] : "Unknown",
716                 transport->cos, transport->tos, hoststr);
717
718         if (context->show_details
719                 || (context->show_details_only_level_0 && context->indent_level == 0)) {
720                 ast_str_append(&context->output_buffer, 0, "\n");
721                 ast_sip_cli_print_sorcery_objectset(transport, context, 0);
722         }
723
724         return 0;
725 }
726
727 static struct ast_cli_entry cli_commands[] = {
728         AST_CLI_DEFINE(handle_pjsip_list_ciphers, "List available OpenSSL cipher names"),
729         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Transports",
730                 .command = "pjsip list transports",
731                 .usage = "Usage: pjsip list transports [ like <pattern> ]\n"
732                                 "       List the configured PJSIP Transports\n"
733                                 "       Optional regular expression pattern is used to filter the list.\n"),
734         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transports",
735                 .command = "pjsip show transports",
736                 .usage = "Usage: pjsip show transports [ like <pattern> ]\n"
737                                 "       Show the configured PJSIP Transport\n"
738                                 "       Optional regular expression pattern is used to filter the list.\n"),
739         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transport",
740                 .command = "pjsip show transport",
741                 .usage = "Usage: pjsip show transport <id>\n"
742                                  "       Show the configured PJSIP Transport\n"),
743 };
744
745 static struct ast_sip_cli_formatter_entry *cli_formatter;
746
747 /*! \brief Initialize sorcery with transport support */
748 int ast_sip_initialize_sorcery_transport(void)
749 {
750         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
751
752         ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport");
753
754         if (ast_sorcery_object_register_no_reload(sorcery, "transport", transport_alloc, NULL, transport_apply)) {
755                 return -1;
756         }
757
758         ast_sorcery_object_field_register(sorcery, "transport", "type", "", OPT_NOOP_T, 0, 0);
759         ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, NULL, 0, 0);
760         ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, NULL, 0, 0);
761         ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
762         ast_sorcery_object_field_register(sorcery, "transport", "ca_list_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_file));
763         ast_sorcery_object_field_register(sorcery, "transport", "ca_list_path", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_path));
764         ast_sorcery_object_field_register(sorcery, "transport", "cert_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, cert_file));
765         ast_sorcery_object_field_register(sorcery, "transport", "priv_key_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, privkey_file));
766         ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
767         ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_signaling_address));
768         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);
769         ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
770         ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
771         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, NULL, 0, 0);
772         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, NULL, 0, 0);
773         ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, NULL, 0, 0);
774         ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, NULL, 0, 0);
775         ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, NULL, 0, 0);
776         ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, localnet_to_vl, 0, 0);
777         ast_sorcery_object_field_register_custom(sorcery, "transport", "tos", "0", transport_tos_handler, tos_to_str, NULL, 0, 0);
778         ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
779         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);
780
781         internal_sip_register_endpoint_formatter(&endpoint_transport_formatter);
782
783         cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
784         if (!cli_formatter) {
785                 ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
786                 return -1;
787         }
788         cli_formatter->name = "transport";
789         cli_formatter->print_header = cli_print_header;
790         cli_formatter->print_body = cli_print_body;
791         cli_formatter->get_container = cli_get_container;
792         cli_formatter->iterate = cli_iterate;
793         cli_formatter->get_id = ast_sorcery_object_get_id;
794         cli_formatter->retrieve_by_id = cli_retrieve_by_id;
795
796         ast_sip_register_cli_formatter(cli_formatter);
797         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
798
799         return 0;
800 }
801
802 int ast_sip_destroy_sorcery_transport(void)
803 {
804         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
805         ast_sip_unregister_cli_formatter(cli_formatter);
806
807         internal_sip_unregister_endpoint_formatter(&endpoint_transport_formatter);
808
809         return 0;
810 }