Finesse TCP timeouts for upstream connections.

Timeouts for TCP connections to non-responive servers are very long.
This in not appropriate for DNS connections.

Set timeouts for connection setup, sending data and recieving data.
The timeouts for connection setup and sending data are set at 5 seconds.
For recieving the reply this is doubled, to take into account the
time for usptream to actually get the answer.

Thanks to Petr Menšík for pointing out this problem, and finding a better
and more portable solution than the one in place heretofore.
This commit is contained in:
Simon Kelley
2024-11-25 23:18:07 +00:00
parent 481ff0ed10
commit 32a8f3e009
10 changed files with 113 additions and 85 deletions

View File

@@ -683,7 +683,7 @@ int main (int argc, char **argv)
if (getuid() == 0 && ent_pw && ent_pw->pw_uid != 0 && fchown(fd, ent_pw->pw_uid, ent_pw->pw_gid) == -1)
chown_warn = errno;
if (!read_write(fd, (unsigned char *)daemon->namebuff, strlen(daemon->namebuff), 0))
if (!read_write(fd, (unsigned char *)daemon->namebuff, strlen(daemon->namebuff), RW_WRITE))
err = 1;
else
{
@@ -1396,14 +1396,14 @@ static int read_event(int fd, struct event_desc *evp, char **msg)
{
char *buf;
if (!read_write(fd, (unsigned char *)evp, sizeof(struct event_desc), 1))
if (!read_write(fd, (unsigned char *)evp, sizeof(struct event_desc), RW_READ))
return 0;
*msg = NULL;
if (evp->msg_sz != 0 &&
(buf = malloc(evp->msg_sz + 1)) &&
read_write(fd, (unsigned char *)buf, evp->msg_sz, 1))
read_write(fd, (unsigned char *)buf, evp->msg_sz, RW_READ))
{
buf[evp->msg_sz] = 0;
*msg = buf;
@@ -1977,7 +1977,7 @@ static void check_dns_listeners(time_t now)
netlink socket. */
unsigned char a;
read_write(pipefd[0], &a, 1, 1);
read_write(pipefd[0], &a, 1, RW_READ);
#endif
/* i holds index of free slot */
@@ -2025,7 +2025,7 @@ static void check_dns_listeners(time_t now)
unsigned char a = 0;
close(daemon->netlinkfd);
read_write(pipefd[1], &a, 1, 0);
read_write(pipefd[1], &a, 1, RW_WRITE);
#endif
alarm(CHILD_LIFETIME);
close(pipefd[0]); /* close read end in child. */
@@ -2118,7 +2118,7 @@ int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header
single byte comes back up the pipe, which
is sent by the child after it has closed the
netlink socket. */
read_write(pipefd[0], &a, 1, 1);
read_write(pipefd[0], &a, 1, RW_READ);
#endif
/* i holds index of free slot */
@@ -2140,7 +2140,7 @@ int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header
#ifdef HAVE_LINUX_NETWORK
/* See comment above re: netlink socket. */
close(daemon->netlinkfd);
read_write(pipefd[1], &a, 1, 0);
read_write(pipefd[1], &a, 1, RW_WRITE);
#endif
close(pipefd[0]); /* close read end in child. */
daemon->pipe_to_parent = pipefd[1];
@@ -2164,16 +2164,16 @@ int swap_to_tcp(struct frec *forward, time_t now, int status, struct dns_header
ssize_t m = -2;
/* tell our parent we're done, and what the result was then exit. */
read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&status, sizeof(status), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)plen, sizeof(*plen), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)header, *plen, 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&forward, sizeof(forward), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&forward->uid, sizeof(forward->uid), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)keycount, sizeof(*keycount), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&keycount, sizeof(keycount), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)validatecount, sizeof(*validatecount), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&validatecount, sizeof(validatecount), 0);
read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&status, sizeof(status), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)plen, sizeof(*plen), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)header, *plen, RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&forward, sizeof(forward), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&forward->uid, sizeof(forward->uid), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)keycount, sizeof(*keycount), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&keycount, sizeof(keycount), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)validatecount, sizeof(*validatecount), RW_WRITE);
read_write(daemon->pipe_to_parent, (unsigned char *)&validatecount, sizeof(validatecount), RW_WRITE);
close(daemon->pipe_to_parent);
flush_log();