]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
augment quic demos to support ipv4/6 connections
authorNeil Horman <nhorman@openssl.org>
Tue, 31 Oct 2023 15:54:03 +0000 (11:54 -0400)
committerRichard Levitte <levitte@openssl.org>
Tue, 21 Nov 2023 12:01:54 +0000 (13:01 +0100)
Because the quicserver utility supports expressly listening in ipv4/6
mode, its possible/likely that the server will listen on an ipv4
address, while the clients will connect via ipv6, leading to connection
failures.

Augment quic demo clients to afford them the same -6 option that the
server has so that connection family can be co-ordinated

Reviewed-by: Nicola Tuveri <nic.tuv@gmail.com>
Reviewed-by: Hugo Landau <hlandau@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22577)

demos/guide/quic-client-block.c
demos/guide/quic-client-non-block.c
demos/guide/quic-multi-stream.c
demos/guide/tls-client-block.c
demos/guide/tls-client-non-block.c
doc/man7/ossl-guide-quic-client-block.pod
doc/man7/ossl-guide-tls-client-block.pod

index 782f57155937a47821158f20c07308ca8cd28c89..baf5292c477992bf81ff719aa9d8a8f95e2dac02 100644 (file)
@@ -27,7 +27,7 @@
 
 /* Helper function to create a BIO connected to the server */
 static BIO *create_socket_bio(const char *hostname, const char *port,
-                              BIO_ADDR **peer_addr)
+                              int family, BIO_ADDR **peer_addr)
 {
     int sock = -1;
     BIO_ADDRINFO *res;
@@ -37,7 +37,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_DGRAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_DGRAM, 0,
                        &res))
         return NULL;
 
@@ -128,14 +128,24 @@ int main(int argc, char *argv[])
     char buf[160];
     BIO_ADDR *peer_addr = NULL;
     char *hostname, *port;
+    int argnext = 1;
+    int ipv6 = 0;
 
-    if (argc != 3) {
-        printf("Usage: quic-client-block hostname port\n");
+    if (argc < 3) {
+        printf("Usage: quic-client-block [-6] hostname port\n");
         goto end;
     }
 
-    hostname = argv[1];
-    port = argv[2];
+    if (!strcmp(argv[argnext], "-6")) {
+        if (argc < 4) {
+            printf("Usage: quic-client-block [-6] hostname port\n");
+            goto end;
+        }
+        ipv6 = 1;
+        argnext++;
+    }
+    hostname = argv[argnext++];
+    port = argv[argnext];
 
     /*
      * Create an SSL_CTX which we can use to create SSL objects from. We
@@ -172,7 +182,7 @@ int main(int argc, char *argv[])
      * 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, ipv6 ? AF_INET6 : AF_INET, &peer_addr);
     if (bio == NULL) {
         printf("Failed to crete the BIO\n");
         goto end;
index 31596d84c570e7e082177db4f2d34c56563ac8d4..a6c1802fcd68abe3b58d5f345f73264a6aec6bb4 100644 (file)
@@ -28,7 +28,7 @@
 
 /* Helper function to create a BIO connected to the server */
 static BIO *create_socket_bio(const char *hostname, const char *port,
-                              BIO_ADDR **peer_addr)
+                              int family, BIO_ADDR **peer_addr)
 {
     int sock = -1;
     BIO_ADDRINFO *res;
@@ -38,7 +38,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_DGRAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_DGRAM, 0,
                        &res))
         return NULL;
 
@@ -236,14 +236,24 @@ int main(int argc, char *argv[])
     BIO_ADDR *peer_addr = NULL;
     int eof = 0;
     char *hostname, *port;
+    int ipv6 = 0;
+    int argnext = 1;
 
