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)
This commit is contained in:
Matt Caswell 2023-10-30 12:04:40 +00:00 committed by Hugo Landau
parent a2b824730e
commit 420037c82c
3 changed files with 90 additions and 51 deletions

View File

@ -108,21 +108,13 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
return bio; 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 * 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 * 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 * non-standard and will not typically be supported by real world servers. This
* is for demonstration purposes only. * is for demonstration purposes only.
*/ */
int main(void) int main(int argc, char *argv[])
{ {
SSL_CTX *ctx = NULL; SSL_CTX *ctx = NULL;
SSL *ssl = NULL; SSL *ssl = NULL;
@ -130,11 +122,20 @@ int main(void)
int res = EXIT_FAILURE; int res = EXIT_FAILURE;
int ret; int ret;
unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
const char *request = const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: ";
"GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; const char *request_end = "\r\n\r\n";
size_t written, readbytes; size_t written, readbytes;
char buf[160]; char buf[160];
BIO_ADDR *peer_addr = NULL; 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 * 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 * Create the underlying transport socket/BIO and associate it with the
* connection. * connection.
*/ */
bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); bio = create_socket_bio(hostname, port, &peer_addr);
if (bio == NULL) { if (bio == NULL) {
printf("Failed to crete the BIO\n"); printf("Failed to crete the BIO\n");
goto end; goto end;
@ -182,7 +183,7 @@ int main(void)
* Tell the server during the handshake which hostname we are attempting * Tell the server during the handshake which hostname we are attempting
* to connect to in case the server supports multiple hosts. * 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"); printf("Failed to set the SNI hostname\n");
goto end; goto end;
} }
@ -193,7 +194,7 @@ int main(void)
* Virtually all clients should do this unless you really know what you * Virtually all clients should do this unless you really know what you
* are doing. * are doing.
*/ */
if (!SSL_set1_host(ssl, HOSTNAME)) { if (!SSL_set1_host(ssl, hostname)) {
printf("Failed to set the certificate verification hostname"); printf("Failed to set the certificate verification hostname");
goto end; goto end;
} }
@ -224,8 +225,16 @@ int main(void)
} }
/* Write an HTTP GET request to the peer */ /* Write an HTTP GET request to the peer */
if (!SSL_write_ex(ssl, request, strlen(request), &written)) { if (!SSL_write_ex(ssl, request_start, strlen(request_start), &written)) {
printf("Failed to write HTTP request\n"); 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; goto end;
} }

View File

@ -215,22 +215,13 @@ static int handle_io_failure(SSL *ssl, int res)
return -1; 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 * 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 * 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 * non-standard and will not typically be supported by real world servers. This
* is for demonstration purposes only. * is for demonstration purposes only.
*/ */
int main(void) int main(int argc, char *argv[])
{ {
SSL_CTX *ctx = NULL; SSL_CTX *ctx = NULL;
SSL *ssl = NULL; SSL *ssl = NULL;
@ -238,12 +229,21 @@ int main(void)
int res = EXIT_FAILURE; int res = EXIT_FAILURE;
int ret; int ret;
unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
const char *request = const char *request_start = "GET / HTTP/1.0\r\nConnection: close\r\nHost: ";
"GET / HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; const char *request_end = "\r\n\r\n";
size_t written, readbytes; size_t written, readbytes;
char buf[160]; char buf[160];
BIO_ADDR *peer_addr = NULL; BIO_ADDR *peer_addr = NULL;
int eof = 0; 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 * 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 * Create the underlying transport socket/BIO and associate it with the
* connection. * connection.
*/ */
bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); bio = create_socket_bio(hostname, port, &peer_addr);
if (bio == NULL) { if (bio == NULL) {
printf("Failed to crete the BIO\n"); printf("Failed to crete the BIO\n");
goto end; goto end;
@ -291,7 +291,7 @@ int main(void)
* Tell the server during the handshake which hostname we are attempting * Tell the server during the handshake which hostname we are attempting
* to connect to in case the server supports multiple hosts. * 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"); printf("Failed to set the SNI hostname\n");
goto end; goto end;
} }
@ -302,7 +302,7 @@ int main(void)
* Virtually all clients should do this unless you really know what you * Virtually all clients should do this unless you really know what you
* are doing. * are doing.
*/ */
if (!SSL_set1_host(ssl, HOSTNAME)) { if (!SSL_set1_host(ssl, hostname)) {
printf("Failed to set the certificate verification hostname"); printf("Failed to set the certificate verification hostname");
goto end; goto end;
} }
@ -338,10 +338,22 @@ int main(void)
} }
/* Write an HTTP GET request to the peer */ /* 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) if (handle_io_failure(ssl, 0) == 1)
continue; /* Retry */ continue; /* Retry */
printf("Failed to write HTTP request\n"); 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 end of HTTP request\n");
goto end; /* Cannot retry: error */ goto end; /* Cannot retry: error */
} }

