servers: minor socket error handling fixes

- sws: fix socket error code in `select()` failure message.
  Spotted by Copilot
  Bug: https://github.com/curl/curl/pull/21998#discussion_r3409469444
- sws: do not call `SOCKERRNO` twice on error.
- dnsd: do not call `SOCKERRNO` twice on error.
- dnsd: replace `goto` with `while()` to sync with rest of code.
- dnsd: `sendto()` fail message fixes:
  - replace `int` cast with `%zu` mask.
  - drop redundant newline.
  - show socket error string like rest of code.
  - report not-fully-sent error separately from socket errors.

Closes #22007
This commit is contained in:
Viktor Szakats 2026-06-14 14:36:24 +02:00
parent 945938de10
commit 9002d3350c
No known key found for this signature in database
2 changed files with 18 additions and 11 deletions

View file

@ -346,15 +346,20 @@ static struct resp *resp_queue;
static CURLcode send_resp(curl_socket_t sock, struct resp *resp)
{
ssize_t rc;
int sockerr = 0;
sending:
rc = sendto(sock, (const void *)resp->body.data, (SENDTO3)resp->body.dlen, 0,
&resp->addr, resp->addrlen);
if((rc < 0) && (SOCKERRNO == SOCKEINTR))
goto sending;
if(rc != (ssize_t)resp->body.dlen) {
logmsg("failed sending %d bytes, errno=%d\n",
(int)resp->body.dlen, SOCKERRNO);
do {
rc = sendto(sock, (const void *)resp->body.data, (SENDTO3)resp->body.dlen,
0, &resp->addr, resp->addrlen);
} while((rc < 0) && ((sockerr = SOCKERRNO) == SOCKEINTR));
if(rc < 0) {
char errbuf[STRERROR_LEN];
logmsg("failed sending %zu bytes, error: (%d) %s", resp->body.dlen,
sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
return CURLE_SEND_ERROR;
}
else if(rc != (ssize_t)resp->body.dlen) {
logmsg("failed sending %zu bytes, sent: %zd", resp->body.dlen, rc);
return CURLE_SEND_ERROR;
}
logmsg("[%d] sent response", resp->qid);

View file

@ -1343,7 +1343,8 @@ static curl_socket_t connect_to(const char *ipaddr, unsigned short port)
FD_SET(serverfd, &output);
while(1) {
rc = select((int)serverfd + 1, NULL, &output, NULL, &timeout);
if(rc < 0 && SOCKERRNO != SOCKEINTR)
sockerr = SOCKERRNO;
if(rc < 0 && sockerr != SOCKEINTR)
goto error;
else if(rc > 0) {
curl_socklen_t errSize = sizeof(sockerr);
@ -2328,15 +2329,16 @@ static int test_sws(int argc, const char *argv[])
if(got_exit_signal)
goto sws_cleanup;
sockerr = 0;
do {
rc = select((int)maxfd + 1, &input, &output, NULL, &timeout);
} while(rc < 0 && SOCKERRNO == SOCKEINTR && !got_exit_signal);
} while(rc < 0 && ((sockerr = SOCKERRNO) == SOCKEINTR &&
!got_exit_signal));
if(got_exit_signal)
goto sws_cleanup;
if(rc < 0) {
sockerr = SOCKERRNO;
logmsg("select() failed with error (%d) %s",
sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
goto sws_cleanup;