-    if (argc != 3) {
-        printf("Usage: quic-client-non-block hostname port\n");
+    if (argc < 3) {
+        printf("Usage: quic-client-non-block [-6] hostname port\n");
         goto end;
     }
 
-    hostname = argv[1];
-    port = argv[2];
+    if (!strcmp(argv[argnext], "-6")) {
+        if (argc < 4) {
+            printf("Usage: quic-client-non-block [-6] hostname port\n");
+            goto end;
+        }
+        ipv6 = 1;
+        argnext++;
+    }
+    hostname = argv[argnext++];
+    port = argv[argnext];
 
     /*
      * Create an SSL_CTX which we can use to create SSL objects from. We
@@ -280,7 +290,8 @@ int main(int argc, char *argv[])
      * 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, ipv6 ? AF_INET6 : AF_INET,
+                            &peer_addr);
     if (bio == NULL) {
         printf("Failed to crete the BIO\n");
         goto end;
index 469c5ba4b282efa39181b7d7addbcafd9e86d9c1..d31ea245c8015dc175f16d7be753a5524bfd058a 100644 (file)
@@ -27,7 +27,7 @@
 
 /* Helper function to create a BIO connected to the server */
 static BIO *create_socket_bio(const char *hostname, const char *port,
-                              BIO_ADDR **peer_addr)
+                              int family, BIO_ADDR **peer_addr)
 {
     int sock = -1;
     BIO_ADDRINFO *res;
@@ -37,7 +37,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port,
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_DGRAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_DGRAM, 0,
                        &res))
         return NULL;
 
@@ -148,14 +148,24 @@ int main(int argc, char *argv[])
     char buf[160];
     BIO_ADDR *peer_addr = NULL;
     char *hostname, *port;
+    int argnext = 1;
+    int ipv6 = 0;
 
-    if (argc != 3) {
-        printf("Usage: quic-client-non-block hostname port\n");
+    if (argc < 3) {
+        printf("Usage: quic-client-non-block [-6] hostname port\n");
         goto end;
     }
 
-    hostname = argv[1];
-    port = argv[2];
+    if (!strcmp(argv[argnext], "-6")) {
+        if (argc < 4) {
+            printf("Usage: quic-client-non-block [-6] hostname port\n");
+            goto end;
+        }
+        ipv6 = 1;
+        argnext++;
+    }
+    hostname = argv[argnext++];
+    port = argv[argnext];
 
     /*
      * Create an SSL_CTX which we can use to create SSL objects from. We
@@ -201,7 +211,7 @@ int main(int argc, char *argv[])
      * 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, ipv6 ? AF_INET6 : AF_INET, &peer_addr);
     if (bio == NULL) {
         printf("Failed to crete the BIO\n");
         goto end;
index ea7d68467ac6ccaebd59b9221094df4077372f9d..c6ba5850f7ff983310fe139b5b6403c4497bdeaa 100644 (file)
@@ -26,7 +26,7 @@
 #include <openssl/err.h>
 
 /* Helper function to create a BIO connected to the server */
-static BIO *create_socket_bio(const char *hostname, const char *port)
+static BIO *create_socket_bio(const char *hostname, const char *port, int family)
 {
     int sock = -1;
     BIO_ADDRINFO *res;
@@ -36,7 +36,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port)
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_STREAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_STREAM, 0,
                        &res))
         return NULL;
 
@@ -109,14 +109,24 @@ int main(int argc, char *argv[])
     size_t written, readbytes;
     char buf[160];
     char *hostname, *port;
+    int argnext = 1;
+    int ipv6 = 0;
 
