commit 1a23c43: [Minor] Connect UDP sockets when send was successful

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Apr 11 17:21:06 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-04-11 13:36:53 +0100
URL: https://github.com/rspamd/rspamd/commit/1a23c4377601c5c550d616c709e6e1ee56fd7f05

[Minor] Connect UDP sockets when send was successful

---
 contrib/librdns/curve.c       |  2 +-
 contrib/librdns/dns_private.h |  1 +
 contrib/librdns/resolver.c    | 41 +++++++++++++++++++++++++++++++++--------
 3 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/contrib/librdns/curve.c b/contrib/librdns/curve.c
index a4bc15418..f53d078b2 100644
--- a/contrib/librdns/curve.c
+++ b/contrib/librdns/curve.c
@@ -782,7 +782,7 @@ rdns_curve_recv (struct rdns_io_channel *ioc, void *buf, size_t len, void *plugi
 	struct rdns_resolver *resolver;
 
 	resolver = ctx->resolver;
-	ret = recvfrom (ioc->sock, buf, len, 0, saddr, &slen);
+	ret = recv (ioc->sock, buf, len, 0);
 
 	if (ret <= 0 || ret < 64) {
 		/* Definitely not a DNSCurve packet */
diff --git a/contrib/librdns/dns_private.h b/contrib/librdns/dns_private.h
index 3d25d21b8..f734e2312 100644
--- a/contrib/librdns/dns_private.h
+++ b/contrib/librdns/dns_private.h
@@ -108,6 +108,7 @@ struct rdns_io_channel {
 	socklen_t slen;
 	int sock; /**< persistent socket                                          */
 	bool active;
+	bool connected;
 	void *async_io; /** async opaque ptr */
 	struct rdns_request *requests; /**< requests in flight                                         */
 	uint64_t uses;
diff --git a/contrib/librdns/resolver.c b/contrib/librdns/resolver.c
index 3130a5c96..2cc3695a7 100644
--- a/contrib/librdns/resolver.c
+++ b/contrib/librdns/resolver.c
@@ -68,15 +68,28 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req)
 	}
 
 	if (resolver->curve_plugin == NULL) {
-		r = sendto (fd, req->packet, req->pos, 0,
-				req->io->saddr,
-				req->io->slen);
+		if (!req->io->connected) {
+			r = sendto (fd, req->packet, req->pos, 0,
+					req->io->saddr,
+					req->io->slen);
+		}
+		else {
+			r = send (fd, req->packet, req->pos, 0);
+		}
 	}
 	else {
-		r = resolver->curve_plugin->cb.curve_plugin.send_cb (req,
-				resolver->curve_plugin->data,
-				req->io->saddr,
-				req->io->slen);
+		if (!req->io->connected) {
+			r = resolver->curve_plugin->cb.curve_plugin.send_cb (req,
+					resolver->curve_plugin->data,
+					req->io->saddr,
+					req->io->slen);
+		}
+		else {
+			r = resolver->curve_plugin->cb.curve_plugin.send_cb (req,
+					resolver->curve_plugin->data,
+					NULL,
+					0);
+		}
 	}
 	if (r == -1) {
 		if (errno == EAGAIN || errno == EINTR) {
@@ -98,6 +111,18 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req)
 			return -1;
 		}
 	}
+	else if (!req->io->connected) {
+		/* Connect socket */
+		r = connect (fd, req->io->saddr, req->io->slen);
+
+		if (r == -1) {
+			rdns_err ("cannot connect after sending request: %s for server %s",
+					strerror (errno), serv->name);
+		}
+		else {
+			req->io->connected = true;
+		}
+	}
 
 	if (new_req) {
 		/* Add request to hash table */
@@ -257,7 +282,7 @@ rdns_process_read (int fd, void *arg)
 
 	/* First read packet from socket */
 	if (resolver->curve_plugin == NULL) {
-		r = recvfrom (fd, in, sizeof (in), 0, ioc->saddr, &ioc->slen);
+		r = recv (fd, in, sizeof (in), 0);
 		if (r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) {
 			req = rdns_find_dns_request (in, ioc);
 		}


More information about the Commits mailing list