Skip to content

Commit 5f9f871

Browse files
committed
Add wolfCLU_Recv wrapper and refactor all raw send()/recv() calls
1 parent 29298d6 commit 5f9f871

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

src/client/client.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ static int StartTLS_Init(SOCKET_T* sockfd)
11921192

11931193
/* S: 220 <host> SMTP service ready */
11941194
XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
1195-
if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
1195+
if (wolfCLU_Recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1) < 0)
11961196
err_sys("failed to read STARTTLS command\n");
11971197

11981198
if (!XSTRNCMP(tmpBuf, starttlsCmd[0], XSTRLEN(starttlsCmd[0]))) {
@@ -1202,13 +1202,14 @@ static int StartTLS_Init(SOCKET_T* sockfd)
12021202
}
12031203

12041204
/* C: EHLO mail.example.com */
1205-
if (send(*sockfd, starttlsCmd[1], (int)XSTRLEN(starttlsCmd[1]), 0) !=
1205+
if (wolfCLU_SendAll(*sockfd, starttlsCmd[1],
1206+
(int)XSTRLEN(starttlsCmd[1])) !=
12061207
(int)XSTRLEN(starttlsCmd[1]))
12071208
err_sys("failed to send STARTTLS EHLO command\n");
12081209

12091210
/* S: 250 <host> offers a warm hug of welcome */
12101211
XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
1211-
if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
1212+
if (wolfCLU_Recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1) < 0)
12121213
err_sys("failed to read STARTTLS command\n");
12131214

12141215
if (!XSTRNCMP(tmpBuf, starttlsCmd[2], XSTRLEN(starttlsCmd[2]))) {
@@ -1218,14 +1219,15 @@ static int StartTLS_Init(SOCKET_T* sockfd)
12181219
}
12191220

12201221
/* C: STARTTLS */
1221-
if (send(*sockfd, starttlsCmd[3], (int)XSTRLEN(starttlsCmd[3]), 0) !=
1222+
if (wolfCLU_SendAll(*sockfd, starttlsCmd[3],
1223+
(int)XSTRLEN(starttlsCmd[3])) !=
12221224
(int)XSTRLEN(starttlsCmd[3])) {
12231225
err_sys("failed to send STARTTLS command\n");
12241226
}
12251227

12261228
/* S: 220 Go ahead */
12271229
XMEMSET(tmpBuf, 0, sizeof(tmpBuf));
1228-
if (recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1, 0) < 0)
1230+
if (wolfCLU_Recv(*sockfd, tmpBuf, sizeof(tmpBuf)-1) < 0)
12291231
err_sys("failed to read STARTTLS command\n");
12301232
tmpBuf[sizeof(tmpBuf)-1] = '\0';
12311233
if (!XSTRNCMP(tmpBuf, starttlsCmd[4], XSTRLEN(starttlsCmd[4]))) {

src/tools/clu_http.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ int wolfCLU_HttpServerRecv(SOCKET_T clientfd, byte* buffer, int bufferSz)
265265
int headerSz = 0;
266266

267267
while (totalLen < bufferSz - 1) {
268-
int n = (int)recv(clientfd, (char*)buffer + totalLen,
269-
(size_t)(bufferSz - 1 - totalLen), 0);
268+
int n = wolfCLU_Recv(clientfd, (char*)buffer + totalLen,
269+
bufferSz - 1 - totalLen);
270270
if (n <= 0)
271271
break;
272272
totalLen += n;
@@ -295,6 +295,22 @@ int wolfCLU_HttpServerRecv(SOCKET_T clientfd, byte* buffer, int bufferSz)
295295
return totalLen;
296296
}
297297

298+
/* Receive bytes, retrying on EINTR */
299+
int wolfCLU_Recv(SOCKET_T sockfd, char* buf, int len)
300+
{
301+
int n;
302+
do {
303+
n = (int)recv(sockfd, buf, (size_t)len, 0);
304+
} while (
305+
#ifndef _WIN32
306+
n < 0 && errno == EINTR
307+
#else
308+
0
309+
#endif
310+
);
311+
return n;
312+
}
313+
298314
/* Send all bytes, looping on partial writes and EINTR */
299315
int wolfCLU_SendAll(SOCKET_T sockfd, const char* buf, int len)
300316
{

src/tools/clu_scgi.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@
6969
static int readExactly(SOCKET_T sockfd, byte* buffer, int n)
7070
{
7171
int totalRead = 0;
72-
72+
7373
while (totalRead < n) {
74-
int ret = (int)recv(sockfd, (char*)buffer + totalRead, n - totalRead, 0);
74+
int ret = wolfCLU_Recv(sockfd, (char*)buffer + totalRead,
75+
n - totalRead);
7576
if (ret <= 0) {
7677
return -1;
7778
}
@@ -85,11 +86,11 @@ static int parseNetstringLength(SOCKET_T sockfd, int* length)
8586
{
8687
char lenBuf[16];
8788
int i = 0;
88-
89+
8990
*length = 0;
90-
91+
9192
while (i < (int)sizeof(lenBuf) - 1) {
92-
int ret = (int)recv(sockfd, &lenBuf[i], 1, 0);
93+
int ret = wolfCLU_Recv(sockfd, &lenBuf[i], 1);
9394
if (ret <= 0) {
9495
return -1;
9596
}
@@ -103,7 +104,7 @@ static int parseNetstringLength(SOCKET_T sockfd, int* length)
103104
}
104105
i++;
105106
}
106-
107+
107108
return -1;
108109
}
109110

wolfclu/clu_header_main.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,15 @@ int wolfCLU_HttpServerSendError(SOCKET_T clientfd, int statusCode,
712712
*/
713713
void wolfCLU_ServerClose(SOCKET_T sockfd);
714714

715+
/**
716+
* @brief Receive bytes from a socket, retrying on EINTR
717+
* @param sockfd socket descriptor
718+
* @param buf buffer to receive into
719+
* @param len maximum number of bytes to receive
720+
* @return number of bytes received, 0 on connection closed, negative on error
721+
*/
722+
int wolfCLU_Recv(SOCKET_T sockfd, char* buf, int len);
723+
715724
/**
716725
* @brief Send all bytes on a socket, looping on partial writes and EINTR
717726
* @param sockfd socket descriptor

0 commit comments

Comments
 (0)