-    if (argc != 3) {
-        printf("Usage: tls-client-block hostname port\n");
+    if (argc < 3) {
+        printf("Usage: tls-client-block [-6]  hostname port\n");
         goto end;
     }
 
-    hostname = argv[1];
-    port = argv[2];
+    if (!strcmp(argv[argnext], "-6")) {
+        if (argc < 4) {
+            printf("Usage: tls-client-block [-6]  hostname port\n");
+            goto end;
+        }
+        ipv6 = 1;
+        argnext++;
+    }
+    hostname = argv[argnext++];
+    port = argv[argnext];
 
     /*
      * Create an SSL_CTX which we can use to create SSL objects from. We
@@ -162,7 +172,7 @@ int main(int argc, char *argv[])
      * Create the underlying transport socket/BIO and associate it with the
      * connection.
      */
-    bio = create_socket_bio(hostname, port);
+    bio = create_socket_bio(hostname, port, ipv6 ? AF_INET6 : AF_INET);
     if (bio == NULL) {
         printf("Failed to crete the BIO\n");
         goto end;
index 8748e4fffc772e904b0d4b54e62af8f5b7a00c13..0b19d67762674e192fee28a6621b9b37233620cf 100644 (file)
@@ -27,7 +27,7 @@
 #include <openssl/err.h>
 
 /* Helper function to create a BIO connected to the server */
-static BIO *create_socket_bio(const char *hostname, const char *port)
+static BIO *create_socket_bio(const char *hostname, const char *port, int family)
 {
     int sock = -1;
     BIO_ADDRINFO *res;
@@ -37,7 +37,7 @@ static BIO *create_socket_bio(const char *hostname, const char *port)
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_STREAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_STREAM, 0,
                        &res))
         return NULL;
 
@@ -187,14 +187,25 @@ int main(int argc, char *argv[])
     char buf[160];
     int eof = 0;
     char *hostname, *port;
+    int argnext = 1;
+    int ipv6 = 0;
 
-    if (argc != 3) {
-        printf("Usage: tls-client-non-block hostname port\n");
+    if (argc < 3) {
+        printf("Usage: tls-client-non-block [-6] hostname port\n");
         goto end;
     }
 
-    hostname = argv[1];
-    port = argv[2];
+    if (!strcmp(argv[argnext], "-6")) {
+        if (argc < 4) {
+            printf("Usage: tls-client-non-block [-6]  hostname port\n");
+            goto end;
+        }
+        ipv6 = 1;
+        argnext++;
+    }
+
+    hostname = argv[argnext++];
+    port = argv[argnext];
 
     /*
      * Create an SSL_CTX which we can use to create SSL objects from. We
@@ -240,7 +251,7 @@ int main(int argc, char *argv[])
      * Create the underlying transport socket/BIO and associate it with the
      * connection.
      */
-    bio = create_socket_bio(hostname, port);
+    bio = create_socket_bio(hostname, port, ipv6 ? AF_INET6 : AF_INET);
     if (bio == NULL) {
         printf("Failed to crete the BIO\n");
         goto end;
index fc8912086dae6419dd10f53aa14bc0d2218bf197..ab018e4a220b8500d72dc67adb78a3c912060570 100644 (file)
@@ -94,7 +94,7 @@ for TCP).
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_DGRAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_DGRAM, 0,
                        &res))
         return NULL;
 
index cb67bf8fa9bd413608ec6a52822b4219f1c47549..ba59bd4ab3c8131b11e5c86a0071ad76dbd292a2 100644 (file)
@@ -174,7 +174,7 @@ integrate into the OpenSSL error system to log error data, e.g.
     /*
      * Lookup IP address info for the server.
      */
-    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, 0, SOCK_STREAM, 0,
+    if (!BIO_lookup_ex(hostname, port, BIO_LOOKUP_CLIENT, family, SOCK_STREAM, 0,
                        &res))
         return NULL;
 
@@ -212,7 +212,9 @@ See L<BIO_lookup_ex(3)>, L<BIO_socket(3)>, L<BIO_connect(3)>,
 L<BIO_closesocket(3)>, L<BIO_ADDRINFO_next(3)>, L<BIO_ADDRINFO_address(3)> and
 L<BIO_ADDRINFO_free(3)> for further information on the functions used here. In
 the above example code the B<hostname> and B<port> variables are strings, e.g.
-"www.example.com" and "443".
+"www.example.com" and "443".  Note also the use of the family variable, which
+can take the values of AF_INET or AF_INET6 based on the command line -6 option,
+to allow specific connections to an ipv4 or ipv6 enabled host.
 
 Sockets created using the methods described above will automatically be blocking
 sockets - which is exactly what we want for this example.