poll_immediate.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/common.h"
  10. #include <openssl/ssl.h>
  11. #include <openssl/err.h>
  12. #include "../ssl_local.h"
  13. #if defined(_AIX)
  14. /*
  15. * Some versions of AIX define macros for events and revents for use when
  16. * accessing pollfd structures (see Github issue #24236). That interferes
  17. * with our use of these names here. We simply undef them.
  18. */
  19. # undef revents
  20. # undef events
  21. #endif
  22. #define ITEM_N(items, stride, n) \
  23. (*(SSL_POLL_ITEM *)((char *)(items) + (n)*(stride)))
  24. #define FAIL_FROM(n) \
  25. do { \
  26. size_t j; \
  27. \
  28. for (j = (n); j < num_items; ++j) \
  29. ITEM_N(items, stride, j).revents = 0; \
  30. \
  31. ok = 0; \
  32. goto out; \
  33. } while (0)
  34. #define FAIL_ITEM(i) \
  35. do { \
  36. ITEM_N(items, stride, i).revents = SSL_POLL_EVENT_F; \
  37. ++result_count; \
  38. FAIL_FROM(i + 1); \
  39. } while (0)
  40. int SSL_poll(SSL_POLL_ITEM *items,
  41. size_t num_items,
  42. size_t stride,
  43. const struct timeval *timeout,
  44. uint64_t flags,
  45. size_t *p_result_count)
  46. {
  47. int ok = 1;
  48. size_t i, result_count = 0;
  49. SSL_POLL_ITEM *item;
  50. SSL *ssl;
  51. uint64_t revents;
  52. ossl_unused uint64_t events;
  53. ossl_unused int do_tick = ((flags & SSL_POLL_FLAG_NO_HANDLE_EVENTS) == 0);
  54. int is_immediate
  55. = (timeout != NULL
  56. && timeout->tv_sec == 0 && timeout->tv_usec == 0);
  57. /*
  58. * Prevent calls which use SSL_poll functionality which is not currently
  59. * supported.
  60. */
  61. if (!is_immediate) {
  62. ERR_raise_data(ERR_LIB_SSL, SSL_R_POLL_REQUEST_NOT_SUPPORTED,
  63. "SSL_poll does not currently support blocking "
  64. "operation");
  65. FAIL_FROM(0);
  66. }
  67. /* Trivial case. */
  68. if (num_items == 0)
  69. goto out;
  70. /* Poll current state of each item. */
  71. for (i = 0; i < num_items; ++i) {
  72. item = &ITEM_N(items, stride, i);
  73. events = item->events;
  74. revents = 0;
  75. switch (item->desc.type) {
  76. case BIO_POLL_DESCRIPTOR_TYPE_SSL:
  77. ssl = item->desc.value.ssl;
  78. if (ssl == NULL)
  79. /* NULL items are no-ops and have revents reported as 0 */
  80. break;
  81. switch (ssl->type) {
  82. #ifndef OPENSSL_NO_QUIC
  83. case SSL_TYPE_QUIC_CONNECTION:
  84. case SSL_TYPE_QUIC_XSO:
  85. if (!ossl_quic_conn_poll_events(ssl, events, do_tick, &revents))
  86. /* above call raises ERR */
  87. FAIL_ITEM(i);
  88. if (revents != 0)
  89. ++result_count;
  90. break;
  91. #endif
  92. default:
  93. ERR_raise_data(ERR_LIB_SSL, SSL_R_POLL_REQUEST_NOT_SUPPORTED,
  94. "SSL_poll currently only supports QUIC SSL "
  95. "objects");
  96. FAIL_ITEM(i);
  97. }
  98. break;
  99. case BIO_POLL_DESCRIPTOR_TYPE_SOCK_FD:
  100. ERR_raise_data(ERR_LIB_SSL, SSL_R_POLL_REQUEST_NOT_SUPPORTED,
  101. "SSL_poll currently does not support polling "
  102. "sockets");
  103. FAIL_ITEM(i);
  104. default:
  105. ERR_raise_data(ERR_LIB_SSL, SSL_R_POLL_REQUEST_NOT_SUPPORTED,
  106. "SSL_poll does not support unknown poll descriptor "
  107. "type %d", item->desc.type);
  108. FAIL_ITEM(i);
  109. }
  110. item->revents = revents;
  111. }
  112. /* TODO(QUIC POLLING): Blocking mode */
  113. /* TODO(QUIC POLLING): Support for polling FDs */
  114. out:
  115. if (p_result_count != NULL)
  116. *p_result_count = result_count;
  117. return ok;
  118. }