View File

@ -108,13 +108,22 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
return bio; return bio;
} }
/* Server hostname and port details. Must be in quotes */ int write_a_request(SSL *stream, const char *request_start,
#ifndef HOSTNAME const char *hostname)
# define HOSTNAME "www.example.com" {
#endif const char *request_end = "\r\n\r\n";
#ifndef PORT size_t written;
# define PORT "443"
#endif 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 * 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 * and will not be supported by real world servers. This is for demonstration
* purposes only. * purposes only.
*/ */
int main(void) int main(int argc, char *argv[])
{ {
SSL_CTX *ctx = NULL; SSL_CTX *ctx = NULL;
SSL *ssl = NULL; SSL *ssl = NULL;
@ -131,13 +140,22 @@ int main(void)
int res = EXIT_FAILURE; int res = EXIT_FAILURE;
int ret; int ret;
unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' }; unsigned char alpn[] = { 8, 'h', 't', 't', 'p', '/', '1', '.', '0' };
const char *request1 = const char *request1_start =
"GET /request1.html HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; "GET /request1.html HTTP/1.0\r\nConnection: close\r\nHost: ";
const char *request2 = const char *request2_start =
"GET /request2.html HTTP/1.0\r\nConnection: close\r\nHost: "HOSTNAME"\r\n\r\n"; "GET /request2.html HTTP/1.0\r\nConnection: close\r\nHost: ";
size_t written, readbytes; size_t readbytes;
char buf[160]; char buf[160];
BIO_ADDR *peer_addr = NULL; 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 * 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 * Create the underlying transport socket/BIO and associate it with the
* connection. * connection.
*/ */
bio = create_socket_bio(HOSTNAME, PORT, &peer_addr); bio = create_socket_bio(hostname, port, &peer_addr);
if (bio == NULL) { if (bio == NULL) {
printf("Failed to crete the BIO\n"); printf("Failed to crete the BIO\n");
goto end; goto end;
@ -194,7 +212,7 @@ int main(void)
* Tell the server during the handshake which hostname we are attempting * Tell the server during the handshake which hostname we are attempting
* to connect to in case the server supports multiple hosts. * 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"); printf("Failed to set the SNI hostname\n");
goto end; goto end;
} }
@ -205,7 +223,7 @@ int main(void)
* Virtually all clients should do this unless you really know what you * Virtually all clients should do this unless you really know what you
* are doing. * are doing.
*/ */
if (!SSL_set1_host(ssl, HOSTNAME)) { if (!SSL_set1_host(ssl, hostname)) {
printf("Failed to set the certificate verification hostname"); printf("Failed to set the certificate verification hostname");
goto end; goto end;
} }
@ -247,12 +265,12 @@ int main(void)
} }
/* Write an HTTP GET request on each of our streams to the peer */ /* 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"); printf("Failed to write HTTP request on stream 1\n");
goto end; 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"); printf("Failed to write HTTP request on stream 2\n");
goto end; goto end;
} }