Mercurial > dovecot > core-2.2
changeset 11795:16db2af2b831
doveadm director/penalty/who: Support also communicating via TCP sockets.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Mon, 12 Jul 2010 00:33:12 +0100 |
parents | 5ea9394b5ced |
children | e546af72d6bb |
files | src/doveadm/doveadm-director.c src/doveadm/doveadm-penalty.c src/doveadm/doveadm-who.c src/doveadm/doveadm.c src/doveadm/doveadm.h |
diffstat | 5 files changed, 52 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- 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);
--- 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);
--- 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);
--- 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;
--- 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);