changeset 19681:5a3aac8f8ee1

Replace host:port parsers with net_str2hostport().
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Fri, 29 Jan 2016 17:42:51 +0200
parents 55831f9d2e66
children afc700a2e242
files src/director/director-host.c src/doveadm/doveadm-util.c src/lib-lda/smtp-client.c src/lib/iostream-rawlog.c
diffstat 4 files changed, 11 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/src/director/director-host.c	Fri Jan 29 17:32:30 2016 +0200
+++ b/src/director/director-host.c	Fri Jan 29 17:42:51 2016 +0200
@@ -152,16 +152,9 @@
 	struct ip_addr *ips;
 	in_port_t port;
 	unsigned int i, ips_count;
-	const char *p;
 
-	p = strrchr(host, ':');
-	if (p != NULL) {
-		if (net_str2port(p + 1, &port) < 0)
-			i_fatal("Invalid director port in %s", host);
-		host = t_strdup_until(host, p);
-	} else {
-		port = dir->self_port;
-	}
+	if (net_str2hostport(host, dir->self_port, &host, &port) < 0)
+		i_fatal("Invalid director host:port in '%s'", host);
 
 	if (net_gethostbyname(host, &ips, &ips_count) < 0)
 		i_fatal("Unknown director host: %s", host);
--- a/src/doveadm/doveadm-util.c	Fri Jan 29 17:32:30 2016 +0200
+++ b/src/doveadm/doveadm-util.c	Fri Jan 29 17:42:51 2016 +0200
@@ -96,25 +96,6 @@
 	return NULL;
 }
 
-static bool
-parse_hostport(const char *str, in_port_t default_port,
-	       const char **host_r, in_port_t *port_r)
-{
-	const char *p;
-
-	/* host:port */
-	p = strrchr(str, ':');
-	if (p == NULL && default_port != 0) {
-		*host_r = str;
-		*port_r = default_port;
-	} else {
-		if (p == NULL || net_str2port(p+1, port_r) < 0)
-			return FALSE;
-		*host_r = t_strdup_until(str, p);
-	}
-	return TRUE;
-}
-
 static int
 doveadm_tcp_connect_port(const char *host, in_port_t port)
 {
@@ -140,7 +121,7 @@
 	const char *host;
 	in_port_t port;
 
-	if (!parse_hostport(target, default_port, &host, &port)) {
+	if (net_str2hostport(target, default_port, &host, &port) < 0) {
 		i_fatal("Port not known for %s. Either set proxy_port "
 			"or use %s:port", target, target);
 	}
--- a/src/lib-lda/smtp-client.c	Fri Jan 29 17:32:30 2016 +0200
+++ b/src/lib-lda/smtp-client.c	Fri Jan 29 17:42:51 2016 +0200
@@ -255,17 +255,13 @@
 	struct ioloop *ioloop;
 	struct istream *input;
 	const char *host, *p, *const *destp;
-	in_port_t port = DEFAULT_SUBMISSION_PORT;
+	in_port_t port;
 
-	host = smtp_client->set->submission_host;
-	p = strchr(host, ':');
-	if (p != NULL) {
-		host = t_strdup_until(host, p);
-		if (net_str2port(p + 1, &port) < 0) {
-			*error_r = t_strdup_printf(
-				"Invalid port in submission_host: %s", p+1);
-			return -1;
-		}
+	if (net_str2hostport(smtp_client->set->submission_host,
+			     DEFAULT_SUBMISSION_PORT, &host, &port) < 0) {
+		*error_r = t_strdup_printf(
+			"Invalid submission_host: %s", host);
+		return -1;
 	}
 
 	if (o_stream_nfinish(smtp_client->output) < 0) {
--- a/src/lib/iostream-rawlog.c	Fri Jan 29 17:32:30 2016 +0200
+++ b/src/lib/iostream-rawlog.c	Fri Jan 29 17:42:51 2016 +0200
@@ -165,7 +165,7 @@
 iostream_rawlog_try_create_tcp(const char *path,
 			       struct istream **input, struct ostream **output)
 {
-	const char *p, *host;
+	const char *host;
 	struct ip_addr *ips;
 	unsigned int ips_count;
 	in_port_t port;
@@ -178,11 +178,8 @@
 
 	if (strchr(path, '/') != NULL)
 		return 0;
-	if ((p = strchr(path, ':')) == NULL)
+	if (net_str2hostport(path, 0, &host, &port) < 0 || port == 0)
 		return 0;
-	if (net_str2port(p+1, &port) < 0)
-		return 0;
-	host = t_strdup_until(path, p);
 
 	ret = net_gethostbyname(host, &ips, &ips_count);
 	if (ret != 0) {