diff options
Diffstat (limited to 'crypto/openssl/apps/lib/s_socket.c')
| -rw-r--r-- | crypto/openssl/apps/lib/s_socket.c | 115 |
1 files changed, 59 insertions, 56 deletions
diff --git a/crypto/openssl/apps/lib/s_socket.c b/crypto/openssl/apps/lib/s_socket.c index b2a1e3e51a4b..6f9cf23f5e57 100644 --- a/crypto/openssl/apps/lib/s_socket.c +++ b/crypto/openssl/apps/lib/s_socket.c @@ -1,5 +1,5 @@ /* - * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -22,28 +22,28 @@ * needed to have fileno() declared correctly... So let's define u_int */ #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT) -# define __U_INT +#define __U_INT typedef unsigned int u_int; #endif #ifdef _WIN32 -# include <process.h> +#include <process.h> /* MSVC renamed some POSIX functions to have an underscore prefix. */ -# ifdef _MSC_VER -# define getpid _getpid -# endif +#ifdef _MSC_VER +#define getpid _getpid +#endif #endif #ifndef OPENSSL_NO_SOCK -# include "internal/e_os.h" -# include "apps.h" -# include "s_apps.h" -# include "internal/sockets.h" /* for openssl_fdset() */ +#include "internal/e_os.h" +#include "apps.h" +#include "s_apps.h" +#include "internal/sockets.h" /* for openssl_fdset() */ -# include <openssl/bio.h> -# include <openssl/err.h> +#include <openssl/bio.h> +#include <openssl/err.h> /* Keep track of our peer's address for the cookie callback */ BIO_ADDR *ourpeer = NULL; @@ -73,9 +73,9 @@ BIO_ADDR *ourpeer = NULL; * Returns 1 on success, 0 on failure. */ int init_client(int *sock, const char *host, const char *port, - const char *bindhost, const char *bindport, - int family, int type, int protocol, int tfo, int doconn, - BIO_ADDR **ba_ret) + const char *bindhost, const char *bindport, + int family, int type, int protocol, int tfo, int doconn, + BIO_ADDR **ba_ret) { BIO_ADDRINFO *res = NULL; BIO_ADDRINFO *bindaddr = NULL; @@ -89,7 +89,7 @@ int init_client(int *sock, const char *host, const char *port, return 0; ret = BIO_lookup_ex(host, port, BIO_LOOKUP_CLIENT, family, type, protocol, - &res); + &res); if (ret == 0) { ERR_print_errors(bio_err); return 0; @@ -97,9 +97,9 @@ int init_client(int *sock, const char *host, const char *port, if (bindhost != NULL || bindport != NULL) { ret = BIO_lookup_ex(bindhost, bindport, BIO_LOOKUP_CLIENT, - family, type, protocol, &bindaddr); + family, type, protocol, &bindaddr); if (ret == 0) { - ERR_print_errors (bio_err); + ERR_print_errors(bio_err); goto out; } } @@ -110,10 +110,10 @@ int init_client(int *sock, const char *host, const char *port, * anything in the BIO_ADDRINFO chain that we haven't * asked for. */ OPENSSL_assert((family == AF_UNSPEC - || family == BIO_ADDRINFO_family(ai)) - && (type == 0 || type == BIO_ADDRINFO_socktype(ai)) - && (protocol == 0 - || protocol == BIO_ADDRINFO_protocol(ai))); + || family == BIO_ADDRINFO_family(ai)) + && (type == 0 || type == BIO_ADDRINFO_socktype(ai)) + && (protocol == 0 + || protocol == BIO_ADDRINFO_protocol(ai))); if (bindaddr != NULL) { for (bi = bindaddr; bi != NULL; bi = BIO_ADDRINFO_next(bi)) { @@ -126,7 +126,7 @@ int init_client(int *sock, const char *host, const char *port, } *sock = BIO_socket(BIO_ADDRINFO_family(ai), BIO_ADDRINFO_socktype(ai), - BIO_ADDRINFO_protocol(ai), 0); + BIO_ADDRINFO_protocol(ai), 0); if (*sock == INVALID_SOCKET) { /* Maybe the kernel doesn't support the socket family, even if * BIO_lookup() added it in the returned result... @@ -136,7 +136,7 @@ int init_client(int *sock, const char *host, const char *port, if (bi != NULL) { if (!BIO_bind(*sock, BIO_ADDRINFO_address(bi), - BIO_SOCK_REUSEADDR)) { + BIO_SOCK_REUSEADDR)) { BIO_closesocket(*sock); *sock = INVALID_SOCKET; break; @@ -154,8 +154,9 @@ int init_client(int *sock, const char *host, const char *port, BIO *tmpbio = BIO_new_dgram_sctp(*sock, BIO_NOCLOSE); if (tmpbio == NULL) { - ERR_print_errors(bio_err); - return 0; + BIO_closesocket(*sock); + *sock = INVALID_SOCKET; + continue; } BIO_free(tmpbio); } @@ -192,13 +193,14 @@ int init_client(int *sock, const char *host, const char *port, if (bindaddr != NULL && !found) { BIO_printf(bio_err, "Can't bind %saddress for %s%s%s\n", #ifdef AF_INET6 - BIO_ADDRINFO_family(res) == AF_INET6 ? "IPv6 " : + BIO_ADDRINFO_family(res) == AF_INET6 ? "IPv6 " : #endif - BIO_ADDRINFO_family(res) == AF_INET ? "IPv4 " : - BIO_ADDRINFO_family(res) == AF_UNIX ? "unix " : "", - bindhost != NULL ? bindhost : "", - bindport != NULL ? ":" : "", - bindport != NULL ? bindport : ""); + BIO_ADDRINFO_family(res) == AF_INET ? "IPv4 " + : BIO_ADDRINFO_family(res) == AF_UNIX ? "unix " + : "", + bindhost != NULL ? bindhost : "", + bindport != NULL ? ":" : "", + bindport != NULL ? bindport : ""); ERR_clear_error(); ret = 0; } @@ -217,7 +219,7 @@ int init_client(int *sock, const char *host, const char *port, } out: if (bindaddr != NULL) { - BIO_ADDRINFO_free (bindaddr); + BIO_ADDRINFO_free(bindaddr); } BIO_ADDRINFO_free(res); return ret; @@ -233,7 +235,7 @@ void get_sock_info_address(int asock, char **hostname, char **service) *service = NULL; if ((info.addr = BIO_ADDR_new()) != NULL - && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)) { + && BIO_sock_info(asock, BIO_SOCK_INFO_ADDRESS, &info)) { if (hostname != NULL) *hostname = BIO_ADDR_hostname_string(info.addr, 1); if (service != NULL) @@ -255,10 +257,11 @@ int report_server_accept(BIO *out, int asock, int with_address, int with_pid) success = hostname != NULL && service != NULL; if (success) success = BIO_printf(out, - strchr(hostname, ':') == NULL - ? /* IPv4 */ " %s:%s" - : /* IPv6 */ " [%s]:%s", - hostname, service) > 0; + strchr(hostname, ':') == NULL + ? /* IPv4 */ " %s:%s" + : /* IPv6 */ " [%s]:%s", + hostname, service) + > 0; else (void)BIO_printf(out, "unknown:error\n"); OPENSSL_free(hostname); @@ -293,9 +296,9 @@ int report_server_accept(BIO *out, int asock, int with_address, int with_pid) * 0 on failure, something other on success. */ 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, - int tfo) + int family, int type, int protocol, do_server_cb cb, + unsigned char *context, int naccept, BIO *bio_s_out, + int tfo) { int asock = 0; int sock; @@ -313,7 +316,7 @@ int do_server(int *accept_sock, const char *host, const char *port, return 0; if (!BIO_lookup_ex(host, port, BIO_LOOKUP_SERVER, family, type, protocol, - &res)) { + &res)) { ERR_print_errors(bio_err); return 0; } @@ -321,8 +324,8 @@ int do_server(int *accept_sock, const char *host, const char *port, /* Admittedly, these checks are quite paranoid, we should not get * anything in the BIO_ADDRINFO chain that we haven't asked for */ OPENSSL_assert((family == AF_UNSPEC || family == BIO_ADDRINFO_family(res)) - && (type == 0 || type == BIO_ADDRINFO_socktype(res)) - && (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res))); + && (type == 0 || type == BIO_ADDRINFO_socktype(res)) + && (protocol == 0 || protocol == BIO_ADDRINFO_protocol(res))); sock_family = BIO_ADDRINFO_family(res); sock_type = BIO_ADDRINFO_socktype(res); @@ -335,10 +338,10 @@ int do_server(int *accept_sock, const char *host, const char *port, if (sock_family == AF_INET6) sock_options |= BIO_SOCK_V6_ONLY; if (next != NULL - && BIO_ADDRINFO_socktype(next) == sock_type - && BIO_ADDRINFO_protocol(next) == sock_protocol) { + && BIO_ADDRINFO_socktype(next) == sock_type + && BIO_ADDRINFO_protocol(next) == sock_protocol) { if (sock_family == AF_INET - && BIO_ADDRINFO_family(next) == AF_INET6) { + && BIO_ADDRINFO_family(next) == AF_INET6) { /* In case AF_INET6 is returned but not supported by the * kernel, retry with the first detected address family */ sock_family_fallback = sock_family; @@ -346,7 +349,7 @@ int do_server(int *accept_sock, const char *host, const char *port, sock_family = AF_INET6; sock_address = BIO_ADDRINFO_address(next); } else if (sock_family == AF_INET6 - && BIO_ADDRINFO_family(next) == AF_INET) { + && BIO_ADDRINFO_family(next) == AF_INET) { sock_options &= ~BIO_SOCK_V6_ONLY; } } @@ -377,6 +380,7 @@ int do_server(int *accept_sock, const char *host, const char *port, BIO *tmpbio = BIO_new_dgram_sctp(asock, BIO_NOCLOSE); if (tmpbio == NULL) { + BIO_ADDRINFO_free(res); BIO_closesocket(asock); ERR_print_errors(bio_err); goto end; @@ -422,8 +426,6 @@ int do_server(int *accept_sock, const char *host, const char *port, if (naccept != -1) naccept--; - if (naccept == 0) - BIO_closesocket(asock); BIO_set_tcp_ndelay(sock, 1); i = (*cb)(sock, type, protocol, context); @@ -446,12 +448,12 @@ int do_server(int *accept_sock, const char *host, const char *port, * alerts are passed on...] */ timeout.tv_sec = 0; - timeout.tv_usec = 500000; /* some extreme round-trip */ + timeout.tv_usec = 500000; /* some extreme round-trip */ do { FD_ZERO(&readfds); openssl_fdset(sock, &readfds); } while (select(sock + 1, &readfds, NULL, NULL, &timeout) > 0 - && readsocket(sock, sink, sizeof(sink)) > 0); + && readsocket(sock, sink, sizeof(sink)) > 0); BIO_closesocket(sock); } else { @@ -463,15 +465,16 @@ int do_server(int *accept_sock, const char *host, const char *port, if (i < 0 || naccept == 0) { BIO_closesocket(asock); + asock = INVALID_SOCKET; ret = i; break; } } - end: -# ifdef AF_UNIX +end: +#ifdef AF_UNIX if (family == AF_UNIX) unlink(host); -# endif +#endif BIO_ADDR_free(ourpeer); ourpeer = NULL; return ret; @@ -498,4 +501,4 @@ void do_ssl_shutdown(SSL *ssl) } while (ret < 0); } -#endif /* OPENSSL_NO_SOCK */ +#endif /* OPENSSL_NO_SOCK */ |
