From e9ef523d5ee730c7d93a0bcc97ebfe04c98d3a60 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 3 Jan 2022 00:27:13 +0000 Subject: [PATCH] [Project] Rdns: Fix TCP connection mess --- contrib/librdns/resolver.c | 38 +++++++++++++------------------------- contrib/librdns/util.c | 10 ++++------ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/contrib/librdns/resolver.c b/contrib/librdns/resolver.c index 3197230ed..264b46b24 100644 --- a/contrib/librdns/resolver.c +++ b/contrib/librdns/resolver.c @@ -295,29 +295,9 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) static void rdns_process_tcp_connect (int fd, struct rdns_io_channel *ioc) { - struct rdns_resolver *resolver = ioc->resolver; - struct rdns_server *serv = ioc->srv; - int r = connect (ioc->sock, ioc->saddr, ioc->slen); - - if (r == -1) { - if (errno != EAGAIN && errno != EINTR && errno != EINPROGRESS) { - rdns_err ("cannot connect a TCP socket: %s for server %s", - strerror(errno), serv->name); - resolver->async->del_write (resolver->async->data, ioc->async_io); - } - else { - /* We need to wait again for write readiness here */ - ioc->async_io = resolver->async->add_write (resolver->async->data, - ioc->sock, ioc); - } - } - else { - /* Always be ready to read from a TCP socket */ - resolver->async->del_write (resolver->async->data, ioc->async_io); - ioc->flags |= RDNS_CHANNEL_CONNECTED|RDNS_CHANNEL_ACTIVE; - ioc->tcp->async_read = resolver->async->add_read(resolver->async->data, - ioc->sock, ioc); - } + ioc->flags |= RDNS_CHANNEL_CONNECTED|RDNS_CHANNEL_ACTIVE; + ioc->tcp->async_read = ioc->resolver->async->add_read(ioc->resolver->async->data, + ioc->sock, ioc); } static void @@ -373,14 +353,16 @@ void rdns_process_read (int fd, void *arg) { struct rdns_io_channel *ioc = (struct rdns_io_channel *)arg; + struct rdns_resolver *resolver; + resolver = ioc->resolver; if (IS_CHANNEL_TCP(ioc)) { if (IS_CHANNEL_CONNECTED(ioc)) { rdns_process_tcp_read (fd, ioc); } else { - rdns_process_tcp_connect (fd, ioc); + rdns_err ("read readiness on non connected TCP channel!"); } } else { @@ -628,7 +610,12 @@ rdns_process_udp_retransmit (int fd, struct rdns_request *req) static void rdns_process_tcp_write (int fd, struct rdns_io_channel *ioc) { - + if (ioc->tcp->cur_output_chains == 0) { + /* Unregister write event */ + ioc->resolver->async->del_write (ioc->resolver->async->data, + ioc->tcp->async_write); + ioc->tcp->async_write = NULL; + } } void @@ -656,6 +643,7 @@ rdns_process_write (int fd, void *arg) } else { rdns_process_tcp_connect(fd, ioc); + rdns_process_tcp_write(fd, ioc); } } else { diff --git a/contrib/librdns/util.c b/contrib/librdns/util.c index 61c244199..fd71179e9 100644 --- a/contrib/librdns/util.c +++ b/contrib/librdns/util.c @@ -548,6 +548,9 @@ rdns_ioc_new (struct rdns_server *serv, } nioc->struct_magic = RDNS_IO_CHANNEL_TAG; + nioc->srv = serv; + nioc->resolver = resolver; + nioc->sock = rdns_make_client_socket (serv->name, serv->port, is_tcp ? SOCK_STREAM : SOCK_DGRAM, &nioc->saddr, &nioc->slen); if (nioc->sock == -1) { @@ -570,12 +573,7 @@ rdns_ioc_new (struct rdns_server *serv, nioc->flags |= RDNS_CHANNEL_TCP; } - - nioc->srv = serv; - nioc->resolver = resolver; - - /* If it is not NULL then we are in a delayed connection state */ - if (!is_tcp) { + else { nioc->flags |= RDNS_CHANNEL_ACTIVE; nioc->async_io = resolver->async->add_read(resolver->async->data, nioc->sock, nioc); -- 2.39.5