From b94cb0c8fb2fcb50a8054da958eb59ce604f1d1e Mon Sep 17 00:00:00 2001 From: Daniel McCarney Date: Mon, 9 Dec 2024 16:56:38 -0500 Subject: [PATCH] tests: move server specific code out of common After the client rewrite some of the pieces in common are now server specific. Move these bits into `server.c`. For now I've put them into `server.c` in the order required for compilation pending a larger top-down order rewrite. --- tests/common.c | 80 ----------------------------------------------- tests/common.h | 22 ------------- tests/server.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 102 deletions(-) diff --git a/tests/common.c b/tests/common.c index b1cc264f..7755a7dc 100644 --- a/tests/common.c +++ b/tests/common.c @@ -108,21 +108,6 @@ write_cb(void *userdata, const unsigned char *buf, const size_t len, return 0; } -rustls_io_result -write_tls(rustls_connection *rconn, conndata *conn, size_t *n) -{ -#ifdef _WIN32 - return rustls_connection_write_tls(rconn, write_cb, conn, n); -#else - if(getenv("VECTORED_IO")) { - return rustls_connection_write_tls_vectored( - rconn, write_vectored_cb, conn, n); - } - - return rustls_connection_write_tls(rconn, write_cb, conn, n); -#endif /* _WIN32 */ -} - #ifndef _WIN32 rustls_io_result write_vectored_cb(void *userdata, const rustls_iovec *iov, size_t count, @@ -183,60 +168,6 @@ bytevec_ensure_available(bytevec *vec, const size_t n) return DEMO_OK; } -/* - * Do one read from the socket, and process all resulting bytes into the - * rustls_connection. - * Returns: - * - DEMO_OK for success - * - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the - * socket - * - DEMO_EOF if we got EOF - * - DEMO_ERROR for other errors. - */ -demo_result -do_read(conndata *conn, rustls_connection *rconn) -{ - size_t n = 0; - char buf[1]; - const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n); - if(err == EWOULDBLOCK) { - LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno)); - return DEMO_AGAIN; - } - else if(err != 0) { - LOG("reading from socket: errno %d", err); - return DEMO_ERROR; - } - else if(n == 0) { - return DEMO_EOF; - } - LOG("read %zu bytes from socket", n); - - const rustls_result rr = rustls_connection_process_new_packets(rconn); - if(rr != RUSTLS_RESULT_OK) { - print_error("in process_new_packets", rr); - return DEMO_ERROR; - } - - const demo_result dr = copy_plaintext_to_buffer(conn); - if(dr != DEMO_EOF) { - return dr; - } - - /* If we got an EOF on the plaintext stream (peer closed connection cleanly), - * verify that the sender then closed the TCP connection. */ - const ssize_t signed_n = read(conn->fd, buf, sizeof(buf)); - if(signed_n > 0) { - LOG("error: read returned %zu bytes after receiving close_notify", n); - return DEMO_ERROR; - } - else if(signed_n < 0 && errno != EWOULDBLOCK) { - LOG("wrong error after receiving close_notify: %s", strerror(errno)); - return DEMO_ERROR; - } - return DEMO_EOF; -} - /** * Copy all available plaintext from rustls into our own buffer, growing * our buffer as much as needed. @@ -320,17 +251,6 @@ memmem(const void *haystack, size_t haystacklen, const void *needle, return NULL; } -char * -body_beginning(const bytevec *vec) -{ - const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4); - if(result == NULL) { - return NULL; - } - - return (char *)result + 4; -} - void log_cb(void *userdata, const rustls_log_params *params) { diff --git a/tests/common.h b/tests/common.h index e1c4251e..0db4d6aa 100644 --- a/tests/common.h +++ b/tests/common.h @@ -64,11 +64,6 @@ demo_result nonblock(int sockfd); /* A callback that reads bytes from the network. */ int read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n); -/* Invoke rustls_connection_write_tls with either a vectored or unvectored - callback, depending on environment variable. */ -rustls_io_result write_tls(rustls_connection *rconn, conndata *conn, - size_t *n); - /* A callback that writes bytes to the network. */ int write_cb(void *userdata, const uint8_t *buf, uintptr_t len, uintptr_t *out_n); @@ -92,18 +87,6 @@ void bytevec_consume(bytevec *vec, size_t n); * DEMO_ERROR. */ demo_result bytevec_ensure_available(bytevec *vec, size_t n); -/* - * Do one read from the socket, and process all resulting bytes into the - * rustls_connection. - * Returns: - * - DEMO_OK for success - * - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the - * socket - * - DEMO_EOF if we got EOF - * - DEMO_ERROR for other errors. - */ -demo_result do_read(conndata *conn, rustls_connection *rconn); - /* Read all available bytes from the rustls_connection until EOF. * Note that EOF here indicates "no more bytes until * process_new_packets", not "stream is closed". @@ -118,11 +101,6 @@ demo_result copy_plaintext_to_buffer(conndata *conn); void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen); -/* If headers are done (received \r\n\r\n), return a pointer to the beginning - * of the body. Otherwise return NULL. - */ -char *body_beginning(const bytevec *vec); - void log_cb(void *userdata, const rustls_log_params *params); demo_result read_file(const char *filename, char *buf, size_t buflen, diff --git a/tests/server.c b/tests/server.c index 28b5ae27..ea7d1db0 100644 --- a/tests/server.c +++ b/tests/server.c @@ -61,6 +61,91 @@ send_response(const conndata *conn) return DEMO_OK; } +/* Invoke rustls_connection_write_tls with either a vectored or unvectored + callback, depending on environment variable. */ +rustls_io_result +write_tls(rustls_connection *rconn, conndata *conn, size_t *n) +{ +#ifdef _WIN32 + return rustls_connection_write_tls(rconn, write_cb, conn, n); +#else + if(getenv("VECTORED_IO")) { + return rustls_connection_write_tls_vectored( + rconn, write_vectored_cb, conn, n); + } + + return rustls_connection_write_tls(rconn, write_cb, conn, n); +#endif /* _WIN32 */ +} + +/* + * Do one read from the socket, and process all resulting bytes into the + * rustls_connection. + * Returns: + * - DEMO_OK for success + * - DEMO_AGAIN if we got an EAGAIN or EWOULDBLOCK reading from the + * socket + * - DEMO_EOF if we got EOF + * - DEMO_ERROR for other errors. + */ +demo_result +do_read(conndata *conn, rustls_connection *rconn) +{ + size_t n = 0; + char buf[1]; + const int err = rustls_connection_read_tls(rconn, read_cb, conn, &n); + if(err == EWOULDBLOCK) { + LOG("reading from socket: EAGAIN or EWOULDBLOCK: %s", strerror(errno)); + return DEMO_AGAIN; + } + else if(err != 0) { + LOG("reading from socket: errno %d", err); + return DEMO_ERROR; + } + else if(n == 0) { + return DEMO_EOF; + } + LOG("read %zu bytes from socket", n); + + const rustls_result rr = rustls_connection_process_new_packets(rconn); + if(rr != RUSTLS_RESULT_OK) { + print_error("in process_new_packets", rr); + return DEMO_ERROR; + } + + const demo_result dr = copy_plaintext_to_buffer(conn); + if(dr != DEMO_EOF) { + return dr; + } + + /* If we got an EOF on the plaintext stream (peer closed connection cleanly), + * verify that the sender then closed the TCP connection. */ + const ssize_t signed_n = read(conn->fd, buf, sizeof(buf)); + if(signed_n > 0) { + LOG("error: read returned %zu bytes after receiving close_notify", n); + return DEMO_ERROR; + } + else if(signed_n < 0 && errno != EWOULDBLOCK) { + LOG("wrong error after receiving close_notify: %s", strerror(errno)); + return DEMO_ERROR; + } + return DEMO_EOF; +} + +/* If headers are done (received \r\n\r\n), return a pointer to the beginning + * of the body. Otherwise return NULL. + */ +char * +body_beginning(const bytevec *vec) +{ + const void *result = memmem(vec->data, vec->len, "\r\n\r\n", 4); + if(result == NULL) { + return NULL; + } + + return (char *)result + 4; +} + void handle_conn(conndata *conn) {