dovecot-2.2: doveadm: Improved deciding whether to use TCP or UN...

dovecot at dovecot.org dovecot at dovecot.org
Tue Feb 26 10:54:23 EET 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/91631438abce
changeset: 15970:91631438abce
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Feb 26 10:54:09 2013 +0200
description:
doveadm: Improved deciding whether to use TCP or UNIX socket for a given name.

diffstat:

 src/doveadm/doveadm-util.c |  63 ++++++++++++++++++++++++++++-----------------
 src/doveadm/doveadm-util.h |   1 +
 2 files changed, 40 insertions(+), 24 deletions(-)

diffs (94 lines):

diff -r c9491a07998b -r 91631438abce src/doveadm/doveadm-util.c
--- a/src/doveadm/doveadm-util.c	Tue Feb 26 10:30:15 2013 +0200
+++ b/src/doveadm/doveadm-util.c	Tue Feb 26 10:54:09 2013 +0200
@@ -112,40 +112,55 @@
 			return FALSE;
 		*host_r = t_strdup_until(str, p);
 	}
+	return TRUE;
+}
 
-	/* there is any '/' character (unlikely to be found from host names),
-	   assume ':' is part of a file path */
-	if (strchr(str, '/') != NULL)
-		return FALSE;
-	return TRUE;
+static int
+doveadm_tcp_connect_port(const char *host, unsigned int port)
+{
+	struct ip_addr *ips;
+	unsigned int ips_count;
+	int ret, fd;
+
+	ret = net_gethostbyname(host, &ips, &ips_count);
+	if (ret != 0) {
+		i_fatal("Lookup of host %s failed: %s",
+			host, net_gethosterror(ret));
+	}
+	fd = net_connect_ip_blocking(&ips[0], port, NULL);
+	if (fd == -1) {
+		i_fatal("connect(%s:%u) failed: %m",
+			net_ip2addr(&ips[0]), port);
+	}
+	return fd;
+}
+
+int doveadm_tcp_connect(const char *target, unsigned int default_port)
+{
+	const char *host;
+	unsigned int port;
+
+	if (!parse_hostport(target, default_port, &host, &port)) {
+		i_fatal("Port not known for %s. Either set proxy_port "
+			"or use %s:port", target, target);
+	}
+	return doveadm_tcp_connect_port(host, port);
 }
 
 int doveadm_connect_with_default_port(const char *path,
 				      unsigned int default_port)
 {
-	struct stat st;
-	const char *host;
-	struct ip_addr *ips;
-	unsigned int port, ips_count;
-	int fd, ret;
+	int fd;
 
-	if (parse_hostport(path, default_port, &host, &port) &&
-	    stat(path, &st) < 0) {
-		/* it's a host:port, connect via TCP */
-		ret = net_gethostbyname(host, &ips, &ips_count);
-		if (ret != 0) {
-			i_fatal("Lookup of host %s failed: %s",
-				host, net_gethosterror(ret));
-		}
-		fd = net_connect_ip_blocking(&ips[0], port, NULL);
-		if (fd == -1) {
-			i_fatal("connect(%s:%u) failed: %m",
-				net_ip2addr(&ips[0]), port);
-		}
-	} else {
+	/* we'll assume UNIX sockets typically have an absolute path,
+	   or at the very least '/' somewhere. */
+	if (strchr(path, '/') == NULL)
+		fd = doveadm_tcp_connect(path, default_port);
+	else {
 		fd = net_connect_unix(path);
 		if (fd == -1)
 			i_fatal("net_connect_unix(%s) failed: %m", path);
+		return -1;
 	}
 	return fd;
 }
diff -r c9491a07998b -r 91631438abce src/doveadm/doveadm-util.h
--- a/src/doveadm/doveadm-util.h	Tue Feb 26 10:30:15 2013 +0200
+++ b/src/doveadm/doveadm-util.h	Tue Feb 26 10:54:09 2013 +0200
@@ -8,6 +8,7 @@
 const char *unixdate2str(time_t timestamp);
 const char *doveadm_plugin_getenv(const char *name);
 int doveadm_connect(const char *path);
+int doveadm_tcp_connect(const char *target, unsigned int default_port);
 int doveadm_connect_with_default_port(const char *path,
 				      unsigned int default_port);
 


More information about the dovecot-cvs mailing list