Skip to content

Commit

Permalink
Replace host:port parsers with net_str2hostport().
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Jan 29, 2016
1 parent 8bb311a commit 7c92514
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 46 deletions.
13 changes: 3 additions & 10 deletions src/director/director-host.c
Expand Up @@ -152,16 +152,9 @@ static void director_host_add_string(struct director *dir, const char *host)
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);
Expand Down
21 changes: 1 addition & 20 deletions src/doveadm/doveadm-util.c
Expand Up @@ -96,25 +96,6 @@ const char *doveadm_plugin_getenv(const char *name)
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)
{
Expand All @@ -140,7 +121,7 @@ int doveadm_tcp_connect(const char *target, in_port_t default_port)
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);
}
Expand Down
18 changes: 7 additions & 11 deletions src/lib-lda/smtp-client.c
Expand Up @@ -255,17 +255,13 @@ smtp_client_send_flush(struct smtp_client *smtp_client,
struct ioloop *ioloop;
struct istream *input;
const char *host, *p, *const *destp;
in_port_t port = DEFAULT_SUBMISSION_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;
}
in_port_t port;

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) {
Expand Down
7 changes: 2 additions & 5 deletions src/lib/iostream-rawlog.c
Expand Up @@ -165,7 +165,7 @@ static int
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;
Expand All @@ -178,11 +178,8 @@ iostream_rawlog_try_create_tcp(const char *path,

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) {
Expand Down

0 comments on commit 7c92514

Please sign in to comment.