diff options
author | Matt Caswell <matt@openssl.org> | 2023-10-30 12:04:40 +0000 |
---|---|---|
committer | Hugo Landau <hlandau@openssl.org> | 2023-11-02 08:16:25 +0000 |
commit | 3b60efa109a4637bf30d8d2b6067a5ea7151eff3 (patch) | |
tree | 2ad06683da6e37bbd868a4f9f9601cbfc244d1ab /demos/guide/quic-client-block.c | |
parent | 70de52650d9aeab475745edcfa268112ed2a3eff (diff) |
Update the QUIC demos to accept hostname/port on the command line
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22552)
(cherry picked from commit 420037c82c4b2bfea952cbe00730930844969438)
Diffstat (limited to 'demos/guide/quic-client-block.c')
-rw-r--r-- | demos/guide/quic-client-block.c | 41 |
1 files changed, 25 insertions, 16 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; } |