Asterisk - The Open Source Telephony Project  18.5.0
config_transport.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <[email protected]>
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"
33 /* We're only using a #define from http_websocket.h, no OPTIONAL_API symbols are used. */
35 
36 #define MAX_POINTER_STRING 33
37 
38 /*! \brief Default number of state container buckets */
39 #define DEFAULT_STATE_BUCKETS 53
41 
43  char *id;
44  /*! Set if there was a change detected */
46  /*! \brief Transport configuration object */
48  /*! \brief Transport state information */
50 };
51 
52 static void temp_state_store_cleanup(void *data)
53 {
54  struct ast_sip_transport_state **temp_state = data;
55 
56  ao2_cleanup(*temp_state);
57  ast_free(data);
58 }
59 
61 
62 /*! \brief hashing function for state objects */
63 static int internal_state_hash(const void *obj, const int flags)
64 {
65  const struct internal_state *object;
66  const char *key;
67 
68  switch (flags & OBJ_SEARCH_MASK) {
69  case OBJ_SEARCH_KEY:
70  key = obj;
71  break;
72  case OBJ_SEARCH_OBJECT:
73  object = obj;
74  key = object->id;
75  break;
76  default:
77  ast_assert(0);
78  return 0;
79  }
80  return ast_str_hash(key);
81 }
82 
83 /*! \brief comparator function for state objects */
84 static int internal_state_cmp(void *obj, void *arg, int flags)
85 {
86  const struct internal_state *object_left = obj;
87  const struct internal_state *object_right = arg;
88  const char *right_key = arg;
89  int cmp;
90 
91  switch (flags & OBJ_SEARCH_MASK) {
92  case OBJ_SEARCH_OBJECT:
93  right_key = object_right->id;
94  /* Fall through */
95  case OBJ_SEARCH_KEY:
96  cmp = strcmp(object_left->id, right_key);
97  break;
99  /* Not supported by container. */
100  ast_assert(0);
101  return 0;
102  default:
103  cmp = 0;
104  break;
105  }
106  if (cmp) {
107  return 0;
108  }
109  return CMP_MATCH;
110 }
111 
112 /*! \brief hashing function for state objects */
113 static int transport_state_hash(const void *obj, const int flags)
114 {
115  const struct ast_sip_transport_state *object;
116  const char *key;
117 
118  switch (flags & OBJ_SEARCH_MASK) {
119  case OBJ_SEARCH_KEY:
120  key = obj;
121  break;
122  case OBJ_SEARCH_OBJECT:
123  object = obj;
124  key = object->id;
125  break;
126  default:
127  ast_assert(0);
128  return 0;
129  }
130  return ast_str_hash(key);
131 }
132 
133 /*! \brief comparator function for state objects */
134 static int transport_state_cmp(void *obj, void *arg, int flags)
135 {
136  const struct ast_sip_transport_state *object_left = obj;
137  const struct ast_sip_transport_state *object_right = arg;
138  const char *right_key = arg;
139  int cmp;
140 
141  switch (flags & OBJ_SEARCH_MASK) {
142  case OBJ_SEARCH_OBJECT:
143  right_key = object_right->id;
144  /* Fall through */
145  case OBJ_SEARCH_KEY:
146  cmp = strcmp(object_left->id, right_key);
147  break;
149  /* Not supported by container. */
150  ast_assert(0);
151  return 0;
152  default:
153  cmp = 0;
154  break;
155  }
156  if (cmp) {
157  return 0;
158  }
159  return CMP_MATCH;
160 }
161 
163  struct ast_str **buf)
164 {
165  return ast_sip_sorcery_object_to_ami(transport, buf);
166 }
167 
168 static int format_ami_endpoint_transport(const struct ast_sip_endpoint *endpoint,
169  struct ast_sip_ami *ami)
170 {
171  RAII_VAR(struct ast_str *, buf, NULL, ast_free);
173 
174  if (ast_strlen_zero(endpoint->transport)) {
175  return 0;
176  }
177 
178  buf = ast_sip_create_ami_event("TransportDetail", ami);
179  if (!buf) {
180  return -1;
181  }
182 
184  endpoint->transport);
185  if (!transport) {
186  astman_send_error_va(ami->s, ami->m, "Unable to retrieve "
187  "transport %s\n", endpoint->transport);
188  return -1;
189  }
190 
192 
193  ast_str_append(&buf, 0, "EndpointName: %s\r\n",
194  ast_sorcery_object_get_id(endpoint));
195 
196  astman_append(ami->s, "%s\r\n", ast_str_buffer(buf));
197  ami->count++;
198 
199  return 0;
200 }
201 
204 };
205 
206 int ast_sip_transport_state_set_transport(const char *transport_name, pjsip_transport *transport)
207 {
208  struct ast_sip_transport_state *transport_state;
209 
210  /* To make it easier on callers we allow an empty transport name */
211  if (ast_strlen_zero(transport_name)) {
212  return 0;
213  }
214 
215  transport_state = ast_sip_get_transport_state(transport_name);
216  if (!transport_state) {
217  return -1;
218  }
219 
220  if (!transport_state->flow) {
221  ao2_ref(transport_state, -1);
222  return 0;
223  }
224 
225  ao2_lock(transport_state);
226  if (transport_state->transport != transport) {
227  if (transport_state->transport) {
228  pjsip_transport_dec_ref(transport_state->transport);
229  }
230  transport_state->transport = transport;
231  if (transport_state->transport) {
232  pjsip_transport_add_ref(transport_state->transport);
233  }
234  }
235  ao2_unlock(transport_state);
236 
237  ao2_ref(transport_state, -1);
238 
239  return 0;
240 }
241 
242 int ast_sip_transport_state_set_preferred_identity(const char *transport_name, const char *identity)
243 {
244  struct ast_sip_transport_state *transport_state;
245 
246  if (ast_strlen_zero(transport_name)) {
247  return 0;
248  }
249 
250  transport_state = ast_sip_get_transport_state(transport_name);
251  if (!transport_state) {
252  return -1;
253  }
254 
255  if (!transport_state->flow) {
256  ao2_ref(transport_state, -1);
257  return 0;
258  }
259 
260  ao2_lock(transport_state);
261  ast_free(transport_state->preferred_identity);
262  transport_state->preferred_identity = ast_strdup(identity);
263  ao2_unlock(transport_state);
264 
265  ao2_ref(transport_state, -1);
266 
267  return 0;
268 }
269 
270 int ast_sip_transport_state_set_service_routes(const char *transport_name, struct ast_sip_service_route_vector *service_routes)
271 {
272  struct ast_sip_transport_state *transport_state;
273 
274  if (ast_strlen_zero(transport_name)) {
275  ast_sip_service_route_vector_destroy(service_routes);
276  return 0;
277  }
278 
279  transport_state = ast_sip_get_transport_state(transport_name);
280  if (!transport_state) {
281  ast_sip_service_route_vector_destroy(service_routes);
282  return -1;
283  }
284 
285  if (!transport_state->flow) {
286  ao2_ref(transport_state, -1);
287  ast_sip_service_route_vector_destroy(service_routes);
288  return 0;
289  }
290 
291  ao2_lock(transport_state);
293  transport_state->service_routes = service_routes;
294  ao2_unlock(transport_state);
295 
296  ao2_ref(transport_state, -1);
297 
298  return 0;
299 }
300 
301 void ast_sip_message_apply_transport(const char *transport_name, pjsip_tx_data *tdata)
302 {
303  struct ast_sip_transport_state *transport_state;
304 
305  if (ast_strlen_zero(transport_name)) {
306  return;
307  }
308 
309  /* We only currently care about requests that are of the INVITE, CANCEL, or OPTIONS
310  * type but in the future we could support other messages.
311  */
312  if (tdata->msg->type != PJSIP_REQUEST_MSG ||
313  (pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_invite_method) &&
314  pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_cancel_method) &&
315  pjsip_method_cmp(&tdata->msg->line.req.method, &pjsip_options_method))) {
316  return;
317  }
318 
319  transport_state = ast_sip_get_transport_state(transport_name);
320  if (!transport_state) {
321  return;
322  }
323 
324  if (!transport_state->flow) {
325  ao2_ref(transport_state, -1);
326  return;
327  }
328 
329  ao2_lock(transport_state);
330 
331  /* If a Preferred Identity has been set then add it to the request */
332  if (transport_state->preferred_identity) {
333  ast_sip_add_header(tdata, "P-Preferred-Identity", transport_state->preferred_identity);
334  }
335 
336  /* If Service Routes have been set then add them to the request */
337  if (transport_state->service_routes) {
338  int idx;
339 
340  for (idx = 0; idx < AST_VECTOR_SIZE(transport_state->service_routes); ++idx) {
341  char *service_route = AST_VECTOR_GET(transport_state->service_routes, idx);
342 
343  ast_sip_add_header(tdata, "Route", service_route);
344  }
345  }
346 
347  ao2_unlock(transport_state);
348 
349  ao2_ref(transport_state, -1);
350 }
351 
352 struct ast_sip_service_route_vector *ast_sip_service_route_vector_alloc(void)
353 {
354  struct ast_sip_service_route_vector *service_routes;
355 
356  service_routes = ast_calloc(1, sizeof(*service_routes));
357  if (!service_routes) {
358  return NULL;
359  }
360 
361  AST_VECTOR_INIT(service_routes, 0);
362 
363  return service_routes;
364 }
365 
366 void ast_sip_service_route_vector_destroy(struct ast_sip_service_route_vector *service_routes)
367 {
368  if (!service_routes) {
369  return;
370  }
371 
372  AST_VECTOR_CALLBACK_VOID(service_routes, ast_free);
373  ast_free(service_routes);
374 }
375 
376 static void set_qos(struct ast_sip_transport *transport, pj_qos_params *qos)
377 {
378  int tos_as_dscp = transport->tos >> 2;
379 
380  if (transport->tos) {
381  qos->flags |= PJ_QOS_PARAM_HAS_DSCP;
382  qos->dscp_val = tos_as_dscp;
383  }
384  if (transport->cos) {
385  qos->flags |= PJ_QOS_PARAM_HAS_SO_PRIO;
386  qos->so_prio = transport->cos;
387  }
388 }
389 
390 /*! \brief Destructor for transport */
391 static void sip_transport_destroy(void *obj)
392 {
393  struct ast_sip_transport *transport = obj;
394 
395  ast_string_field_free_memory(transport);
396 }
397 
398 /*! \brief Allocator for transport */
399 static void *sip_transport_alloc(const char *name)
400 {
402 
403  if (!transport) {
404  return NULL;
405  }
406 
407  if (ast_string_field_init(transport, 256)) {
408  ao2_cleanup(transport);
409  return NULL;
410  }
411 
412  return transport;
413 }
414 
415 static int destroy_sip_transport_state(void *data)
416 {
417  struct ast_sip_transport_state *transport_state = data;
418 
419  ast_free(transport_state->id);
420  ast_free_ha(transport_state->localnet);
421 
422  if (transport_state->external_signaling_address_refresher) {
424  }
425  if (transport_state->external_media_address_refresher) {
427  }
428  if (transport_state->transport) {
429  pjsip_transport_shutdown(transport_state->transport);
430  }
431 
432  return 0;
433 }
434 
435 /*! \brief Destructor for ast_sip_transport state information */
436 static void sip_transport_state_destroy(void *obj)
437 {
438  struct ast_sip_transport_state *state = obj;
439 
441 }
442 
443 /*! \brief Destructor for ast_sip_transport state information */
444 static void internal_state_destroy(void *obj)
445 {
446  struct internal_state *state = obj;
447 
448  ast_free(state->id);
449  ao2_cleanup(state->transport);
450  ao2_cleanup(state->state);
451 }
452 
454 {
455  const char *key = ast_sorcery_object_get_id(transport);
456 
457  return ao2_find(transport_states, key, OBJ_SEARCH_KEY | OBJ_NOLOCK);
458 }
459 
461 {
462  struct internal_state *state;
463  struct ast_sip_transport_state *trans_state;
464 
465  state = find_internal_state_by_transport(transport);
466  if (!state) {
467  return NULL;
468  }
469  trans_state = ao2_bump(state->state);
470  ao2_ref(state, -1);
471 
472  return trans_state;
473 }
474 
475 static int remove_temporary_state(void)
476 {
478 
479  state = ast_threadstorage_get(&temp_state_store, sizeof(state));
480  if (!state) {
481  return -1;
482  }
483 
484  ao2_cleanup(*state);
485  *state = NULL;
486  return 0;
487 }
488 
490 {
492 
493  state = ast_threadstorage_get(&temp_state_store, sizeof(state));
494  if (state && *state) {
495  ao2_ref(*state, +1);
496  return *state;
497  }
498 
499  return NULL;
500 }
501 
503 {
505 
506  internal_state = ao2_alloc(sizeof(*internal_state), internal_state_destroy);
507  if (!internal_state) {
508  return NULL;
509  }
510 
511  internal_state->id = ast_strdup(ast_sorcery_object_get_id(transport));
512  if (!internal_state->id) {
513  ao2_cleanup(internal_state);
514  return NULL;
515  }
516 
517  /* We're transferring the reference from find_temporary_state */
518  internal_state->state = find_temporary_state(transport);
519  if (!internal_state->state) {
520  ao2_cleanup(internal_state);
521  return NULL;
522  }
523  internal_state->transport = ao2_bump(transport);
524  internal_state->transport->state = internal_state->state;
526 
527  return internal_state;
528 }
529 
530 /*!
531  * \internal
532  * \brief Should only be called by the individual field handlers
533  */
535 {
537  struct ast_sip_transport_state *new_state;
538 
539  if ((new_state = find_temporary_state(transport))) {
540  return new_state;
541  }
542 
543  state = ast_threadstorage_get(&temp_state_store, sizeof(state));
544  if (!state || *state) {
545  return NULL;
546  }
547 
548  new_state = ao2_alloc(sizeof(**state), sip_transport_state_destroy);
549  if (!new_state) {
550  return NULL;
551  }
552  new_state->id = ast_strdup(ast_sorcery_object_get_id(transport));
553  new_state->type = transport->type;
554 
555  pjsip_tls_setting_default(&new_state->tls);
556 #ifdef HAVE_PJSIP_TLS_TRANSPORT_PROTO
557  /* proto must be forced to 0 to enable all protocols otherwise only TLS will work */
558  new_state->tls.proto = 0;
559 #endif
560  new_state->tls.ciphers = new_state->ciphers;
561 
562  ao2_ref(new_state, +1);
563  *state = new_state;
564 
565  return new_state;
566 }
567 
569 {
570  ast_assert(transport && transport->state);
571 
572  memcpy(&transport->host, &transport->state->host, sizeof(transport->host));
573  memcpy(&transport->tls, &transport->state->tls, sizeof(transport->tls));
574  memcpy(&transport->ciphers, &transport->state->ciphers, sizeof(transport->ciphers));
575  transport->localnet = transport->state->localnet;
577  memcpy(&transport->external_address, &transport->state->external_signaling_address, sizeof(transport->external_signaling_address));
578 }
579 
581 {
582  if (a->type != b->type) {
583  return -1;
584  }
585 
586  if (pj_sockaddr_cmp(&a->host, &b->host)) {
587  return -1;
588  }
589 
590  if ((a->localnet || b->localnet)
591  && ((!a->localnet != !b->localnet)
594  {
595  return -1;
596  }
597 
599  return -1;
600  }
601 
603  return -1;
604  }
605 
606  if (a->tls.method != b->tls.method
607  || a->tls.ciphers_num != b->tls.ciphers_num
609  || a->tls.proto != b->tls.proto
610 #endif
611  || a->tls.verify_client != b->tls.verify_client
612  || a->tls.verify_server != b->tls.verify_server
613  || a->tls.require_client_cert != b->tls.require_client_cert) {
614  return -1;
615  }
616 
617  if (memcmp(a->ciphers, b->ciphers, sizeof(pj_ssl_cipher) * fmax(a->tls.ciphers_num, b->tls.ciphers_num))) {
618  return -1;
619  }
620 
621  return 0;
622 }
623 
624 static void states_cleanup(void *states)
625 {
626  if (states) {
627  ao2_unlock(states);
628  }
629 }
630 
631 /*! \brief Apply handler for transports */
632 static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
633 {
634  struct ast_sip_transport *transport = obj;
635  const char *transport_id = ast_sorcery_object_get_id(obj);
636  RAII_VAR(struct ao2_container *, states, transport_states, states_cleanup);
637  RAII_VAR(struct internal_state *, temp_state, NULL, ao2_cleanup);
638  RAII_VAR(struct internal_state *, perm_state, NULL, ao2_cleanup);
639  RAII_VAR(struct ast_variable *, changes, NULL, ast_variables_destroy);
640  pj_status_t res = -1;
641  int i;
642 #define BIND_TRIES 3
643 #define BIND_DELAY_US 100000
644 
645  if (!states) {
646  return -1;
647  }
648 
649  /*
650  * transport_apply gets called for EVERY retrieval of a transport when using realtime.
651  * We need to prevent multiple threads from trying to mess with underlying transports
652  * at the same time. The container is the only thing we have to lock on.
653  */
654  ao2_wrlock(states);
655 
656  temp_state = internal_state_alloc(transport);
657  if (!temp_state) {
658  ast_log(LOG_ERROR, "Transport '%s' failed to allocate memory\n", transport_id);
659  return -1;
660  }
661 
662  perm_state = find_internal_state_by_transport(transport);
663  if (perm_state) {
664  ast_sorcery_diff(sorcery, perm_state->transport, transport, &changes);
665  if (!changes && !has_state_changed(perm_state->state, temp_state->state)) {
666  /* In case someone is using the deprecated fields, reset them */
667  transport->state = perm_state->state;
668  copy_state_to_transport(transport);
669  ao2_replace(perm_state->transport, transport);
670  return 0;
671  }
672 
673  /* If we aren't allowed to reload then we copy values that can't be changed from perm_state */
674  if (!transport->allow_reload) {
675  memcpy(&temp_state->state->host, &perm_state->state->host, sizeof(temp_state->state->host));
676  memcpy(&temp_state->state->tls, &perm_state->state->tls, sizeof(temp_state->state->tls));
677  memcpy(&temp_state->state->ciphers, &perm_state->state->ciphers, sizeof(temp_state->state->ciphers));
678  }
679  }
680 
681  if (!transport->flow && (!perm_state || transport->allow_reload)) {
682  if (temp_state->state->host.addr.sa_family != PJ_AF_INET && temp_state->state->host.addr.sa_family != PJ_AF_INET6) {
683  ast_log(LOG_ERROR, "Transport '%s' could not be started as binding not specified\n", transport_id);
684  return -1;
685  }
686 
687  /* Set default port if not present */
688  if (!pj_sockaddr_get_port(&temp_state->state->host)) {
689  pj_sockaddr_set_port(&temp_state->state->host, (transport->type == AST_TRANSPORT_TLS) ? 5061 : 5060);
690  }
691  }
692 
693  /* Now that we know what address family we can set up a dnsmgr refresh for the external addresses if present */
694  if (!ast_strlen_zero(transport->external_signaling_address)) {
695  if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
696  temp_state->state->external_signaling_address.ss.ss_family = AF_INET;
697  } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
698  temp_state->state->external_signaling_address.ss.ss_family = AF_INET6;
699  } else {
700  ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external signaling address\n",
701  transport_id);
702  return -1;
703  }
704 
705  if (ast_dnsmgr_lookup(transport->external_signaling_address, &temp_state->state->external_signaling_address, &temp_state->state->external_signaling_address_refresher, NULL) < 0) {
706  ast_log(LOG_ERROR, "Could not create dnsmgr for external signaling address on '%s'\n", transport_id);
707  return -1;
708  }
709  }
710 
711  if (!ast_strlen_zero(transport->external_media_address)) {
712  if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
713  temp_state->state->external_media_address.ss.ss_family = AF_INET;
714  } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
715  temp_state->state->external_media_address.ss.ss_family = AF_INET6;
716  } else {
717  ast_log(LOG_ERROR, "Unknown address family for transport '%s', could not get external media address\n",
718  transport_id);
719  return -1;
720  }
721 
722  if (ast_dnsmgr_lookup(transport->external_media_address, &temp_state->state->external_media_address, &temp_state->state->external_media_address_refresher, NULL) < 0) {
723  ast_log(LOG_ERROR, "Could not create dnsmgr for external media address on '%s'\n", transport_id);
724  return -1;
725  }
726  }
727 
728  if (transport->flow) {
729  pj_str_t address;
730 
731  ast_debug(1, "Ignoring any bind configuration on transport '%s' as it is a child of another\n",
732  transport_id);
733  pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&address, "0.0.0.0"), &temp_state->state->host);
734 
735  temp_state->state->flow = 1;
736  res = PJ_SUCCESS;
737  } else if (!transport->allow_reload && perm_state) {
738  /* We inherit the transport from perm state, untouched */
739  ast_log(LOG_WARNING, "Transport '%s' is not fully reloadable, not reloading: protocol, bind, TLS, TCP, ToS, or CoS options.\n", transport_id);
740  temp_state->state->transport = perm_state->state->transport;
741  perm_state->state->transport = NULL;
742  temp_state->state->factory = perm_state->state->factory;
743  perm_state->state->factory = NULL;
744  res = PJ_SUCCESS;
745  } else if (transport->type == AST_TRANSPORT_UDP) {
746 
747  for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
748  if (perm_state && perm_state->state && perm_state->state->transport) {
749  pjsip_udp_transport_pause(perm_state->state->transport,
750  PJSIP_UDP_TRANSPORT_DESTROY_SOCKET);
751  usleep(BIND_DELAY_US);
752  }
753 
754  if (temp_state->state->host.addr.sa_family == pj_AF_INET()) {
755  res = pjsip_udp_transport_start(ast_sip_get_pjsip_endpoint(),
756  &temp_state->state->host.ipv4, NULL, transport->async_operations,
757  &temp_state->state->transport);
758  } else if (temp_state->state->host.addr.sa_family == pj_AF_INET6()) {
759  res = pjsip_udp_transport_start6(ast_sip_get_pjsip_endpoint(),
760  &temp_state->state->host.ipv6, NULL, transport->async_operations,
761  &temp_state->state->transport);
762  }
763  }
764 
765  if (res == PJ_SUCCESS) {
766  temp_state->state->transport->info = pj_pool_alloc(temp_state->state->transport->pool,
767  (AST_SIP_X_AST_TXP_LEN + strlen(transport_id) + 2));
768 
769  sprintf(temp_state->state->transport->info, "%s:%s", AST_SIP_X_AST_TXP, transport_id);
770 
771  if (transport->tos || transport->cos) {
772  pj_sock_t sock;
773  pj_qos_params qos_params;
774  sock = pjsip_udp_transport_get_socket(temp_state->state->transport);
775  pj_sock_get_qos_params(sock, &qos_params);
776  set_qos(transport, &qos_params);
777  pj_sock_set_qos_params(sock, &qos_params);
778  }
779  }
780  } else if (transport->type == AST_TRANSPORT_TCP) {
781  pjsip_tcp_transport_cfg cfg;
782  static int option = 1;
783 
784  pjsip_tcp_transport_cfg_default(&cfg, temp_state->state->host.addr.sa_family);
785  cfg.bind_addr = temp_state->state->host;
786  cfg.async_cnt = transport->async_operations;
787  set_qos(transport, &cfg.qos_params);
788  /* sockopt_params.options is copied to each newly connected socket */
789  cfg.sockopt_params.options[0].level = pj_SOL_TCP();
790  cfg.sockopt_params.options[0].optname = pj_TCP_NODELAY();
791  cfg.sockopt_params.options[0].optval = &option;
792  cfg.sockopt_params.options[0].optlen = sizeof(option);
793  cfg.sockopt_params.cnt = 1;
794 
795  for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
796  if (perm_state && perm_state->state && perm_state->state->factory
797  && perm_state->state->factory->destroy) {
798  perm_state->state->factory->destroy(perm_state->state->factory);
799  usleep(BIND_DELAY_US);
800  }
801 
802  res = pjsip_tcp_transport_start3(ast_sip_get_pjsip_endpoint(), &cfg,
803  &temp_state->state->factory);
804  }
805  } else if (transport->type == AST_TRANSPORT_TLS) {
806 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
807  static int option = 1;
808 
809  if (transport->async_operations > 1 && ast_compare_versions(pj_get_version(), "2.5.0") < 0) {
810  ast_log(LOG_ERROR, "Transport: %s: When protocol=tls and pjproject version < 2.5.0, async_operations can't be > 1\n",
812  return -1;
813  }
814 
815  temp_state->state->tls.password = pj_str((char*)transport->password);
816  set_qos(transport, &temp_state->state->tls.qos_params);
817 
818  /* sockopt_params.options is copied to each newly connected socket */
819  temp_state->state->tls.sockopt_params.options[0].level = pj_SOL_TCP();
820  temp_state->state->tls.sockopt_params.options[0].optname = pj_TCP_NODELAY();
821  temp_state->state->tls.sockopt_params.options[0].optval = &option;
822  temp_state->state->tls.sockopt_params.options[0].optlen = sizeof(option);
823  temp_state->state->tls.sockopt_params.cnt = 1;
824 
825  for (i = 0; i < BIND_TRIES && res != PJ_SUCCESS; i++) {
826  if (perm_state && perm_state->state && perm_state->state->factory
827  && perm_state->state->factory->destroy) {
828  perm_state->state->factory->destroy(perm_state->state->factory);
829  usleep(BIND_DELAY_US);
830  }
831 
832  res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &temp_state->state->tls,
833  &temp_state->state->host, NULL, transport->async_operations,
834  &temp_state->state->factory);
835  }
836 #else
837  ast_log(LOG_ERROR, "Transport: %s: PJSIP has not been compiled with TLS transport support, ensure OpenSSL development packages are installed\n",
839  return -1;
840 #endif
841  } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) {
842  if (transport->cos || transport->tos) {
843  ast_log(LOG_WARNING, "TOS and COS values ignored for websocket transport\n");
844  } else if (!ast_strlen_zero(transport->ca_list_file) || !ast_strlen_zero(transport->ca_list_path) ||
845  !ast_strlen_zero(transport->cert_file) || !ast_strlen_zero(transport->privkey_file)) {
846  ast_log(LOG_WARNING, "TLS certificate values ignored for websocket transport as they are configured in http.conf\n");
847  }
848  res = PJ_SUCCESS;
849  }
850 
851  if (res != PJ_SUCCESS) {
852  char msg[PJ_ERR_MSG_SIZE];
853 
854  pj_strerror(res, msg, sizeof(msg));
855  ast_log(LOG_ERROR, "Transport '%s' could not be started: %s\n", ast_sorcery_object_get_id(obj), msg);
856  return -1;
857  }
858 
859  copy_state_to_transport(transport);
860  if (perm_state) {
861  ao2_unlink_flags(states, perm_state, OBJ_NOLOCK);
862  }
863  ao2_link_flags(states, temp_state, OBJ_NOLOCK);
864 
865  return 0;
866 }
867 
868 /*! \brief Custom handler for type just makes sure the state is created */
869 static int transport_state_init(const struct aco_option *opt, struct ast_variable *var, void *obj)
870 {
871  struct ast_sip_transport *transport = obj;
873 
874  ao2_cleanup(state);
875 
876  return 0;
877 }
878 
879 /*! \brief Custom handler for TLS method setting */
880 static int transport_tls_file_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
881 {
882  struct ast_sip_transport *transport = obj;
884 
885  if (!state) {
886  return -1;
887  }
888 
889  if (ast_strlen_zero(var->value)) {
890  /* Ignore empty options */
891  return 0;
892  }
893 
894  if (!ast_file_is_readable(var->value)) {
895  ast_log(LOG_ERROR, "Transport: %s: %s %s is either missing or not readable\n",
896  ast_sorcery_object_get_id(obj), var->name, var->value);
897  return -1;
898  }
899 
900  if (!strcasecmp(var->name, "ca_list_file")) {
901  state->tls.ca_list_file = pj_str((char*)var->value);
902  ast_string_field_set(transport, ca_list_file, var->value);
903  } else if (!strcasecmp(var->name, "ca_list_path")) {
904 #ifdef HAVE_PJ_SSL_CERT_LOAD_FROM_FILES2
905  state->tls.ca_list_path = pj_str((char*)var->value);
906  ast_string_field_set(transport, ca_list_path, var->value);
907 #else
908  ast_log(LOG_WARNING, "Asterisk has been built against a version of pjproject that does not "
909  "support the 'ca_list_path' option. Please upgrade to version 2.4 or later.\n");
910 #endif
911  } else if (!strcasecmp(var->name, "cert_file")) {
912  state->tls.cert_file = pj_str((char*)var->value);
913  ast_string_field_set(transport, cert_file, var->value);
914  } else if (!strcasecmp(var->name, "priv_key_file")) {
915  state->tls.privkey_file = pj_str((char*)var->value);
916  ast_string_field_set(transport, privkey_file, var->value);
917  }
918 
919  return 0;
920 }
921 
922 static int ca_list_file_to_str(const void *obj, const intptr_t *args, char **buf)
923 {
924  const struct ast_sip_transport *transport = obj;
925 
926  *buf = ast_strdup(transport->ca_list_file);
927 
928  return 0;
929 }
930 
931 static int ca_list_path_to_str(const void *obj, const intptr_t *args, char **buf)
932 {
933  const struct ast_sip_transport *transport = obj;
934 
935  *buf = ast_strdup(transport->ca_list_path);
936 
937  return 0;
938 }
939 
940 static int cert_file_to_str(const void *obj, const intptr_t *args, char **buf)
941 {
942  const struct ast_sip_transport *transport = obj;
943 
944  *buf = ast_strdup(transport->cert_file);
945 
946  return 0;
947 }
948 
949 static int privkey_file_to_str(const void *obj, const intptr_t *args, char **buf)
950 {
951  const struct ast_sip_transport *transport = obj;
952 
953  *buf = ast_strdup(transport->privkey_file);
954 
955  return 0;
956 }
957 
958 /*! \brief Custom handler for turning a string protocol into an enum */
959 static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
960 {
961  struct ast_sip_transport *transport = obj;
963 
964  if (!state) {
965  return -1;
966  }
967 
968  if (!strcasecmp(var->value, "flow")) {
969  transport->flow = 1;
970  } else {
971  if (!strcasecmp(var->value, "udp")) {
972  transport->type = AST_TRANSPORT_UDP;
973  } else if (!strcasecmp(var->value, "tcp")) {
974  transport->type = AST_TRANSPORT_TCP;
975  } else if (!strcasecmp(var->value, "tls")) {
976  transport->type = AST_TRANSPORT_TLS;
977  } else if (!strcasecmp(var->value, "ws")) {
978  transport->type = AST_TRANSPORT_WS;
979  } else if (!strcasecmp(var->value, "wss")) {
980  transport->type = AST_TRANSPORT_WSS;
981  } else {
982  return -1;
983  }
984  transport->flow = 0;
985  }
986 
987  state->type = transport->type;
988 
989  return 0;
990 }
991 
992 static const char *transport_types[] = {
993  [AST_TRANSPORT_UDP] = "udp",
994  [AST_TRANSPORT_TCP] = "tcp",
995  [AST_TRANSPORT_TLS] = "tls",
996  [AST_TRANSPORT_WS] = "ws",
997  [AST_TRANSPORT_WSS] = "wss"
998 };
999 
1000 static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
1001 {
1002  const struct ast_sip_transport *transport = obj;
1003 
1004  if (transport->flow) {
1005  *buf = ast_strdup("flow");
1006  } else if (ARRAY_IN_BOUNDS(transport->type, transport_types)) {
1007  *buf = ast_strdup(transport_types[transport->type]);
1008  }
1009 
1010  return 0;
1011 }
1012 
1013 /*! \brief Custom handler for turning a string bind into a pj_sockaddr */
1014 static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1015 {
1016  struct ast_sip_transport *transport = obj;
1017  pj_str_t buf;
1018  int rc;
1020 
1021  if (!state) {
1022  return -1;
1023  }
1024 
1025  rc = pj_sockaddr_parse(pj_AF_UNSPEC(), 0, pj_cstr(&buf, var->value), &state->host);
1026 
1027  return rc != PJ_SUCCESS ? -1 : 0;
1028 }
1029 
1030 static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
1031 {
1032  const struct ast_sip_transport *transport = obj;
1034 
1035  if (!state) {
1036  return -1;
1037  }
1038 
1039  if (!(*buf = ast_calloc(MAX_OBJECT_FIELD, sizeof(char)))) {
1040  return -1;
1041  }
1042 
1043  /* include port as well as brackets if IPv6 */
1044  pj_sockaddr_print(&state->host, *buf, MAX_OBJECT_FIELD, 1 | 2);
1045 
1046  return 0;
1047 }
1048 
1049 /*! \brief Custom handler for TLS boolean settings */
1050 static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1051 {
1052  struct ast_sip_transport *transport = obj;
1054 
1055  if (!state) {
1056  return -1;
1057  }
1058 
1059  if (!strcasecmp(var->name, "verify_server")) {
1060  state->tls.verify_server = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
1061  } else if (!strcasecmp(var->name, "verify_client")) {
1062  state->tls.verify_client = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
1063  } else if (!strcasecmp(var->name, "require_client_cert")) {
1064  state->tls.require_client_cert = ast_true(var->value) ? PJ_TRUE : PJ_FALSE;
1065  } else {
1066  return -1;
1067  }
1068 
1069  return 0;
1070 }
1071 
1072 static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
1073 {
1074  const struct ast_sip_transport *transport = obj;
1076 
1077  if (!state) {
1078  return -1;
1079  }
1080 
1081  *buf = ast_strdup(AST_YESNO(state->tls.verify_server));
1082 
1083  return 0;
1084 }
1085 
1086 static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
1087 {
1088  const struct ast_sip_transport *transport = obj;
1090 
1091  if (!state) {
1092  return -1;
1093  }
1094 
1095  *buf = ast_strdup(AST_YESNO(state->tls.verify_client));
1096 
1097  return 0;
1098 }
1099 
1100 static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
1101 {
1102  const struct ast_sip_transport *transport = obj;
1104 
1105  if (!state) {
1106  return -1;
1107  }
1108 
1109  *buf = ast_strdup(AST_YESNO(state->tls.require_client_cert));
1110 
1111  return 0;
1112 }
1113 
1114 /*! \brief Custom handler for TLS method setting */
1115 static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1116 {
1117  struct ast_sip_transport *transport = obj;
1119 
1120  if (!state) {
1121  return -1;
1122  }
1123 
1124  if (ast_strlen_zero(var->value) || !strcasecmp(var->value, "default")) {
1125  state->tls.method = PJSIP_SSL_DEFAULT_METHOD;
1126  } else if (!strcasecmp(var->value, "unspecified")) {
1127  state->tls.method = PJSIP_SSL_UNSPECIFIED_METHOD;
1128  } else if (!strcasecmp(var->value, "tlsv1")) {
1129  state->tls.method = PJSIP_TLSV1_METHOD;
1130 #ifdef HAVE_PJSIP_TLS_TRANSPORT_PROTO
1131  } else if (!strcasecmp(var->value, "tlsv1_1")) {
1132  state->tls.method = PJSIP_TLSV1_1_METHOD;
1133  } else if (!strcasecmp(var->value, "tlsv1_2")) {
1134  state->tls.method = PJSIP_TLSV1_2_METHOD;
1135 #endif
1136  } else if (!strcasecmp(var->value, "sslv2")) {
1137  state->tls.method = PJSIP_SSLV2_METHOD;
1138  } else if (!strcasecmp(var->value, "sslv3")) {
1139  state->tls.method = PJSIP_SSLV3_METHOD;
1140  } else if (!strcasecmp(var->value, "sslv23")) {
1141  state->tls.method = PJSIP_SSLV23_METHOD;
1142  } else {
1143  return -1;
1144  }
1145 
1146  return 0;
1147 }
1148 
1149 static const char *tls_method_map[] = {
1150  [PJSIP_SSL_UNSPECIFIED_METHOD] = "unspecified",
1151  [PJSIP_TLSV1_METHOD] = "tlsv1",
1152 #ifdef HAVE_PJSIP_TLS_TRANSPORT_PROTO
1153  [PJSIP_TLSV1_1_METHOD] = "tlsv1_1",
1154  [PJSIP_TLSV1_2_METHOD] = "tlsv1_2",
1155 #endif
1156  [PJSIP_SSLV2_METHOD] = "sslv2",
1157  [PJSIP_SSLV3_METHOD] = "sslv3",
1158  [PJSIP_SSLV23_METHOD] = "sslv23",
1159 };
1160 
1161 static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
1162 {
1163  const struct ast_sip_transport *transport = obj;
1165 
1166  if (!state) {
1167  return -1;
1168  }
1169 
1170  if (ARRAY_IN_BOUNDS(state->tls.method, tls_method_map)) {
1171  *buf = ast_strdup(tls_method_map[state->tls.method]);
1172  }
1173 
1174  return 0;
1175 }
1176 
1177 /*! \brief Helper function which turns a cipher name into an identifier */
1178 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1179 static pj_ssl_cipher cipher_name_to_id(const char *name)
1180 {
1181  pj_ssl_cipher ciphers[PJ_SSL_SOCK_MAX_CIPHERS];
1182  unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
1183  unsigned int pos;
1184 
1185  if (pj_ssl_cipher_get_availables(ciphers, &cipher_num)) {
1186  return 0;
1187  }
1188 
1189  for (pos = 0; pos < cipher_num; ++pos) {
1190  const char *pos_name = pj_ssl_cipher_name(ciphers[pos]);
1191  if (pos_name && !strcmp(pos_name, name)) {
1192  return ciphers[pos];
1193  }
1194  }
1195 
1196  return 0;
1197 }
1198 #endif
1199 
1200 /*!
1201  * \internal
1202  * \brief Add a new cipher to the transport's cipher list array.
1203  *
1204  * \param transport Which transport to add the cipher to.
1205  * \param name Cipher identifier name.
1206  *
1207  * \retval 0 on success.
1208  * \retval -1 on error.
1209  */
1210 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1211 static int transport_cipher_add(struct ast_sip_transport_state *state, const char *name)
1212 {
1213  pj_ssl_cipher cipher;
1214  int idx;
1215 
1216  cipher = cipher_name_to_id(name);
1217  if (!cipher) {
1218  /* TODO: Check this over/tweak - it's taken from pjsua for now */
1219  if (!strnicmp(name, "0x", 2)) {
1220  pj_str_t cipher_st = pj_str((char *) name + 2);
1221  cipher = pj_strtoul2(&cipher_st, NULL, 16);
1222  } else {
1223  cipher = atoi(name);
1224  }
1225  }
1226 
1227  if (pj_ssl_cipher_is_supported(cipher)) {
1228  for (idx = state->tls.ciphers_num; idx--;) {
1229  if (state->ciphers[idx] == cipher) {
1230  /* The cipher is already in the list. */
1231  return 0;
1232  }
1233  }
1234  state->ciphers[state->tls.ciphers_num++] = cipher;
1235  return 0;
1236  } else {
1237  ast_log(LOG_ERROR, "Cipher '%s' is unsupported\n", name);
1238  return -1;
1239  }
1240 }
1241 #endif
1242 
1243 /*! \brief Custom handler for TLS cipher setting */
1244 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1245 static int transport_tls_cipher_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1246 {
1247  struct ast_sip_transport *transport = obj;
1248  char *parse;
1249  char *name;
1250  int res = 0;
1252 
1253  if (!state) {
1254  return -1;
1255  }
1256 
1257  parse = ast_strdupa(S_OR(var->value, ""));
1258  while ((name = ast_strip(strsep(&parse, ",")))) {
1259  if (ast_strlen_zero(name)) {
1260  continue;
1261  }
1262  if (ARRAY_LEN(state->ciphers) <= state->tls.ciphers_num) {
1263  ast_log(LOG_ERROR, "Too many ciphers specified\n");
1264  res = -1;
1265  break;
1266  }
1267  res |= transport_cipher_add(state, name);
1268  }
1269  return res ? -1 : 0;
1270 }
1271 #endif
1272 
1273 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1274 static void cipher_to_str(char **buf, const pj_ssl_cipher *ciphers, unsigned int cipher_num)
1275 {
1276  struct ast_str *str;
1277  unsigned int idx;
1278 
1279  str = ast_str_create(128);
1280  if (!str) {
1281  *buf = NULL;
1282  return;
1283  }
1284 
1285  for (idx = 0; idx < cipher_num; ++idx) {
1286  ast_str_append(&str, 0, "%s", pj_ssl_cipher_name(ciphers[idx]));
1287  if (idx < cipher_num - 1) {
1288  ast_str_append(&str, 0, ", ");
1289  }
1290  }
1291 
1292  *buf = ast_strdup(ast_str_buffer(str));
1293  ast_free(str);
1294 }
1295 #endif
1296 
1297 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1298 static int transport_tls_cipher_to_str(const void *obj, const intptr_t *args, char **buf)
1299 {
1300  const struct ast_sip_transport *transport = obj;
1302 
1303  if (!state) {
1304  return -1;
1305  }
1306 
1307  cipher_to_str(buf, state->ciphers, state->tls.ciphers_num);
1308  return *buf ? 0 : -1;
1309 }
1310 #endif
1311 
1312 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1313 static char *handle_pjsip_list_ciphers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1314 {
1315  pj_ssl_cipher ciphers[PJ_SSL_SOCK_MAX_CIPHERS];
1316  unsigned int cipher_num = PJ_ARRAY_SIZE(ciphers);
1317  char *buf;
1318 
1319  switch (cmd) {
1320  case CLI_INIT:
1321  e->command = "pjsip list ciphers";
1322  e->usage = "Usage: pjsip list ciphers\n"
1323  " List available OpenSSL cipher names.\n";
1324  return NULL;
1325  case CLI_GENERATE:
1326  return NULL;
1327  }
1328 
1329  if (pj_ssl_cipher_get_availables(ciphers, &cipher_num) || !cipher_num) {
1330  buf = NULL;
1331  } else {
1332  cipher_to_str(&buf, ciphers, cipher_num);
1333  }
1334 
1335  if (!ast_strlen_zero(buf)) {
1336  ast_cli(a->fd, "Available ciphers: '%s'\n", buf);
1337  } else {
1338  ast_cli(a->fd, "No available ciphers\n");
1339  }
1340  ast_free(buf);
1341  return CLI_SUCCESS;
1342 }
1343 #endif
1344 
1345 /*! \brief Custom handler for localnet setting */
1346 static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1347 {
1348  struct ast_sip_transport *transport = obj;
1349  int error = 0;
1351 
1352  if (!state) {
1353  return -1;
1354  }
1355 
1356  if (ast_strlen_zero(var->value)) {
1357  ast_free_ha(state->localnet);
1358  state->localnet = NULL;
1359  return 0;
1360  }
1361 
1362  /* We use only the ast_apply_ha() which defaults to ALLOW
1363  * ("permit"), so we add DENY rules. */
1364  if (!(state->localnet = ast_append_ha("deny", var->value, state->localnet, &error))) {
1365  return -1;
1366  }
1367 
1368  return error;
1369 }
1370 
1371 static void localnet_to_vl_append(struct ast_variable **head, struct ast_ha *ha)
1372 {
1373  char str[MAX_OBJECT_FIELD];
1374  const char *addr = ast_strdupa(ast_sockaddr_stringify_addr(&ha->addr));
1375  snprintf(str, MAX_OBJECT_FIELD, "%s%s/%s", ha->sense == AST_SENSE_ALLOW ? "!" : "",
1376  addr, ast_sockaddr_stringify_addr(&ha->netmask));
1377 
1378  ast_variable_list_append(head, ast_variable_new("local_net", str, ""));
1379 }
1380 
1381 static int localnet_to_vl(const void *obj, struct ast_variable **fields)
1382 {
1383  const struct ast_sip_transport *transport = obj;
1384  struct ast_variable *head = NULL;
1385  struct ast_ha *ha;
1387 
1388  if (!state) {
1389  return -1;
1390  }
1391 
1392  for (ha = state->localnet; ha; ha = ha->next) {
1393  localnet_to_vl_append(&head, ha);
1394  }
1395 
1396  if (head) {
1397  *fields = head;
1398  }
1399 
1400  return 0;
1401 }
1402 
1403 static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
1404 {
1406  const struct ast_sip_transport *transport = obj;
1408 
1409  if (!state) {
1410  return -1;
1411  }
1412 
1413  ast_ha_join(state->localnet, &str);
1414  *buf = ast_strdup(ast_str_buffer(str));
1415  return 0;
1416 }
1417 
1418 /*! \brief Custom handler for TOS setting */
1419 static int transport_tos_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1420 {
1421  struct ast_sip_transport *transport = obj;
1422  unsigned int value;
1423 
1424  if (ast_str2tos(var->value, &value)) {
1425  ast_log(LOG_ERROR, "Error configuring transport '%s' - Could not "
1426  "interpret 'tos' value '%s'\n",
1427  ast_sorcery_object_get_id(transport), var->value);
1428  return -1;
1429  }
1430 
1431  if (value % 4) {
1432  value = value >> 2;
1433  value = value << 2;
1435  "transport '%s' - 'tos' value '%s' uses bits that are "
1436  "discarded when converted to DSCP. Using equivalent %u instead.\n",
1437  ast_sorcery_object_get_id(transport), var->value, value);
1438  }
1439 
1440  transport->tos = value;
1441  return 0;
1442 }
1443 
1444 static int tos_to_str(const void *obj, const intptr_t *args, char **buf)
1445 {
1446  const struct ast_sip_transport *transport = obj;
1447 
1448  if (ast_asprintf(buf, "%u", transport->tos) == -1) {
1449  return -1;
1450  }
1451  return 0;
1452 }
1453 
1454 static struct ao2_container *cli_get_container(const char *regex)
1455 {
1457  struct ao2_container *s_container;
1458 
1460  regex);
1461  if (!container) {
1462  return NULL;
1463  }
1464 
1467  if (!s_container) {
1468  return NULL;
1469  }
1470 
1471  if (ao2_container_dup(s_container, container, 0)) {
1472  ao2_ref(s_container, -1);
1473  return NULL;
1474  }
1475 
1476  return s_container;
1477 }
1478 
1479 static int cli_iterate(void *container, ao2_callback_fn callback, void *args)
1480 {
1481  const struct ast_sip_endpoint *endpoint = container;
1483  "transport", endpoint->transport);
1484 
1485  if (!transport) {
1486  return -1;
1487  }
1488 
1489  return callback(transport, args, 0);
1490 }
1491 
1492 static void *cli_retrieve_by_id(const char *id)
1493 {
1494  return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", id);
1495 }
1496 
1497 static int cli_print_header(void *obj, void *arg, int flags)
1498 {
1499  struct ast_sip_cli_context *context = arg;
1500  int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1501  int filler = CLI_MAX_WIDTH - indent - 61;
1502 
1503  ast_assert(context->output_buffer != NULL);
1504 
1505  ast_str_append(&context->output_buffer, 0,
1506  "%*s: <TransportId........> <Type> <cos> <tos> <BindAddress%*.*s>\n",
1507  indent, "Transport", filler, filler, CLI_HEADER_FILLER);
1508 
1509  return 0;
1510 }
1511 
1512 static int cli_print_body(void *obj, void *arg, int flags)
1513 {
1514  struct ast_sip_transport *transport = obj;
1515  struct ast_sip_cli_context *context = arg;
1516  char hoststr[PJ_INET6_ADDRSTRLEN];
1518 
1519  if (!state) {
1520  return -1;
1521  }
1522 
1523  ast_assert(context->output_buffer != NULL);
1524 
1525  pj_sockaddr_print(&state->host, hoststr, sizeof(hoststr), 3);
1526 
1527  ast_str_append(&context->output_buffer, 0, "%*s: %-21s %6s %5u %5u %s\n",
1528  CLI_INDENT_TO_SPACES(context->indent_level), "Transport",
1529  ast_sorcery_object_get_id(transport),
1530  ARRAY_IN_BOUNDS(transport->type, transport_types) ? transport_types[transport->type] : "Unknown",
1531  transport->cos, transport->tos, hoststr);
1532 
1533  if (context->show_details
1534  || (context->show_details_only_level_0 && context->indent_level == 0)) {
1535  ast_str_append(&context->output_buffer, 0, "\n");
1536  ast_sip_cli_print_sorcery_objectset(transport, context, 0);
1537  }
1538 
1539  return 0;
1540 }
1541 
1542 static struct ast_cli_entry cli_commands[] = {
1543 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1544  AST_CLI_DEFINE(handle_pjsip_list_ciphers, "List available OpenSSL cipher names"),
1545 #endif
1546  AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "List PJSIP Transports",
1547  .command = "pjsip list transports",
1548  .usage = "Usage: pjsip list transports [ like <pattern> ]\n"
1549  " List the configured PJSIP Transports\n"
1550  " Optional regular expression pattern is used to filter the list.\n"),
1551  AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transports",
1552  .command = "pjsip show transports",
1553  .usage = "Usage: pjsip show transports [ like <pattern> ]\n"
1554  " Show the configured PJSIP Transport\n"
1555  " Optional regular expression pattern is used to filter the list.\n"),
1556  AST_CLI_DEFINE(ast_sip_cli_traverse_objects, "Show PJSIP Transport",
1557  .command = "pjsip show transport",
1558  .usage = "Usage: pjsip show transport <id>\n"
1559  " Show the configured PJSIP Transport\n"),
1560 };
1561 
1563 
1564 struct ast_sip_transport_state *ast_sip_get_transport_state(const char *transport_id)
1565 {
1566  struct internal_state *state = NULL;
1567  struct ast_sip_transport_state *trans_state;
1568 
1569  if (!transport_states) {
1570  return NULL;
1571  }
1572 
1573  state = ao2_find(transport_states, transport_id, OBJ_SEARCH_KEY);
1574  if (!state) {
1575  return NULL;
1576  }
1577 
1578  trans_state = ao2_bump(state->state);
1579  ao2_ref(state, -1);
1580 
1581  /* If this is a child transport see if the transport is actually dead */
1582  if (trans_state->flow) {
1583  ao2_lock(trans_state);
1584  if (trans_state->transport && trans_state->transport->is_shutdown == PJ_TRUE) {
1585  pjsip_transport_dec_ref(trans_state->transport);
1586  trans_state->transport = NULL;
1587  }
1588  ao2_unlock(trans_state);
1589  }
1590 
1591  return trans_state;
1592 }
1593 
1594 static int populate_transport_states(void *obj, void *arg, int flags)
1595 {
1596  struct internal_state *state = obj;
1597  struct ao2_container *container = arg;
1598 
1599  ao2_link(container, state->state);
1600 
1601  return CMP_MATCH;
1602 }
1603 
1605 {
1608 
1609  if (!states) {
1610  return NULL;
1611  }
1612 
1613  ao2_callback(transport_states, OBJ_NODATA | OBJ_MULTIPLE, populate_transport_states, states);
1614  return states;
1615 }
1616 
1617 /*! \brief Initialize sorcery with transport support */
1619 {
1621  struct ao2_container *transports = NULL;
1622 
1623  /* Create outbound registration states container. */
1624  transport_states = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1626  if (!transport_states) {
1627  ast_log(LOG_ERROR, "Unable to allocate transport states container\n");
1628  return -1;
1629  }
1630 
1631  ast_sorcery_apply_default(sorcery, "transport", "config", "pjsip.conf,criteria=type=transport");
1632 
1634  return -1;
1635  }
1636 
1637  /* Normally type is a OPT_NOOP_T but we're using it to make sure that state is created */
1638  ast_sorcery_object_field_register_custom(sorcery, "transport", "type", "", transport_state_init, NULL, NULL, 0, 0);
1641  ast_sorcery_object_field_register(sorcery, "transport", "async_operations", "1", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, async_operations));
1642 
1643  ast_sorcery_object_field_register_custom(sorcery, "transport", "ca_list_file", "", transport_tls_file_handler, ca_list_file_to_str, NULL, 0, 0);
1644  ast_sorcery_object_field_register_custom(sorcery, "transport", "ca_list_path", "", transport_tls_file_handler, ca_list_path_to_str, NULL, 0, 0);
1645  ast_sorcery_object_field_register_custom(sorcery, "transport", "cert_file", "", transport_tls_file_handler, cert_file_to_str, NULL, 0, 0);
1646  ast_sorcery_object_field_register_custom(sorcery, "transport", "priv_key_file", "", transport_tls_file_handler, privkey_file_to_str, NULL, 0, 0);
1647 
1648  ast_sorcery_object_field_register(sorcery, "transport", "password", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, password));
1649  ast_sorcery_object_field_register(sorcery, "transport", "external_signaling_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_signaling_address));
1650  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);
1651  ast_sorcery_object_field_register(sorcery, "transport", "external_media_address", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, external_media_address));
1652  ast_sorcery_object_field_register(sorcery, "transport", "domain", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_transport, domain));
1653  ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_server", "", transport_tls_bool_handler, verify_server_to_str, NULL, 0, 0);
1654  ast_sorcery_object_field_register_custom(sorcery, "transport", "verify_client", "", transport_tls_bool_handler, verify_client_to_str, NULL, 0, 0);
1655  ast_sorcery_object_field_register_custom(sorcery, "transport", "require_client_cert", "", transport_tls_bool_handler, require_client_cert_to_str, NULL, 0, 0);
1657 #if defined(PJ_HAS_SSL_SOCK) && PJ_HAS_SSL_SOCK != 0
1658  ast_sorcery_object_field_register_custom(sorcery, "transport", "cipher", "", transport_tls_cipher_handler, transport_tls_cipher_to_str, NULL, 0, 0);
1659 #endif
1661  ast_sorcery_object_field_register_custom(sorcery, "transport", "tos", "0", transport_tos_handler, tos_to_str, NULL, 0, 0);
1662  ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos));
1663  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);
1664  ast_sorcery_object_field_register(sorcery, "transport", "allow_reload", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, allow_reload));
1665  ast_sorcery_object_field_register(sorcery, "transport", "symmetric_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, symmetric_transport));
1666 
1667  ast_sip_register_endpoint_formatter(&endpoint_transport_formatter);
1668 
1669  cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
1670  if (!cli_formatter) {
1671  ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
1672  return -1;
1673  }
1674  cli_formatter->name = "transport";
1675  cli_formatter->print_header = cli_print_header;
1676  cli_formatter->print_body = cli_print_body;
1677  cli_formatter->get_container = cli_get_container;
1678  cli_formatter->iterate = cli_iterate;
1679  cli_formatter->get_id = ast_sorcery_object_get_id;
1680  cli_formatter->retrieve_by_id = cli_retrieve_by_id;
1681 
1682  ast_sip_register_cli_formatter(cli_formatter);
1683  ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
1684 
1685  /* trigger load of transports from realtime by trying to revrieve them all */
1687  ao2_cleanup(transports);
1688 
1689  return 0;
1690 }
1691 
1693 {
1694  ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
1695  ast_sip_unregister_cli_formatter(cli_formatter);
1696 
1697  ast_sip_unregister_endpoint_formatter(&endpoint_transport_formatter);
1698 
1699  ao2_ref(transport_states, -1);
1700  transport_states = NULL;
1701 
1702  return 0;
1703 }
static char * ast_sockaddr_stringify_addr(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() to return an address only.
Definition: netsock2.h:290
enum ast_transport type
Definition: res_pjsip.h:101
struct ast_ha * next
Definition: acl.h:56
struct ast_str * output_buffer
Definition: res_pjsip_cli.h:36
struct ao2_container *(* get_container)(const char *regex)
Definition: res_pjsip_cli.h:64
#define ARRAY_IN_BOUNDS(v, a)
Checks to see if value is within the bounds of the given array.
Definition: utils.h:658
int ast_sip_transport_state_set_transport(const char *transport_name, pjsip_transport *transport)
Sets the PJSIP transport on a child transport.
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
struct ast_ha * localnet
Definition: res_pjsip.h:124
unsigned int cos
Definition: chan_iax2.c:352
struct ast_dnsmgr_entry * external_address_refresher
Definition: res_pjsip.h:227
static int format_ami_endpoint_transport(const struct ast_sip_endpoint *endpoint, struct ast_sip_ami *ami)
void astman_append(struct mansession *s, const char *fmt,...)
Definition: manager.c:3080
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int transport_localnet_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Helper function which turns a cipher name into an identifier.
An entity responsible formatting endpoint information.
Definition: res_pjsip.h:2763
Definition: res_pjsip_cli.h:52
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static int transport_bind_to_str(const void *obj, const intptr_t *args, char **buf)
static int transport_bind_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for turning a string bind into a pj_sockaddr.
struct ast_sip_transport_state * ast_sip_get_transport_state(const char *transport_id)
Retrieve transport state.
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
struct ast_sockaddr addr
Definition: acl.h:53
const ast_string_field transport
Definition: res_pjsip.h:817
static struct @111 qos
char * address
Definition: f2c.h:59
static int transport_state_cmp(void *obj, void *arg, int flags)
comparator function for state objects
static int transport_tls_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for TLS method setting.
const struct message * m
Definition: res_pjsip.h:2741
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
void ast_sip_service_route_vector_destroy(struct ast_sip_service_route_vector *service_routes)
Destroy a vector of service routes.
static int transport_state_init(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for type just makes sure the state is created.
static void sip_transport_destroy(void *obj)
Destructor for transport.
struct ast_sip_transport_state * state
Transport state information.
descriptor for a cli entry.
Definition: cli.h:171
#define LOG_WARNING
Definition: logger.h:274
int ast_file_is_readable(const char *filename)
Test that a file exists and is readable by the effective user.
Definition: main/utils.c:2855
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
const ast_string_field external_media_address
Definition: res_pjsip.h:191
struct ast_ha * localnet
Definition: res_pjsip.h:221
AMI variable container.
Definition: res_pjsip.h:2737
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1335
static int has_state_changed(struct ast_sip_transport_state *a, struct ast_sip_transport_state *b)
Structure for variables, used for configurations and for channel variables.
#define var
Definition: ast_expr2f.c:614
static int cli_print_header(void *obj, void *arg, int flags)
static struct ao2_container * transport_states
Perform no matching, return all objects.
Definition: sorcery.h:123
int ast_sorcery_object_id_compare(void *obj, void *arg, int flags)
ao2 object comparator based on sorcery id.
Definition: sorcery.c:2459
static int cert_file_to_str(const void *obj, const intptr_t *args, char **buf)
Definition: cli.h:152
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
int ast_sorcery_diff(const struct ast_sorcery *sorcery, const void *original, const void *modified, struct ast_variable **changes)
Create a changeset of two objects.
Definition: sorcery.c:1805
Full structure for sorcery.
Definition: sorcery.c:230
int(* iterate)(void *container, ao2_callback_fn callback, void *args)
Definition: res_pjsip_cli.h:66
static struct internal_state * find_internal_state_by_transport(const struct ast_sip_transport *transport)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
enum ast_acl_sense sense
Definition: acl.h:55
Return all matching objects.
Definition: sorcery.h:120
static int sip_transport_to_ami(const struct ast_sip_transport *transport, struct ast_str **buf)
#define ast_assert(a)
Definition: utils.h:695
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
static int localnet_to_vl(const void *obj, struct ast_variable **fields)
#define ao2_unlock(a)
Definition: astobj2.h:730
pj_sockaddr host
Definition: res_pjsip.h:199
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
const char * str
Definition: app_jack.c:147
void ast_sip_unregister_endpoint_formatter(struct ast_sip_endpoint_formatter *obj)
Unregister an endpoint formatter.
Definition: res_pjsip.c:3685
int ast_str2tos(const char *value, unsigned int *tos)
Convert a string to the appropriate TOS value.
Definition: acl.c:967
const char * args
#define NULL
Definition: resample.c:96
int ast_sockaddr_cmp(const struct ast_sockaddr *a, const struct ast_sockaddr *b)
Compares two ast_sockaddr structures.
Definition: netsock2.c:388
unsigned int cos
Definition: res_pjsip.h:243
Domain data structure.
Definition: sip.h:888
struct ast_sip_transport_state * state
Definition: res_pjsip.h:239
#define ao2_wrlock(a)
Definition: astobj2.h:720
int value
Definition: syslog.c:37
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
int ast_sip_push_task_wait_servant(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to SIP servants and wait for it to complete.
Definition: res_pjsip.c:5204
static void set_qos(struct ast_sip_transport *transport, pj_qos_params *qos)
enum ast_transport type
Definition: res_pjsip.h:193
static struct ast_str * password
Definition: cdr_mysql.c:77
static int verify_client_to_str(const void *obj, const intptr_t *args, char **buf)
void astman_send_error_va(struct mansession *s, const struct message *m, const char *fmt,...)
Send error in manager transaction (with va_args support)
Definition: manager.c:3164
void ast_ha_join(const struct ast_ha *ha, struct ast_str **buf)
Convert HAs to a comma separated string value.
Definition: acl.c:723
static int transport_apply(const struct ast_sorcery *sorcery, void *obj)
Apply handler for transports.
char * ast_sip_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: pjsip_cli.c:109
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:5063
struct ao2_container * ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
Retrieve multiple objects using a regular expression on their id.
Definition: sorcery.c:1949
static int transport_state_hash(const void *obj, const int flags)
hashing function for state objects
Utility functions.
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269
struct ast_sockaddr external_address
Definition: res_pjsip.h:233
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_sip_register_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
Registers a CLI formatter.
Definition: pjsip_cli.c:310
#define ao2_bump(obj)
Definition: astobj2.h:491
internal representation of ACL entries In principle user applications would have no need for this...
Definition: acl.h:51
const ast_string_field password
Definition: res_pjsip.h:191
struct ast_sockaddr external_media_address
Definition: res_pjsip.h:144
static struct ast_sip_transport_state * find_state_by_transport(const struct ast_sip_transport *transport)
static int internal_state_hash(const void *obj, const int flags)
hashing function for state objects
static const char * transport_types[]
#define DEFAULT_STATE_BUCKETS
Default number of state container buckets.
void ast_sip_register_endpoint_formatter(struct ast_sip_endpoint_formatter *obj)
Register an endpoint formatter.
Definition: res_pjsip.c:3679
static struct ast_sip_transport_state * find_or_create_temporary_state(struct ast_sip_transport *transport)
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
int() ao2_callback_fn(void *obj, void *arg, int flags)
Type of a generic callback function.
Definition: astobj2.h:1230
#define ast_log
Definition: astobj2.c:42
#define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags,...)
Register a field within an object with custom handlers.
Definition: sorcery.h:1005
const ast_string_field ca_list_path
Definition: res_pjsip.h:191
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
static int require_client_cert_to_str(const void *obj, const intptr_t *args, char **buf)
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
Structure for SIP transport information.
Definition: res_pjsip.h:87
static void states_cleanup(void *states)
struct ast_sockaddr netmask
Definition: acl.h:54
int ast_sip_initialize_sorcery_transport(void)
Initialize sorcery with transport support.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
Type for default option handler for unsigned integers.
const int fd
Definition: cli.h:159
void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
Free a DNS manager entry.
Definition: dnsmgr.c:136
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define CLI_INDENT_TO_SPACES(x)
Definition: res_pjsip_cli.h:29
struct pjsip_transport * transport
Transport itself.
Definition: res_pjsip.h:89
static void localnet_to_vl_append(struct ast_variable **head, struct ast_ha *ha)
pjsip_tls_setting tls
Definition: res_pjsip.h:111
Access Control of various sorts.
static int tls_method_to_str(const void *obj, const intptr_t *args, char **buf)
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static int transport_tls_bool_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for TLS boolean settings.
#define ao2_lock(a)
Definition: astobj2.h:718
static void * cli_retrieve_by_id(const char *id)
static void * sip_transport_alloc(const char *name)
Allocator for transport.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
int ast_sorcery_object_id_sort(const void *obj, const void *arg, int flags)
ao2 object sorter based on sorcery id.
Definition: sorcery.c:2435
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
void *(* retrieve_by_id)(const char *id)
Definition: res_pjsip_cli.h:68
struct mansession * s
Definition: res_pjsip.h:2739
static void copy_state_to_transport(struct ast_sip_transport *transport)
struct ao2_container * container
Definition: res_fax.c:502
#define ast_variable_new(name, value, filename)
int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
Unregisters a CLI formatter.
Definition: pjsip_cli.c:326
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
Support for WebSocket connections within the Asterisk HTTP server and client WebSocket connections to...
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:838
static struct ast_threadstorage temp_state_store
static int destroy_sip_transport_state(void *data)
#define MAX_OBJECT_FIELD
Maximum length of an object field name.
Definition: sorcery.h:110
static struct internal_state * internal_state_alloc(struct ast_sip_transport *transport)
static int populate_transport_states(void *obj, void *arg, int flags)
static int ca_list_path_to_str(const void *obj, const intptr_t *args, char **buf)
#define CLI_MAX_WIDTH
Definition: res_pjsip_cli.h:26
#define CLI_HEADER_FILLER
Definition: res_pjsip_cli.h:24
static int verify_server_to_str(const void *obj, const intptr_t *args, char **buf)
#define AST_SIP_X_AST_TXP_LEN
Definition: res_pjsip.h:910
static int cli_iterate(void *container, ao2_callback_fn callback, void *args)
#define HAVE_PJSIP_TLS_TRANSPORT_PROTO
Definition: autoconfig.h:672
void ast_sip_message_apply_transport(const char *transport_name, pjsip_tx_data *tdata)
Apply the configuration for a transport to an outgoing message.
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
Definition: main/utils.c:1951
const ast_string_field external_signaling_address
Definition: res_pjsip.h:191
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static void internal_state_destroy(void *obj)
Destructor for ast_sip_transport state information.
char * usage
Definition: utils/frame.c:37
static const char * tls_method_map[]
int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
Copy all object references in the src container into the dest container.
Type for default option handler for bools (ast_true/ast_false)
Transport to bind to.
Definition: res_pjsip.h:171
struct ast_sip_endpoint_formatter endpoint_transport_formatter
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
unsigned int async_operations
Definition: res_pjsip.h:201
static int transport_tos_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for TOS setting.
static int internal_state_cmp(void *obj, void *arg, int flags)
comparator function for state objects
struct ast_sip_service_route_vector * ast_sip_service_route_vector_alloc(void)
Allocate a vector of service routes.
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
static struct ao2_container * cli_get_container(const char *regex)
const ast_string_field ca_list_file
Definition: res_pjsip.h:191
const ast_string_field privkey_file
Definition: res_pjsip.h:191
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
char * command
Definition: cli.h:186
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:948
static int localnet_to_str(const void *obj, const intptr_t *args, char **buf)
static int transport_protocol_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for turning a string protocol into an enum.
int ast_sip_transport_state_set_service_routes(const char *transport_name, struct ast_sip_service_route_vector *service_routes)
Sets the service routes on a child transport.
int(* format_ami)(const struct ast_sip_endpoint *endpoint, struct ast_sip_ami *ami)
Callback used to format endpoint information over AMI.
Definition: res_pjsip.h:2767
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
const char *(* get_id)(const void *obj)
Definition: res_pjsip_cli.h:70
static int transport_tls_file_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for TLS method setting.
struct ast_str * ast_sip_create_ami_event(const char *event, struct ast_sip_ami *ami)
Creates a string to store AMI event data in.
Support for logging to various files, console and syslog Configuration in file logger.conf.
struct ast_sockaddr external_signaling_address
Definition: res_pjsip.h:134
const char * usage
Definition: cli.h:177
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
#define AST_THREADSTORAGE_CUSTOM(a, b, c)
Define a thread storage variable, with custom initialization and cleanup.
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
static int ca_list_file_to_str(const void *obj, const intptr_t *args, char **buf)
void ast_free_ha(struct ast_ha *ha)
Free a list of HAs.
Definition: acl.c:222
#define CLI_SUCCESS
Definition: cli.h:44
#define AST_YESNO(x)
return Yes or No depending on the argument.
Definition: strings.h:139
static struct ast_sip_cli_formatter_entry * cli_formatter
static int remove_temporary_state(void)
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
char * strsep(char **str, const char *delims)
#define ao2_replace(dst, src)
Definition: astobj2.h:517
static struct ast_sorcery * sorcery
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_dnsmgr_lookup(const char *name, struct ast_sockaddr *result, struct ast_dnsmgr_entry **dnsmgr, const char *service)
Allocate and initialize a DNS manager entry.
Definition: dnsmgr.c:191
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
#define AST_SIP_X_AST_TXP
Definition: res_pjsip.h:909
struct ao2_container * ast_sip_get_transport_states(void)
Retrieves all transport states.
static struct ast_sip_transport_state * find_temporary_state(struct ast_sip_transport *transport)
#define AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR
Default websocket write timeout, in ms (as a string)
pj_ssl_cipher ciphers[SIP_TLS_MAX_CIPHERS]
Definition: res_pjsip.h:215
struct ast_sip_transport * transport
Transport configuration object.
int ast_sip_sorcery_object_to_ami(const void *obj, struct ast_str **buf)
Converts a sorcery object to a string of object properties.
#define ao2_unlink_flags(container, obj, flags)
Definition: astobj2.h:1622
Type for default option handler for stringfields.
static int tos_to_str(const void *obj, const intptr_t *args, char **buf)
ao2_callback_fn * print_header
Definition: res_pjsip_cli.h:60
static struct test_val b
unsigned int tos
Definition: res_pjsip.h:241
struct ast_ha * ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error)
Add a new rule to a list of HAs.
Definition: acl.c:713
int error(const char *format,...)
Definition: utils/frame.c:999
static int privkey_file_to_str(const void *obj, const intptr_t *args, char **buf)
#define BIND_DELAY_US
int ast_compare_versions(const char *version1, const char *version2)
Compare 2 major.minor.patch.extra version strings.
Definition: main/utils.c:2872
struct ast_sip_service_route_vector * service_routes
Definition: res_pjsip.h:159
Generic container type.
static int transport_protocol_to_str(const void *obj, const intptr_t *args, char **buf)
int ast_sip_transport_state_set_preferred_identity(const char *transport_name, const char *identity)
Sets the P-Preferred-Identity on a child transport.
Search option field mask.
Definition: astobj2.h:1076
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
int ast_sip_destroy_sorcery_transport(void)
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728
static int cli_print_body(void *obj, void *arg, int flags)
Type for default option handler for signed integers.
ao2_callback_fn * print_body
Definition: res_pjsip_cli.h:62
struct ast_dnsmgr_entry * external_media_address_refresher
Definition: res_pjsip.h:139
int ast_sip_cli_print_sorcery_objectset(void *obj, void *arg, int flags)
Prints a sorcery object&#39;s ast_variable list.
Definition: pjsip_cli.c:36
const char * name
Definition: res_pjsip_cli.h:58
static void temp_state_store_cleanup(void *data)
#define BIND_TRIES
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:611
pjsip_tls_setting tls
Definition: res_pjsip.h:209
#define ast_variable_list_append(head, new_var)
static struct ast_cli_entry cli_commands[]
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:865
struct ast_dnsmgr_entry * external_signaling_address_refresher
Definition: res_pjsip.h:129
const ast_string_field cert_file
Definition: res_pjsip.h:191
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
Sorcery Data Access Layer API.
pj_ssl_cipher ciphers[SIP_TLS_MAX_CIPHERS]
Definition: res_pjsip.h:116
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206
static void sip_transport_state_destroy(void *obj)
Destructor for ast_sip_transport state information.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514
unsigned show_details_only_level_0
Definition: res_pjsip_cli.h:46
static struct test_val a
#define ao2_link(container, obj)
Definition: astobj2.h:1549