Skip to content

Commit

Permalink
Use select call for socket waiting (Fixes guanzhi#1788)
Browse files Browse the repository at this point in the history
It eliminates the (perhaps non-blocking version of) speedup loop by using a
generic `select` call which should be available both to POSIX and Winsock2.
  • Loading branch information
goodspeed34 committed Feb 15, 2025
1 parent 34fa519 commit e7e8ed9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions include/gmssl/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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

Expand All @@ -45,6 +44,7 @@ typedef int tls_socklen_t;
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <unistd.h>

Expand All @@ -56,7 +56,6 @@ 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

Expand All @@ -67,7 +66,8 @@ 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_listen(tls_socket_t sock, int backlog);
int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t *conn_sock);

int tls_socket_wait_recv(tls_socket_t sock);
int tls_socket_wait_send(tls_socket_t sock);

#ifdef __cplusplus
}
Expand Down
18 changes: 18 additions & 0 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,21 @@ int tls_socket_accept(tls_socket_t sock, struct sockaddr_in *addr, tls_socket_t
return 1;
}
#endif

int tls_socket_wait_recv(tls_socket_t sock)
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sock, &readfds);

return select(sock+1, &readfds, NULL, NULL, NULL);
}

int tls_socket_wait_send(tls_socket_t sock)
{
fd_set writefds;
FD_ZERO(&writefds);
FD_SET(sock, &writefds);

return select(sock+1, NULL, &writefds, NULL, NULL);
}
22 changes: 12 additions & 10 deletions src/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1476,16 +1476,14 @@ int tls_record_send(const uint8_t *record, size_t recordlen, tls_socket_t sock)
recordlen -= n;

} else if (n == 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
tls_socket_wait();
} else {
if ((errno != EAGAIN && errno != EWOULDBLOCK)
|| tls_socket_wait_send(sock)) {
error_puts("TCP connection closed");
return 0;
}
} else {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
tls_socket_wait();
} else {
if ((errno != EAGAIN && errno != EWOULDBLOCK)
|| tls_socket_wait_send(sock)) {
error_print();
return -1;
}
Expand Down Expand Up @@ -1514,12 +1512,17 @@ int tls_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock)
if (len == 5) {
return -EAGAIN;
}
tls_socket_wait();
} else {
perror("recv");
error_print();
return -1;
}

if (tls_socket_wait_recv(sock)) {
perror("recv");
error_print();
return -1;
}
}
}
if (!tls_record_type_name(tls_record_type(record))) {
Expand Down Expand Up @@ -1548,9 +1551,8 @@ int tls_record_recv(uint8_t *record, size_t *recordlen, tls_socket_t sock)
*recordlen = 0;
return 0;
} else {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
tls_socket_wait();
} else {
if ((errno != EAGAIN && errno != EWOULDBLOCK)
|| tls_socket_wait_recv(sock)) {
perror("recv");
error_print();
return -1;
Expand Down

0 comments on commit e7e8ed9

Please sign in to comment.