Commit 2216d3b10f for openssl.org

commit 2216d3b10f7a71f9fd166a4f741da9ab0b73107b
Author: Eugene Syromiatnikov <esyr@openssl.org>
Date:   Sat Jun 27 13:16:07 2026 +0200

    demos/sslecho/echecho.c: apply Windows-specific changes

    Similar to the ones present in demos/sslecho/main.c.

    Complements: 50580382caca "Documents initial agreed APIs for Encrypted Client Hello (ECH) and includes a minimal demo for some of those APIs."
    Signed-off-by: Eugene Syromiatnikov <esyr@openssl.org>

    Reviewed-by: Neil Horman <nhorman@openssl.org>
    Reviewed-by: Bob Beck <beck@openssl.org>
    MergeDate: Mon Jul 20 11:19:03 2026
    (Merged from https://github.com/openssl/openssl/pull/31751)

diff --git a/demos/sslecho/echecho.c b/demos/sslecho/echecho.c
index 71b3d5565b..d9c757cba2 100644
--- a/demos/sslecho/echecho.c
+++ b/demos/sslecho/echecho.c
@@ -9,13 +9,23 @@

 #include <stdbool.h>
 #include <stdio.h>
-#include <unistd.h>
 #include <string.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
 #include <openssl/ssl.h>
 #include <openssl/err.h>

+#if !defined(OPENSSL_SYS_WINDOWS)
+#include <unistd.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+#define SOCKET int
+#define INVALID_SOCKET -1
+#define closesocket(s) close(s)
+#else /* defined(OPENSSL_SYS_WINDOWS) */
+#include <winsock.h>
+#include <ws2tcpip.h>
+#endif /* !defined(OPENSSL_SYS_WINDOWS) */
+
 static const int server_port = 4433;

 static const char echconfig[]
@@ -36,14 +46,14 @@ static const char echprivbuf[]
  */
 static volatile bool server_running = true;

-static int create_socket(bool isServer)
+static SOCKET create_socket(bool isServer)
 {
-    int s;
+    SOCKET s;
     int optval = 1;
     struct sockaddr_in addr = { 0 };

     s = socket(AF_INET, SOCK_STREAM, 0);
-    if (s < 0) {
+    if (s == INVALID_SOCKET) {
         perror("Unable to create socket");
         exit(EXIT_FAILURE);
     }
@@ -54,7 +64,7 @@ static int create_socket(bool isServer)
         addr.sin_addr.s_addr = INADDR_ANY;

         /* Reuse the address; good for quick restarts */
-        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))
+        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(optval))
             < 0) {
             perror("setsockopt(SO_REUSEADDR) failed");
             exit(EXIT_FAILURE);
@@ -98,7 +108,7 @@ static int configure_ech(SSL_CTX *ctx, int server,
     unsigned char *buf, size_t len)
 {
     OSSL_ECHSTORE *es = NULL;
-    BIO *es_in = BIO_new_mem_buf(buf, len);
+    BIO *es_in = BIO_new_mem_buf(buf, (int)len);

     if (es_in == NULL || (es = OSSL_ECHSTORE_new(NULL, NULL)) == NULL)
         goto err;
@@ -172,6 +182,7 @@ static void usage(void)
     exit(1);
 }

+#define BUFFERSIZE 1024
 int main(int argc, char **argv)
 {
     bool isServer;
@@ -180,13 +191,13 @@ int main(int argc, char **argv)
     SSL_CTX *ssl_ctx = NULL;
     SSL *ssl = NULL;

-    int server_skt = -1;
-    int client_skt = -1;
+    SOCKET server_skt = INVALID_SOCKET;
+    SOCKET client_skt = INVALID_SOCKET;

-    /* used by getline relying on realloc, can't be statically allocated */
+    /* used by fgets */
+    char buffer[BUFFERSIZE];
     char *txbuf = NULL;
     size_t txcap = 0;
-    int txlen;

     char rxbuf[128];
     size_t rxcap = sizeof(rxbuf);
@@ -195,7 +206,7 @@ int main(int argc, char **argv)
     char *rem_server_ip = NULL;

     struct sockaddr_in addr = { 0 };
-    unsigned int addr_len = sizeof(addr);
+    socklen_t addr_len = (socklen_t)sizeof(addr);

     char *outer_sni = NULL, *inner_sni = NULL;
     int ech_status;
@@ -242,7 +253,7 @@ int main(int argc, char **argv)
             /* Wait for TCP connection from client */
             client_skt = accept(server_skt, (struct sockaddr *)&addr,
                 &addr_len);
-            if (client_skt < 0) {
+            if (client_skt == INVALID_SOCKET) {
                 perror("Unable to accept");
                 exit(EXIT_FAILURE);
             }
@@ -251,7 +262,7 @@ int main(int argc, char **argv)

             /* Create server SSL structure using newly accepted client socket */
             ssl = SSL_new(ssl_ctx);
-            if (SSL_set_fd(ssl, client_skt) <= 0) {
+            if (SSL_set_fd(ssl, (int)client_skt) <= 0) {
                 puts("Unable to set fd for the SSL object");
                 ERR_print_errors_fp(stderr);
                 exit(EXIT_FAILURE);
@@ -279,7 +290,7 @@ int main(int argc, char **argv)
                      * Get message from client; will fail if client closes
                      * connection
                      */
-                    if ((rxlen = SSL_read(ssl, rxbuf, rxcap)) <= 0) {
+                    if ((rxlen = SSL_read(ssl, rxbuf, (int)rxcap)) <= 0) {
                         if (rxlen == 0) {
                             printf("Client closed connection\n");
                         }
@@ -307,7 +318,12 @@ int main(int argc, char **argv)
                 /* Cleanup for next client */
                 SSL_shutdown(ssl);
                 SSL_free(ssl);
-                close(client_skt);
+                closesocket(client_skt);
+                /*
+                 * Set client_skt to INVALID_SOCKET to avoid double close when
+                 * server_running become false before next accept
+                 */
+                client_skt = INVALID_SOCKET;
             }
         }
         printf("Server exiting...\n");
@@ -336,7 +352,7 @@ int main(int argc, char **argv)

         /* Create client SSL structure using dedicated client socket */
         ssl = SSL_new(ssl_ctx);
-        if (SSL_set_fd(ssl, client_skt) <= 0) {
+        if (SSL_set_fd(ssl, (int)client_skt) <= 0) {
             puts("Unable to set fd for the SSL object");
             ERR_print_errors_fp(stderr);
             exit(EXIT_FAILURE);
@@ -366,9 +382,11 @@ int main(int argc, char **argv)
             /* Loop to send input from keyboard */
             while (true) {
                 /* Get a line of input */
-                txlen = getline(&txbuf, &txcap, stdin);
+                memset(buffer, 0, BUFFERSIZE);
+                txbuf = fgets(buffer, BUFFERSIZE, stdin);
+
                 /* Exit loop on error */
-                if (txlen < 0 || txbuf == NULL) {
+                if (txbuf == NULL) {
                     break;
                 }
                 /* Exit loop if just a carriage return */
@@ -376,14 +394,14 @@ int main(int argc, char **argv)
                     break;
                 }
                 /* Send it to the server */
-                if ((result = SSL_write(ssl, txbuf, txlen)) <= 0) {
+                if ((result = SSL_write(ssl, txbuf, (int)strlen(txbuf))) <= 0) {
                     printf("Server closed connection\n");
                     ERR_print_errors_fp(stderr);
                     break;
                 }

                 /* Wait for the echo */
-                rxlen = SSL_read(ssl, rxbuf, rxcap);
+                rxlen = SSL_read(ssl, rxbuf, (int)rxcap);
                 if (rxlen <= 0) {
                     printf("Server closed connection\n");
                     ERR_print_errors_fp(stderr);
@@ -410,10 +428,10 @@ exit:
     }
     SSL_CTX_free(ssl_ctx);

-    if (client_skt != -1)
-        close(client_skt);
-    if (server_skt != -1)
-        close(server_skt);
+    if (client_skt != INVALID_SOCKET)
+        closesocket(client_skt);
+    if (server_skt != INVALID_SOCKET)
+        closesocket(server_skt);

     if (txbuf != NULL && txcap > 0)
         free(txbuf);