mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-20 03:44:15 +08:00
Update socket wrapper
This commit is contained in:
@@ -768,7 +768,7 @@ endif()
|
|||||||
#
|
#
|
||||||
set(CPACK_PACKAGE_NAME "GmSSL")
|
set(CPACK_PACKAGE_NAME "GmSSL")
|
||||||
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
||||||
set(CPACK_PACKAGE_VERSION "3.2.0-dev.1023")
|
set(CPACK_PACKAGE_VERSION "3.2.0-dev.1024")
|
||||||
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
||||||
set(CPACK_NSIS_MODIFY_PATH ON)
|
set(CPACK_NSIS_MODIFY_PATH ON)
|
||||||
include(CPack)
|
include(CPack)
|
||||||
|
|||||||
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -32,12 +32,6 @@ typedef SOCKET tls_socket_t;
|
|||||||
typedef int tls_ret_t;
|
typedef int tls_ret_t;
|
||||||
typedef int tls_socklen_t;
|
typedef int tls_socklen_t;
|
||||||
|
|
||||||
|
|
||||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,(int)(len),flags)
|
|
||||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,(int)(len),flags)
|
|
||||||
#define tls_socket_close(sock) closesocket(sock)
|
|
||||||
#define tls_socket_wait() Sleep(1)
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -53,16 +47,33 @@ typedef int tls_socket_t;
|
|||||||
typedef ssize_t tls_ret_t;
|
typedef ssize_t tls_ret_t;
|
||||||
typedef socklen_t tls_socklen_t;
|
typedef socklen_t tls_socklen_t;
|
||||||
|
|
||||||
|
|
||||||
#define tls_socket_send(sock,buf,len,flags) send(sock,buf,len,flags)
|
|
||||||
#define tls_socket_recv(sock,buf,len,flags) recv(sock,buf,len,flags)
|
|
||||||
#define tls_socket_close(sock) close(sock)
|
|
||||||
#define tls_socket_wait() usleep(1000)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
TLS_SOCKET_ERR_NONE = 0,
|
||||||
|
TLS_SOCKET_ERR_WANT_READ,
|
||||||
|
TLS_SOCKET_ERR_WANT_WRITE,
|
||||||
|
TLS_SOCKET_ERR_INTERRUPTED,
|
||||||
|
TLS_SOCKET_ERR_CLOSED,
|
||||||
|
TLS_SOCKET_ERR_RESET,
|
||||||
|
TLS_SOCKET_ERR_TIMEOUT,
|
||||||
|
TLS_SOCKET_ERR_INVAL,
|
||||||
|
TLS_SOCKET_ERR_SYSTEM,
|
||||||
|
} tls_socket_err_t;
|
||||||
|
|
||||||
int tls_socket_lib_init(void);
|
int tls_socket_lib_init(void);
|
||||||
int tls_socket_lib_cleanup(void);
|
int tls_socket_lib_cleanup(void);
|
||||||
|
tls_ret_t tls_socket_send(tls_socket_t sock, const void *buf, size_t len, int flags);
|
||||||
|
tls_ret_t tls_socket_recv(tls_socket_t sock, void *buf, size_t len, int flags);
|
||||||
|
int tls_socket_close(tls_socket_t sock);
|
||||||
|
void tls_socket_wait(void);
|
||||||
|
int tls_socket_get_error(void);
|
||||||
|
tls_socket_err_t tls_socket_get_error_type(int err, int is_read);
|
||||||
|
const char *tls_socket_get_error_string(int err);
|
||||||
|
void tls_socket_print_error(const char *func, int err);
|
||||||
|
int tls_socket_set_nonblocking(tls_socket_t sock, int nonblock);
|
||||||
|
tls_socket_t tls_socket_invalid(void);
|
||||||
|
int tls_socket_is_valid(tls_socket_t sock);
|
||||||
int tls_socket_create(tls_socket_t *sock, int af, int type, int protocl);
|
int tls_socket_create(tls_socket_t *sock, int af, int type, int protocl);
|
||||||
int tls_socket_connect(tls_socket_t sock, const struct sockaddr_in *addr);
|
int tls_socket_connect(tls_socket_t sock, const struct sockaddr_in *addr);
|
||||||
int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr);
|
int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ extern "C" {
|
|||||||
|
|
||||||
// Also update CPACK_PACKAGE_VERSION in CMakeLists.txt
|
// Also update CPACK_PACKAGE_VERSION in CMakeLists.txt
|
||||||
#define GMSSL_VERSION_NUM 30200
|
#define GMSSL_VERSION_NUM 30200
|
||||||
#define GMSSL_VERSION_STR "GmSSL 3.2.0-dev.1023"
|
#define GMSSL_VERSION_STR "GmSSL 3.2.0-dev.1024"
|
||||||
|
|
||||||
int gmssl_version_num(void);
|
int gmssl_version_num(void);
|
||||||
const char *gmssl_version_str(void);
|
const char *gmssl_version_str(void);
|
||||||
|
|||||||
24
src/http.c
24
src/http.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014-2023 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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -50,11 +50,11 @@ int http_parse_uri(const char *uri, char host[128], int *port, char path[256])
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int socket_recv_all(int sock, uint8_t *buf, size_t len)
|
static int socket_recv_all(tls_socket_t sock, uint8_t *buf, size_t len)
|
||||||
{
|
{
|
||||||
size_t n;
|
tls_ret_t n;
|
||||||
while (len) {
|
while (len) {
|
||||||
if ((n = recv(sock, buf, len, 0)) <= 0) {
|
if ((n = tls_socket_recv(sock, buf, len, 0)) <= 0) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -116,12 +116,12 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
char path[256];
|
char path[256];
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
tls_socket_t sock;
|
tls_socket_t sock = tls_socket_invalid();
|
||||||
char get[sizeof(HTTP_GET_TEMPLATE) + sizeof(host) + sizeof(path)];
|
char get[sizeof(HTTP_GET_TEMPLATE) + sizeof(host) + sizeof(path)];
|
||||||
int getlen;
|
int getlen;
|
||||||
char response[1024];
|
char response[1024];
|
||||||
uint8_t *p;
|
uint8_t *p;
|
||||||
size_t len;
|
tls_ret_t len;
|
||||||
size_t left;
|
size_t left;
|
||||||
|
|
||||||
// parse uri and compose request
|
// parse uri and compose request
|
||||||
@@ -143,26 +143,26 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
server.sin_family = AF_INET;
|
server.sin_family = AF_INET;
|
||||||
server.sin_port = htons(port);
|
server.sin_port = htons(port);
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
if (tls_socket_create(&sock, AF_INET, SOCK_STREAM, 0) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) < 0) {
|
if (tls_socket_connect(sock, &server) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (send(sock, get, strlen(get), 0) != getlen) {
|
if (tls_socket_send(sock, get, strlen(get), 0) != getlen) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if ((len = recv(sock, response, sizeof(response) - 1, 0)) <= 0) {
|
if ((len = tls_socket_recv(sock, response, sizeof(response) - 1, 0)) <= 0) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
response[len] = 0;
|
response[len] = 0;
|
||||||
|
|
||||||
// process response header and retrieve left
|
// process response header and retrieve left
|
||||||
if (http_parse_response(response, len, &p, contentlen, &left) != 1) {
|
if (http_parse_response(response, (size_t)len, &p, contentlen, &left) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -180,6 +180,6 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
end:
|
end:
|
||||||
close(sock);
|
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014-2023 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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -52,7 +52,7 @@ int http_parse_uri(const char *uri, char host[128], int *port, char path[256])
|
|||||||
|
|
||||||
static int socket_recv_all(tls_socket_t sock, uint8_t *buf, size_t len)
|
static int socket_recv_all(tls_socket_t sock, uint8_t *buf, size_t len)
|
||||||
{
|
{
|
||||||
size_t n;
|
tls_ret_t n;
|
||||||
while (len) {
|
while (len) {
|
||||||
if ((n = tls_socket_recv(sock, buf, len, 0)) <= 0) {
|
if ((n = tls_socket_recv(sock, buf, len, 0)) <= 0) {
|
||||||
error_print();
|
error_print();
|
||||||
@@ -116,20 +116,14 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
char path[256];
|
char path[256];
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
tls_socket_t sock;
|
tls_socket_t sock = tls_socket_invalid();
|
||||||
char get[sizeof(HTTP_GET_TEMPLATE) + sizeof(host) + sizeof(path)];
|
char get[sizeof(HTTP_GET_TEMPLATE) + sizeof(host) + sizeof(path)];
|
||||||
int getlen;
|
int getlen;
|
||||||
char response[1024];
|
char response[1024];
|
||||||
uint8_t *p;
|
uint8_t *p;
|
||||||
size_t len;
|
tls_ret_t len;
|
||||||
size_t left;
|
size_t left;
|
||||||
|
int socket_lib_inited = 0;
|
||||||
// WIN32
|
|
||||||
WORD wVersion;
|
|
||||||
WSADATA wsaData;
|
|
||||||
wVersion = MAKEWORD(2, 2);
|
|
||||||
int err;
|
|
||||||
int sock_inited = 0;
|
|
||||||
|
|
||||||
// parse uri and compose request
|
// parse uri and compose request
|
||||||
if (http_parse_uri(uri, host, &port, path) != 1) {
|
if (http_parse_uri(uri, host, &port, path) != 1) {
|
||||||
@@ -141,11 +135,11 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// WIN32
|
if (tls_socket_lib_init() != 1) {
|
||||||
if ((err = WSAStartup(wVersion, &wsaData)) != 0) {
|
error_print();
|
||||||
fprintf(stderr, "WSAStartup error %d\n", err);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
socket_lib_inited = 1;
|
||||||
|
|
||||||
// connect and send request
|
// connect and send request
|
||||||
if (!(hp = gethostbyname(host))) {
|
if (!(hp = gethostbyname(host))) {
|
||||||
@@ -156,16 +150,13 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
server.sin_family = AF_INET;
|
server.sin_family = AF_INET;
|
||||||
server.sin_port = htons(port);
|
server.sin_port = htons(port);
|
||||||
|
|
||||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
if (tls_socket_create(&sock, AF_INET, SOCK_STREAM, 0) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
fprintf(stderr, "%s %d: open socket error : %u\n", __FILE__, __LINE__, WSAGetLastError());
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
sock_inited = 1;
|
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR) {
|
if (tls_socket_connect(sock, &server) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
fprintf(stderr, "%s %d: connect error : %u\n", __FILE__, __LINE__, WSAGetLastError());
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if (tls_socket_send(sock, get, strlen(get), 0) != getlen) {
|
if (tls_socket_send(sock, get, strlen(get), 0) != getlen) {
|
||||||
@@ -179,7 +170,7 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
response[len] = 0;
|
response[len] = 0;
|
||||||
|
|
||||||
// process response header and retrieve left
|
// process response header and retrieve left
|
||||||
if (http_parse_response(response, len, &p, contentlen, &left) != 1) {
|
if (http_parse_response(response, (size_t)len, &p, contentlen, &left) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -197,7 +188,7 @@ int http_get(const char *uri, uint8_t *buf, size_t *contentlen, size_t buflen)
|
|||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if (sock_inited) tls_socket_close(sock);
|
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
|
||||||
WSACleanup();
|
if (socket_lib_inited) tls_socket_lib_cleanup();
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
306
src/socket.c
306
src/socket.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2014-2023 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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -9,14 +9,93 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/socket.h>
|
#include <gmssl/socket.h>
|
||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static int tls_socket_should_print_error(int err, int is_read)
|
||||||
|
{
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, is_read);
|
||||||
|
return type != TLS_SOCKET_ERR_WANT_READ
|
||||||
|
&& type != TLS_SOCKET_ERR_WANT_WRITE
|
||||||
|
&& type != TLS_SOCKET_ERR_INTERRUPTED;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
|
int tls_socket_get_error(void)
|
||||||
|
{
|
||||||
|
return WSAGetLastError();
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_socket_err_t tls_socket_get_error_type(int err, int is_read)
|
||||||
|
{
|
||||||
|
switch (err) {
|
||||||
|
case 0:
|
||||||
|
return TLS_SOCKET_ERR_NONE;
|
||||||
|
case WSAEWOULDBLOCK:
|
||||||
|
case WSAEINPROGRESS:
|
||||||
|
case WSAEALREADY:
|
||||||
|
return is_read ? TLS_SOCKET_ERR_WANT_READ : TLS_SOCKET_ERR_WANT_WRITE;
|
||||||
|
case WSAEINTR:
|
||||||
|
return TLS_SOCKET_ERR_INTERRUPTED;
|
||||||
|
case WSAECONNRESET:
|
||||||
|
return TLS_SOCKET_ERR_RESET;
|
||||||
|
case WSAECONNABORTED:
|
||||||
|
case WSAESHUTDOWN:
|
||||||
|
case WSAENOTCONN:
|
||||||
|
return TLS_SOCKET_ERR_CLOSED;
|
||||||
|
case WSAETIMEDOUT:
|
||||||
|
return TLS_SOCKET_ERR_TIMEOUT;
|
||||||
|
case WSAEINVAL:
|
||||||
|
case WSAENOTSOCK:
|
||||||
|
return TLS_SOCKET_ERR_INVAL;
|
||||||
|
default:
|
||||||
|
return TLS_SOCKET_ERR_SYSTEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *tls_socket_get_error_string(int err)
|
||||||
|
{
|
||||||
|
static char buf[256];
|
||||||
|
DWORD len;
|
||||||
|
|
||||||
|
len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL, err, 0, buf, sizeof(buf), NULL);
|
||||||
|
if (len > 0) {
|
||||||
|
while (len > 0 && (buf[len - 1] == '\r' || buf[len - 1] == '\n')) {
|
||||||
|
buf[--len] = 0;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
snprintf(buf, sizeof(buf), "Windows socket error %d", err);
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tls_socket_print_error(const char *func, int err)
|
||||||
|
{
|
||||||
|
error_print_msg("%s error: %d (%s)\n",
|
||||||
|
func ? func : "socket", err, tls_socket_get_error_string(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_socket_t tls_socket_invalid(void)
|
||||||
|
{
|
||||||
|
return INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_is_valid(tls_socket_t sock)
|
||||||
|
{
|
||||||
|
return sock != INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
int tls_socket_lib_init(void)
|
int tls_socket_lib_init(void)
|
||||||
{
|
{
|
||||||
WORD wVersion = MAKEWORD(2, 2);
|
WORD wVersion = MAKEWORD(2, 2);
|
||||||
@@ -24,8 +103,7 @@ int tls_socket_lib_init(void)
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
if ((err = WSAStartup(wVersion, &wsaData)) != 0) {
|
if ((err = WSAStartup(wVersion, &wsaData)) != 0) {
|
||||||
fprintf(stderr, "WSAStartup() return error %d\n", err);
|
tls_socket_print_error("WSAStartup", err);
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -34,8 +112,69 @@ int tls_socket_lib_init(void)
|
|||||||
int tls_socket_lib_cleanup(void)
|
int tls_socket_lib_cleanup(void)
|
||||||
{
|
{
|
||||||
if (WSACleanup() != 0) {
|
if (WSACleanup() != 0) {
|
||||||
fprintf(stderr, "WSACleanup() return error %d\n", WSAGetLastError());
|
tls_socket_print_error("WSACleanup", tls_socket_get_error());
|
||||||
error_print();
|
return -1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_ret_t tls_socket_send(tls_socket_t sock, const void *buf, size_t len, int flags)
|
||||||
|
{
|
||||||
|
tls_ret_t ret;
|
||||||
|
|
||||||
|
if (len > INT_MAX) {
|
||||||
|
WSASetLastError(WSAEMSGSIZE);
|
||||||
|
tls_socket_print_error("send", WSAEMSGSIZE);
|
||||||
|
return SOCKET_ERROR;
|
||||||
|
}
|
||||||
|
ret = send(sock, (const char *)buf, (int)len, flags);
|
||||||
|
if (ret == SOCKET_ERROR) {
|
||||||
|
int err = tls_socket_get_error();
|
||||||
|
if (tls_socket_should_print_error(err, 0)) {
|
||||||
|
tls_socket_print_error("send", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_ret_t tls_socket_recv(tls_socket_t sock, void *buf, size_t len, int flags)
|
||||||
|
{
|
||||||
|
tls_ret_t ret;
|
||||||
|
|
||||||
|
if (len > INT_MAX) {
|
||||||
|
WSASetLastError(WSAEMSGSIZE);
|
||||||
|
tls_socket_print_error("recv", WSAEMSGSIZE);
|
||||||
|
return SOCKET_ERROR;
|
||||||
|
}
|
||||||
|
ret = recv(sock, (char *)buf, (int)len, flags);
|
||||||
|
if (ret == SOCKET_ERROR) {
|
||||||
|
int err = tls_socket_get_error();
|
||||||
|
if (tls_socket_should_print_error(err, 1)) {
|
||||||
|
tls_socket_print_error("recv", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_close(tls_socket_t sock)
|
||||||
|
{
|
||||||
|
int ret = closesocket(sock);
|
||||||
|
if (ret == SOCKET_ERROR) {
|
||||||
|
tls_socket_print_error("closesocket", tls_socket_get_error());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tls_socket_wait(void)
|
||||||
|
{
|
||||||
|
Sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_set_nonblocking(tls_socket_t sock, int nonblock)
|
||||||
|
{
|
||||||
|
u_long mode = nonblock ? 1 : 0;
|
||||||
|
if (ioctlsocket(sock, FIONBIO, &mode) != 0) {
|
||||||
|
tls_socket_print_error("ioctlsocket", tls_socket_get_error());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -49,8 +188,7 @@ int tls_socket_create(tls_socket_t *sock, int af, int type, int protocol)
|
|||||||
}
|
}
|
||||||
// INVALID_SOCKET == -1
|
// INVALID_SOCKET == -1
|
||||||
if ((*sock = socket(af, type, protocol)) == INVALID_SOCKET) {
|
if ((*sock = socket(af, type, protocol)) == INVALID_SOCKET) {
|
||||||
fprintf(stderr, "%s %d: socket error: %d\n", __FILE__, __LINE__, WSAGetLastError());
|
tls_socket_print_error("socket", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -60,8 +198,10 @@ int tls_socket_connect(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
{
|
{
|
||||||
int addr_len = (int)sizeof(struct sockaddr_in);
|
int addr_len = (int)sizeof(struct sockaddr_in);
|
||||||
if (connect(sock, (const struct sockaddr *)addr, addr_len) == SOCKET_ERROR) {
|
if (connect(sock, (const struct sockaddr *)addr, addr_len) == SOCKET_ERROR) {
|
||||||
fprintf(stderr, "%s %d: socket error: %d\n", __FILE__, __LINE__, WSAGetLastError());
|
int err = tls_socket_get_error();
|
||||||
error_print();
|
if (tls_socket_should_print_error(err, 0)) {
|
||||||
|
tls_socket_print_error("connect", err);
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -71,8 +211,7 @@ int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
{
|
{
|
||||||
int addr_len = (int)sizeof(struct sockaddr_in);
|
int addr_len = (int)sizeof(struct sockaddr_in);
|
||||||
if (bind(sock, (const struct sockaddr *)addr, addr_len) == SOCKET_ERROR) {
|
if (bind(sock, (const struct sockaddr *)addr, addr_len) == SOCKET_ERROR) {
|
||||||
fprintf(stderr, "%s %d: socket bind error: %u\n", __FILE__, __LINE__, WSAGetLastError());
|
tls_socket_print_error("bind", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -81,8 +220,7 @@ int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
int tls_socket_listen(tls_socket_t sock, int backlog)
|
int tls_socket_listen(tls_socket_t sock, int backlog)
|
||||||
{
|
{
|
||||||
if (listen(sock, backlog) == SOCKET_ERROR) {
|
if (listen(sock, backlog) == SOCKET_ERROR) {
|
||||||
fprintf(stderr, "%s %d: socket listen error: %u\n", __FILE__, __LINE__, WSAGetLastError());
|
tls_socket_print_error("listen", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -92,8 +230,10 @@ int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t
|
|||||||
{
|
{
|
||||||
int addr_len = (int)sizeof(struct sockaddr_in);
|
int addr_len = (int)sizeof(struct sockaddr_in);
|
||||||
if ((*conn_sock = accept(sock, (struct sockaddr *)addr, &addr_len)) == INVALID_SOCKET) {
|
if ((*conn_sock = accept(sock, (struct sockaddr *)addr, &addr_len)) == INVALID_SOCKET) {
|
||||||
fprintf(stderr, "%s %d: accept error: %u\n", __FILE__, __LINE__, WSAGetLastError());
|
int err = tls_socket_get_error();
|
||||||
error_print();
|
if (tls_socket_should_print_error(err, 1)) {
|
||||||
|
tls_socket_print_error("accept", err);
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -101,6 +241,63 @@ int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
int tls_socket_get_error(void)
|
||||||
|
{
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_socket_err_t tls_socket_get_error_type(int err, int is_read)
|
||||||
|
{
|
||||||
|
switch (err) {
|
||||||
|
case 0:
|
||||||
|
return TLS_SOCKET_ERR_NONE;
|
||||||
|
case EAGAIN:
|
||||||
|
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
|
||||||
|
case EWOULDBLOCK:
|
||||||
|
#endif
|
||||||
|
case EINPROGRESS:
|
||||||
|
case EALREADY:
|
||||||
|
return is_read ? TLS_SOCKET_ERR_WANT_READ : TLS_SOCKET_ERR_WANT_WRITE;
|
||||||
|
case EINTR:
|
||||||
|
return TLS_SOCKET_ERR_INTERRUPTED;
|
||||||
|
case ECONNRESET:
|
||||||
|
return TLS_SOCKET_ERR_RESET;
|
||||||
|
case ECONNABORTED:
|
||||||
|
case EPIPE:
|
||||||
|
case ENOTCONN:
|
||||||
|
return TLS_SOCKET_ERR_CLOSED;
|
||||||
|
case ETIMEDOUT:
|
||||||
|
return TLS_SOCKET_ERR_TIMEOUT;
|
||||||
|
case EINVAL:
|
||||||
|
case EBADF:
|
||||||
|
case ENOTSOCK:
|
||||||
|
return TLS_SOCKET_ERR_INVAL;
|
||||||
|
default:
|
||||||
|
return TLS_SOCKET_ERR_SYSTEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *tls_socket_get_error_string(int err)
|
||||||
|
{
|
||||||
|
return strerror(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tls_socket_print_error(const char *func, int err)
|
||||||
|
{
|
||||||
|
error_print_msg("%s error: %d (%s)\n",
|
||||||
|
func ? func : "socket", err, tls_socket_get_error_string(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_socket_t tls_socket_invalid(void)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_is_valid(tls_socket_t sock)
|
||||||
|
{
|
||||||
|
return sock >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
int tls_socket_lib_init(void)
|
int tls_socket_lib_init(void)
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
@@ -111,6 +308,64 @@ int tls_socket_lib_cleanup(void)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tls_ret_t tls_socket_send(tls_socket_t sock, const void *buf, size_t len, int flags)
|
||||||
|
{
|
||||||
|
tls_ret_t ret = send(sock, buf, len, flags);
|
||||||
|
if (ret < 0) {
|
||||||
|
int err = tls_socket_get_error();
|
||||||
|
if (tls_socket_should_print_error(err, 0)) {
|
||||||
|
tls_socket_print_error("send", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
tls_ret_t tls_socket_recv(tls_socket_t sock, void *buf, size_t len, int flags)
|
||||||
|
{
|
||||||
|
tls_ret_t ret = recv(sock, buf, len, flags);
|
||||||
|
if (ret < 0) {
|
||||||
|
int err = tls_socket_get_error();
|
||||||
|
if (tls_socket_should_print_error(err, 1)) {
|
||||||
|
tls_socket_print_error("recv", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_close(tls_socket_t sock)
|
||||||
|
{
|
||||||
|
int ret = close(sock);
|
||||||
|
if (ret < 0) {
|
||||||
|
tls_socket_print_error("close", tls_socket_get_error());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tls_socket_wait(void)
|
||||||
|
{
|
||||||
|
usleep(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
int tls_socket_set_nonblocking(tls_socket_t sock, int nonblock)
|
||||||
|
{
|
||||||
|
int flags;
|
||||||
|
|
||||||
|
if ((flags = fcntl(sock, F_GETFL)) < 0) {
|
||||||
|
tls_socket_print_error("fcntl(F_GETFL)", tls_socket_get_error());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (nonblock) {
|
||||||
|
flags |= O_NONBLOCK;
|
||||||
|
} else {
|
||||||
|
flags &= ~O_NONBLOCK;
|
||||||
|
}
|
||||||
|
if (fcntl(sock, F_SETFL, flags) < 0) {
|
||||||
|
tls_socket_print_error("fcntl(F_SETFL)", tls_socket_get_error());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int tls_socket_create(tls_socket_t *sock, int af, int type, int protocol)
|
int tls_socket_create(tls_socket_t *sock, int af, int type, int protocol)
|
||||||
{
|
{
|
||||||
if (!sock) {
|
if (!sock) {
|
||||||
@@ -118,8 +373,7 @@ int tls_socket_create(tls_socket_t *sock, int af, int type, int protocol)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if ((*sock = socket(af, type, protocol)) == -1) {
|
if ((*sock = socket(af, type, protocol)) == -1) {
|
||||||
fprintf(stderr, "%s %d: socket error: %s\n", __FILE__, __LINE__, strerror(errno));
|
tls_socket_print_error("socket", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -129,8 +383,10 @@ int tls_socket_connect(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
{
|
{
|
||||||
socklen_t addr_len = sizeof(struct sockaddr_in);
|
socklen_t addr_len = sizeof(struct sockaddr_in);
|
||||||
if (connect(sock, (const struct sockaddr *)addr, addr_len) == -1) {
|
if (connect(sock, (const struct sockaddr *)addr, addr_len) == -1) {
|
||||||
fprintf(stderr, "%s %d: socket error: %s\n", __FILE__, __LINE__, strerror(errno));
|
int err = tls_socket_get_error();
|
||||||
error_print();
|
if (tls_socket_should_print_error(err, 0)) {
|
||||||
|
tls_socket_print_error("connect", err);
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -140,8 +396,7 @@ int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
{
|
{
|
||||||
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in);
|
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in);
|
||||||
if (bind(sock, (const struct sockaddr *)addr, addr_len) == -1) {
|
if (bind(sock, (const struct sockaddr *)addr, addr_len) == -1) {
|
||||||
fprintf(stderr, "%s %d: socket bind error: %s\n", __FILE__, __LINE__, strerror(errno));
|
tls_socket_print_error("bind", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -150,8 +405,7 @@ int tls_socket_bind(tls_socket_t sock, const struct sockaddr_in *addr)
|
|||||||
int tls_socket_listen(tls_socket_t sock, int backlog)
|
int tls_socket_listen(tls_socket_t sock, int backlog)
|
||||||
{
|
{
|
||||||
if (listen(sock, backlog) == -1) {
|
if (listen(sock, backlog) == -1) {
|
||||||
fprintf(stderr, "%s %d: socket listen error: %s\n", __FILE__, __LINE__, strerror(errno));
|
tls_socket_print_error("listen", tls_socket_get_error());
|
||||||
error_print();
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -161,8 +415,10 @@ int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t
|
|||||||
{
|
{
|
||||||
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in);
|
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in);
|
||||||
if ((*conn_sock = accept(sock, (struct sockaddr *)addr, &addr_len)) == -1) {
|
if ((*conn_sock = accept(sock, (struct sockaddr *)addr, &addr_len)) == -1) {
|
||||||
fprintf(stderr, "%s %d: accept: %s\n", __FILE__, __LINE__, strerror(errno));
|
int err = tls_socket_get_error();
|
||||||
error_print();
|
if (tls_socket_should_print_error(err, 1)) {
|
||||||
|
tls_socket_print_error("accept", err);
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
42
src/tls.c
42
src/tls.c
@@ -11,9 +11,7 @@
|
|||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <gmssl/rand.h>
|
#include <gmssl/rand.h>
|
||||||
@@ -1626,18 +1624,18 @@ int tls_record_send(const uint8_t *record, size_t recordlen, tls_socket_t sock)
|
|||||||
recordlen -= n;
|
recordlen -= n;
|
||||||
|
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
||||||
tls_socket_wait();
|
|
||||||
} else {
|
|
||||||
error_puts("TCP connection closed");
|
error_puts("TCP connection closed");
|
||||||
return 0;
|
return TLS_ERROR_TCP_CLOSED;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 0);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_WRITE) {
|
||||||
tls_socket_wait();
|
tls_socket_wait();
|
||||||
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return TLS_ERROR_SYSCALL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1659,15 +1657,18 @@ int tls_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock)
|
|||||||
*recordlen = 0;
|
*recordlen = 0;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
if (len == 5) {
|
if (len == 5) {
|
||||||
return -EAGAIN;
|
return TLS_ERROR_RECV_AGAIN;
|
||||||
}
|
}
|
||||||
tls_socket_wait();
|
tls_socket_wait();
|
||||||
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
perror("recv");
|
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return TLS_ERROR_SYSCALL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1696,12 +1697,15 @@ int tls_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock)
|
|||||||
*recordlen = 0;
|
*recordlen = 0;
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
tls_socket_wait();
|
tls_socket_wait();
|
||||||
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
perror("recv");
|
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return TLS_ERROR_SYSCALL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3566,14 +3570,10 @@ int tls_set_verbose(TLS_CONNECT *conn, int verbose)
|
|||||||
|
|
||||||
int tls_set_socket(TLS_CONNECT *conn, tls_socket_t sock)
|
int tls_set_socket(TLS_CONNECT *conn, tls_socket_t sock)
|
||||||
{
|
{
|
||||||
#ifndef WIN32
|
if (!conn || !tls_socket_is_valid(sock)) {
|
||||||
int flags = 0;
|
|
||||||
if ((flags = fcntl(sock, F_GETFL)) == -1) {
|
|
||||||
error_print();
|
error_print();
|
||||||
perror("fcntl error");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
conn->sock = sock;
|
conn->sock = sock;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/tls12.c
22
src/tls12.c
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -316,14 +315,15 @@ int tls_send_record(TLS_CONNECT *conn)
|
|||||||
while (left) {
|
while (left) {
|
||||||
n = tls_socket_send(conn->sock, conn->record + conn->record_offset, left, 0);
|
n = tls_socket_send(conn->sock, conn->record + conn->record_offset, left, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 0);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_WRITE) {
|
||||||
return TLS_ERROR_SEND_AGAIN;
|
return TLS_ERROR_SEND_AGAIN;
|
||||||
} else if (errno == EINTR) {
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "%s %d: send() error: %s\n", __FILE__, __LINE__, strerror(errno));
|
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return TLS_ERROR_SYSCALL;
|
||||||
}
|
}
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
return TLS_ERROR_TCP_CLOSED;
|
return TLS_ERROR_TCP_CLOSED;
|
||||||
@@ -348,9 +348,11 @@ int tls_recv_record(TLS_CONNECT *conn)
|
|||||||
while (left) {
|
while (left) {
|
||||||
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, left, 0);
|
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, left, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
return TLS_ERROR_RECV_AGAIN;
|
return TLS_ERROR_RECV_AGAIN;
|
||||||
} else if (errno == EINTR) {
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
error_print();
|
error_print();
|
||||||
@@ -389,9 +391,11 @@ int tls_recv_record(TLS_CONNECT *conn)
|
|||||||
while (left) {
|
while (left) {
|
||||||
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, left, 0);
|
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, left, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
return TLS_ERROR_RECV_AGAIN;
|
return TLS_ERROR_RECV_AGAIN;
|
||||||
} else if (errno == EINTR) {
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
error_print();
|
error_print();
|
||||||
|
|||||||
30
src/tls13.c
30
src/tls13.c
@@ -1138,11 +1138,15 @@ int tls13_send(TLS_CONNECT *conn, const uint8_t *data, size_t datalen, size_t *s
|
|||||||
|
|
||||||
n = tls_socket_send(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
n = tls_socket_send(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 0);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_WRITE) {
|
||||||
return TLS_ERROR_SEND_AGAIN;
|
return TLS_ERROR_SEND_AGAIN;
|
||||||
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return TLS_ERROR_SYSCALL;
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
return TLS_ERROR_TCP_CLOSED;
|
return TLS_ERROR_TCP_CLOSED;
|
||||||
}
|
}
|
||||||
@@ -1178,12 +1182,15 @@ int tls13_do_recv(TLS_CONNECT *conn)
|
|||||||
|
|
||||||
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
return TLS_ERROR_RECV_AGAIN;
|
return TLS_ERROR_RECV_AGAIN;
|
||||||
} else {
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
error_print();
|
continue;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
error_print();
|
||||||
|
return TLS_ERROR_SYSCALL;
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
return TLS_ERROR_TCP_CLOSED;
|
return TLS_ERROR_TCP_CLOSED;
|
||||||
}
|
}
|
||||||
@@ -1205,12 +1212,15 @@ int tls13_do_recv(TLS_CONNECT *conn)
|
|||||||
while (conn->recordlen) {
|
while (conn->recordlen) {
|
||||||
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
n = tls_socket_recv(conn->sock, conn->record + conn->record_offset, conn->recordlen, 0);
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
int err = tls_socket_get_error();
|
||||||
|
tls_socket_err_t type = tls_socket_get_error_type(err, 1);
|
||||||
|
if (type == TLS_SOCKET_ERR_WANT_READ) {
|
||||||
return TLS_ERROR_RECV_AGAIN;
|
return TLS_ERROR_RECV_AGAIN;
|
||||||
} else {
|
} else if (type == TLS_SOCKET_ERR_INTERRUPTED) {
|
||||||
error_print();
|
continue;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
error_print();
|
||||||
|
return TLS_ERROR_SYSCALL;
|
||||||
} else if (n == 0) {
|
} else if (n == 0) {
|
||||||
return TLS_ERROR_TCP_CLOSED;
|
return TLS_ERROR_TCP_CLOSED;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/tls.h>
|
#include <gmssl/tls.h>
|
||||||
@@ -55,25 +54,6 @@ static const char *help =
|
|||||||
"\n";
|
"\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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -196,7 +176,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
|
|||||||
return 1;
|
return 1;
|
||||||
case TLS_ERROR_RECV_AGAIN:
|
case TLS_ERROR_RECV_AGAIN:
|
||||||
case TLS_ERROR_SEND_AGAIN:
|
case TLS_ERROR_SEND_AGAIN:
|
||||||
case -EAGAIN:
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "%s: tls_recv error\n", prog);
|
fprintf(stderr, "%s: tls_recv error\n", prog);
|
||||||
@@ -251,7 +230,7 @@ int tlcp_client_main(int argc, char *argv[])
|
|||||||
int verbose = 0;
|
int verbose = 0;
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
tls_socket_t sock = -1;
|
tls_socket_t sock = tls_socket_invalid();
|
||||||
TLS_CTX ctx;
|
TLS_CTX ctx;
|
||||||
TLS_CONNECT conn;
|
TLS_CONNECT conn;
|
||||||
char buf[1024] = {0};
|
char buf[1024] = {0};
|
||||||
@@ -496,7 +475,7 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(sock) != 1) {
|
if (tls_socket_set_nonblocking(sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -624,8 +603,7 @@ bad:
|
|||||||
do_shutdown_select(&conn);
|
do_shutdown_select(&conn);
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto end;
|
goto end;
|
||||||
} else if (rv == -EAGAIN
|
} else if (rv == TLS_ERROR_RECV_AGAIN
|
||||||
|| rv == TLS_ERROR_RECV_AGAIN
|
|
||||||
|| rv == TLS_ERROR_SEND_AGAIN) {
|
|| rv == TLS_ERROR_SEND_AGAIN) {
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
@@ -638,7 +616,7 @@ bad:
|
|||||||
|
|
||||||
end:
|
end:
|
||||||
// FIXME: clean ctx and connection ASAP, as Ctrl-C is not handled
|
// 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_ctx_cleanup(&ctx);
|
||||||
tls_cleanup(&conn);
|
tls_cleanup(&conn);
|
||||||
return ret;
|
return ret;
|
||||||
|
|||||||
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
@@ -35,25 +34,6 @@ static const char *help =
|
|||||||
#include "tlcp_help.h"
|
#include "tlcp_help.h"
|
||||||
"\n";
|
"\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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -323,7 +303,7 @@ restart:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(conn_sock) != 1) {
|
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -349,8 +329,7 @@ restart:
|
|||||||
|
|
||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||||
if (rv == -EAGAIN
|
if (rv == TLS_ERROR_RECV_AGAIN
|
||||||
|| rv == TLS_ERROR_RECV_AGAIN
|
|
||||||
|| rv == TLS_ERROR_SEND_AGAIN) {
|
|| rv == TLS_ERROR_SEND_AGAIN) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||||
* not use this file except in compliance with the License.
|
* not use this file except in compliance with the License.
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/tls.h>
|
#include <gmssl/tls.h>
|
||||||
@@ -47,25 +46,6 @@ static const char *help =
|
|||||||
#include "tls12_help.h"
|
#include "tls12_help.h"
|
||||||
"\n";
|
"\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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -188,7 +168,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
|
|||||||
return 1;
|
return 1;
|
||||||
case TLS_ERROR_RECV_AGAIN:
|
case TLS_ERROR_RECV_AGAIN:
|
||||||
case TLS_ERROR_SEND_AGAIN:
|
case TLS_ERROR_SEND_AGAIN:
|
||||||
case -EAGAIN:
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "%s: tls_recv error\n", prog);
|
fprintf(stderr, "%s: tls_recv error\n", prog);
|
||||||
@@ -243,7 +222,7 @@ int tls12_client_main(int argc, char *argv[])
|
|||||||
TLS_CONNECT conn;
|
TLS_CONNECT conn;
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
tls_socket_t sock = -1;
|
tls_socket_t sock = tls_socket_invalid();
|
||||||
char buf[1024] = {0};
|
char buf[1024] = {0};
|
||||||
size_t len = sizeof(buf);
|
size_t len = sizeof(buf);
|
||||||
char send_buf[1024] = {0};
|
char send_buf[1024] = {0};
|
||||||
@@ -482,7 +461,7 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(sock) != 1) {
|
if (tls_socket_set_nonblocking(sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -561,8 +540,7 @@ bad:
|
|||||||
memset(buf, 0, sizeof(buf));
|
memset(buf, 0, sizeof(buf));
|
||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||||
if (rv == -EAGAIN
|
if (rv == TLS_ERROR_RECV_AGAIN
|
||||||
|| rv == TLS_ERROR_RECV_AGAIN
|
|
||||||
|| rv == TLS_ERROR_SEND_AGAIN) {
|
|| rv == TLS_ERROR_SEND_AGAIN) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -604,7 +582,7 @@ bad:
|
|||||||
|
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if (sock != -1) tls_socket_close(sock);
|
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
|
||||||
tls_ctx_cleanup(&ctx);
|
tls_ctx_cleanup(&ctx);
|
||||||
tls_cleanup(&conn);
|
tls_cleanup(&conn);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
@@ -41,25 +40,6 @@ static const char *help =
|
|||||||
"\n";
|
"\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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -418,7 +398,7 @@ restart:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(conn_sock) != 1) {
|
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -444,8 +424,7 @@ restart:
|
|||||||
|
|
||||||
len = sizeof(buf);
|
len = sizeof(buf);
|
||||||
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
if ((rv = tls_recv(&conn, (uint8_t *)buf, sizeof(buf), &len)) != 1) {
|
||||||
if (rv == -EAGAIN
|
if (rv == TLS_ERROR_RECV_AGAIN
|
||||||
|| rv == TLS_ERROR_RECV_AGAIN
|
|
||||||
|| rv == TLS_ERROR_SEND_AGAIN) {
|
|| rv == TLS_ERROR_SEND_AGAIN) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/hex.h>
|
#include <gmssl/hex.h>
|
||||||
@@ -23,25 +22,6 @@
|
|||||||
#endif
|
#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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -164,7 +144,6 @@ static int do_recv_until_timeout(TLS_CONNECT *conn, char *prog)
|
|||||||
return 1;
|
return 1;
|
||||||
case TLS_ERROR_RECV_AGAIN:
|
case TLS_ERROR_RECV_AGAIN:
|
||||||
case TLS_ERROR_SEND_AGAIN:
|
case TLS_ERROR_SEND_AGAIN:
|
||||||
case -EAGAIN:
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "%s: tls_recv error\n", prog);
|
fprintf(stderr, "%s: tls_recv error\n", prog);
|
||||||
@@ -240,7 +219,7 @@ int tls13_client_main(int argc, char *argv[])
|
|||||||
|
|
||||||
struct hostent *hp;
|
struct hostent *hp;
|
||||||
struct sockaddr_in server;
|
struct sockaddr_in server;
|
||||||
tls_socket_t sock = -1;
|
tls_socket_t sock = tls_socket_invalid();
|
||||||
char buf[1024] = {0};
|
char buf[1024] = {0};
|
||||||
size_t len = sizeof(buf);
|
size_t len = sizeof(buf);
|
||||||
char send_buf[1024] = {0};
|
char send_buf[1024] = {0};
|
||||||
@@ -813,7 +792,7 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(sock) != 1) {
|
if (tls_socket_set_nonblocking(sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
@@ -952,7 +931,7 @@ bad:
|
|||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if (sock != -1) tls_socket_close(sock);
|
if (tls_socket_is_valid(sock)) tls_socket_close(sock);
|
||||||
tls_ctx_cleanup(&ctx);
|
tls_ctx_cleanup(&ctx);
|
||||||
tls_cleanup(&conn);
|
tls_cleanup(&conn);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
@@ -53,25 +52,6 @@ static const char *help =
|
|||||||
"\n";
|
"\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)
|
static int do_handshake_select(TLS_CONNECT *conn)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -551,7 +531,7 @@ bad:
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (set_socket_nonblocking(conn_sock) != 1) {
|
if (tls_socket_set_nonblocking(conn_sock, 1) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user