tls_channel_handler_shared.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/io/private/tls_channel_handler_shared.h>
  6. #include <aws/common/clock.h>
  7. #include <aws/io/tls_channel_handler.h>
  8. static void s_tls_timeout_task_fn(struct aws_channel_task *channel_task, void *arg, enum aws_task_status status) {
  9. (void)channel_task;
  10. if (status != AWS_TASK_STATUS_RUN_READY) {
  11. return;
  12. }
  13. struct aws_tls_channel_handler_shared *tls_handler_shared = arg;
  14. if (tls_handler_shared->stats.handshake_status != AWS_TLS_NEGOTIATION_STATUS_ONGOING) {
  15. return;
  16. }
  17. struct aws_channel *channel = tls_handler_shared->handler->slot->channel;
  18. aws_channel_shutdown(channel, AWS_IO_TLS_NEGOTIATION_TIMEOUT);
  19. }
  20. void aws_tls_channel_handler_shared_init(
  21. struct aws_tls_channel_handler_shared *tls_handler_shared,
  22. struct aws_channel_handler *handler,
  23. struct aws_tls_connection_options *options) {
  24. tls_handler_shared->handler = handler;
  25. tls_handler_shared->tls_timeout_ms = options->timeout_ms;
  26. aws_crt_statistics_tls_init(&tls_handler_shared->stats);
  27. aws_channel_task_init(&tls_handler_shared->timeout_task, s_tls_timeout_task_fn, tls_handler_shared, "tls_timeout");
  28. }
  29. void aws_tls_channel_handler_shared_clean_up(struct aws_tls_channel_handler_shared *tls_handler_shared) {
  30. (void)tls_handler_shared;
  31. }
  32. void aws_on_drive_tls_negotiation(struct aws_tls_channel_handler_shared *tls_handler_shared) {
  33. if (tls_handler_shared->stats.handshake_status == AWS_TLS_NEGOTIATION_STATUS_NONE) {
  34. tls_handler_shared->stats.handshake_status = AWS_TLS_NEGOTIATION_STATUS_ONGOING;
  35. uint64_t now = 0;
  36. aws_channel_current_clock_time(tls_handler_shared->handler->slot->channel, &now);
  37. tls_handler_shared->stats.handshake_start_ns = now;
  38. if (tls_handler_shared->tls_timeout_ms > 0) {
  39. uint64_t timeout_ns =
  40. now + aws_timestamp_convert(
  41. tls_handler_shared->tls_timeout_ms, AWS_TIMESTAMP_MILLIS, AWS_TIMESTAMP_NANOS, NULL);
  42. aws_channel_schedule_task_future(
  43. tls_handler_shared->handler->slot->channel, &tls_handler_shared->timeout_task, timeout_ns);
  44. }
  45. }
  46. }
  47. void aws_on_tls_negotiation_completed(struct aws_tls_channel_handler_shared *tls_handler_shared, int error_code) {
  48. tls_handler_shared->stats.handshake_status =
  49. (error_code == AWS_ERROR_SUCCESS) ? AWS_TLS_NEGOTIATION_STATUS_SUCCESS : AWS_TLS_NEGOTIATION_STATUS_FAILURE;
  50. aws_channel_current_clock_time(
  51. tls_handler_shared->handler->slot->channel, &tls_handler_shared->stats.handshake_end_ns);
  52. }
  53. bool aws_tls_options_buf_is_set(const struct aws_byte_buf *buf) {
  54. return buf->allocator != NULL;
  55. }