Skip to content

Commit 9293758

Browse files
danielfojtcorecode
authored andcommitted
Properly handle partial socket writes.
For both SSL and plaintext connections, properly handle partial writes to a socket, and retry operation after recoverable errors.
1 parent 83481f2 commit 9293758

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

net.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,25 +95,29 @@ send_remote_command(int fd, const char* fmt, ...)
9595
strcat(cmd, "\r\n");
9696
len = strlen(cmd);
9797

98-
if (((config.features & SECURETRANSFER) != 0) &&
99-
((config.features & NOSSL) == 0)) {
100-
while ((s = SSL_write(config.ssl, (const char*)cmd, len)) <= 0) {
101-
s = SSL_get_error(config.ssl, s);
102-
if (s != SSL_ERROR_WANT_READ &&
103-
s != SSL_ERROR_WANT_WRITE) {
104-
strlcpy(neterr, ssl_errstr(), sizeof(neterr));
105-
return (-1);
98+
pos = 0;
99+
while (pos < len) {
100+
if (((config.features & SECURETRANSFER) != 0) &&
101+
((config.features & NOSSL) == 0)) {
102+
if ((n = SSL_write(config.ssl, (const char*)(cmd + pos), len - pos)) <= 0) {
103+
s = SSL_get_error(config.ssl, n);
104+
if (s == SSL_ERROR_ZERO_RETURN ||
105+
s == SSL_ERROR_SYSCALL ||
106+
s == SSL_ERROR_SSL) {
107+
strlcpy(neterr, ssl_errstr(), sizeof(neterr));
108+
return (-1);
109+
}
110+
n = 0;
106111
}
107-
}
108-
}
109-
else {
110-
pos = 0;
111-
while (pos < len) {
112+
} else {
112113
n = write(fd, cmd + pos, len - pos);
113-
if (n < 0)
114-
return (-1);
115-
pos += n;
114+
if (n < 0) {
115+
if ((errno != EAGAIN) && (errno != EINTR))
116+
return (-1);
117+
n = 0;
118+
}
116119
}
120+
pos += n;
117121
}
118122

119123
return (len);

0 commit comments

Comments
 (0)