dovecot-2.0: doveadm director/penalty/who: Support also communic...

dovecot at dovecot.org dovecot at dovecot.org
Mon Jul 12 02:33:16 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/16db2af2b831
changeset: 11795:16db2af2b831
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Jul 12 00:33:12 2010 +0100
description:
doveadm director/penalty/who: Support also communicating via TCP sockets.

diffstat:

 src/doveadm/doveadm-director.c |   4 +---
 src/doveadm/doveadm-penalty.c  |   4 +---
 src/doveadm/doveadm-who.c      |   4 +---
 src/doveadm/doveadm.c          |  48 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/doveadm/doveadm.h          |   1 +
 5 files changed, 52 insertions(+), 9 deletions(-)

diffs (125 lines):

diff -r 5ea9394b5ced -r 16db2af2b831 src/doveadm/doveadm-director.c
--- a/src/doveadm/doveadm-director.c	Mon Jul 12 00:32:30 2010 +0100
+++ b/src/doveadm/doveadm-director.c	Mon Jul 12 00:33:12 2010 +0100
@@ -32,9 +32,7 @@
 	const char *line;
 	int fd;
 
-	fd = net_connect_unix(ctx->socket_path);
-	if (fd == -1)
-		i_fatal("net_connect_unix(%s) failed: %m", ctx->socket_path);
+	fd = doveadm_connect(ctx->socket_path);
 	net_set_nonblock(fd, FALSE);
 
 	ctx->input = i_stream_create_fd(fd, (size_t)-1, TRUE);
diff -r 5ea9394b5ced -r 16db2af2b831 src/doveadm/doveadm-penalty.c
--- a/src/doveadm/doveadm-penalty.c	Mon Jul 12 00:32:30 2010 +0100
+++ b/src/doveadm/doveadm-penalty.c	Mon Jul 12 00:33:12 2010 +0100
@@ -68,9 +68,7 @@
 	const char *line;
 	int fd;
 
-	fd = net_connect_unix(ctx->anvil_path);
-	if (fd == -1)
-		i_fatal("net_connect_unix(%s) failed: %m", ctx->anvil_path);
+	fd = doveadm_connect(ctx->anvil_path);
 	net_set_nonblock(fd, FALSE);
 
 	input = i_stream_create_fd(fd, (size_t)-1, TRUE);
diff -r 5ea9394b5ced -r 16db2af2b831 src/doveadm/doveadm-who.c
--- a/src/doveadm/doveadm-who.c	Mon Jul 12 00:32:30 2010 +0100
+++ b/src/doveadm/doveadm-who.c	Mon Jul 12 00:33:12 2010 +0100
@@ -137,9 +137,7 @@
 	const char *line;
 	int fd;
 
-	fd = net_connect_unix(ctx->anvil_path);
-	if (fd == -1)
-		i_fatal("net_connect_unix(%s) failed: %m", ctx->anvil_path);
+	fd = doveadm_connect(ctx->anvil_path);
 	net_set_nonblock(fd, FALSE);
 
 	input = i_stream_create_fd(fd, (size_t)-1, TRUE);
diff -r 5ea9394b5ced -r 16db2af2b831 src/doveadm/doveadm.c
--- a/src/doveadm/doveadm.c	Mon Jul 12 00:32:30 2010 +0100
+++ b/src/doveadm/doveadm.c	Mon Jul 12 00:33:12 2010 +0100
@@ -5,6 +5,7 @@
 #include "str.h"
 #include "env-util.h"
 #include "execv-const.h"
+#include "network.h"
 #include "module-dir.h"
 #include "master-service.h"
 #include "master-service-settings.h"
@@ -17,6 +18,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <time.h>
+#include <sys/stat.h>
 
 bool doveadm_verbose = FALSE, doveadm_debug = FALSE;
 
@@ -148,6 +150,52 @@
 	return NULL;
 }
 
+static bool
+parse_hostport(const char *str, const char **host_r, unsigned int *port_r)
+{
+	const char *p;
+
+	/* host:port */
+	p = strrchr(str, ':');
+	if (p == NULL || str_to_uint(p+1, port_r) < 0)
+		return FALSE;
+	*host_r = t_strdup_until(str, p);
+
+	/* 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;
+}
+
+int doveadm_connect(const char *path)
+{
+	struct stat st;
+	const char *host;
+	struct ip_addr *ips;
+	unsigned int port, ips_count;
+	int fd, ret;
+
+	if (parse_hostport(path, &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 {
+		fd = net_connect_unix(path);
+		if (fd == -1)
+			i_fatal("net_connect_unix(%s) failed: %m", path);
+	}
+	return fd;
+}
+
 static bool doveadm_has_subcommands(const char *cmd_name)
 {
 	const struct doveadm_cmd *cmd;
diff -r 5ea9394b5ced -r 16db2af2b831 src/doveadm/doveadm.h
--- a/src/doveadm/doveadm.h	Mon Jul 12 00:32:30 2010 +0100
+++ b/src/doveadm/doveadm.h	Mon Jul 12 00:33:12 2010 +0100
@@ -34,6 +34,7 @@
 
 const char *unixdate2str(time_t timestamp);
 const char *doveadm_plugin_getenv(const char *name);
+int doveadm_connect(const char *path);
 void doveadm_master_send_signal(int signo);
 
 void doveadm_register_director_commands(void);


More information about the dovecot-cvs mailing list