quic_port.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2023-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. #ifndef OSSL_QUIC_PORT_H
  10. # define OSSL_QUIC_PORT_H
  11. # include <openssl/ssl.h>
  12. # include "internal/quic_types.h"
  13. # include "internal/quic_reactor.h"
  14. # include "internal/quic_demux.h"
  15. # include "internal/quic_predef.h"
  16. # include "internal/thread_arch.h"
  17. # ifndef OPENSSL_NO_QUIC
  18. /*
  19. * QUIC Port
  20. * =========
  21. *
  22. * A QUIC Port (QUIC_PORT) represents a single UDP network socket and contains
  23. * zero or more subsidiary QUIC_CHANNEL instances, each of which represents a
  24. * single QUIC connection. All QUIC_CHANNEL instances must belong to a
  25. * QUIC_PORT.
  26. *
  27. * A QUIC port is responsible for managing a set of channels which all use the
  28. * same UDP socket, and (in future) for automatically creating new channels when
  29. * incoming connections are received.
  30. *
  31. * In order to retain compatibility with QUIC_TSERVER, it also supports a point
  32. * of legacy compatibility where a caller can create an incoming (server role)
  33. * channel and that channel will be automatically be bound to the next incoming
  34. * connection. In the future this will go away once QUIC_TSERVER is removed.
  35. *
  36. * All QUIC_PORT instances are created by a QUIC_ENGINE.
  37. */
  38. typedef struct quic_port_args_st {
  39. /* The engine which the QUIC port is to be a child of. */
  40. QUIC_ENGINE *engine;
  41. /*
  42. * This callback allows port_new_handshake_layer to pre-create a quic
  43. * connection object for the incoming channel
  44. * user_ssl_arg is expected to point to a quic listener object
  45. */
  46. SSL *(*get_conn_user_ssl)(QUIC_CHANNEL *ch, void *arg);
  47. void *user_ssl_arg;
  48. /*
  49. * This SSL_CTX will be used when constructing the handshake layer object
  50. * inside newly created channels.
  51. */
  52. SSL_CTX *channel_ctx;
  53. /*
  54. * If 1, this port is to be used for multiple connections, so
  55. * non-zero-length CIDs should be used. If 0, this port will only be used
  56. * for a single connection, so a zero-length local CID can be used.
  57. */
  58. int is_multi_conn;
  59. /*
  60. * if 1, this port should do server address validation
  61. */
  62. int do_addr_validation;
  63. } QUIC_PORT_ARGS;
  64. /* Only QUIC_ENGINE should use this function. */
  65. QUIC_PORT *ossl_quic_port_new(const QUIC_PORT_ARGS *args);
  66. void ossl_quic_port_free(QUIC_PORT *port);
  67. /*
  68. * Operations
  69. * ==========
  70. */
  71. /* Create an outgoing channel using this port. */
  72. QUIC_CHANNEL *ossl_quic_port_create_outgoing(QUIC_PORT *port, SSL *tls);
  73. /*
  74. * Create an incoming channel using this port.
  75. *
  76. * TODO(QUIC FUTURE): temporary TSERVER use only - will be removed.
  77. */
  78. QUIC_CHANNEL *ossl_quic_port_create_incoming(QUIC_PORT *port, SSL *tls);
  79. /*
  80. * Pop an incoming channel from the incoming channel queue. Returns NULL if
  81. * there are no pending incoming channels.
  82. */
  83. QUIC_CHANNEL *ossl_quic_port_pop_incoming(QUIC_PORT *port);
  84. /* Returns 1 if there is at least one connection incoming. */
  85. int ossl_quic_port_have_incoming(QUIC_PORT *port);
  86. /*
  87. * Delete any channels which are pending acceptance.
  88. */
  89. void ossl_quic_port_drop_incoming(QUIC_PORT *port);
  90. /*
  91. * Queries and Accessors
  92. * =====================
  93. */
  94. /* Gets/sets the underlying network read and write BIO. */
  95. BIO *ossl_quic_port_get_net_rbio(QUIC_PORT *port);
  96. BIO *ossl_quic_port_get_net_wbio(QUIC_PORT *port);
  97. int ossl_quic_port_set_net_rbio(QUIC_PORT *port, BIO *net_rbio);
  98. int ossl_quic_port_set_net_wbio(QUIC_PORT *port, BIO *net_wbio);
  99. SSL_CTX *ossl_quic_port_get_channel_ctx(QUIC_PORT *port);
  100. /*
  101. * Re-poll the network BIOs already set to determine if their support for
  102. * polling has changed. If force is 0, only check again if the BIOs have been
  103. * changed.
  104. */
  105. int ossl_quic_port_update_poll_descriptors(QUIC_PORT *port, int force);
  106. /* Gets the engine which this port is a child of. */
  107. QUIC_ENGINE *ossl_quic_port_get0_engine(QUIC_PORT *port);
  108. /* Gets the reactor which can be used to tick/poll on the port. */
  109. QUIC_REACTOR *ossl_quic_port_get0_reactor(QUIC_PORT *port);
  110. /* Gets the demuxer belonging to the port. */
  111. QUIC_DEMUX *ossl_quic_port_get0_demux(QUIC_PORT *port);
  112. /* Gets the mutex used by the port. */
  113. CRYPTO_MUTEX *ossl_quic_port_get0_mutex(QUIC_PORT *port);
  114. /* Gets the current time. */
  115. OSSL_TIME ossl_quic_port_get_time(QUIC_PORT *port);
  116. int ossl_quic_port_get_rx_short_dcid_len(const QUIC_PORT *port);
  117. int ossl_quic_port_get_tx_init_dcid_len(const QUIC_PORT *port);
  118. /* Returns 1 if the port is running/healthy, 0 if it has failed. */
  119. int ossl_quic_port_is_running(const QUIC_PORT *port);
  120. /*
  121. * Restores port-level error to the error stack. To be called only if
  122. * the port is no longer running.
  123. */
  124. void ossl_quic_port_restore_err_state(const QUIC_PORT *port);
  125. /* For use by QUIC_ENGINE. You should not need to call this directly. */
  126. void ossl_quic_port_subtick(QUIC_PORT *port, QUIC_TICK_RESULT *r,
  127. uint32_t flags);
  128. /* Returns the number of queued incoming channels. */
  129. size_t ossl_quic_port_get_num_incoming_channels(const QUIC_PORT *port);
  130. /* Sets if incoming connections should currently be allowed. */
  131. void ossl_quic_port_set_allow_incoming(QUIC_PORT *port, int allow_incoming);
  132. /* Returns 1 if we are using addressed mode on the read side. */
  133. int ossl_quic_port_is_addressed_r(const QUIC_PORT *port);
  134. /* Returns 1 if we are using addressed mode on the write side. */
  135. int ossl_quic_port_is_addressed_w(const QUIC_PORT *port);
  136. /* Returns 1 if we are using addressed mode. */
  137. int ossl_quic_port_is_addressed(const QUIC_PORT *port);
  138. /*
  139. * Returns the current network BIO epoch. This increments whenever the network
  140. * BIO configuration changes.
  141. */
  142. uint64_t ossl_quic_port_get_net_bio_epoch(const QUIC_PORT *port);
  143. /*
  144. * Events
  145. * ======
  146. */
  147. /*
  148. * Called if a permanent network error occurs. Terminates all channels
  149. * immediately. triggering_ch is an optional argument designating
  150. * a channel which encountered the network error.
  151. */
  152. void ossl_quic_port_raise_net_error(QUIC_PORT *port,
  153. QUIC_CHANNEL *triggering_ch);
  154. # endif
  155. #endif