res_pjsip: Fix warning by deferring implicit type cast.
[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 <math.h>
22 #include <pjsip.h>
23 #include <pjlib.h>
24
25 #include "asterisk/res_pjsip.h"
26 #include "asterisk/res_pjsip_cli.h"
27 #include "asterisk/logger.h"
28 #include "asterisk/astobj2.h"
29 #include "asterisk/sorcery.h"
30 #include "asterisk/acl.h"
31 #include "asterisk/utils.h"
32 #include "include/res_pjsip_private.h"
33 #include "asterisk/http_websocket.h"
34
35 #define MAX_POINTER_STRING 33
36
37 /*! \brief Default number of state container buckets */
38 #define DEFAULT_STATE_BUCKETS 53
39 static struct ao2_container *transport_states;
40
41 struct internal_state {
42         char *id;
43         /*! Set if there was a change detected */
44         int change_detected;
45         /*! \brief Transport configuration object */
46         struct ast_sip_transport *transport;
47         /*! \brief Transport state information */
48         struct ast_sip_transport_state *state;
49 };
50
51 static void temp_state_store_cleanup(void *data)
52 {
53         struct ast_sip_transport_state **temp_state = data;
54
55         ao2_cleanup(*temp_state);
56         ast_free(data);
57 }
58
59 AST_THREADSTORAGE_CUSTOM(temp_state_store, NULL, temp_state_store_cleanup);
60
61 /*! \brief hashing function for state objects */
62 static int internal_state_hash(const void *obj, const int flags)
63 {
64         const struct internal_state *object;
65         const char *key;
66
67         switch (flags & OBJ_SEARCH_MASK) {
68         case OBJ_SEARCH_KEY:
69                 key = obj;
70                 break;
71         case OBJ_SEARCH_OBJECT:
72                 object = obj;
73                 key = object->id;
74                 break;
75         default:
76                 ast_assert(0);
77                 return 0;
78         }
79         return ast_str_hash(key);
80 }
81
82 /*! \brief comparator function for state objects */
83 static int internal_state_cmp(void *obj, void *arg, int flags)
84 {
85         const struct internal_state *object_left = obj;
86         const struct internal_state *object_right = arg;
87         const char *right_key = arg;
88         int cmp;
89
90         switch (flags & OBJ_SEARCH_MASK) {
91         case OBJ_SEARCH_OBJECT:
92                 right_key = object_right->id;
93                 /* Fall through */
94         case OBJ_SEARCH_KEY:
95                 cmp = strcmp(object_left->id, right_key);
96                 break;
97         case OBJ_SEARCH_PARTIAL_KEY:
98                 /* Not supported by container. */
99                 ast_assert(0);
100                 return 0;
101         default:
102                 cmp = 0;
103                 break;
104         }
105         if (cmp) {
106                 return 0;
107         }
108         return CMP_MATCH;
109 }
110
111 /*! \brief hashing function for state objects */
112 static int transport_state_hash(const void *obj, const int flags)
113 {
114         const struct ast_sip_transport_state *object;
115         const char *key;
116
117         switch (flags & OBJ_SEARCH_MASK) {
118         case OBJ_SEARCH_KEY:
119                 key = obj;
120                 break;
121         case OBJ_SEARCH_OBJECT:
122                 object = obj;
123                 key = object->id;
124                 break;
125         default:
126                 ast_assert(0);
127                 return 0;
128         }
129         return ast_str_hash(key);
130 }
131
132 /*! \brief comparator function for state objects */
133 static int transport_state_cmp(void *obj, void *arg, int flags)
134 {
135         const struct ast_sip_transport_state *object_left = obj;
136         const struct ast_sip_transport_state *object_right = arg;
137         const char *right_key = arg;
138         int cmp;
139
140         switch (flags & OBJ_SEARCH_MASK) {
141         case OBJ_SEARCH_OBJECT:
142                 right_key = object_right->id;
143                 /* Fall through */
144         case OBJ_SEARCH_KEY:
145                 cmp = strcmp(object_left->id, right_key);
146                 break;
147         case OBJ_SEARCH_PARTIAL_KEY:
148                 /* Not supported by container. */
149                 ast_assert(0);
150                 return 0;
151         default:
152                 cmp = 0;
153                 break;
154         }
155         if (cmp) {
156                 return 0;
157         }
158         return CMP_MATCH;
159 }
160
161 static int sip_transport_to_ami(const struct ast_sip_transport *transport,
162                                 struct ast_str **buf)
163 {
164         return ast_sip_sorcery_object_to_ami(transport, buf);
165 }
166
167 static int format_ami_endpoint_transport(const struct ast_sip_endpoint *endpoint,
168                                          struct ast_sip_ami *ami)
169 {
170         RAII_VAR(struct ast_str *, buf, NULL, ast_free);
171         RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup);
172
173         if (ast_strlen_zero(endpoint->transport)) {
174                 return 0;
175         }
176
177         buf = ast_sip_create_ami_event("TransportDetail", ami);
178         if (!buf) {
179                 return -1;
180         }
181
182         transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
183                 endpoint->transport);
184         if (!transport) {
185                 astman_send_error_va(ami->s, ami->m, "Unable to retrieve "
186                                      "transport %s\n", endpoint->transport);
187                 return -1;
188         }
189
190         sip_transport_to_ami(transport, &buf);
191
192         ast_str_append(&buf, 0, "EndpointName: %s\r\n",
193                        ast_sorcery_object_get_id(endpoint));
194
195         astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
196         ami->count++;
197
198         return 0;
199 }
200
201 struct ast_sip_endpoint_formatter endpoint_transport_formatter = {
202         .format_ami = format_ami_endpoint_transport
203 };
204
205 static void set_qos(struct ast_sip_transport *transport, pj_qos_params *qos)
206 {
207         int tos_as_dscp = transport->tos >> 2;
208
209         if (transport->tos) {
210                 qos->flags |= PJ_QOS_PARAM_HAS_DSCP;
211                 qos->dscp_val = tos_as_dscp;
212         }
213         if (transport->cos) {
214                 qos->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
215                 qos->so_prio = transport->cos;
216         }
217 }
218
219 /*! \brief Destructor for transport */
220 static void sip_transport_destroy(void *obj)
221 {
222         struct ast_sip_transport *transport = obj;
223
224         ast_string_field_free_memory(transport);
225 }
226
227 /*! \brief Allocator for transport */
228 static void *sip_transport_alloc(const char *name)
229 {
230         struct ast_sip_transport *transport = ast_sorcery_generic_alloc(sizeof(*transport), sip_transport_destroy);
231
232         if (!transport) {
233                 return NULL;
234         }
235
236         if (ast_string_field_init(transport, 256)) {
237                 ao2_cleanup(transport);
238                 return NULL;
239         }
240
241         return transport;
242 }
243
244 static int destroy_sip_transport_state(void *data)
245 {
246         struct ast_sip_transport_state *transport_state = data;
247
248         ast_free(transport_state->id);
249         ast_free_ha(transport_state->localnet);
250
251         if (transport_state->external_signaling_address_refresher) {
252                 ast_dnsmgr_release(transport_state->external_signaling_address_refresher);
253         }
254         if (transport_state->external_media_address_refresher) {
255                 ast_dnsmgr_release(transport_state->external_media_address_refresher);
256         }
257         if (transport_state->transport) {
258                 pjsip_transport_shutdown(transport_state->transport);
259         }
260
261         return 0;
262 }
263
264 /*! \brief Destructor for ast_sip_transport state information */
265 static void sip_transport_state_destroy(void *obj)
266 {
267         struct ast_sip_transport_state *state = obj;
268
269         ast_sip_push_task_synchronous(NULL, destroy_sip_transport_state, state);
270 }
271
272 /*! \brief Destructor for ast_sip_transport state information */
273 static void internal_state_destroy(void *obj)
274 {
275         struct internal_state *state = obj;
276
277         ast_free(state->id);
278         ao2_cleanup(state->transport);
279         ao2_cleanup(state->state);
280 }
281
282 static struct internal_state *find_internal_state_by_transport(const struct ast_sip_transport *transport)
283 {
284         const char *key = ast_sorcery_object_get_id(transport);
285
286         return ao2_find(transport_states, key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
287 }
288
289 static struct ast_sip_transport_state *find_state_by_transport(const struct ast_sip_transport *transport)
290 {
291         struct internal_state *state;
292         struct ast_sip_transport_state *trans_state;
293
294         state = find_internal_state_by_transport(transport);
295         if (!state) {
296                 return NULL;
297         }
298         trans_state = ao2_bump(state->state);
299         ao2_ref(state, -1);
300
301         return trans_state;
302 }
303
304 static int remove_temporary_state(void)
305 {
306         struct ast_sip_transport_state **state;
307
308         state = ast_threadstorage_get(&temp_state_store, sizeof(state));
309         if (!state) {
310                 return -1;
311         }
312
313         ao2_cleanup(*state);
314         *state = NULL;
315         return 0;
316 }
317
318 static struct ast_sip_transport_state *find_temporary_state(struct ast_sip_transport *transport)
319 {
320         struct ast_sip_transport_state **state;
321
322         state = ast_threadstorage_get(&temp_state_store, sizeof(state));
323         if (state && *state) {
324                 ao2_ref(*state, +1);
325                 return *state;
326         }
327
328         return NULL;
329 }
330
331 static struct internal_state *internal_state_alloc(struct ast_sip_transport *transport)
332 {
333         struct internal_state *internal_state;
334
335         internal_state = ao2_alloc(sizeof(*internal_state), internal_state_destroy);
336         if (!internal_state) {
337                 return NULL;
338         }
339
340         internal_state->id = ast_strdup(ast_sorcery_object_get_id(transport));
341         if (!internal_state->id) {
342                 ao2_cleanup(internal_state);
343                 return NULL;
344         }
345
346         /* We're transferring the reference from find_temporary_state */
347         internal_state->state = find_temporary_state(transport);
348         if (!internal_state->state) {
349                 ao2_cleanup(internal_state);
350                 return NULL;
351         }
352         internal_state->transport = ao2_bump(transport);
353         internal_state->transport->state = internal_state->state;
354         remove_temporary_state();
355
356         return internal_state;
357 }
358
359 /*!
360  * \internal
361  * \brief Should only be called by the individual field handlers
362  */
363 static struct ast_sip_transport_state *find_or_create_temporary_state(struct ast_sip_transport *transport)
364 {
365         struct ast_sip_transport_state **state;
366         struct ast_sip_transport_state *new_state;
367
368         if ((new_state = find_temporary_state(transport))) {
369                 return new_state;
370         }
371
372         state = ast_threadstorage_get(&temp_state_store, sizeof(state));
373         if (!state || *state) {
374                 return NULL;
375         }
376
377         new_state = ao2_alloc(sizeof(**state), sip_transport_state_destroy);
378         if (!new_state) {
379                 return NULL;
380         }
381         new_state->id = ast_strdup(ast_sorcery_object_get_id(transport));
382         new_state->type = transport->type;
383
384         pjsip_tls_setting_default(&new_state->tls);
385 #ifdef HAVE_PJSIP_TLS_TRANSPORT_PROTO
386         /* proto must be forced to 0 to enable all protocols otherwise only TLS will work */
387         new_state->tls.proto = 0;
388 #endif
389         new_state->tls.ciphers = new_state->ciphers;
390
391         ao2_ref(new_state, +1);
392         *state = new_state;
393
394         return new_state;
395 }
396
397 static void copy_state_to_transport(struct ast_sip_transport *transport)
398 {
399         ast_assert(transport && transport->state);
400
401         memcpy(&transport->host, &transport->state->host, sizeof(transport->host));
402         memcpy(&transport->tls, &transport->state->tls, sizeof(transport->tls));
403         memcpy(&transport->ciphers, &transport->state->ciphers, sizeof(transport->ciphers));
404         transport->localnet = transport->state->localnet;
405         transport->external_address_refresher = transport->state->external_signaling_address_refresher;
406         memcpy(&transport->external_address, &transport->state->external_signaling_address, sizeof(transport->external_signaling_address));
407 }
408
409 static int has_state_changed(struct ast_sip_transport_state *a, struct ast_sip_transport_state *b)
410 {
411         if (a->type != b->type) {
412                 return -1;
413         }
414
415         if (pj_sockaddr_cmp(&a->host, &b->host)) {
416                 return -1;
417         }
418
419         if ((a->localnet || b->localnet)
420                 && ((!a->localnet != !b->localnet)
421                 || ast_sockaddr_cmp(&a->localnet->addr, &b->localnet->addr)
422                 || ast_sockaddr_cmp(&a->localnet->netmask, &b->localnet->netmask)))
423         {
424                 return -1;
425         }
426
427         if (ast_sockaddr_cmp(&a->external_signaling_address, &b->external_signaling_address)) {
428                 return -1;
429         }
430
431         if (ast_sockaddr_cmp(&a->external_media_address, &b->external_media_address)) {
432                 return -1;
433         }
434
435         if (a->tls.method != b->tls.method
436                 || a->tls.ciphers_num != b->tls.ciphers_num
437 #ifdef HAVE_PJSIP_TLS_TRANSPORT_PROTO
438                 || a->tls.proto != b->tls.proto
439 #endif
440                 || a->tls.verify_client != b->tls.verify_client
441                 || a->tls.verify_server != b->tls.verify_server
442                 || a->tls.require_client_cert != b->tls.require_client_cert) {
443                 return -1;
444         }
445
446         if (memcmp(a->ciphers, b->ciphers, sizeof(pj_ssl_cipher) * fmax(a->tls.ciphers_num, b->tls.ciphers_num))) {
447                 return -1;
448         }
449
450         return 0;
451 }
452
453 static void states_cleanup(void *states)
454 {
455         if (states) {
456                 ao2_unlock(states);
457         }
458 }
459
460 /*! \brief Apply handler for transports */
461 static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
462 {
463         struct ast_sip_transport *transport = obj;
464         const char *transport_id = ast_sorcery_object_get_id(obj);
465         RAII_VAR(struct ao2_container *, states, transport_states, states_cleanup);
466         RAII_VAR(struct internal_state *, temp_state, NULL, ao2_cleanup);
467         RAII_VAR(struct internal_state *, perm_state, NULL, ao2_cleanup);
468         RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
469         pj_status_t res = -1;
470         int i;
471 #define BIND_TRIES 3
472 #define BIND_DELAY_US 100000
473
474         if (!states) {
475                 return -1;
476         }
477
478         /*
479          * transport_apply gets called for EVERY retrieval of a transport when using realtime.
480          * We need to prevent multiple threads from trying to mess with underlying transports
481          * at the same time.  The container is the only thing we have to lock on.
482          */
483         ao2_wrlock(states);
484
485         temp_state = internal_state_alloc(transport);
486         if (!temp_state) {
487                 ast_log(LOG_ERROR, "Transport '%s' failed to allocate memory\n", transport_id);
488                 return -1;
489         }
490
491         perm_state = find_internal_state_by_transport(transport);
492         if (perm_state) {
493                 ast_sorcery_diff(sorcery, perm_state->transport, transport, &changes);
494                 if (!changes && !has_state_changed(perm_state->state, temp_state->state)) {
495                         /* In case someone is using the deprecated fields, reset them */
496                         transport->state = perm_state->state;
497                         copy_state_to_transport(transport);
498                         ao2_replace(perm_state->transport, transport);
499                         return 0;
500                 }
501
502                 if (!transport->allow_reload) {
503                         if (!perm_state->change_detected) {
504                                 perm_state->change_detected = 1;
505                                 ast_log(LOG_WARNING, "Transport '%s' is not reloadable, maintaining previous values\n", transport_id);
506                         }
507                         /* In case someone is using the deprecated fields, reset them */
508                         transport->state = perm_state->state;
509                         copy_state_to_transport(transport);
510                         ao2_replace(perm_state->transport, transport);
511                         return 0;
512                 }
513         }
514
515         if (temp_state->state->host.addr.sa_family != PJ_AF_INET && temp_state->state->host.addr.sa_family != PJ_AF_INET6) {
516                 ast_log(LOG_ERROR, "Transport '%s' could not be started as binding not specified\n", transport_id);
517                 return -1;
518         }
519
520         /* Set default port if not present */
521         if (!pj_sockaddr_get_port(&temp_state->state->host)) {
522                 pj_sockaddr_set_port(&temp_state->state->host, (transport->type == AST_TRANSPORT_TLS) ? 5061 : 5060);
523         }
524
525         /* Now that we know what address family we can set up a dnsmgr refresh for the external addresses if present */
526         if (!ast_strlen_zero(transport->external_signaling_address)) {
527                 if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
528                         temp_state->state->external_signaling_address.ss.ss_family = AF_INET;
529                 } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
530                         temp_state->state->external_signaling_address.ss.ss_family = AF_INET6;
531                 } else {
532                         ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external signaling address\n",
533                                         transport_id);
534                         return -1;
535                 }
536
537                 if (ast_dnsmgr_lookup(transport->external_signaling_address, &temp_state->state->external_signaling_address, &temp_state->state->external_signaling_address_refresher, NULL) < 0) {
538                         ast_log(LOG_ERROR, "Could not create dnsmgr for external signaling address on '%s'\n", transport_id);
539                         return -1;
540                 }
541         }
542
543         if (!ast_strlen_zero(transport->external_media_address)) {
544                 if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
545                         temp_state->state->external_media_address.ss.ss_family = AF_INET;
546                 } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
547                         temp_state->state->external_media_address.ss.ss_family = AF_INET6;
548                 } else {
549                         ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external media address\n",
550                                         transport_id);
551                         return -1;
552                 }
553
554                 if (ast_dnsmgr_lookup(transport->external_media_address, &temp_state->state->external_media_address, &temp_state->state->external_media_address_refresher, NULL) < 0) {
555                         ast_log(LOG_ERROR, "Could not create dnsmgr for external media address on '%s'\n", transport_id);
556                         return -1;
557                 }
558         }
559
560         if (transport->type == AST_TRANSPORT_UDP) {
561
562                 for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
563                         if (perm_state && perm_state->state && perm_state->state->transport) {
564                                 pjsip_udp_transport_pause(perm_state->state->transport,
565                                         PJSIP_UDP_TRANSPORT_DESTROY_SOCKET);
566                                 usleep(BIND_DELAY_US);
567                         }
568
569                         if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
570                                 res = pjsip_udp_transport_start(ast_sip_get_pjsip_endpoint(),
571                                         &temp_state->state->host.ipv4, NULL, transport->async_operations,
572                                         &temp_state->state->transport);
573                         } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
574                                 res = pjsip_udp_transport_start6(ast_sip_get_pjsip_endpoint(),
575                                         &temp_state->state->host.ipv6, NULL, transport->async_operations,
576                                         &temp_state->state->transport);
577                         }
578                 }
579
580                 if (res == PJ_SUCCESS) {
581                         temp_state->state->transport->info = pj_pool_alloc(temp_state->state->transport->pool,
582                                 (AST_SIP_X_AST_TXP_LEN + strlen(transport_id) + 2));
583
584                         sprintf(temp_state->state->transport->info, "%s:%s", AST_SIP_X_AST_TXP, transport_id);
585
586                         if (transport->tos || transport->cos) {
587                                 pj_sock_t sock;
588                                 pj_qos_params qos_params;
589                                 sock = pjsip_udp_transport_get_socket(temp_state->state->transport);
590                                 pj_sock_get_qos_params(sock, &qos_params);
591                                 set_qos(transport, &qos_params);
592                                 pj_sock_set_qos_params(sock, &qos_params);
593                         }
594                 }
595         } else if (transport->type == AST_TRANSPORT_TCP) {
596                 pjsip_tcp_transport_cfg cfg;
597                 static int option = 1;
598
599                 pjsip_tcp_transport_cfg_default(&cfg, temp_state->state->host.addr.sa_family);
600                 cfg.bind_addr = temp_state->state->host;
601                 cfg.async_cnt = transport->async_operations;
602                 set_qos(transport, &cfg.qos_params);
603                 /* sockopt_params.options is copied to each newly connected socket */
604                 cfg.sockopt_params.options[0].level = pj_SOL_TCP();
605                 cfg.sockopt_params.options[0].optname = pj_TCP_NODELAY();
606                 cfg.sockopt_params.options[0].optval = &option;
607                 cfg.sockopt_params.options[0].optlen = sizeof(option);
608                 cfg.sockopt_params.cnt = 1;
609
610                 for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
611                         if (perm_state && perm_state->state && perm_state->state->factory
612                                 && perm_state->state->factory->destroy) {
613                                 perm_state->state->factory->destroy(perm_state->state->factory);
614                                 usleep(BIND_DELAY_US);
615                         }
616
617                         res = pjsip_tcp_transport_start3(ast_sip_get_pjsip_endpoint(), &cfg,
618                                 &temp_state->state->factory);
619                 }
620         } else if (transport->type == AST_TRANSPORT_TLS) {
621                 if (transport->async_operations > 1 && ast_compare_versions(pj_get_version(), "2.5.0") < 0) {
622                         ast_log(LOG_ERROR, "Transport: %s: When protocol=tls and pjproject version < 2.5.0, async_operations can't be > 1\n",
623                                         ast_sorcery_object_get_id(obj));
624                         return -1;
625                 }
626
627                 temp_state->state->tls.password = pj_str((char*)transport->password);
628                 set_qos(transport, &temp_state->state->tls.qos_params);
629
630                 for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
631                         if (perm_state && perm_state->state && perm_state->state->factory
632                                 && perm_state->state->factory->destroy) {
633                                 perm_state->state->factory->destroy(perm_state->state->factory);
634                                 usleep(BIND_DELAY_US);
635                         }
636
637                         res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &temp_state->state->tls,
638                                 &temp_state->state->host, NULL, transport->async_operations,
639                                 &temp_state->state->factory);
640                 }
641         } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) {
642                 if (transport->cos || transport->tos) {
643                         ast_log(LOG_WARNING, "TOS and COS values ignored for websocket transport\n");
644                 }
645                 res = PJ_SUCCESS;
646         }
647
648         if (res != PJ_SUCCESS) {
649                 char msg[PJ_ERR_MSG_SIZE];
650
651                 pj_strerror(res, msg, sizeof(msg));
652                 ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
653                 return -1;
654         }
655
656         copy_state_to_transport(transport);
657         if (perm_state) {
658                 ao2_unlink_flags(states, perm_state, OBJ_NOLOCK);
659         }
660         ao2_link_flags(states, temp_state, OBJ_NOLOCK);
661
662         return 0;
663 }
664
665 /*! \brief Custom handler for type just makes sure the state is created */
666 static int transport_state_init(const struct aco_option *opt, struct ast_variable *var, void *obj)
667 {
668         struct ast_sip_transport *transport = obj;
669         struct ast_sip_transport_state *state = find_or_create_temporary_state(transport);
670
671         ao2_cleanup(state);
672
673         return 0;
674 }
675
676 /*! \brief Custom handler for TLS method setting */
677 static int transport_tls_file_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
678 {
679         struct ast_sip_transport *transport = obj;
680         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
681
682         if (!state) {
683                 return -1;
684         }
685
686         if (ast_strlen_zero(var->value)) {
687                 /* Ignore empty options */
688                 return 0;
689         }
690
691         if (!ast_file_is_readable(var->value)) {
692                 ast_log(LOG_ERROR, "Transport: %s: %s %s is either missing or not readable\n",
693                         ast_sorcery_object_get_id(obj), var->name, var->value);
694                 return -1;
695         }
696
697         if (!strcasecmp(var->name, "ca_list_file")) {
698                 state->tls.ca_list_file = pj_str((char*)var->value);
699                 ast_string_field_set(transport, ca_list_file, var->value);
700         } else if (!strcasecmp(var->name, "ca_list_path")) {
701 #ifdef HAVE_PJ_SSL_CERT_LOAD_FROM_FILES2
702                 state->tls.ca_list_path = pj_str((char*)var->value);
703                 ast_string_field_set(transport, ca_list_path, var->value);
704 #else
705                 ast_log(LOG_WARNING, "Asterisk has been built against a version of pjproject that does not "
706                                 "support the 'ca_list_path' option. Please upgrade to version 2.4 or later.\n");
707 #endif
708         } else if (!strcasecmp(var->name, "cert_file")) {
709                 state->tls.cert_file = pj_str((char*)var->value);
710                 ast_string_field_set(transport, cert_file, var->value);
711         } else if (!strcasecmp(var->name, "priv_key_file")) {
712                 state->tls.privkey_file = pj_str((char*)var->value);
713                 ast_string_field_set(transport, privkey_file, var->value);
714         }
715
716         return 0;
717 }
718
719 static int ca_list_file_to_str(const void *obj, const intptr_t *args, char **buf)
720 {
721         const struct ast_sip_transport *transport = obj;
722
723         *buf = ast_strdup(transport->ca_list_file);
724
725         return 0;
726 }
727
728 static int ca_list_path_to_str(const void *obj, const intptr_t *args, char **buf)
729 {
730         const struct ast_sip_transport *transport = obj;
731
732         *buf = ast_strdup(transport->ca_list_path);
733
734         return 0;
735 }
736
737 static int cert_file_to_str(const void *obj, const intptr_t *args, char **buf)
738 {
739         const struct ast_sip_transport *transport = obj;
740
741         *buf = ast_strdup(transport->cert_file);
742
743         return 0;
744 }
745
746 static int privkey_file_to_str(const void *obj, const intptr_t *args, char **buf)
747 {
748         const struct ast_sip_transport *transport = obj;
749
750         *buf = ast_strdup(transport->privkey_file);
751
752         return 0;
753 }
754
755 /*! \brief Custom handler for turning a string protocol into an enum */
756 static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
757 {
758         struct ast_sip_transport *transport = obj;
759         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
760
761         if (!state) {
762                 return -1;
763         }
764
765         if (!strcasecmp(var->value, "udp")) {
766                 transport->type = AST_TRANSPORT_UDP;
767         } else if (!strcasecmp(var->value, "tcp")) {
768                 transport->type = AST_TRANSPORT_TCP;
769         } else if (!strcasecmp(var->value, "tls")) {
770                 transport->type = AST_TRANSPORT_TLS;
771         } else if (!strcasecmp(var->value, "ws")) {
772                 transport->type = AST_TRANSPORT_WS;
773         } else if (!strcasecmp(var->value, "wss")) {
774                 transport->type = AST_TRANSPORT_WSS;
775         } else {
776                 return -1;
777         }
778
779         state->type = transport->type;
780
781         return 0;
782 }
783
784 static const char *transport_types[] = {
785         [AST_TRANSPORT_UDP] = "udp",
786         [AST_TRANSPORT_TCP] = "tcp",
787         [AST_TRANSPORT_TLS] = "tls",
788         [AST_TRANSPORT_WS] = "ws",
789         [AST_TRANSPORT_WSS] = "wss"
790 };
791
792 static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
793 {
794         const struct ast_sip_transport *transport = obj;
795
796         if (ARRAY_IN_BOUNDS(transport->type, transport_types)) {
797                 *buf = ast_strdup(transport_types[transport->type]);
798         }
799
800         return 0;
801 }
802
803 /*! \brief Custom handler for turning a string bind into a pj_sockaddr */
804 static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
805 {
806         struct ast_sip_transport *transport = obj;
807         pj_str_t buf;
808         int rc;
809         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
810
811         if (!state) {
812                 return -1;
813         }
814
815         rc = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &state->host);
816
817         return rc != PJ_SUCCESS ? -1 : 0;
818 }
819
820 static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
821 {
822         const struct ast_sip_transport *transport = obj;
823         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
824
825         if (!state) {
826                 return -1;
827         }
828
829         if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
830                 return -1;
831         }
832
833         /* include port as well as brackets if IPv6 */
834         pj_sockaddr_print(&state->host, *buf, MAX_OBJECT_FIELD, 1 | 2);
835
836         return 0;
837 }
838
839 /*! \brief Custom handler for TLS boolean settings */
840 static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
841 {
842         struct ast_sip_transport *transport = obj;
843         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
844
845         if (!state) {
846                 return -1;
847         }
848
849         if (!strcasecmp(var->name, "verify_server")) {
850                 state->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
851         } else if (!strcasecmp(var->name, "verify_client")) {
852                 state->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
853         } else if (!strcasecmp(var->name, "require_client_cert")) {
854                 state->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
855         } else {
856                 return -1;
857         }
858
859         return 0;
860 }
861
862 static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
863 {
864         const struct ast_sip_transport *transport = obj;
865         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
866
867         if (!state) {
868                 return -1;
869         }
870
871         *buf = ast_strdup(AST_YESNO(state->tls.verify_server));
872
873         return 0;
874 }
875
876 static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
877 {
878         const struct ast_sip_transport *transport = obj;
879         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
880
881         if (!state) {
882                 return -1;
883         }
884
885         *buf = ast_strdup(AST_YESNO(state->tls.verify_client));
886
887         return 0;
888 }
889
890 static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
891 {
892         const struct ast_sip_transport *transport = obj;
893         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
894
895         if (!state) {
896                 return -1;
897         }
898
899         *buf = ast_strdup(AST_YESNO(state->tls.require_client_cert));
900
901         return 0;
902 }
903
904 /*! \brief Custom handler for TLS method setting */
905 static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
906 {
907         struct ast_sip_transport *transport = obj;
908         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
909
910         if (!state) {
911                 return -1;
912         }
913
914         if (ast_strlen_zero(var->value) || !strcasecmp(var->value, "default")) {
915                 state->tls.method = PJSIP_SSL_DEFAULT_METHOD;
916         } else if (!strcasecmp(var->value, "unspecified")) {
917                 state->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
918         } else if (!strcasecmp(var->value, "tlsv1")) {
919                 state->tls.method = PJSIP_TLSV1_METHOD;
920         } else if (!strcasecmp(var->value, "sslv2")) {
921                 state->tls.method = PJSIP_SSLV2_METHOD;
922         } else if (!strcasecmp(var->value, "sslv3")) {
923                 state->tls.method = PJSIP_SSLV3_METHOD;
924         } else if (!strcasecmp(var->value, "sslv23")) {
925                 state->tls.method = PJSIP_SSLV23_METHOD;
926         } else {
927                 return -1;
928         }
929
930         return 0;
931 }
932
933 static const char *tls_method_map[] = {
934         [PJSIP_SSL_UNSPECIFIED_METHOD] = "unspecified",
935         [PJSIP_TLSV1_METHOD] = "tlsv1",
936         [PJSIP_SSLV2_METHOD] = "sslv2",
937         [PJSIP_SSLV3_METHOD] = "sslv3",
938         [PJSIP_SSLV23_METHOD] = "sslv23",
939 };
940
941 static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
942 {
943         const struct ast_sip_transport *transport = obj;
944         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
945
946         if (!state) {
947                 return -1;
948         }
949
950         if (ARRAY_IN_BOUNDS(state->tls.method, tls_method_map)) {
951                 *buf = ast_strdup(tls_method_map[state->tls.method]);
952         }
953
954         return 0;
955 }
956
957 /*! \brief Helper function which turns a cipher name into an identifier */
958 static pj_ssl_cipher cipher_name_to_id(const char *name)
959 {
960         pj_ssl_cipher ciphers[100];
961         pj_ssl_cipher id = 0;
962         unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
963         int pos;
964         const char *pos_name;
965
966         if (pj_ssl_cipher_get_availables(ciphers, &cipher_num)) {
967                 return 0;
968         }
969
970         for (pos = 0; pos < cipher_num; ++pos) {
971                 pos_name = pj_ssl_cipher_name(ciphers[pos]);
972                 if (!pos_name || strcmp(pos_name, name)) {
973                         continue;
974                 }
975
976                 id = ciphers[pos];
977                 break;
978         }
979
980         return id;
981 }
982
983 /*!
984  * \internal
985  * \brief Add a new cipher to the transport's cipher list array.
986  *
987  * \param transport Which transport to add the cipher to.
988  * \param name Cipher identifier name.
989  *
990  * \retval 0 on success.
991  * \retval -1 on error.
992  */
993 static int transport_cipher_add(struct ast_sip_transport_state *state, const char *name)
994 {
995         pj_ssl_cipher cipher;
996         int idx;
997
998         cipher = cipher_name_to_id(name);
999         if (!cipher) {
1000                 /* TODO: Check this over/tweak - it's taken from pjsua for now */
1001                 if (!strnicmp(name, "0x", 2)) {
1002                         pj_str_t cipher_st = pj_str((char *) name + 2);
1003                         cipher = pj_strtoul2(&cipher_st, NULL, 16);
1004                 } else {
1005                         cipher = atoi(name);
1006                 }
1007         }
1008
1009         if (pj_ssl_cipher_is_supported(cipher)) {
1010                 for (idx = state->tls.ciphers_num; idx--;) {
1011                         if (state->ciphers[idx] == cipher) {
1012                                 /* The cipher is already in the list. */
1013                                 return 0;
1014                         }
1015                 }
1016                 state->ciphers[state->tls.ciphers_num++] = cipher;
1017                 return 0;
1018         } else {
1019                 ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", name);
1020                 return -1;
1021         }
1022 }
1023
1024 /*! \brief Custom handler for TLS cipher setting */
1025 static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1026 {
1027         struct ast_sip_transport *transport = obj;
1028         char *parse;
1029         char *name;
1030         int res = 0;
1031         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
1032
1033         if (!state) {
1034                 return -1;
1035         }
1036
1037         parse = ast_strdupa(S_OR(var->value, ""));
1038         while ((name = ast_strip(strsep(&parse, ",")))) {
1039                 if (ast_strlen_zero(name)) {
1040                         continue;
1041                 }
1042                 if (ARRAY_LEN(state->ciphers) <= state->tls.ciphers_num) {
1043                         ast_log(LOG_ERROR, "Too many ciphers specified\n");
1044                         res = -1;
1045                         break;
1046                 }
1047                 res |= transport_cipher_add(state, name);
1048         }
1049         return res ? -1 : 0;
1050 }
1051
1052 static void cipher_to_str(char **buf, const pj_ssl_cipher *ciphers, unsigned int cipher_num)
1053 {
1054         struct ast_str *str;
1055         int idx;
1056
1057         str = ast_str_create(128);
1058         if (!str) {
1059                 *buf = NULL;
1060                 return;
1061         }
1062
1063         for (idx = 0; idx < cipher_num; ++idx) {
1064                 ast_str_append(&str, 0, "%s", pj_ssl_cipher_name(ciphers[idx]));
1065                 if (idx < cipher_num - 1) {
1066                         ast_str_append(&str, 0, ", ");
1067                 }
1068         }
1069
1070         *buf = ast_strdup(ast_str_buffer(str));
1071         ast_free(str);
1072 }
1073
1074 static int transport_tls_cipher_to_str(const void *obj, const intptr_t *args, char **buf)
1075 {
1076         const struct ast_sip_transport *transport = obj;
1077         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
1078
1079         if (!state) {
1080                 return -1;
1081         }
1082
1083         cipher_to_str(buf, state->ciphers, state->tls.ciphers_num);
1084         return *buf ? 0 : -1;
1085 }
1086
1087 static char *handle_pjsip_list_ciphers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1088 {
1089         pj_ssl_cipher ciphers[100];
1090         unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
1091         char *buf;
1092
1093         switch (cmd) {
1094         case CLI_INIT:
1095                 e->command = "pjsip list ciphers";
1096                 e->usage = "Usage: pjsip list ciphers\n"
1097                         "       List available OpenSSL cipher names.\n";
1098                 return NULL;
1099         case CLI_GENERATE:
1100                 return NULL;
1101         }
1102
1103         if (pj_ssl_cipher_get_availables(ciphers, &cipher_num) || !cipher_num) {
1104                 buf = NULL;
1105         } else {
1106                 cipher_to_str(&buf, ciphers, cipher_num);
1107         }
1108
1109         if (!ast_strlen_zero(buf)) {
1110                 ast_cli(a->fd, "Available ciphers: '%s'\n", buf);
1111         } else {
1112                 ast_cli(a->fd, "No available ciphers\n");
1113         }
1114         ast_free(buf);
1115         return CLI_SUCCESS;
1116 }
1117
1118 /*! \brief Custom handler for localnet setting */
1119 static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1120 {
1121         struct ast_sip_transport *transport = obj;
1122         int error = 0;
1123         RAII_VAR(struct ast_sip_transport_state *, state, find_or_create_temporary_state(transport), ao2_cleanup);
1124
1125         if (!state) {
1126                 return -1;
1127         }
1128
1129         if (ast_strlen_zero(var->value)) {
1130                 ast_free_ha(state->localnet);
1131                 state->localnet = NULL;
1132                 return 0;
1133         }
1134
1135         /* We use only the ast_apply_ha() which defaults to ALLOW
1136          * ("permit"), so we add DENY rules. */
1137         if (!(state->localnet = ast_append_ha("deny", var->value, state->localnet, &error))) {
1138                 return -1;
1139         }
1140
1141         return error;
1142 }
1143
1144 static int localnet_to_vl(const void *obj, struct ast_variable **fields)
1145 {
1146         const struct ast_sip_transport *transport = obj;
1147         char str[MAX_OBJECT_FIELD];
1148         struct ast_variable *head = NULL;
1149         struct ast_ha *ha;
1150         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
1151
1152         if (!state) {
1153                 return -1;
1154         }
1155
1156         for (ha = state->localnet; ha; ha = ha->next) {
1157                 const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
1158                 snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
1159                         addr, ast_sockaddr_stringify_addr(&ha->netmask));
1160
1161                 ast_variable_list_append(&head, ast_variable_new("local_net", str, ""));
1162         }
1163
1164         if (head) {
1165                 *fields = head;
1166         }
1167
1168         return 0;
1169 }
1170
1171 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
1172 {
1173         RAII_VAR(struct ast_str *, str, ast_str_create(MAX_OBJECT_FIELD), ast_free);
1174         const struct ast_sip_transport *transport = obj;
1175         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
1176
1177         if (!state) {
1178                 return -1;
1179         }
1180
1181         ast_ha_join(state->localnet, &str);
1182         *buf = ast_strdup(ast_str_buffer(str));
1183         return 0;
1184 }
1185
1186 /*! \brief Custom handler for TOS setting */
1187 static int transport_tos_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1188 {
1189         struct ast_sip_transport *transport = obj;
1190         unsigned int value;
1191
1192         if (ast_str2tos(var->value, &value)) {
1193                 ast_log(LOG_ERROR, "Error configuring transport '%s' - Could not "
1194                         "interpret 'tos' value '%s'\n",
1195                         ast_sorcery_object_get_id(transport), var->value);
1196                 return -1;
1197         }
1198
1199         if (value % 4) {
1200                 value = value >> 2;
1201                 value = value << 2;
1202                 ast_log(LOG_WARNING,
1203                         "transport '%s' - 'tos' value '%s' uses bits that are "
1204                         "discarded when converted to DSCP. Using equivalent %u instead.\n",
1205                         ast_sorcery_object_get_id(transport), var->value, value);
1206         }
1207
1208         transport->tos = value;
1209         return 0;
1210 }
1211
1212 static int tos_to_str(const void *obj, const intptr_t *args, char **buf)
1213 {
1214         const struct ast_sip_transport *transport = obj;
1215
1216         if (ast_asprintf(buf, "%u", transport->tos) == -1) {
1217                 return -1;
1218         }
1219         return 0;
1220 }
1221
1222 static struct ao2_container *cli_get_container(const char *regex)
1223 {
1224         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
1225         struct ao2_container *s_container;
1226
1227         container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "transport",
1228                 regex);
1229         if (!container) {
1230                 return NULL;
1231         }
1232
1233         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
1234                 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
1235         if (!s_container) {
1236                 return NULL;
1237         }
1238
1239         if (ao2_container_dup(s_container, container, 0)) {
1240                 ao2_ref(s_container, -1);
1241                 return NULL;
1242         }
1243
1244         return s_container;
1245 }
1246
1247 static int cli_iterate(void *container, ao2_callback_fn callback, void *args)
1248 {
1249         const struct ast_sip_endpoint *endpoint = container;
1250         struct ast_sip_transport *transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(),
1251                 "transport", endpoint->transport);
1252
1253         if (!transport) {
1254                 return -1;
1255         }
1256
1257         return callback(transport, args, 0);
1258 }
1259
1260 static void *cli_retrieve_by_id(const char *id)
1261 {
1262         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", id);
1263 }
1264
1265 static int cli_print_header(void *obj, void *arg, int flags)
1266 {
1267         struct ast_sip_cli_context *context = arg;
1268         int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1269         int filler = CLI_MAX_WIDTH - indent - 61;
1270
1271         ast_assert(context->output_buffer != NULL);
1272
1273         ast_str_append(&context->output_buffer, 0,
1274                 "%*s:  <TransportId........>  <Type>  <cos>  <tos>  <BindAddress%*.*s>\n",
1275                 indent, "Transport", filler, filler, CLI_HEADER_FILLER);
1276
1277         return 0;
1278 }
1279
1280 static int cli_print_body(void *obj, void *arg, int flags)
1281 {
1282         struct ast_sip_transport *transport = obj;
1283         struct ast_sip_cli_context *context = arg;
1284         char hoststr[PJ_INET6_ADDRSTRLEN];
1285         RAII_VAR(struct ast_sip_transport_state *, state, find_state_by_transport(transport), ao2_cleanup);
1286
1287         if (!state) {
1288                 return -1;
1289         }
1290
1291         ast_assert(context->output_buffer != NULL);
1292
1293         pj_sockaddr_print(&state->host, hoststr, sizeof(hoststr), 3);
1294
1295         ast_str_append(&context->output_buffer, 0, "%*s:  %-21s  %6s  %5u  %5u  %s\n",
1296                 CLI_INDENT_TO_SPACES(context->indent_level), "Transport",
1297                 ast_sorcery_object_get_id(transport),
1298                 ARRAY_IN_BOUNDS(transport->type, transport_types) ? transport_types[transport->type] : "Unknown",
1299                 transport->cos, transport->tos, hoststr);
1300
1301         if (context->show_details
1302                 || (context->show_details_only_level_0 && context->indent_level == 0)) {
1303                 ast_str_append(&context->output_buffer, 0, "\n");
1304                 ast_sip_cli_print_sorcery_objectset(transport, context, 0);
1305         }
1306
1307         return 0;
1308 }
1309
1310 static struct ast_cli_entry cli_commands[] = {
1311         AST_CLI_DEFINE(handle_pjsip_list_ciphers, "List available OpenSSL cipher names"),
1312         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Transports",
1313                 .command = "pjsip list transports",
1314                 .usage = "Usage: pjsip list transports [ like <pattern> ]\n"
1315                                 "       List the configured PJSIP Transports\n"
1316                                 "       Optional regular expression pattern is used to filter the list.\n"),
1317         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transports",
1318                 .command = "pjsip show transports",
1319                 .usage = "Usage: pjsip show transports [ like <pattern> ]\n"
1320                                 "       Show the configured PJSIP Transport\n"
1321                                 "       Optional regular expression pattern is used to filter the list.\n"),
1322         AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transport",
1323                 .command = "pjsip show transport",
1324                 .usage = "Usage: pjsip show transport <id>\n"
1325                                  "       Show the configured PJSIP Transport\n"),
1326 };
1327
1328 static struct ast_sip_cli_formatter_entry *cli_formatter;
1329
1330 struct ast_sip_transport_state *ast_sip_get_transport_state(const char *transport_id)
1331 {
1332         struct internal_state *state = NULL;
1333         struct ast_sip_transport_state *trans_state;
1334
1335         if (!transport_states) {
1336                 return NULL;
1337         }
1338
1339         state = ao2_find(transport_states, transport_id, OBJ_SEARCH_KEY);
1340         if (!state) {
1341                 return NULL;
1342         }
1343
1344         trans_state = ao2_bump(state->state);
1345         ao2_ref(state, -1);
1346
1347         return trans_state;
1348 }
1349
1350 static int populate_transport_states(void *obj, void *arg, int flags)
1351 {
1352         struct internal_state *state = obj;
1353         struct ao2_container *container = arg;
1354
1355         ao2_link(container, state->state);
1356
1357         return CMP_MATCH;
1358 }
1359
1360 struct ao2_container *ast_sip_get_transport_states(void)
1361 {
1362         struct ao2_container *states = ao2_container_alloc(DEFAULT_STATE_BUCKETS, transport_state_hash, transport_state_cmp);
1363
1364         if (!states) {
1365                 return NULL;
1366         }
1367
1368         ao2_callback(transport_states, OBJ_NODATA | OBJ_MULTIPLE, populate_transport_states, states);
1369         return states;
1370 }
1371
1372 /*! \brief Initialize sorcery with transport support */
1373 int ast_sip_initialize_sorcery_transport(void)
1374 {
1375         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
1376         struct ao2_container *transports = NULL;
1377
1378         /* Create outbound registration states container. */
1379         transport_states = ao2_container_alloc(DEFAULT_STATE_BUCKETS, internal_state_hash, internal_state_cmp);
1380         if (!transport_states) {
1381                 ast_log(LOG_ERROR, "Unable to allocate transport states container\n");
1382                 return -1;
1383         }
1384
1385         ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport");
1386
1387         if (ast_sorcery_object_register(sorcery, "transport", sip_transport_alloc, NULL, transport_apply)) {
1388                 return -1;
1389         }
1390
1391         /* Normally type is a OPT_NOOP_T but we're using it to make sure that state is created */
1392         ast_sorcery_object_field_register_custom(sorcery, "transport", "type", "", transport_state_init, NULL, NULL, 0, 0);
1393         ast_sorcery_object_field_register_custom(sorcery, "transport", "protocol", "udp", transport_protocol_handler, transport_protocol_to_str, NULL, 0, 0);
1394         ast_sorcery_object_field_register_custom(sorcery, "transport", "bind", "", transport_bind_handler, transport_bind_to_str, NULL, 0, 0);
1395         ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
1396
1397         ast_sorcery_object_field_register_custom(sorcery, "transport", "ca_list_file", "", transport_tls_file_handler, ca_list_file_to_str, NULL, 0, 0);
1398         ast_sorcery_object_field_register_custom(sorcery, "transport", "ca_list_path", "", transport_tls_file_handler, ca_list_path_to_str, NULL, 0, 0);
1399         ast_sorcery_object_field_register_custom(sorcery, "transport", "cert_file", "", transport_tls_file_handler, cert_file_to_str, NULL, 0, 0);
1400         ast_sorcery_object_field_register_custom(sorcery, "transport", "priv_key_file", "", transport_tls_file_handler, privkey_file_to_str, NULL, 0, 0);
1401
1402         ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
1403         ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_signaling_address));
1404         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);
1405         ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
1406         ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
1407         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, NULL, 0, 0);
1408         ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, NULL, 0, 0);
1409         ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, NULL, 0, 0);
1410         ast_sorcery_object_field_register_custom(sorcery, "transport", "method", "", transport_tls_method_handler, tls_method_to_str, NULL, 0, 0);
1411         ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, NULL, 0, 0);
1412         ast_sorcery_object_field_register_custom(sorcery, "transport", "local_net", "", transport_localnet_handler, localnet_to_str, localnet_to_vl, 0, 0);
1413         ast_sorcery_object_field_register_custom(sorcery, "transport", "tos", "0", transport_tos_handler, tos_to_str, NULL, 0, 0);
1414         ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
1415         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);
1416         ast_sorcery_object_field_register(sorcery, "transport", "allow_reload", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, allow_reload));
1417         ast_sorcery_object_field_register(sorcery, "transport", "symmetric_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, symmetric_transport));
1418
1419         internal_sip_register_endpoint_formatter(&endpoint_transport_formatter);
1420
1421         cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1422         if (!cli_formatter) {
1423                 ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
1424                 return -1;
1425         }
1426         cli_formatter->name = "transport";
1427         cli_formatter->print_header = cli_print_header;
1428         cli_formatter->print_body = cli_print_body;
1429         cli_formatter->get_container = cli_get_container;
1430         cli_formatter->iterate = cli_iterate;
1431         cli_formatter->get_id = ast_sorcery_object_get_id;
1432         cli_formatter->retrieve_by_id = cli_retrieve_by_id;
1433
1434         ast_sip_register_cli_formatter(cli_formatter);
1435         ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1436
1437         /* trigger load of transports from realtime by trying to revrieve them all */
1438         transports = ast_sorcery_retrieve_by_fields(sorcery, "transport", AST_RETRIEVE_FLAG_ALL | AST_RETRIEVE_FLAG_MULTIPLE, NULL);
1439         ao2_cleanup(transports);
1440
1441         return 0;
1442 }
1443
1444 int ast_sip_destroy_sorcery_transport(void)
1445 {
1446         ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1447         ast_sip_unregister_cli_formatter(cli_formatter);
1448
1449         internal_sip_unregister_endpoint_formatter(&endpoint_transport_formatter);
1450
1451         ao2_ref(transport_states, -1);
1452         transport_states = NULL;
1453
1454         return 0;
1455 }