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