Skip to content

Commit

Permalink
Update the QUIC demos to accept hostname/port on the command line
Browse files Browse the repository at this point in the history
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 #22552)
  • Loading branch information
mattcaswell authored and hlandau committed Nov 2, 2023
1 parent a2b8247 commit 420037c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 51 deletions.
41 changes: 25 additions & 16 deletions demos/guide/quic-client-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,34 @@ 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;
BIO *bio = NULL;
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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down
46 changes: 29 additions & 17 deletions demos/guide/quic-client-non-block.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,35 +215,35 @@ 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;
BIO *bio = NULL;
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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 */
}

Expand Down
54 changes: 36 additions & 18 deletions demos/guide/quic-multi-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,30 @@ 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
* response on the screen. Note that HTTP/1.0 over QUIC is not a real protocol
* 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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 420037c

Please sign in to comment.