From 3b60efa109a4637bf30d8d2b6067a5ea7151eff3 Mon Sep 17 00:00:00 2001 From: Matt Caswell Date: Mon, 30 Oct 2023 12:04:40 +0000 Subject: Update the QUIC demos to accept hostname/port on the command line Reviewed-by: Paul Dale Reviewed-by: Tomas Mraz Reviewed-by: Richard Levitte Reviewed-by: Hugo Landau (Merged from https://github.com/openssl/openssl/pull/22552) (cherry picked from commit 420037c82c4b2bfea952cbe00730930844969438) --- demos/guide/quic-client-block.c | 41 +++++++++++++++++----------- demos/guide/quic-client-non-block.c | 46 +++++++++++++++++++------------ demos/guide/quic-multi-stream.c | 54 ++++++++++++++++++++++++------------- 3 files changed, 90 insertions(+), 51 deletions(-) diff --git a/demos/guide/quic-client-block.c b/demos/guide/quic-client-block.c index 65822fe8c4..782f571559 100644 --- a/demos/guide/quic-client-block.c +++ b/demos/guide/quic-client-block.c @@ -108,21 +108,13 @@ static BIO *create_socket_bio(const char *hostname, const char *port, return bio; } -/* Server hostname and port details. Must be in quotes */ -#ifndef HOSTNAME -# define HOSTNAME "www.example.com" -#endif -#ifndef PORT -# define PORT "443" -#endif - /* * Simple application to send a basic HTTP/1.0 request to a server and * print the response on the screen. Note that HTTP/1.0 over QUIC is * non-standard and will not typically be supported by real world servers. This * is for demonstration purposes only. */ -int main(void) +int main(int argc, char *argv[]) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; @@ -130,11 +122,20 @@ int main(void) int res = EXIT_FAILURE; int ret; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; - const char *request = - "GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; + const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: "; + const char *request_end = "\r\n\r\n"; size_t written, readbytes; char buf[160]; BIO_ADDR *peer_addr = NULL; + char *hostname, *port; + + if (argc != 3) { + printf("Usage: quic-client-block hostname port\n"); + goto end; + } + + hostname = argv[1]; + port = argv[2]; /* * Create an SSL_CTX which we can use to create SSL objects from. We @@ -171,7 +172,7 @@ int main(void) * Create the underlying transport socket/BIO and associate it with the * connection. */ - bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); + bio = create_socket_bio(hostname, port, &peer_addr); if (bio == NULL) { printf("Failed to crete the BIO\n"); goto end; @@ -182,7 +183,7 @@ int main(void) * Tell the server during the handshake which hostname we are attempting * to connect to in case the server supports multiple hosts. */ - if (!SSL_set_tlsext_host_name(ssl, HOSTNAME)) { + if (!SSL_set_tlsext_host_name(ssl, hostname)) { printf("Failed to set the SNI hostname\n"); goto end; } @@ -193,7 +194,7 @@ int main(void) * Virtually all clients should do this unless you really know what you * are doing. */ - if (!SSL_set1_host(ssl, HOSTNAME)) { + if (!SSL_set1_host(ssl, hostname)) { printf("Failed to set the certificate verification hostname"); goto end; } @@ -224,8 +225,16 @@ int main(void) } /* Write an HTTP GET request to the peer */ - if (!SSL_write_ex(ssl, request, strlen(request), &written)) { - printf("Failed to write HTTP request\n"); + if (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) { + printf("Failed to write start of HTTP request\n"); + goto end; + } + if (!SSL_write_ex(ssl, hostname, strlen(hostname), &written)) { + printf("Failed to write hostname in HTTP request\n"); + goto end; + } + if (!SSL_write_ex(ssl, request_end, strlen(request_end), &written)) { + printf("Failed to write end of HTTP request\n"); goto end; } diff --git a/demos/guide/quic-client-non-block.c b/demos/guide/quic-client-non-block.c index 61d339c79c..31596d84c5 100644 --- a/demos/guide/quic-client-non-block.c +++ b/demos/guide/quic-client-non-block.c @@ -215,22 +215,13 @@ static int handle_io_failure(SSL *ssl, int res) return -1; } } - -/* Server hostname and port details. Must be in quotes */ -#ifndef HOSTNAME -# define HOSTNAME "www.example.com" -#endif -#ifndef PORT -# define PORT "443" -#endif - /* * Simple application to send a basic HTTP/1.0 request to a server and * print the response on the screen. Note that HTTP/1.0 over QUIC is * non-standard and will not typically be supported by real world servers. This * is for demonstration purposes only. */ -int main(void) +int main(int argc, char *argv[]) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; @@ -238,12 +229,21 @@ int main(void) int res = EXIT_FAILURE; int ret; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; - const char *request = - "GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; + const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: "; + const char *request_end = "\r\n\r\n"; size_t written, readbytes; char buf[160]; BIO_ADDR *peer_addr = NULL; int eof = 0; + char *hostname, *port; + + if (argc != 3) { + printf("Usage: quic-client-non-block hostname port\n"); + goto end; + } + + hostname = argv[1]; + port = argv[2]; /* * Create an SSL_CTX which we can use to create SSL objects from. We @@ -280,7 +280,7 @@ int main(void) * Create the underlying transport socket/BIO and associate it with the * connection. */ - bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); + bio = create_socket_bio(hostname, port, &peer_addr); if (bio == NULL) { printf("Failed to crete the BIO\n"); goto end; @@ -291,7 +291,7 @@ int main(void) * Tell the server during the handshake which hostname we are attempting * to connect to in case the server supports multiple hosts. */ - if (!SSL_set_tlsext_host_name(ssl, HOSTNAME)) { + if (!SSL_set_tlsext_host_name(ssl, hostname)) { printf("Failed to set the SNI hostname\n"); goto end; } @@ -302,7 +302,7 @@ int main(void) * Virtually all clients should do this unless you really know what you * are doing. */ - if (!SSL_set1_host(ssl, HOSTNAME)) { + if (!SSL_set1_host(ssl, hostname)) { printf("Failed to set the certificate verification hostname"); goto end; } @@ -338,10 +338,22 @@ int main(void) } /* Write an HTTP GET request to the peer */ - while (!SSL_write_ex(ssl, request, strlen(request), &written)) { + while (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) { + if (handle_io_failure(ssl, 0) == 1) + continue; /* Retry */ + printf("Failed to write start of HTTP request\n"); + goto end; /* Cannot retry: error */ + } + while (!SSL_write_ex(ssl, hostname, strlen(hostname), &written)) { + if (handle_io_failure(ssl, 0) == 1) + continue; /* Retry */ + printf("Failed to write hostname in HTTP request\n"); + goto end; /* Cannot retry: error */ + } + while (!SSL_write_ex(ssl, request_end, strlen(request_end), &written)) { if (handle_io_failure(ssl, 0) == 1) continue; /* Retry */ - printf("Failed to write HTTP request\n"); + printf("Failed to write end of HTTP request\n"); goto end; /* Cannot retry: error */ } diff --git a/demos/guide/quic-multi-stream.c b/demos/guide/quic-multi-stream.c index 44ee36e0ad..469c5ba4b2 100644 --- a/demos/guide/quic-multi-stream.c +++ b/demos/guide/quic-multi-stream.c @@ -108,13 +108,22 @@ static BIO *create_socket_bio(const char *hostname, const char *port, return bio; } -/* Server hostname and port details. Must be in quotes */ -#ifndef HOSTNAME -# define HOSTNAME "www.example.com" -#endif -#ifndef PORT -# define PORT "443" -#endif +int write_a_request(SSL *stream, const char *request_start, + const char *hostname) +{ + const char *request_end = "\r\n\r\n"; + size_t written; + + if (!SSL_write_ex(stream, request_start, strlen(request_start), + &written)) + return 0; + if (!SSL_write_ex(stream, hostname, strlen(hostname), &written)) + return 0; + if (!SSL_write_ex(stream, request_end, strlen(request_end), &written)) + return 0; + + return 1; +} /* * Simple application to send basic HTTP/1.0 requests to a server and print the @@ -122,7 +131,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port, * and will not be supported by real world servers. This is for demonstration * purposes only. */ -int main(void) +int main(int argc, char *argv[]) { SSL_CTX *ctx = NULL; SSL *ssl = NULL; @@ -131,13 +140,22 @@ int main(void) int res = EXIT_FAILURE; int ret; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; - const char *request1 = - "GET /request1.html HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; - const char *request2 = - "GET /request2.html HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; - size_t written, readbytes; + const char *request1_start = + "GET /request1.html HTTP/1.0\r\nConnection: close\r\nHost: "; + const char *request2_start = + "GET /request2.html HTTP/1.0\r\nConnection: close\r\nHost: "; + size_t readbytes; char buf[160]; BIO_ADDR *peer_addr = NULL; + char *hostname, *port; + + if (argc != 3) { + printf("Usage: quic-client-non-block hostname port\n"); + goto end; + } + + hostname = argv[1]; + port = argv[2]; /* * Create an SSL_CTX which we can use to create SSL objects from. We @@ -183,7 +201,7 @@ int main(void) * Create the underlying transport socket/BIO and associate it with the * connection. */ - bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); + bio = create_socket_bio(hostname, port, &peer_addr); if (bio == NULL) { printf("Failed to crete the BIO\n"); goto end; @@ -194,7 +212,7 @@ int main(void) * Tell the server during the handshake which hostname we are attempting * to connect to in case the server supports multiple hosts. */ - if (!SSL_set_tlsext_host_name(ssl, HOSTNAME)) { + if (!SSL_set_tlsext_host_name(ssl, hostname)) { printf("Failed to set the SNI hostname\n"); goto end; } @@ -205,7 +223,7 @@ int main(void) * Virtually all clients should do this unless you really know what you * are doing. */ - if (!SSL_set1_host(ssl, HOSTNAME)) { + if (!SSL_set1_host(ssl, hostname)) { printf("Failed to set the certificate verification hostname"); goto end; } @@ -247,12 +265,12 @@ int main(void) } /* Write an HTTP GET request on each of our streams to the peer */ - if (!SSL_write_ex(stream1, request1, strlen(request1), &written)) { + if (!write_a_request(stream1, request1_start, hostname)) { printf("Failed to write HTTP request on stream 1\n"); goto end; } - if (!SSL_write_ex(stream2, request2, strlen(request2), &written)) { + if (!write_a_request(stream2, request2_start, hostname)) { printf("Failed to write HTTP request on stream 2\n"); goto end; } -- cgit v1.2.3