APPS: Make the cmp Mock server output the accept address and port
authorRichard Levitte <levitte@openssl.org>
Fri, 14 May 2021 10:25:11 +0000 (12:25 +0200)
committerRichard Levitte <levitte@openssl.org>
Sun, 16 May 2021 10:07:14 +0000 (12:07 +0200)
Fixes #14694

Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/15281)

apps/include/s_apps.h
apps/lib/http_server.c
apps/lib/s_socket.c

index 3d2bace594e0569ad565d02798a60a9ecc24a813..a5e9762aed48287897c2b12413dd99757fc2816e 100644 (file)
@@ -16,6 +16,7 @@
 #define PROTOCOL        "tcp"
 
 typedef int (*do_server_cb)(int s, int stype, int prot, unsigned char *context);
+int report_server_accept(BIO *out, int asock, int with_address);
 int do_server(int *accept_sock, const char *host, const char *port,
               int family, int type, int protocol, do_server_cb cb,
               unsigned char *context, int naccept, BIO *bio_s_out);
index 691e5c905646d531bc11b0de3cc6183b4979f2ad..ae33632598d819bc05e70167204f6a7b536b0333 100644 (file)
@@ -23,6 +23,7 @@
 #include "internal/sockets.h"
 #include <openssl/err.h>
 #include <openssl/rand.h>
+#include "s_apps.h"
 
 #if defined(__TANDEM)
 # if defined(OPENSSL_TANDEM_FLOSS)
@@ -218,6 +219,7 @@ void spawn_loop(const char *prog)
 BIO *http_server_init_bio(const char *prog, const char *port)
 {
     BIO *acbio = NULL, *bufbio;
+    int asock;
 
     bufbio = BIO_new(BIO_f_buffer());
     if (bufbio == NULL)
@@ -237,6 +239,13 @@ BIO *http_server_init_bio(const char *prog, const char *port)
         goto err;
     }
 
+    /* Report back what address and port are used */
+    BIO_get_fd(acbio, &asock);
+    if (!report_server_accept(bio_out, asock, 1)) {
+        log_message(prog, LOG_ERR, "Error printing ACCEPT string");
+        goto err;
+    }
+
     return acbio;
 
  err:
index 65d56c0991e2769375c5ea9eeb1b0098249a7608..e41429df89de796e7801c0421c7943916adcb6c1 100644 (file)
@@ -191,6 +191,38 @@ out:
     return ret;
 }
 
+int report_server_accept(BIO *out, int asock, int with_address)
+{
+    int success = 0;
+
+    if (with_address) {
+        union BIO_sock_info_u info;
+        char *hostname = NULL;
+        char *service = NULL;
+
+        if ((info.addr = BIO_ADDR_new()) != NULL
+            && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)
+            && (hostname = BIO_ADDR_hostname_string(info.addr, 1)) != NULL
+            && (service = BIO_ADDR_service_string(info.addr, 1)) != NULL
+            && BIO_printf(out,
+                          strchr(hostname, ':') == NULL
+                          ? /* IPv4 */ "ACCEPT %s:%s\n"
+                          : /* IPv6 */ "ACCEPT [%s]:%s\n",
+                          hostname, service) > 0)
+            success = 1;
+
+        OPENSSL_free(hostname);
+        OPENSSL_free(service);
+        BIO_ADDR_free(info.addr);
+    } else {
+        (void)BIO_printf(out, "ACCEPT\n");
+        success = 1;
+    }
+    (void)BIO_flush(out);
+
+    return success;
+}
+
 /*
  * do_server - helper routine to perform a server operation
  * @accept_sock: pointer to storage of resulting socket.
@@ -296,36 +328,10 @@ int do_server(int *accept_sock, const char *host, const char *port,
     BIO_ADDRINFO_free(res);
     res = NULL;
 
-    if (sock_port == 0) {
-        /* dynamically allocated port, report which one */
-        union BIO_sock_info_u info;
-        char *hostname = NULL;
-        char *service = NULL;
-        int success = 0;
-
-        if ((info.addr = BIO_ADDR_new()) != NULL
-            && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)
-            && (hostname = BIO_ADDR_hostname_string(info.addr, 1)) != NULL
-            && (service = BIO_ADDR_service_string(info.addr, 1)) != NULL
-            && BIO_printf(bio_s_out,
-                          strchr(hostname, ':') == NULL
-                          ? /* IPv4 */ "ACCEPT %s:%s\n"
-                          : /* IPv6 */ "ACCEPT [%s]:%s\n",
-                          hostname, service) > 0)
-            success = 1;
-
-        (void)BIO_flush(bio_s_out);
-        OPENSSL_free(hostname);
-        OPENSSL_free(service);
-        BIO_ADDR_free(info.addr);
-        if (!success) {
-            BIO_closesocket(asock);
-            ERR_print_errors(bio_err);
-            goto end;
-        }
-    } else {
-        (void)BIO_printf(bio_s_out, "ACCEPT\n");
-        (void)BIO_flush(bio_s_out);
+    if (!report_server_accept(bio_s_out, asock, sock_port == 0)) {
+        BIO_closesocket(asock);
+        ERR_print_errors(bio_err);
+        goto end;
     }
 
     if (accept_sock != NULL)