]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Update the QUIC demos to accept hostname/port on the command line
authorMatt Caswell <matt@openssl.org>
Mon, 30 Oct 2023 12:04:40 +0000 (12:04 +0000)
committerHugo Landau <hlandau@openssl.org>
Thu, 2 Nov 2023 08:14:46 +0000 (08:14 +0000)
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)

demos/guide/quic-client-block.c
demos/guide/quic-client-non-block.c
demos/guide/quic-multi-stream.c

index 65822fe8c4ca6ba906320dd41eea4102e3d1cdfb..782f57155937a47821158f20c07308ca8cd28c89 100644 (file)
@@ -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;
     }
 
index 61d339c79ca57b4244abf7ea82884e36f2a4c5b0..31596d84c570e7e082177db4f2d34c56563ac8d4 100644 (file)
@@ -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 */
     }
 
index 44ee36e0ad26b5914f5ca3f145d3d19433d510d8..469c5ba4b282efa39181b7d7addbcafd9e86d9c1 100644 (file)
@@ -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;
     }