res_pjsip: AMI commands and events.
[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/logger.h"
26 #include "asterisk/astobj2.h"
27 #include "asterisk/sorcery.h"
28 #include "asterisk/acl.h"
29 #include "include/res_pjsip_private.h"
30
31 static int sip_transport_to_ami(const struct ast_sip_transport *transport,
32                                 struct ast_str **buf)
33 {
34         return ast_sip_sorcery_object_to_ami(transport, buf);
35 }
36
37 static int format_ami_endpoint_transport(const struct ast_sip_endpoint *endpoint,
38                                          struct ast_sip_ami *ami)
39 {
40         RAII_VAR(struct ast_str *, buf,
41                  ast_sip_create_ami_event("TransportDetail", ami), ast_free);
42         RAII_VAR(struct ast_sip_transport *,
43                  transport, ast_sorcery_retrieve_by_id(
44                          ast_sip_get_sorcery(), "transport",
45                          endpoint->transport), ao2_cleanup);
46         if (!buf) {
47                 return -1;
48         }
49
50         if (!transport) {
51                 astman_send_error_va(ami->s, ami->m, "Unable to retrieve "
52                                      "transport %s\n", endpoint->transport);
53                 return -1;
54         }
55
56         sip_transport_to_ami(transport, &buf);
57
58         ast_str_append(&buf, 0, "EndpointName: %s\r\n",
59                        ast_sorcery_object_get_id(endpoint));
60
61         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
62         return 0;
63 }
64
65 struct ast_sip_endpoint_formatter endpoint_transport_formatter = {
66         .format_ami = format_ami_endpoint_transport
67 };
68
69 static int destroy_transport_state(void *data)
70 {
71         pjsip_transport *transport = data;
72         pjsip_transport_shutdown(transport);
73         return 0;
74 }
75
76 /*! \brief Destructor for transport state information */
77 static void transport_state_destroy(void *obj)
78 {
79         struct ast_sip_transport_state *state = obj;
80
81         if (state->transport) {
82                 ast_sip_push_task_synchronous(NULL, destroy_transport_state, state->transport);
83         }
84 }
85
86 /*! \brief Destructor for transport */
87 static void transport_destroy(void *obj)
88 {
89         struct ast_sip_transport *transport = obj;
90
91         ast_string_field_free_memory(transport);
92         ast_free_ha(transport->localnet);
93
94         if (transport->external_address_refresher) {
95                 ast_dnsmgr_release(transport->external_address_refresher);
96         }
97
98         ao2_cleanup(transport->state);
99 }
100
101 /*! \brief Allocator for transport */
102 static void *transport_alloc(const char *name)
103 {
104         struct ast_sip_transport *transport = ast_sorcery_generic_alloc(sizeof(*transport), transport_destroy);
105
106         if (!transport) {
107                 return NULL;
108         }
109
110         if (ast_string_field_init(transport, 256)) {
111                 ao2_cleanup(transport);
112                 return NULL;
113         }
114
115         pjsip_tls_setting_default(&transport->tls);
116         transport->tls.ciphers = transport->ciphers;
117
118         return transport;
119 }
120
121 static void set_qos(struct ast_sip_transport *transport, pj_qos_params *qos)
122 {
123         if (transport->tos) {
124                 qos->flags |= PJ_QOS_PARAM_HAS_DSCP;
125                 qos->dscp_val = transport->tos;
126         }
127         if (transport->cos) {
128                 qos->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
129                 qos->so_prio = transport->cos;
130         }
131 }
132
133 /*! \brief Apply handler for transports */
134 static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
135 {
136         struct ast_sip_transport *transport = obj;
137         RAII_VAR(struct ast_sip_transport *, existing, ast_sorcery_retrieve_by_id(sorcery, "transport", ast_sorcery_object_get_id(obj)), ao2_cleanup);
138         pj_status_t res = -1;
139
140         if (!existing || !existing->state) {
141                 if (!(transport->state = ao2_alloc(sizeof(*transport->state), transport_state_destroy))) {
142                         ast_log(LOG_ERROR, "Transport state for '%s' could not be allocated\n", ast_sorcery_object_get_id(obj));
143                         return -1;
144                 }
145         } else {
146                 transport->state = existing->state;
147                 ao2_ref(transport->state, +1);
148         }
149
150         /* Once active a transport can not be reconfigured */
151         if (transport->state->transport || transport->state->factory) {
152                 return -1;
153         }
154
155         if (transport->host.addr.sa_family != PJ_AF_INET && transport->host.addr.sa_family != PJ_AF_INET6) {
156                 ast_log(LOG_ERROR, "Transport '%s' could not be started as binding not specified\n", ast_sorcery_object_get_id(obj));
157                 return -1;
158         }
159
160         /* Set default port if not present */
161         if (!pj_sockaddr_get_port(&transport->host)) {
162                 pj_sockaddr_set_port(&transport->host, (transport->type == AST_TRANSPORT_TLS) ? 5061 : 5060);
163         }
164
165         /* Now that we know what address family we can set up a dnsmgr refresh for the external media address if present */
166         if (!ast_strlen_zero(transport->external_signaling_address)) {
167                 if (transport->host.addr.sa_family == pj_AF_INET()) {
168                         transport->external_address.ss.ss_family = AF_INET;
169                 } else if (transport->host.addr.sa_family == pj_AF_INET6()) {
170                         transport->external_address.ss.ss_family = AF_INET6;
171                 } else {
172                         ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external signaling address\n",
173                                         ast_sorcery_object_get_id(obj));
174                         return -1;
175                 }
176
177                 if (ast_dnsmgr_lookup(transport->external_signaling_address, &transport->external_address, &transport->external_address_refresher, NULL) < 0) {
178                         ast_log(LOG_ERROR, "Could not create dnsmgr for external signaling address on '%s'\n", ast_sorcery_object_get_id(obj));
179                         return -1;
180                 }
181         }
182
183         if (transport->type == AST_TRANSPORT_UDP) {
184                 if (transport->host.addr.sa_family == pj_AF_INET()) {
185                         res = pjsip_udp_transport_start(ast_sip_get_pjsip_endpoint(), &transport->host.ipv4, NULL, transport->async_operations, &transport->state->transport);
186                 } else if (transport->host.addr.sa_family == pj_AF_INET6()) {
187                         res = pjsip_udp_transport_start6(ast_sip_get_pjsip_endpoint(), &transport->host.ipv6, NULL, transport->async_operations, &transport->state->transport);
188                 }
189
190                 if (res == PJ_SUCCESS && (transport->tos || transport->cos)) {
191                         pj_sock_t sock;
192                         pj_qos_params qos_params;
193
194                         sock = pjsip_udp_transport_get_socket(transport->state->transport);
195                         pj_sock_get_qos_params(sock, &qos_params);
196                         set_qos(transport, &qos_params);
197                         pj_sock_set_qos_params(sock, &qos_params);
198                 }
199         } else if (transport->type == AST_TRANSPORT_TCP) {
200                 pjsip_tcp_transport_cfg cfg;
201
202                 pjsip_tcp_transport_cfg_default(&cfg, transport->host.addr.sa_family);
203                 cfg.bind_addr = transport->host;
204                 cfg.async_cnt = transport->async_operations;
205                 set_qos(transport, &cfg.qos_params);
206
207                 res = pjsip_tcp_transport_start3(ast_sip_get_pjsip_endpoint(), &cfg, &transport->state->factory);
208         } else if (transport->type == AST_TRANSPORT_TLS) {
209                 transport->tls.ca_list_file = pj_str((char*)transport->ca_list_file);
210                 transport->tls.cert_file = pj_str((char*)transport->cert_file);
211                 transport->tls.privkey_file = pj_str((char*)transport->privkey_file);
212                 transport->tls.password = pj_str((char*)transport->password);
213                 set_qos(transport, &transport->tls.qos_params);
214
215                 res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host, NULL, transport->async_operations, &transport->state->factory);
216         } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) {
217                 if (transport->cos || transport->tos) {
218                         ast_log(LOG_WARNING, "TOS and COS values ignored for websocket transport\n");
219                 }
220                 res = PJ_SUCCESS;
221         }
222
223         if (res != PJ_SUCCESS) {
224                 char msg[PJ_ERR_MSG_SIZE];
225
226                 pj_strerror(res, msg, sizeof(msg));
227                 ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
228                 return -1;
229         }
230         return 0;
231 }
232
233 /*! \brief Custom handler for turning a string protocol into an enum */
234 static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
235 {
236         struct ast_sip_transport *transport = obj;
237
238         if (!strcasecmp(var->value, "udp")) {
239                 transport->type = AST_TRANSPORT_UDP;
240         } else if (!strcasecmp(var->value, "tcp")) {
241                 transport->type = AST_TRANSPORT_TCP;
242         } else if (!strcasecmp(var->value, "tls")) {
243                 transport->type = AST_TRANSPORT_TLS;
244         } else if (!strcasecmp(var->value, "ws")) {
245                 transport->type = AST_TRANSPORT_WS;
246         } else if (!strcasecmp(var->value, "wss")) {
247                 transport->type = AST_TRANSPORT_WSS;
248         } else {
249                 return -1;
250         }
251
252         return 0;
253 }
254
255 static const char *transport_types[] = {
256         [AST_TRANSPORT_UDP] = "udp",
257         [AST_TRANSPORT_TCP] = "tcp",
258         [AST_TRANSPORT_TLS] = "tls",
259         [AST_TRANSPORT_WS] = "ws",
260         [AST_TRANSPORT_WSS] = "wss"
261 };
262
263 static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
264 {
265         const struct ast_sip_transport *transport = obj;
266
267         if (ARRAY_IN_BOUNDS(transport->type, transport_types)) {
268                 *buf = ast_strdup(transport_types[transport->type]);
269         }
270
271         return 0;
272 }
273
274 /*! \brief Custom handler for turning a string bind into a pj_sockaddr */
275 static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
276 {
277         struct ast_sip_transport *transport = obj;
278         pj_str_t buf;
279
280         return (pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &transport->host) != PJ_SUCCESS) ? -1 : 0;
281 }
282
283 static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
284 {
285         const struct ast_sip_transport *transport = obj;
286
287         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
288                 return -1;
289         }
290
291         /* include port as well as brackets if IPv6 */
292         pj_sockaddr_print(&transport->host, *buf, MAX_OBJECT_FIELD, 1 | 2);
293
294         return 0;
295 }
296
297 /*! \brief Custom handler for TLS boolean settings */
298 static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
299 {
300         struct ast_sip_transport *transport = obj;
301
302         if (!strcasecmp(var->name, "verify_server")) {
303                 transport->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
304         } else if (!strcasecmp(var->name, "verify_client")) {
305                 transport->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
306         } else if (!strcasecmp(var->name, "require_client_cert")) {
307                 transport->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
308         } else {
309                 return -1;
310         }
311
312         return 0;
313 }
314
315 static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
316 {
317         const struct ast_sip_transport *transport = obj;
318         *buf = ast_strdup(AST_YESNO(transport->tls.verify_server));
319         return 0;
320 }
321
322 static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
323 {
324         const struct ast_sip_transport *transport = obj;
325         *buf = ast_strdup(AST_YESNO(transport->tls.verify_client));
326         return 0;
327 }
328
329 static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
330 {
331         const struct ast_sip_transport *transport = obj;
332         *buf = ast_strdup(AST_YESNO(transport->tls.require_client_cert));
333         return 0;
334 }
335
336 /*! \brief Custom handler for TLS method setting */
337 static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
338 {
339         struct ast_sip_transport *transport = obj;
340
341         if (!strcasecmp(var->value, "default")) {
342                 transport->tls.method = PJSIP_SSL_DEFAULT_METHOD;
343         } else if (!strcasecmp(var->value, "unspecified")) {
344                 transport->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
345         } else if (!strcasecmp(var->value, "tlsv1")) {
346                 transport->tls.method = PJSIP_TLSV1_METHOD;
347         } else if (!strcasecmp(var->value, "sslv2")) {
348                 transport->tls.method = PJSIP_SSLV2_METHOD;
349         } else if (!strcasecmp(var->value, "sslv3")) {
350                 transport->tls.method = PJSIP_SSLV3_METHOD;
351         } else if (!strcasecmp(var->value, "sslv23")) {
352                 transport->tls.method = PJSIP_SSLV23_METHOD;
353         } else {
354                 return -1;
355         }
356
357         return 0;
358 }
359
360 static const char *tls_method_map[] = {
361         [PJSIP_SSL_DEFAULT_METHOD] = "default",
362         [PJSIP_SSL_UNSPECIFIED_METHOD] = "unspecified",
363         [PJSIP_TLSV1_METHOD] = "tlsv1",
364         [PJSIP_SSLV2_METHOD] = "sslv2",
365         [PJSIP_SSLV3_METHOD] = "sslv3",
366         [PJSIP_SSLV23_METHOD] = "sslv23",
367 };
368
369 static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
370 {
371         const struct ast_sip_transport *transport = obj;
372         if (ARRAY_IN_BOUNDS(transport->tls.method, tls_method_map)) {
373                 *buf = ast_strdup(tls_method_map[transport->tls.method]);
374         }
375         return 0;
376 }
377
378 /*! \brief Custom handler for TLS cipher setting */
379 static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
380 {
381         struct ast_sip_transport *transport = obj;
382         pj_ssl_cipher cipher;
383
384         if (transport->tls.ciphers_num == (SIP_TLS_MAX_CIPHERS - 1)) {
385                 return -1;
386         }
387
388         /* TODO: Check this over/tweak - it's taken from pjsua for now */
389         if (!strnicmp(var->value, "0x", 2)) {
390                 pj_str_t cipher_st = pj_str((char*)var->value + 2);
391                 cipher = pj_strtoul2(&cipher_st, NULL, 16);
392         } else {
393                 cipher = atoi(var->value);
394         }
395
396         if (pj_ssl_cipher_is_supported(cipher)) {
397                 transport->ciphers[transport->tls.ciphers_num++] = cipher;
398                 return 0;
399         } else {
400                 ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", var->value);
401                 return -1;
402         }
403 }
404
405 static int transport_tls_cipher_to_str(const void *obj, const intptr_t *args, char **buf)
406 {
407         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
408         const struct ast_sip_transport *transport = obj;
409         int i;
410
411         if (!str) {
412                 return -1;
413         }
414
415         for (i = 0; i < transport->tls.ciphers_num; ++i) {
416                 ast_str_append(&str, 0, "%s", pj_ssl_cipher_name(transport->ciphers[i]));
417                 if (i < transport->tls.ciphers_num - 1) {
418                         ast_str_append(&str, 0, ",");
419                 }
420         }
421
422         *buf = ast_strdup(ast_str_buffer(str));
423         return 0;
424 }
425
426 /*! \brief Custom handler for localnet setting */
427 static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
428 {
429         struct ast_sip_transport *transport = obj;
430         int error = 0;
431
432         if (!(transport->localnet = ast_append_ha("d", var->value, transport->localnet, &error))) {
433                 return -1;
434         }
435
436         return error;
437 }
438
439 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
440 {
441         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
442         const struct ast_sip_transport *transport = obj;
443
444         ast_ha_join(transport->localnet, &str);
445         *buf = ast_strdup(ast_str_buffer(str));
446         return 0;
447 }
448
449 /*! \brief Initialize sorcery with transport support */
450 int ast_sip_initialize_sorcery_transport(struct ast_sorcery *sorcery)
451 {
452         ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport");
453
454         if (ast_sorcery_object_register_no_reload(sorcery, "transport", transport_alloc, NULL, transport_apply)) {
455                 return -1;
456         }
457
458         ast_sorcery_object_field_register(sorcery, "transport", "type", "", OPT_NOOP_T, 0, 0);
459         ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, 0, 0);
460         ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, 0, 0);
461         ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
462         ast_sorcery_object_field_register(sorcery, "transport", "ca_list_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, ca_list_file));
463         ast_sorcery_object_field_register(sorcery, "transport", "cert_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, cert_file));
464         ast_sorcery_object_field_register(sorcery, "transport", "priv_key_file", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, privkey_file));
465         ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
466         ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_signaling_address));
467         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);
468         ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
469         ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
470         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, 0, 0);
471         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, 0, 0);
472         ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, 0, 0);
473         ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, 0, 0);
474         ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, 0, 0);
475         ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, 0, 0);
476         ast_sorcery_object_field_register(sorcery, "transport", "tos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, tos));
477         ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
478
479         ast_sip_register_endpoint_formatter(&endpoint_transport_formatter);
480         return 0;
481 }