Update socket wrapper

This commit is contained in:
Zhi Guan
2026-06-13 15:09:45 +08:00
parent 23375d1fa3
commit 19725f3c54
15 changed files with 406 additions and 261 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/tls.h>
@@ -55,25 +54,6 @@ static const char *help =
"\n";
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -196,7 +176,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
return 1;
case TLS_ERROR_RECV_AGAIN:
case TLS_ERROR_SEND_AGAIN:
case -EAGAIN:
break;
default:
fprintf(stderr, "%s: tls_recv error\n", prog);
@@ -251,7 +230,7 @@ int tlcp_client_main(int argc, char *argv[])
int verbose = 0;
struct hostent *hp;
struct sockaddr_in server;
tls_socket_t sock = -1;
tls_socket_t sock = tls_socket_invalid();
TLS_CTX ctx;
TLS_CONNECT conn;
char buf[1024] = {0};
@@ -496,7 +475,7 @@ bad:
goto end;
}
if (set_socket_nonblocking(sock) != 1) {
if (tls_socket_set_nonblocking(sock, 1) != 1) {
error_print();
goto end;
}
@@ -624,8 +603,7 @@ bad:
do_shutdown_select(&conn);
ret = 0;
goto end;
} else if (rv == -EAGAIN
|| rv == TLS_ERROR_RECV_AGAIN
} else if (rv == TLS_ERROR_RECV_AGAIN
|| rv == TLS_ERROR_SEND_AGAIN) {
continue;
} else {
@@ -638,7 +616,7 @@ bad:
end:
// FIXME: clean ctx and connection ASAP, as Ctrl-C is not handled
if (sock != -1) tls_socket_close(sock);
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
tls_ctx_cleanup(&ctx);
tls_cleanup(&conn);
return ret;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
@@ -35,25 +34,6 @@ static const char *help =
#include "tlcp_help.h"
"\n";
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -323,7 +303,7 @@ restart:
return -1;
}
if (set_socket_nonblocking(conn_sock) != 1) {
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
error_print();
return -1;
}
@@ -349,8 +329,7 @@ restart:
len = sizeof(buf);
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
if (rv == -EAGAIN
|| rv == TLS_ERROR_RECV_AGAIN
if (rv == TLS_ERROR_RECV_AGAIN
|| rv == TLS_ERROR_SEND_AGAIN) {
continue;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/tls.h>
@@ -47,25 +46,6 @@ static const char *help =
#include "tls12_help.h"
"\n";
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -188,7 +168,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
return 1;
case TLS_ERROR_RECV_AGAIN:
case TLS_ERROR_SEND_AGAIN:
case -EAGAIN:
break;
default:
fprintf(stderr, "%s: tls_recv error\n", prog);
@@ -243,7 +222,7 @@ int tls12_client_main(int argc, char *argv[])
TLS_CONNECT conn;
struct hostent *hp;
struct sockaddr_in server;
tls_socket_t sock = -1;
tls_socket_t sock = tls_socket_invalid();
char buf[1024] = {0};
size_t len = sizeof(buf);
char send_buf[1024] = {0};
@@ -482,7 +461,7 @@ bad:
goto end;
}
if (set_socket_nonblocking(sock) != 1) {
if (tls_socket_set_nonblocking(sock, 1) != 1) {
error_print();
goto end;
}
@@ -561,8 +540,7 @@ bad:
memset(buf, 0, sizeof(buf));
len = sizeof(buf);
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
if (rv == -EAGAIN
|| rv == TLS_ERROR_RECV_AGAIN
if (rv == TLS_ERROR_RECV_AGAIN
|| rv == TLS_ERROR_SEND_AGAIN) {
break;
}
@@ -604,7 +582,7 @@ bad:
end:
if (sock != -1) tls_socket_close(sock);
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
tls_ctx_cleanup(&ctx);
tls_cleanup(&conn);
return 0;

View File

@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
@@ -41,25 +40,6 @@ static const char *help =
"\n";
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -418,7 +398,7 @@ restart:
goto end;
}
if (set_socket_nonblocking(conn_sock) != 1) {
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
error_print();
goto end;
}
@@ -444,8 +424,7 @@ restart:
len = sizeof(buf);
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
if (rv == -EAGAIN
|| rv == TLS_ERROR_RECV_AGAIN
if (rv == TLS_ERROR_RECV_AGAIN
|| rv == TLS_ERROR_SEND_AGAIN) {
continue;
}

View File

@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
@@ -23,25 +22,6 @@
#endif
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -164,7 +144,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
return 1;
case TLS_ERROR_RECV_AGAIN:
case TLS_ERROR_SEND_AGAIN:
case -EAGAIN:
break;
default:
fprintf(stderr, "%s: tls_recv error\n", prog);
@@ -240,7 +219,7 @@ int tls13_client_main(int argc, char *argv[])
struct hostent *hp;
struct sockaddr_in server;
tls_socket_t sock = -1;
tls_socket_t sock = tls_socket_invalid();
char buf[1024] = {0};
size_t len = sizeof(buf);
char send_buf[1024] = {0};
@@ -813,7 +792,7 @@ bad:
goto end;
}
if (set_socket_nonblocking(sock) != 1) {
if (tls_socket_set_nonblocking(sock, 1) != 1) {
error_print();
goto end;
}
@@ -952,7 +931,7 @@ bad:
}
end:
if (sock != -1) tls_socket_close(sock);
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
tls_ctx_cleanup(&ctx);
tls_cleanup(&conn);
return 0;

View File

@@ -9,7 +9,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
@@ -53,25 +52,6 @@ static const char *help =
"\n";
static int set_socket_nonblocking(tls_socket_t sock)
{
#ifdef WIN32
u_long mode = 1;
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
error_print();
return -1;
}
#else
int flags;
if ((flags = fcntl(sock, F_GETFL)) < 0
|| fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
error_print();
return -1;
}
#endif
return 1;
}
static int do_handshake_select(TLS_CONNECT *conn)
{
int ret;
@@ -551,7 +531,7 @@ bad:
goto end;
}
if (set_socket_nonblocking(conn_sock) != 1) {
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
error_print();
goto end;
}