diff options
author | Hugo Landau <hlandau@openssl.org> | 2023-08-09 17:46:33 +0100 |
---|---|---|
committer | Hugo Landau <hlandau@openssl.org> | 2023-09-01 10:45:35 +0100 |
commit | b3e71dbf6863343cfabdbe2ba0443fcb69343874 (patch) | |
tree | 775377363561cf4d33ca3f065ff883ecb48081a1 | |
parent | 24e583619c6f4bb1e7659b6b4f06cea920710688 (diff) |
QUIC DDD: ddd-04-fd-nonblocking: Planned changes
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/21715)
-rw-r--r-- | doc/designs/ddd/ddd-04-fd-nonblocking.c | 123 |
1 files changed, 117 insertions, 6 deletions
diff --git a/doc/designs/ddd/ddd-04-fd-nonblocking.c b/doc/designs/ddd/ddd-04-fd-nonblocking.c index f8a5162a2e..ef15ecb29a 100644 --- a/doc/designs/ddd/ddd-04-fd-nonblocking.c +++ b/doc/designs/ddd/ddd-04-fd-nonblocking.c @@ -27,7 +27,11 @@ SSL_CTX *create_ssl_ctx(void) { SSL_CTX *ctx; +#ifdef USE_QUIC + ctx = SSL_CTX_new(QUIC_client_method()); +#else ctx = SSL_CTX_new(TLS_client_method()); +#endif if (ctx == NULL) return NULL; @@ -180,7 +184,11 @@ int get_conn_fd(APP_CONN *conn) */ int get_conn_pending_tx(APP_CONN *conn) { +#ifdef USE_QUIC + return POLLIN | POLLOUT | POLLERR; +#else return (conn->tx_need_rx ? POLLIN : 0) | POLLOUT | POLLERR; +#endif } int get_conn_pending_rx(APP_CONN *conn) @@ -188,6 +196,28 @@ int get_conn_pending_rx(APP_CONN *conn) return (conn->rx_need_tx ? POLLOUT : 0) | POLLIN | POLLERR; } +#ifdef USE_QUIC +/* + * Returns the number of milliseconds after which some call to libssl must be + * made. Any call (SSL_read/SSL_write/SSL_pump) will do. Returns -1 if there is + * no need for such a call. This may change after the next call + * to libssl. + */ +int get_conn_pump_timeout(APP_CONN *conn) +{ + return SSL_get_timeout(conn->ssl); +} + +/* + * Called to advance internals of libssl state machines without having to + * perform an application-level read/write. + */ +void pump(APP_CONN *conn) +{ + SSL_pump(conn->ssl); +} +#endif + /* * The application wants to close the connection and free bookkeeping * structures. @@ -216,10 +246,28 @@ void teardown_ctx(SSL_CTX *ctx) #include <sys/types.h> #include <sys/socket.h> #include <sys/signal.h> +#ifdef USE_QUIC +# include <sys/time.h> +#endif #include <netdb.h> #include <unistd.h> #include <fcntl.h> +#ifdef USE_QUIC + +static inline void ms_to_timeval(struct timeval *t, int ms) +{ + t->tv_sec = ms < 0 ? -1 : ms/1000; + t->tv_usec = ms < 0 ? 0 : (ms%1000)*1000; +} + +static inline int timeval_to_ms(const struct timeval *t) +{ + return t->tv_sec*1000 + t->tv_usec/1000; +} + +#endif + int main(int argc, char **argv) { int rc, fd = -1, res = 1; @@ -227,11 +275,19 @@ int main(int argc, char **argv) const char *tx_p = tx_msg; char rx_buf[2048]; int l, tx_len = sizeof(tx_msg)-1; +#ifdef USE_QUIC + struct timeval timeout; +#else int timeout = 2000 /* ms */; +#endif APP_CONN *conn = NULL; struct addrinfo hints = {0}, *result = NULL; SSL_CTX *ctx = NULL; +#ifdef USE_QUIC + ms_to_timeval(&timeout, 2000); +#endif + if (argc < 3) { fprintf(stderr, "usage: %s host port\n", argv[0]); goto fail; @@ -257,7 +313,11 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); +#ifdef USE_QUIC + fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); +#else fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); +#endif if (fd < 0) { fprintf(stderr, "cannot create socket\n"); goto fail; @@ -291,12 +351,38 @@ int main(int argc, char **argv) fprintf(stderr, "tx error\n"); goto fail; } else if (l == -2) { +#ifdef USE_QUIC + struct timeval start, now, deadline, t; +#endif struct pollfd pfd = {0}; + +#ifdef USE_QUIC + ms_to_timeval(&t, get_conn_pump_timeout(conn)); + if (t.tv_sec < 0 || timercmp(&t, &timeout, >)) + t = timeout; + + gettimeofday(&start, NULL); + timeradd(&start, &timeout, &deadline); +#endif + pfd.fd = get_conn_fd(conn); pfd.events = get_conn_pending_tx(conn); - if (poll(&pfd, 1, timeout) == 0) { - fprintf(stderr, "tx timeout\n"); - goto fail; +#ifdef USE_QUIC + if (poll(&pfd, 1, timeval_to_ms(&t)) == 0) +#else + if (poll(&pfd, 1, timeout) == 0) +#endif + { +#ifdef USE_QUIC + pump(conn); + + gettimeofday(&now, NULL); + if (timercmp(&now, &deadline, >=)) +#endif + { + fprintf(stderr, "tx timeout\n"); + goto fail; + } } } } @@ -309,12 +395,37 @@ int main(int argc, char **argv) } else if (l == -1) { break; } else if (l == -2) { +#ifdef USE_QUIC + struct timeval start, now, deadline, t; +#endif struct pollfd pfd = {0}; + +#ifdef USE_QUIC + ms_to_timeval(&t, get_conn_pump_timeout(conn)); + if (t.tv_sec < 0 || timercmp(&t, &timeout, >)) + t = timeout; + + gettimeofday(&start, NULL); + timeradd(&start, &timeout, &deadline); +#endif + pfd.fd = get_conn_fd(conn); pfd.events = get_conn_pending_rx(conn); - if (poll(&pfd, 1, timeout) == 0) { - fprintf(stderr, "rx timeout\n"); - goto fail; +#ifdef USE_QUIC + if (poll(&pfd, 1, timeval_to_ms(&t)) == 0) +#else + if (poll(&pfd, 1, timeout) == 0) +#endif + { +#ifdef USE_QUIC + pump(conn); + gettimeofday(&now, NULL); + if (timercmp(&now, &deadline, >=)) +#endif + { + fprintf(stderr, "rx timeout\n"); + goto fail; + } } } } |