changeset 19035:aabfe48db1cf

Changed type of internet port values to in_port_t everywhere. Created special SET_IN_PORT setting type for internet port values. Created net_str2port() for parsing internet port values. Removed several atoi() invocations in the process.
author Stephan Bosch <stephan@rename-it.nl>
date Sat, 29 Aug 2015 14:26:30 +0300
parents f7c1c1dac689
children f78e38c7cba2
files src/auth/auth-request.c src/auth/auth-request.h src/auth/passdb-imap.c src/config/config-request.c src/director/director-connection.c src/director/director-host.c src/director/director-host.h src/director/director-settings.c src/director/director-settings.h src/director/director-test.c src/director/director.c src/director/director.h src/director/doveadm-connection.c src/director/login-connection.c src/director/main.c src/doveadm/client-connection.h src/doveadm/doveadm-auth.c src/doveadm/doveadm-director.c src/doveadm/doveadm-mail-server.c src/doveadm/doveadm-settings.c src/doveadm/doveadm-settings.h src/doveadm/doveadm-util.c src/doveadm/doveadm-util.h src/doveadm/doveadm-zlib.c src/doveadm/server-connection.c src/imap-hibernate/imap-client.h src/imap-hibernate/imap-hibernate-client.c src/imap-login/client.c src/imap-urlauth/imap-urlauth-worker-settings.c src/imap-urlauth/imap-urlauth-worker-settings.h src/imap/imap-client-hibernate.c src/imap/imap-master-client.c src/imap/imap-settings.c src/imap/imap-settings.h src/lib-auth/auth-client.h src/lib-auth/auth-master.h src/lib-dict/dict-memcached-ascii.c src/lib-dict/dict-memcached.c src/lib-dict/dict-redis.c src/lib-http/http-client-connection.c src/lib-http/http-server-connection.c src/lib-http/test-http-server.c src/lib-imap-client/imapc-client.h src/lib-imap-urlauth/imap-urlauth-private.h src/lib-imap-urlauth/imap-urlauth.h src/lib-lda/lmtp-client.c src/lib-lda/lmtp-client.h src/lib-lda/smtp-client.c src/lib-master/master-service-haproxy.c src/lib-master/master-service.h src/lib-master/service-settings.h src/lib-settings/settings-parser.c src/lib-settings/settings-parser.h src/lib-sql/driver-mysql.c src/lib-storage/index/imapc/imapc-settings.c src/lib-storage/index/imapc/imapc-settings.h src/lib-storage/index/pop3c/pop3c-client.h src/lib-storage/index/pop3c/pop3c-settings.c src/lib-storage/index/pop3c/pop3c-settings.h src/lib-storage/mail-storage-service.h src/lib/connection.c src/lib/connection.h src/lib/fd-close-on-exec.c src/lib/iostream-rawlog.c src/lib/net.c src/lib/net.h src/lib/uri-util.c src/lmtp/client.h src/lmtp/commands.c src/lmtp/lmtp-proxy.h src/login-common/client-common-auth.c src/login-common/client-common.h src/login-common/login-common.h src/login-common/login-proxy-state.c src/login-common/login-proxy-state.h src/login-common/login-proxy.c src/login-common/login-proxy.h src/master/master-settings.c src/master/service-listen.c src/master/service.c src/pop3-login/client.c src/replication/aggregator/aggregator-settings.c src/replication/aggregator/aggregator-settings.h src/replication/aggregator/replicator-connection.c src/replication/aggregator/replicator-connection.h
diffstat 85 files changed, 310 insertions(+), 249 deletions(-) [+]
line wrap: on
line diff
--- a/src/auth/auth-request.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/auth/auth-request.c	Sat Aug 29 14:26:30 2015 +0300
@@ -276,11 +276,11 @@
 		if (request->real_remote_ip.family == 0)
 			request->real_remote_ip = request->remote_ip;
 	} else if (strcmp(key, "lport") == 0) {
-		request->local_port = atoi(value);
+		(void)net_str2port(value, &request->local_port);
 		if (request->real_local_port == 0)
 			request->real_local_port = request->local_port;
 	} else if (strcmp(key, "rport") == 0) {
-		request->remote_port = atoi(value);
+		(void)net_str2port(value, &request->remote_port);
 		if (request->real_remote_port == 0)
 			request->real_remote_port = request->remote_port;
 	}
@@ -289,9 +289,9 @@
 	else if (strcmp(key, "real_rip") == 0)
 		(void)net_addr2ip(value, &request->real_remote_ip);
 	else if (strcmp(key, "real_lport") == 0)
-		request->real_local_port = atoi(value);
+		(void)net_str2port(value, &request->real_local_port);
 	else if (strcmp(key, "real_rport") == 0)
-		request->real_remote_port = atoi(value);
+		(void)net_str2port(value, &request->real_remote_port);
 	else if (strcmp(key, "session") == 0)
 		request->session_id = p_strdup(request->pool, value);
 	else
--- a/src/auth/auth-request.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/auth/auth-request.h	Sat Aug 29 14:26:30 2015 +0300
@@ -73,7 +73,7 @@
 
 	const char *service, *mech_name, *session_id;
 	struct ip_addr local_ip, remote_ip, real_local_ip, real_remote_ip;
-	unsigned int local_port, remote_port, real_local_port, real_remote_port;
+	in_port_t local_port, remote_port, real_local_port, real_remote_port;
 
 	struct timeout *to_abort, *to_penalty;
 	unsigned int last_penalty;
--- a/src/auth/passdb-imap.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/auth/passdb-imap.c	Sat Aug 29 14:26:30 2015 +0300
@@ -133,8 +133,7 @@
 		if (strcmp(key, "host") == 0)
 			module->set.host = value;
 		else if (strcmp(key, "port") == 0) {
-			if (str_to_uint(value, &module->set.port) < 0 ||
-			    module->set.port == 0 || module->set.port > 65535)
+			if (net_str2port(value, &module->set.port) < 0)
 				i_fatal("passdb imap: Invalid port: %s", value);
 			port_set = TRUE;
 		} else if (strcmp(key, "username") == 0)
--- a/src/config/config-request.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/config/config-request.c	Sat Aug 29 14:26:30 2015 +0300
@@ -116,6 +116,13 @@
 		}
 		break;
 	}
+	case SET_IN_PORT: {
+		const in_port_t *val = value, *dval = default_value;
+
+		if (dump_default || dval == NULL || *val != *dval)
+			str_printfa(str, "%u", *val);
+		break;
+	}
 	case SET_STR_VARS: {
 		const char *const *val = value, *sval;
 		const char *const *_dval = default_value;
@@ -245,6 +252,7 @@
 		case SET_UINT:
 		case SET_UINT_OCT:
 		case SET_TIME:
+		case SET_IN_PORT:
 		case SET_STR_VARS:
 		case SET_STR:
 		case SET_ENUM:
--- a/src/director/director-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -345,7 +345,7 @@
 static bool
 director_args_parse_ip_port(struct director_connection *conn,
 			    const char *const *args,
-			    struct ip_addr *ip_r, unsigned int *port_r)
+			    struct ip_addr *ip_r, in_port_t *port_r)
 {
 	if (args[0] == NULL || args[1] == NULL) {
 		director_cmd_error(conn, "Missing IP+port parameters");
@@ -355,7 +355,7 @@
 		director_cmd_error(conn, "Invalid IP address: %s", args[0]);
 		return FALSE;
 	}
-	if (str_to_uint(args[1], port_r) < 0) {
+	if (net_str2port(args[1], port_r) < 0) {
 		director_cmd_error(conn, "Invalid port: %s", args[1]);
 		return FALSE;
 	}
@@ -368,7 +368,7 @@
 	struct director *dir = conn->dir;
 	const char *connect_str;
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 	time_t next_comm_attempt;
 
 	if (!director_args_parse_ip_port(conn, args, &ip, &port))
@@ -650,7 +650,7 @@
 {
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	if (!director_args_parse_ip_port(conn, args, &ip, &port))
 		return FALSE;
@@ -699,7 +699,7 @@
 {
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	if (!director_args_parse_ip_port(conn, args, &ip, &port))
 		return FALSE;
@@ -746,12 +746,13 @@
 {
 	const char *const *args = *_args;
 	struct ip_addr ip;
-	unsigned int port, seq;
+	in_port_t port;
+	unsigned int seq;
 	struct director_host *host;
 
 	if (str_array_length(args) < 3 ||
 	    net_addr2ip(args[0], &ip) < 0 ||
-	    str_to_uint(args[1], &port) < 0 ||
+	    net_str2port(args[1], &port) < 0 ||
 	    str_to_uint(args[2], &seq) < 0) {
 		director_cmd_error(conn, "Invalid parameters");
 		return -1;
@@ -1295,7 +1296,8 @@
 	struct director *dir = conn->dir;
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port, seq, minor_version = 0, timestamp = ioloop_time;
+	in_port_t port;
+	unsigned int seq, minor_version = 0, timestamp = ioloop_time;
 
 	if (str_array_length(args) < 3 ||
 	    !director_args_parse_ip_port(conn, args, &ip, &port) ||
@@ -1332,7 +1334,7 @@
 	struct director *dir = conn->dir;
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	if (str_array_length(args) != 2 ||
 	    !director_args_parse_ip_port(conn, args, &ip, &port)) {
--- a/src/director/director-host.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-host.c	Sat Aug 29 14:26:30 2015 +0300
@@ -24,7 +24,7 @@
 
 struct director_host *
 director_host_add(struct director *dir,
-		  const struct ip_addr *ip, unsigned int port)
+		  const struct ip_addr *ip, in_port_t port)
 {
 	struct director_host *host;
 
@@ -90,7 +90,7 @@
 
 struct director_host *
 director_host_get(struct director *dir, const struct ip_addr *ip,
-		  unsigned int port)
+		  in_port_t port)
 {
 	struct director_host *host;
 
@@ -102,7 +102,7 @@
 
 struct director_host *
 director_host_lookup(struct director *dir, const struct ip_addr *ip,
-		     unsigned int port)
+		     in_port_t port)
 {
 	struct director_host *const *hostp;
 
@@ -150,12 +150,13 @@
 static void director_host_add_string(struct director *dir, const char *host)
 {
 	struct ip_addr *ips;
-	unsigned int i, port, ips_count;
+	in_port_t port;
+	unsigned int i, ips_count;
 	const char *p;
 
 	p = strrchr(host, ':');
 	if (p != NULL) {
-		if (str_to_uint(p + 1, &port) < 0 || port == 0 || port > 65535)
+		if (net_str2port(p + 1, &port) < 0)
 			i_fatal("Invalid director port in %s", host);
 		host = t_strdup_until(host, p);
 	} else {
--- a/src/director/director-host.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-host.h	Sat Aug 29 14:26:30 2015 +0300
@@ -10,7 +10,7 @@
 	int refcount;
 
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	/* name contains "ip:port" */
 	char *name;
@@ -33,7 +33,7 @@
 
 struct director_host *
 director_host_add(struct director *dir, const struct ip_addr *ip,
-		  unsigned int port);
+		  in_port_t port);
 void director_host_free(struct director_host **host);
 
 void director_host_ref(struct director_host *host);
@@ -43,10 +43,10 @@
 
 struct director_host *
 director_host_get(struct director *dir, const struct ip_addr *ip,
-		  unsigned int port);
+		  in_port_t port);
 struct director_host *
 director_host_lookup(struct director *dir, const struct ip_addr *ip,
-		     unsigned int port);
+		     in_port_t port);
 struct director_host *
 director_host_lookup_ip(struct director *dir, const struct ip_addr *ip);
 
--- a/src/director/director-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -72,7 +72,7 @@
 	DEF(SET_STR, director_mail_servers),
 	DEF(SET_STR, director_username_hash),
 	DEF(SET_TIME, director_user_expire),
-	DEF(SET_UINT, director_doveadm_port),
+	DEF(SET_IN_PORT, director_doveadm_port),
 	DEF(SET_BOOL, director_consistent_hashing),
 
 	SETTING_DEFINE_LIST_END
--- a/src/director/director-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef DIRECTOR_SETTINGS_H
 #define DIRECTOR_SETTINGS_H
 
+#include "net.h"
+
 struct director_settings {
 	const char *master_user_separator;
 
@@ -8,7 +10,7 @@
 	const char *director_mail_servers;
 	const char *director_username_hash;
 	unsigned int director_user_expire;
-	unsigned int director_doveadm_port;
+	in_port_t director_doveadm_port;
 	bool director_consistent_hashing;
 };
 
--- a/src/director/director-test.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director-test.c	Sat Aug 29 14:26:30 2015 +0300
@@ -388,7 +388,7 @@
 static void client_connected(struct master_service_connection *conn)
 {
 	struct ip_addr local_ip, remote_ip;
-	unsigned int local_port;
+	in_port_t local_port;
 
 	if (net_getsockname(conn->fd, &local_ip, &local_port) < 0)
 		i_fatal("net_getsockname() failed: %m");
--- a/src/director/director.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director.c	Sat Aug 29 14:26:30 2015 +0300
@@ -103,7 +103,7 @@
 
 int director_connect_host(struct director *dir, struct director_host *host)
 {
-	unsigned int port;
+	in_port_t port;
 	int fd;
 
 	if (director_has_outgoing_connection(dir, host))
@@ -928,7 +928,7 @@
 
 struct director *
 director_init(const struct director_settings *set,
-	      const struct ip_addr *listen_ip, unsigned int listen_port,
+	      const struct ip_addr *listen_ip, in_port_t listen_port,
 	      director_state_change_callback_t *callback)
 {
 	struct director *dir;
--- a/src/director/director.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/director.h	Sat Aug 29 14:26:30 2015 +0300
@@ -38,9 +38,9 @@
 
 	/* IP and port of this director. self_host->ip/port must equal these. */
 	struct ip_addr self_ip;
-	unsigned int self_port;
+	in_port_t self_port;
 
-	unsigned int test_port;
+	in_port_t test_port;
 
 	struct director_host *self_host;
 	/* left and right connections are set only after they have finished
@@ -101,7 +101,7 @@
    without specified port. */
 struct director *
 director_init(const struct director_settings *set,
-	      const struct ip_addr *listen_ip, unsigned int listen_port,
+	      const struct ip_addr *listen_ip, in_port_t listen_port,
 	      director_state_change_callback_t *callback);
 void director_deinit(struct director **dir);
 void director_find_self(struct director *dir);
--- a/src/director/doveadm-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/doveadm-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -199,12 +199,12 @@
 	const char *const *args;
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port = conn->dir->self_port;
+	in_port_t port = conn->dir->self_port;
 
 	args = t_strsplit_tab(line);
 	if (args[0] == NULL ||
 	    net_addr2ip(line, &ip) < 0 ||
-	    (args[1] != NULL && str_to_uint(args[1], &port) < 0)) {
+	    (args[1] != NULL && net_str2port(args[1], &port) < 0)) {
 		i_error("doveadm sent invalid DIRECTOR-ADD parameters");
 		return FALSE;
 	}
@@ -223,12 +223,12 @@
 	const char *const *args;
 	struct director_host *host;
 	struct ip_addr ip;
-	unsigned int port = 0;
+	in_port_t port = 0;
 
 	args = t_strsplit_tab(line);
 	if (args[0] == NULL ||
 	    net_addr2ip(line, &ip) < 0 ||
-	    (args[1] != NULL && str_to_uint(args[1], &port) < 0)) {
+	    (args[1] != NULL && net_str2port(args[1], &port) < 0)) {
 		i_error("doveadm sent invalid DIRECTOR-REMOVE parameters");
 		return FALSE;
 	}
--- a/src/director/login-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/login-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -39,8 +39,8 @@
 	char *line, *username;
 
 	struct ip_addr local_ip;
-	unsigned int local_port;
-	unsigned int dest_port;
+	in_port_t local_port;
+	in_port_t dest_port;
 	bool director_proxy_maybe;
 };
 
@@ -202,10 +202,10 @@
 			if (net_addr2ip((*args) + 4, &temp_request.local_ip) < 0)
 				i_error("auth sent invalid lip field: %s", (*args) + 6);
 		} else if (strncmp(*args, "lport=", 6) == 0) {
-			if (str_to_uint((*args) + 6, &temp_request.local_port) < 0)
+			if (net_str2port((*args) + 6, &temp_request.local_port) < 0)
 				i_error("auth sent invalid lport field: %s", (*args) + 6);
 		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint((*args) + 5, &temp_request.dest_port) < 0)
+			if (net_str2port((*args) + 5, &temp_request.dest_port) < 0)
 				i_error("auth sent invalid port field: %s", (*args) + 6);
 		} else if (strncmp(*args, "destuser=", 9) == 0)
 			username = *args + 9;
--- a/src/director/main.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/director/main.c	Sat Aug 29 14:26:30 2015 +0300
@@ -97,7 +97,7 @@
 listener_get_socket_type_fallback(const struct director_settings *set,
 				  int listen_fd)
 {
-	unsigned int local_port;
+	in_port_t local_port;
 
 	if (net_getsockname(listen_fd, NULL, &local_port) == 0 &&
 	    local_port != 0) {
@@ -112,11 +112,12 @@
 
 static void listener_sockets_init(const struct director_settings *set,
 				  struct ip_addr *listen_ip_r,
-				  unsigned int *listen_port_r)
+				  in_port_t *listen_port_r)
 {
 	const char *name;
-	unsigned int i, socket_count, port;
+	unsigned int i, socket_count;
 	struct ip_addr ip;
+	in_port_t port;
 	enum director_socket_type type;
 
 	*listen_port_r = 0;
@@ -242,7 +243,7 @@
 {
 	const struct director_settings *set;
 	struct ip_addr listen_ip;
-	unsigned int listen_port;
+	in_port_t listen_port;
 
 	if (master_service_settings_get(master_service)->verbose_proctitle) {
 		to_proctitle_refresh =
@@ -291,7 +292,7 @@
 	};
 	const enum master_service_flags service_flags =
 		MASTER_SERVICE_FLAG_NO_IDLE_DIE;
-	unsigned int test_port = 0;
+	in_port_t test_port = 0;
 	const char *error;
 	bool debug = FALSE;
 	int c;
@@ -304,8 +305,8 @@
 			debug = TRUE;
 			break;
 		case 't':
-			if (str_to_uint(optarg, &test_port) < 0)
-				i_fatal("-t: Not a number: %s", optarg);
+			if (net_str2port(optarg, &test_port) < 0)
+				i_fatal("-t: Not a port number: %s", optarg);
 			break;
 		default:
 			return FATAL_DEFAULT;
--- a/src/doveadm/client-connection.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/client-connection.h	Sat Aug 29 14:26:30 2015 +0300
@@ -12,7 +12,7 @@
 	struct ostream *output;
 	struct ssl_iostream *ssl_iostream;
 	struct ip_addr local_ip, remote_ip;
-	unsigned int local_port, remote_port;
+	in_port_t local_port, remote_port;
 	const struct doveadm_settings *set;
 
 	unsigned int handshaked:1;
--- a/src/doveadm/doveadm-auth.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-auth.c	Sat Aug 29 14:26:30 2015 +0300
@@ -190,9 +190,11 @@
 		if (net_addr2ip(arg + 4, &info->remote_ip) < 0)
 			i_fatal("rip: Invalid ip");
 	} else if (strncmp(arg, "lport=", 6) == 0) {
-		info->local_port = atoi(arg + 6);
+		if (net_str2port(arg + 6, &info->local_port) < 0)
+			i_fatal("lport: Invalid port number");
 	} else if (strncmp(arg, "rport=", 6) == 0) {
-		info->remote_port = atoi(arg + 6);
+		if (net_str2port(arg + 6, &info->remote_port) < 0)
+			i_fatal("rport: Invalid port number");
 	} else {
 		i_fatal("Unknown -x argument: %s", arg);
 	}
--- a/src/doveadm/doveadm-director.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-director.c	Sat Aug 29 14:26:30 2015 +0300
@@ -720,13 +720,13 @@
 {
 	struct director_context *ctx;
 	struct ip_addr ip;
+	in_port_t port = 0;
 	string_t *str = t_str_new(64);
-	unsigned int port = 0;
 
 	ctx = cmd_director_init(argc, argv, "a:", cmd_director_ring_add);
 	if (argv[optind] == NULL ||
 	    net_addr2ip(argv[optind], &ip) < 0 ||
-	    (argv[optind+1] != NULL && str_to_uint(argv[optind+1], &port) < 0))
+	    (argv[optind+1] != NULL && net_str2port(argv[optind+1], &port) < 0))
 		director_cmd_help(cmd_director_ring_add);
 
 	str_printfa(str, "DIRECTOR-ADD\t%s", net_ip2addr(&ip));
@@ -743,12 +743,12 @@
 	struct director_context *ctx;
 	struct ip_addr ip;
 	string_t *str = t_str_new(64);
-	unsigned int port = 0;
+	in_port_t port = 0;
 
 	ctx = cmd_director_init(argc, argv, "a:", cmd_director_ring_remove);
 	if (argv[optind] == NULL ||
 	    net_addr2ip(argv[optind], &ip) < 0 ||
-	    (argv[optind+1] != NULL && str_to_uint(argv[optind+1], &port) < 0))
+	    (argv[optind+1] != NULL && net_str2port(argv[optind+1], &port) < 0))
 		director_cmd_help(cmd_director_ring_remove);
 
 	str_printfa(str, "DIRECTOR-REMOVE\t%s", net_ip2addr(&ip));
--- a/src/doveadm/doveadm-mail-server.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-mail-server.c	Sat Aug 29 14:26:30 2015 +0300
@@ -180,7 +180,8 @@
 	struct auth_user_info info;
 	pool_t pool;
 	const char *auth_socket_path, *proxy_host, *const *fields;
-	unsigned int i, proxy_port;
+	unsigned int i;
+	in_port_t proxy_port;
 	bool proxying;
 	int ret;
 
@@ -228,7 +229,7 @@
 			else if (strncmp(fields[i], "destuser=", 9) == 0)
 				*user_r = t_strdup(fields[i]+9);
 			else if (strncmp(fields[i], "port=", 5) == 0) {
-				if (str_to_uint(fields[i]+5, &proxy_port) < 0)
+				if (net_str2port(fields[i]+5, &proxy_port) < 0)
 					proxy_port = 0;
 			}
 		}
--- a/src/doveadm/doveadm-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -59,7 +59,7 @@
 	DEF(SET_STR, auth_socket_path),
 	DEF(SET_STR, doveadm_socket_path),
 	DEF(SET_UINT, doveadm_worker_count),
-	DEF(SET_UINT, doveadm_port),
+	DEF(SET_IN_PORT, doveadm_port),
 	{ SET_ALIAS, "doveadm_proxy_port", 0, NULL },
 	DEF(SET_STR, doveadm_username),
 	DEF(SET_STR, doveadm_password),
--- a/src/doveadm/doveadm-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef DOVEADM_SETTINGS_H
 #define DOVEADM_SETTINGS_H
 
+#include "net.h"
+
 struct doveadm_settings {
 	const char *base_dir;
 	const char *libexec_dir;
@@ -9,7 +11,7 @@
 	const char *auth_socket_path;
 	const char *doveadm_socket_path;
 	unsigned int doveadm_worker_count;
-	unsigned int doveadm_port;
+	in_port_t doveadm_port;
 	const char *doveadm_username;
 	const char *doveadm_password;
 	const char *doveadm_allowed_commands;
--- a/src/doveadm/doveadm-util.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-util.c	Sat Aug 29 14:26:30 2015 +0300
@@ -97,8 +97,8 @@
 }
 
 static bool
-parse_hostport(const char *str, unsigned int default_port,
-	       const char **host_r, unsigned int *port_r)
+parse_hostport(const char *str, in_port_t default_port,
+	       const char **host_r, in_port_t *port_r)
 {
 	const char *p;
 
@@ -108,7 +108,7 @@
 		*host_r = str;
 		*port_r = default_port;
 	} else {
-		if (p == NULL || str_to_uint(p+1, port_r) < 0)
+		if (p == NULL || net_str2port(p+1, port_r) < 0)
 			return FALSE;
 		*host_r = t_strdup_until(str, p);
 	}
@@ -116,7 +116,7 @@
 }
 
 static int
-doveadm_tcp_connect_port(const char *host, unsigned int port)
+doveadm_tcp_connect_port(const char *host, in_port_t port)
 {
 	struct ip_addr *ips;
 	unsigned int ips_count;
@@ -135,10 +135,10 @@
 	return fd;
 }
 
-int doveadm_tcp_connect(const char *target, unsigned int default_port)
+int doveadm_tcp_connect(const char *target, in_port_t default_port)
 {
 	const char *host;
-	unsigned int port;
+	in_port_t port;
 
 	if (!parse_hostport(target, default_port, &host, &port)) {
 		i_fatal("Port not known for %s. Either set proxy_port "
@@ -148,7 +148,7 @@
 }
 
 int doveadm_connect_with_default_port(const char *path,
-				      unsigned int default_port)
+				      in_port_t default_port)
 {
 	int fd;
 
--- a/src/doveadm/doveadm-util.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-util.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef DOVEADM_UTIL_H
 #define DOVEADM_UTIL_H
 
+#include "net.h"
+
 #define DOVEADM_SERVER_PROTOCOL_VERSION_MAJOR 1
 
 extern bool doveadm_verbose, doveadm_debug, doveadm_server;
@@ -8,9 +10,9 @@
 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_tcp_connect(const char *target, in_port_t default_port);
 int doveadm_connect_with_default_port(const char *path,
-				      unsigned int default_port);
+				      in_port_t default_port);
 
 void doveadm_load_modules(void);
 void doveadm_unload_modules(void);
--- a/src/doveadm/doveadm-zlib.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/doveadm-zlib.c	Sat Aug 29 14:26:30 2015 +0300
@@ -141,11 +141,12 @@
 {
 	struct client client;
 	struct ip_addr *ips;
-	unsigned int ips_count, port = 143;
+	unsigned int ips_count;
+	in_port_t port = 143;
 	int fd, ret;
 
 	if (argv[1] == NULL ||
-	    (argv[2] != NULL && str_to_uint(argv[2], &port) < 0))
+	    (argv[2] != NULL && net_str2port(argv[2], &port) < 0))
 		help(&doveadm_cmd_zlibconnect);
 
 	ret = net_gethostbyname(argv[1], &ips, &ips_count);
--- a/src/doveadm/server-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/doveadm/server-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -372,7 +372,7 @@
 	struct master_service_settings_input input;
 	struct master_service_settings_output output;
 	const char *error;
-	unsigned int port;
+	in_port_t port;
 	void *set;
 
 	memset(&input, 0, sizeof(input));
--- a/src/imap-hibernate/imap-client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap-hibernate/imap-client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -14,7 +14,7 @@
 	gid_t gid;
 
 	struct ip_addr peer_ip;
-	unsigned int peer_port;
+	in_port_t peer_port;
 
 	const unsigned char *state;
 	size_t state_size;
--- a/src/imap-hibernate/imap-hibernate-client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap-hibernate/imap-hibernate-client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -84,9 +84,9 @@
 				return -1;
 			}
 		} else if (strcmp(key, "peer_port") == 0) {
-			if (str_to_uint(value, &state_r->peer_port) < 0) {
+			if (net_str2port(value, &state_r->peer_port) < 0) {
 				*error_r = t_strdup_printf(
-					"Invalid peer_ip value: %s", value);
+					"Invalid peer_port value: %s", value);
 				return -1;
 			}
 		} else if (strcmp(key, "uid") == 0) {
--- a/src/imap-login/client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap-login/client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -149,17 +149,17 @@
 client_update_info(struct imap_client *client,
 		   const char *key, const char *value)
 {
-	if (strcasecmp(key, "x-originating-ip") == 0)
+	if (strcasecmp(key, "x-originating-ip") == 0) {
 		(void)net_addr2ip(value, &client->common.ip);
-	else if (strcasecmp(key, "x-originating-port") == 0)
-		client->common.remote_port = atoi(value);
-	else if (strcasecmp(key, "x-connected-ip") == 0)
+	} else if (strcasecmp(key, "x-originating-port") == 0) {
+		(void)net_str2port(value, &client->common.remote_port);
+	} else if (strcasecmp(key, "x-connected-ip") == 0) {
 		(void)net_addr2ip(value, &client->common.local_ip);
-	else if (strcasecmp(key, "x-connected-port") == 0)
-		client->common.local_port = atoi(value);
-	else if (strcasecmp(key, "x-proxy-ttl") == 0)
+	} else if (strcasecmp(key, "x-connected-port") == 0) {
+		(void)net_str2port(value, &client->common.local_port);
+	}	else if (strcasecmp(key, "x-proxy-ttl") == 0) {
 		client->common.proxy_ttl = atoi(value);
-	else if (strcasecmp(key, "x-session-id") == 0 ||
+	} else if (strcasecmp(key, "x-session-id") == 0 ||
 		 strcasecmp(key, "x-session-ext-id") == 0) {
 		if (strlen(value) <= LOGIN_MAX_SESSION_ID_LEN) {
 			client->common.session_id =
--- a/src/imap-urlauth/imap-urlauth-worker-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap-urlauth/imap-urlauth-worker-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -57,7 +57,7 @@
 	DEF(SET_BOOL, verbose_proctitle),
 
 	DEF(SET_STR, imap_urlauth_host),
-	DEF(SET_UINT, imap_urlauth_port),
+	DEF(SET_IN_PORT, imap_urlauth_port),
 
 	SETTING_DEFINE_LIST_END
 };
--- a/src/imap-urlauth/imap-urlauth-worker-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap-urlauth/imap-urlauth-worker-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -8,7 +8,7 @@
 
 	/* imap_urlauth: */
 	const char *imap_urlauth_host;
-	unsigned int imap_urlauth_port;
+	in_port_t imap_urlauth_port;
 };
 
 extern const struct imap_urlauth_worker_settings imap_urlauth_worker_default_settings;
--- a/src/imap/imap-client-hibernate.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap/imap-client-hibernate.c	Sat Aug 29 14:26:30 2015 +0300
@@ -42,7 +42,7 @@
 				     const buffer_t *state, int fd_notify)
 {
 	struct ip_addr peer_ip;
-	unsigned int peer_port;
+	in_port_t peer_port;
 
 	str_append_tabescaped(cmd, client->user->username);
 	str_append_c(cmd, '\t');
--- a/src/imap/imap-master-client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap/imap-master-client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -28,7 +28,7 @@
 	buffer_t *state;
 
 	struct ip_addr peer_ip;
-	unsigned int peer_port;
+	in_port_t peer_port;
 
 	bool state_import_bad_idle_done;
 	bool state_import_idle_continue;
@@ -98,8 +98,7 @@
 				return -1;
 			}
 		} else if (strcmp(key, "peer_port") == 0) {
-			if (str_to_uint32(value, &master_input_r->peer_port) < 0 ||
-			    master_input_r->peer_port == 0) {
+			if (net_str2port(value, &master_input_r->peer_port) < 0) {
 				*error_r = t_strdup_printf(
 					"Invalid peer_port value: %s", value);
 				return -1;
@@ -143,7 +142,7 @@
 				     int fd_client, const char **error_r)
 {
 	struct ip_addr peer_ip;
-	unsigned int peer_port;
+	in_port_t peer_port;
 
 	if (master_input->peer_port == 0)
 		return 0;
--- a/src/imap/imap-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap/imap-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -75,7 +75,7 @@
 	DEF(SET_TIME, imap_hibernate_timeout),
 
 	DEF(SET_STR, imap_urlauth_host),
-	DEF(SET_UINT, imap_urlauth_port),
+	DEF(SET_IN_PORT, imap_urlauth_port),
 
 	SETTING_DEFINE_LIST_END
 };
--- a/src/imap/imap-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/imap/imap-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef IMAP_SETTINGS_H
 #define IMAP_SETTINGS_H
 
+#include "net.h"
+
 struct mail_user_settings;
 
 /* <settings checks> */
@@ -27,7 +29,7 @@
 
 	/* imap urlauth: */
 	const char *imap_urlauth_host;
-	unsigned int imap_urlauth_port;
+	in_port_t imap_urlauth_port;
 
 	enum imap_client_workarounds parsed_workarounds;
 };
--- a/src/lib-auth/auth-client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-auth/auth-client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -42,7 +42,7 @@
 	enum auth_request_flags flags;
 
 	struct ip_addr local_ip, remote_ip, real_local_ip, real_remote_ip;
-	unsigned int local_port, remote_port, real_local_port, real_remote_port;
+	in_port_t local_port, remote_port, real_local_port, real_remote_port;
 
 	const char *initial_resp_base64;
 };
--- a/src/lib-auth/auth-master.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-auth/auth-master.h	Sat Aug 29 14:26:30 2015 +0300
@@ -13,7 +13,7 @@
 struct auth_user_info {
 	const char *service;
 	struct ip_addr local_ip, remote_ip;
-	unsigned int local_port, remote_port;
+	in_port_t local_port, remote_port;
 };
 
 struct auth_user_reply {
--- a/src/lib-dict/dict-memcached-ascii.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-dict/dict-memcached-ascii.c	Sat Aug 29 14:26:30 2015 +0300
@@ -53,7 +53,7 @@
 	struct dict dict;
 	struct ip_addr ip;
 	char *username, *key_prefix;
-	unsigned int port;
+	in_port_t port;
 	unsigned int timeout_msecs;
 
 	struct ioloop *ioloop, *prev_ioloop;
@@ -377,7 +377,7 @@
 				ret = -1;
 			}
 		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &dict->port) < 0) {
+			if (net_str2port(*args+5, &dict->port) < 0) {
 				*error_r = t_strdup_printf("Invalid port: %s",
 							   *args+5);
 				ret = -1;
--- a/src/lib-dict/dict-memcached.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-dict/dict-memcached.c	Sat Aug 29 14:26:30 2015 +0300
@@ -48,7 +48,7 @@
 	struct dict dict;
 	struct ip_addr ip;
 	char *key_prefix;
-	unsigned int port;
+	in_port_t port;
 	unsigned int timeout_msecs;
 
 	struct ioloop *ioloop;
@@ -198,7 +198,7 @@
 				ret = -1;
 			}
 		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &dict->port) < 0) {
+			if (net_str2port(*args+5, &dict->port) < 0) {
 				*error_r = t_strdup_printf("Invalid port: %s",
 							   *args+5);
 				ret = -1;
--- a/src/lib-dict/dict-redis.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-dict/dict-redis.c	Sat Aug 29 14:26:30 2015 +0300
@@ -332,7 +332,8 @@
 {
 	struct redis_dict *dict;
 	struct ip_addr ip;
-	unsigned int secs, port = REDIS_DEFAULT_PORT;
+	unsigned int secs;
+	in_port_t port = REDIS_DEFAULT_PORT;
 	const char *const *args, *unix_path = NULL;
 	int ret = 0;
 
@@ -359,7 +360,7 @@
 				ret = -1;
 			}
 		} else if (strncmp(*args, "port=", 5) == 0) {
-			if (str_to_uint(*args+5, &port) < 0) {
+			if (net_str2port(*args+5, &port) < 0) {
 				*error_r = t_strdup_printf("Invalid port: %s",
 							   *args+5);
 				ret = -1;
--- a/src/lib-http/http-client-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-http/http-client-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -1205,7 +1205,7 @@
 
 static void
 http_client_connection_connect_tunnel(struct http_client_connection *conn,
-	const struct ip_addr *ip, unsigned int port)
+	const struct ip_addr *ip, in_port_t port)
 {
 	unsigned int msecs;
 
--- a/src/lib-http/http-server-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-http/http-server-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -785,7 +785,7 @@
 	struct http_server_connection *conn;
 	static unsigned int id = 0;
 	struct ip_addr addr;
-	unsigned int port;
+	in_port_t port;
 	const char *name;
 
 	conn = i_new(struct http_server_connection, 1);
--- a/src/lib-http/test-http-server.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-http/test-http-server.c	Sat Aug 29 14:26:30 2015 +0300
@@ -109,10 +109,10 @@
 {
 	struct ip_addr my_ip;
 	struct ioloop *ioloop;
-	unsigned int port;
+	in_port_t port;
 
 	lib_init();
-	if (argc < 2 || str_to_uint(argv[1], &port) < 0)
+	if (argc < 2 || net_str2port(argv[1], &port) < 0)
 		i_fatal("Port parameter missing");
 	if (argc < 3)
 		net_get_ip_any4(&my_ip);
--- a/src/lib-imap-client/imapc-client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-imap-client/imapc-client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef IMAPC_CLIENT_H
 #define IMAPC_CLIENT_H
 
+#include "net.h"
+
 /* IMAP RFC defines this to be at least 30 minutes. */
 #define IMAPC_DEFAULT_MAX_IDLE_TIME (60*29)
 
@@ -63,7 +65,7 @@
 
 struct imapc_client_settings {
 	const char *host;
-	unsigned int port;
+	in_port_t port;
 
 	const char *master_user;
 	const char *username;
--- a/src/lib-imap-urlauth/imap-urlauth-private.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-imap-urlauth/imap-urlauth-private.h	Sat Aug 29 14:26:30 2015 +0300
@@ -8,7 +8,7 @@
 	struct imap_urlauth_connection *conn;
 
 	char *url_host;
-	unsigned int url_port;
+	in_port_t url_port;
 
 	char *access_user;
 	const char **access_applications;
--- a/src/lib-imap-urlauth/imap-urlauth.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-imap-urlauth/imap-urlauth.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef IMAP_URLAUTH_H
 #define IMAP_URLAUTH_H
 
+#include "net.h"
+
 #define IMAP_URLAUTH_SOCKET_NAME "imap-urlauth"
 
 struct imap_url;
@@ -9,7 +11,7 @@
 
 struct imap_urlauth_config {
 	const char *url_host;
-	unsigned int url_port;
+	in_port_t url_port;
 
 	const char *socket_path;
 	const char *session_id;
--- a/src/lib-lda/lmtp-client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-lda/lmtp-client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -44,7 +44,7 @@
 	struct lmtp_client_settings set;
 	const char *host;
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 	enum lmtp_client_protocol protocol;
 	enum lmtp_input_state input_state;
 	const char *global_fail_string;
@@ -713,7 +713,7 @@
 
 int lmtp_client_connect_tcp(struct lmtp_client *client,
 			    enum lmtp_client_protocol protocol,
-			    const char *host, unsigned int port)
+			    const char *host, in_port_t port)
 {
 	struct dns_lookup_settings dns_lookup_set;
 	struct ip_addr *ips;
--- a/src/lib-lda/lmtp-client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-lda/lmtp-client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -42,7 +42,7 @@
 	/* if remote server supports XCLIENT capability,
 	   send the these as ADDR/PORT/TTL/TIMEOUT */
 	struct ip_addr source_ip;
-	unsigned int source_port;
+	in_port_t source_port;
 	/* send TTL as this (default 0 means "don't send it") */
 	unsigned int proxy_ttl;
 	/* remote is notified that the connection is going to be closed after
@@ -69,7 +69,7 @@
 
 int lmtp_client_connect_tcp(struct lmtp_client *client,
 			    enum lmtp_client_protocol protocol,
-			    const char *host, unsigned int port);
+			    const char *host, in_port_t port);
 void lmtp_client_close(struct lmtp_client *client);
 
 /* Add headers from given string before the rest of the data. The string must
--- a/src/lib-lda/smtp-client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-lda/smtp-client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -256,14 +256,13 @@
 	struct ioloop *ioloop;
 	struct istream *input;
 	const char *host, *p, *const *destp;
-	unsigned int port = DEFAULT_SUBMISSION_PORT;
+	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 (str_to_uint(p + 1, &port) < 0 ||
-		    port == 0 || port > 65535) {
+		if (net_str2port(p + 1, &port) < 0) {
 			*error_r = t_strdup_printf(
 				"Invalid port in submission_host: %s", p+1);
 			return -1;
--- a/src/lib-master/master-service-haproxy.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-master/master-service-haproxy.c	Sat Aug 29 14:26:30 2015 +0300
@@ -124,7 +124,7 @@
 	struct ip_addr *real_remote_ip = &hpconn->conn.remote_ip;
 	int fd = hpconn->conn.fd;
 	struct ip_addr local_ip, remote_ip;
-	unsigned int local_port, remote_port;
+	in_port_t local_port, remote_port;
 	size_t size;
 	ssize_t ret;
 
@@ -331,8 +331,7 @@
 					"(rip=%s)", net_ip2addr(real_remote_ip));
 				return -1;
 			}
-			if (str_to_uint(*fields, &remote_port) < 0 ||
-				remote_port > 65535) {
+			if (net_str2port(*fields, &remote_port) < 0) {
 				i_error("haproxy(v1): Client disconnected: "
 					"Proxied remote port is invalid "
 					"(port=`%s', rip=%s)", str_sanitize(*fields, 64),
@@ -348,8 +347,7 @@
 					"(rip=%s)", net_ip2addr(real_remote_ip));
 				return -1;
 			}
-			if (str_to_uint(*fields, &local_port) < 0 ||
-				local_port > 65535) {
+			if (net_str2port(*fields, &local_port) < 0) {
 				i_error("haproxy(v1): Client disconnected: "
 					"Proxied local port is invalid "
 					"(port=`%s', rip=%s)", str_sanitize(*fields, 64),
--- a/src/lib-master/master-service.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-master/master-service.h	Sat Aug 29 14:26:30 2015 +0300
@@ -41,10 +41,10 @@
 	const char *name;
 
 	struct ip_addr remote_ip, local_ip;
-	unsigned int remote_port, local_port;
+	in_port_t remote_port, local_port;
 
 	struct ip_addr real_remote_ip, real_local_ip;
-	unsigned int real_remote_port, real_local_port;
+	in_port_t real_remote_port, real_local_port;
 
 	unsigned int fifo:1;
 	unsigned int ssl:1;
--- a/src/lib-master/service-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-master/service-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef SERVICE_SETTINGS_H
 #define SERVICE_SETTINGS_H
 
+#include "net.h"
+
 /* <settings checks> */
 enum service_user_default {
 	SERVICE_USER_DEFAULT_NONE = 0,
@@ -29,7 +31,7 @@
 struct inet_listener_settings {
 	const char *name;
 	const char *address;
-	unsigned int port;
+	in_port_t port;
 	bool ssl;
 	bool reuse_port;
 	bool haproxy;
--- a/src/lib-settings/settings-parser.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-settings/settings-parser.c	Sat Aug 29 14:26:30 2015 +0300
@@ -579,6 +579,18 @@
 }
 
 static int
+get_in_port(struct setting_parser_context *ctx, const char *value,
+	 in_port_t *result_r)
+{
+	if (net_str2port(value, result_r) < 0) {
+		ctx->error = p_strdup_printf(ctx->parser_pool,
+			"Invalid port number %s", value);
+		return -1;
+	}
+	return 0;
+}
+
+static int
 settings_parse(struct setting_parser_context *ctx, struct setting_link *link,
 	       const struct setting_define *def,
 	       const char *key, const char *value)
@@ -626,6 +638,10 @@
 			return -1;
 		}
 		break;
+	case SET_IN_PORT:
+		if (get_in_port(ctx, value, (in_port_t *)ptr) < 0)
+			return -1;
+		break;
 	case SET_STR:
 		*((char **)ptr) = p_strdup(ctx->set_pool, value);
 		break;
@@ -1227,6 +1243,7 @@
 		case SET_UINT_OCT:
 		case SET_TIME:
 		case SET_SIZE:
+		case SET_IN_PORT:
 		case SET_STR:
 		case SET_ENUM:
 		case SET_STRLIST:
@@ -1322,6 +1339,7 @@
 		case SET_UINT_OCT:
 		case SET_TIME:
 		case SET_SIZE:
+		case SET_IN_PORT:
 		case SET_STR:
 		case SET_ENUM:
 		case SET_STRLIST:
@@ -1406,6 +1424,13 @@
 		*dest_size = *src_size;
 		break;
 	}
+	case SET_IN_PORT: {
+		const in_port_t *src_size = src;
+		in_port_t *dest_size = dest;
+
+		*dest_size = *src_size;
+		break;
+	}
 	case SET_STR_VARS:
 	case SET_STR:
 	case SET_ENUM: {
@@ -1519,6 +1544,7 @@
 		case SET_UINT_OCT:
 		case SET_TIME:
 		case SET_SIZE:
+		case SET_IN_PORT:
 		case SET_STR_VARS:
 		case SET_STR:
 		case SET_ENUM:
--- a/src/lib-settings/settings-parser.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-settings/settings-parser.h	Sat Aug 29 14:26:30 2015 +0300
@@ -23,6 +23,7 @@
 	SET_UINT_OCT,
 	SET_TIME,
 	SET_SIZE,
+	SET_IN_PORT, /* internet port */
 	SET_STR,
 	SET_STR_VARS, /* string with %variables */
 	SET_ENUM,
@@ -64,6 +65,10 @@
 	{ SET_STR + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
 		((struct struct_name *)0)->name, const char *), \
 	  #name, offsetof(struct struct_name, name), NULL }
+#define SETTING_DEFINE_STRUCT_IN_PORT(name, struct_name) \
+	{ SET_IN_PORT + COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE( \
+		((struct struct_name *)0)->name, in_port_t), \
+	  #name, offsetof(struct struct_name, name), NULL }
 
 struct setting_parser_info {
 	const char *module_name;
--- a/src/lib-sql/driver-mysql.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-sql/driver-mysql.c	Sat Aug 29 14:26:30 2015 +0300
@@ -4,6 +4,7 @@
 #include "ioloop.h"
 #include "array.h"
 #include "str.h"
+#include "net.h"
 #include "sql-api-private.h"
 
 #ifdef BUILD_MYSQL
@@ -30,7 +31,8 @@
 	const char *ssl_cert, *ssl_key, *ssl_ca, *ssl_ca_path, *ssl_cipher;
 	int ssl_verify_server_cert;
 	const char *option_file, *option_group;
-	unsigned int port, client_flags;
+	in_port_t port;
+	unsigned int client_flags;
 	time_t last_success;
 
 	MYSQL *mysql;
@@ -179,9 +181,10 @@
 			field = &db->password;
 		else if (strcmp(name, "dbname") == 0)
 			field = &db->dbname;
-		else if (strcmp(name, "port") == 0)
-			db->port = atoi(value);
-		else if (strcmp(name, "client_flags") == 0)
+		else if (strcmp(name, "port") == 0) {
+			if (net_str2port(value, &db->port) < 0)
+				i_fatal("mysql: Invalid port number: %s", value);
+		} else if (strcmp(name, "client_flags") == 0)
 			db->client_flags = atoi(value);
 		else if (strcmp(name, "ssl_cert") == 0)
 			field = &db->ssl_cert;
--- a/src/lib-storage/index/imapc/imapc-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/index/imapc/imapc-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -15,7 +15,7 @@
 
 static const struct setting_define imapc_setting_defines[] = {
 	DEF(SET_STR, imapc_host),
-	DEF(SET_UINT, imapc_port),
+	DEF(SET_IN_PORT, imapc_port),
 
 	DEF(SET_STR_VARS, imapc_user),
 	DEF(SET_STR_VARS, imapc_master_user),
@@ -144,10 +144,6 @@
 {
 	struct imapc_settings *set = _set;
 
-	if (set->imapc_port == 0 || set->imapc_port > 65535) {
-		*error_r = "invalid imapc_port";
-		return FALSE;
-	}
 	if (set->imapc_max_idle_time == 0) {
 		*error_r = "imapc_max_idle_time must not be 0";
 		return FALSE;
--- a/src/lib-storage/index/imapc/imapc-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/index/imapc/imapc-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef IMAPC_SETTINGS_H
 #define IMAPC_SETTINGS_H
 
+#include "net.h"
+
 /* <settings checks> */
 enum imapc_features {
 	IMAPC_FEATURE_RFC822_SIZE		= 0x01,
@@ -14,7 +16,7 @@
 
 struct imapc_settings {
 	const char *imapc_host;
-	unsigned int imapc_port;
+	in_port_t imapc_port;
 
 	const char *imapc_user;
 	const char *imapc_master_user;
--- a/src/lib-storage/index/pop3c/pop3c-client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,6 +1,8 @@
 #ifndef POP3C_CLIENT_H
 #define POP3C_CLIENT_H
 
+#include "net.h"
+
 enum pop3c_capability {
 	POP3C_CAPABILITY_PIPELINING	= 0x01,
 	POP3C_CAPABILITY_TOP		= 0x02,
@@ -21,7 +23,7 @@
 
 struct pop3c_client_settings {
 	const char *host;
-	unsigned int port;
+	in_port_t port;
 
 	const char *master_user;
 	const char *username;
--- a/src/lib-storage/index/pop3c/pop3c-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -11,11 +11,9 @@
 #define DEF(type, name) \
 	{ type, #name, offsetof(struct pop3c_settings, name), NULL }
 
-static bool pop3c_settings_check(void *_set, pool_t pool, const char **error_r);
-
 static const struct setting_define pop3c_setting_defines[] = {
 	DEF(SET_STR, pop3c_host),
-	DEF(SET_UINT, pop3c_port),
+	DEF(SET_IN_PORT, pop3c_port),
 
 	DEF(SET_STR_VARS, pop3c_user),
 	DEF(SET_STR_VARS, pop3c_master_user),
@@ -55,24 +53,9 @@
 
 	.parent_offset = (size_t)-1,
 	.parent = &mail_user_setting_parser_info,
-
-	.check_func = pop3c_settings_check
 };
 
 const struct setting_parser_info *pop3c_get_setting_parser_info(void)
 {
 	return &pop3c_setting_parser_info;
 }
-
-/* <settings checks> */
-static bool pop3c_settings_check(void *_set, pool_t pool ATTR_UNUSED,
-				 const char **error_r)
-{
-	struct pop3c_settings *set = _set;
-
-	if (set->pop3c_port == 0 || set->pop3c_port > 65535) {
-		*error_r = "invalid pop3c_port";
-		return FALSE;
-	}
-	return TRUE;
-}
--- a/src/lib-storage/index/pop3c/pop3c-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/index/pop3c/pop3c-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -1,9 +1,11 @@
 #ifndef POP3C_SETTINGS_H
 #define POP3C_SETTINGS_H
 
+#include "net.h"
+
 struct pop3c_settings {
 	const char *pop3c_host;
-	unsigned int pop3c_port;
+	in_port_t pop3c_port;
 
 	const char *pop3c_user;
 	const char *pop3c_master_user;
--- a/src/lib-storage/mail-storage-service.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib-storage/mail-storage-service.h	Sat Aug 29 14:26:30 2015 +0300
@@ -42,7 +42,7 @@
 	const char *username;
 	const char *session_id;
 	struct ip_addr local_ip, remote_ip;
-	unsigned int local_port, remote_port;
+	in_port_t local_port, remote_port;
 
 	const char *const *userdb_fields;
 
--- a/src/lib/connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -183,7 +183,7 @@
 
 void connection_init_client_ip(struct connection_list *list,
 			       struct connection *conn,
-			       const struct ip_addr *ip, unsigned int port)
+			       const struct ip_addr *ip, in_port_t port)
 {
 	i_assert(list->set.client);
 
--- a/src/lib/connection.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/connection.h	Sat Aug 29 14:26:30 2015 +0300
@@ -83,7 +83,7 @@
 
 	/* for IP client: */
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	/* received minor version */
 	unsigned int minor_version;
@@ -106,7 +106,7 @@
 			    int fd_in, int fd_out);
 void connection_init_client_ip(struct connection_list *list,
 			       struct connection *conn,
-			       const struct ip_addr *ip, unsigned int port);
+			       const struct ip_addr *ip, in_port_t port);
 void connection_init_client_unix(struct connection_list *list,
 				 struct connection *conn, const char *path);
 void connection_init_from_streams(struct connection_list *list,
--- a/src/lib/fd-close-on-exec.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/fd-close-on-exec.c	Sat Aug 29 14:26:30 2015 +0300
@@ -25,7 +25,7 @@
 void fd_debug_verify_leaks(int first_fd, int last_fd)
 {
 	struct ip_addr addr, raddr;
-	unsigned int port, rport;
+	in_port_t port, rport;
 	struct stat st;
 	int old_errno;
 
--- a/src/lib/iostream-rawlog.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/iostream-rawlog.c	Sat Aug 29 14:26:30 2015 +0300
@@ -167,7 +167,8 @@
 {
 	const char *p, *host;
 	struct ip_addr *ips;
-	unsigned int port, ips_count;
+	unsigned int ips_count;
+	in_port_t port;
 	int ret, fd;
 
 	/* tcp:host:port */
@@ -179,7 +180,7 @@
 		return 0;
 	if ((p = strchr(path, ':')) == NULL)
 		return 0;
-	if (str_to_uint(p+1, &port) < 0 || port == 0 || port > 65535)
+	if (net_str2port(p+1, &port) < 0)
 		return 0;
 	host = t_strdup_until(path, p);
 
--- a/src/lib/net.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/net.c	Sat Aug 29 14:26:30 2015 +0300
@@ -130,17 +130,17 @@
 		memset(&ip->u, 0, sizeof(ip->u));
 }
 
-static inline void sin_set_port(union sockaddr_union *so, unsigned int port)
+static inline void sin_set_port(union sockaddr_union *so, in_port_t port)
 {
 #ifdef HAVE_IPV6
 	if (so->sin.sin_family == AF_INET6)
-                so->sin6.sin6_port = htons((unsigned short) port);
+                so->sin6.sin6_port = htons(port);
 	else
 #endif
-		so->sin.sin_port = htons((unsigned short) port);
+		so->sin.sin_port = htons(port);
 }
 
-static inline unsigned int sin_get_port(union sockaddr_union *so)
+static inline in_port_t sin_get_port(union sockaddr_union *so)
 {
 #ifdef HAVE_IPV6
 	if (so->sin.sin_family == AF_INET6)
@@ -154,11 +154,11 @@
 
 #ifdef __FreeBSD__
 static int
-net_connect_ip_full_freebsd(const struct ip_addr *ip, unsigned int port,
+net_connect_ip_full_freebsd(const struct ip_addr *ip, in_port_t port,
 			    const struct ip_addr *my_ip, int sock_type,
 			    bool blocking);
 
-static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
+static int net_connect_ip_full(const struct ip_addr *ip, in_port_t port,
 			       const struct ip_addr *my_ip, int sock_type,
 			       bool blocking)
 {
@@ -183,7 +183,7 @@
 #define net_connect_ip_full net_connect_ip_full_freebsd
 #endif
 
-static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
+static int net_connect_ip_full(const struct ip_addr *ip, in_port_t port,
 			       const struct ip_addr *my_ip, int sock_type, bool blocking)
 {
 	union sockaddr_union so;
@@ -242,19 +242,19 @@
 #  undef net_connect_ip_full
 #endif
 
-int net_connect_ip(const struct ip_addr *ip, unsigned int port,
+int net_connect_ip(const struct ip_addr *ip, in_port_t port,
 		   const struct ip_addr *my_ip)
 {
 	return net_connect_ip_full(ip, port, my_ip, SOCK_STREAM, FALSE);
 }
 
-int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
+int net_connect_ip_blocking(const struct ip_addr *ip, in_port_t port,
 			    const struct ip_addr *my_ip)
 {
 	return net_connect_ip_full(ip, port, my_ip, SOCK_STREAM, TRUE);
 }
 
-int net_connect_udp(const struct ip_addr *ip, unsigned int port,
+int net_connect_udp(const struct ip_addr *ip, in_port_t port,
 			       const struct ip_addr *my_ip)
 {
 	return net_connect_ip_full(ip, port, my_ip, SOCK_DGRAM, FALSE);
@@ -391,14 +391,14 @@
 #endif
 }
 
-int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog)
+int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog)
 {
 	enum net_listen_flags flags = 0;
 
 	return net_listen_full(my_ip, port, &flags, backlog);
 }
 
-int net_listen_full(const struct ip_addr *my_ip, unsigned int *port,
+int net_listen_full(const struct ip_addr *my_ip, in_port_t *port,
 		    enum net_listen_flags *flags, int backlog)
 {
 	union sockaddr_union so;
@@ -551,7 +551,7 @@
 	return fd;
 }
 
-int net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r)
+int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r)
 {
 	union sockaddr_union so;
 	int ret;
@@ -706,7 +706,7 @@
 	return 0;
 }
 
-int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
+int net_getsockname(int fd, struct ip_addr *addr, in_port_t *port)
 {
 	union sockaddr_union so;
 	socklen_t addrlen;
@@ -727,7 +727,7 @@
 	return 0;
 }
 
-int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
+int net_getpeername(int fd, struct ip_addr *addr, in_port_t *port)
 {
 	union sockaddr_union so;
 	socklen_t addrlen;
@@ -944,6 +944,19 @@
 	return 0;
 }
 
+int net_str2port(const char *str, in_port_t *port_r)
+{
+	uintmax_t l;
+
+	if (str_to_uintmax(str, &l) < 0)
+		return -1;
+
+	if (l == 0 || l > (in_port_t)-1)
+		return -1;
+	*port_r = (in_port_t)l;
+	return 0;
+}
+
 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
 				 struct ip_addr *dest)
 {
@@ -1014,7 +1027,7 @@
 #endif
 }
 
-const char *net_getservbyport(unsigned short port)
+const char *net_getservbyport(in_port_t port)
 {
 	struct servent *entry;
 
--- a/src/lib/net.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/net.h	Sat Aug 29 14:26:30 2015 +0300
@@ -69,13 +69,13 @@
 
 /* Connect to TCP socket with ip address. The socket and connect() is
    non-blocking. */
-int net_connect_ip(const struct ip_addr *ip, unsigned int port,
+int net_connect_ip(const struct ip_addr *ip, in_port_t port,
 		   const struct ip_addr *my_ip) ATTR_NULL(3);
 /* Like net_connect_ip(), but do a blocking connect(). */
-int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
+int net_connect_ip_blocking(const struct ip_addr *ip, in_port_t port,
 			    const struct ip_addr *my_ip) ATTR_NULL(3);
 /* Like net_connect_ip(), but open a UDP socket. */ 
-int net_connect_udp(const struct ip_addr *ip, unsigned int port,
+int net_connect_udp(const struct ip_addr *ip, in_port_t port,
 		    const struct ip_addr *my_ip);
 /* Returns 0 if we can bind() as given IP, -1 if not. */
 int net_try_bind(const struct ip_addr *ip);
@@ -99,8 +99,8 @@
 void net_get_ip_any6(struct ip_addr *ip);
 
 /* Listen for connections on a socket */
-int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
-int net_listen_full(const struct ip_addr *my_ip, unsigned int *port,
+int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog);
+int net_listen_full(const struct ip_addr *my_ip, in_port_t *port,
 		    enum net_listen_flags *flags, int backlog);
 /* Listen for connections on an UNIX socket */
 int net_listen_unix(const char *path, int backlog);
@@ -110,7 +110,7 @@
 int net_listen_unix_unlink_stale(const char *path, int backlog);
 /* Accept a connection on a socket. Returns -1 if the connection got closed,
    -2 for other failures. For UNIX sockets addr_r->family=port=0. */
-int net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r)
+int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r)
 	ATTR_NULL(2, 3);
 
 /* Read data from socket, return number of bytes read,
@@ -133,10 +133,10 @@
 int net_hosterror_notfound(int error) ATTR_CONST;
 
 /* Get socket local address/port. For UNIX sockets addr->family=port=0. */
-int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
+int net_getsockname(int fd, struct ip_addr *addr, in_port_t *port)
 	ATTR_NULL(2, 3);
 /* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
-int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
+int net_getpeername(int fd, struct ip_addr *addr, in_port_t *port)
 	ATTR_NULL(2, 3);
 /* Get UNIX socket name. */
 int net_getunixname(int fd, const char **name_r);
@@ -148,6 +148,9 @@
 const char *net_ip2addr(const struct ip_addr *ip);
 /* char* -> struct ip_addr translation. */
 int net_addr2ip(const char *addr, struct ip_addr *ip);
+/* char* -> net_port_t translation */
+int net_str2port(const char *str, in_port_t *port_r);
+
 /* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
    successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
@@ -157,7 +160,7 @@
 int net_geterror(int fd);
 
 /* Get name of TCP service */
-const char *net_getservbyport(unsigned short port) ATTR_CONST;
+const char *net_getservbyport(in_port_t port) ATTR_CONST;
 
 bool is_ipv4_address(const char *addr) ATTR_PURE;
 bool is_ipv6_address(const char *addr) ATTR_PURE;
--- a/src/lib/uri-util.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lib/uri-util.c	Sat Aug 29 14:26:30 2015 +0300
@@ -482,32 +482,30 @@
 uri_parse_port(struct uri_parser *parser,
 	struct uri_authority *auth) ATTR_NULL(2)
 {
-	unsigned long port = 0;
-	int count = 0;
+	const unsigned char *first;
+	in_port_t port;
 
 	/* RFC 3986:
 	 *
 	 * port        = *DIGIT
 	 */
 
-	while (parser->cur < parser->end && i_isdigit(*parser->cur)) {
-		port = port * 10 + (parser->cur[0] - '0');
-		if (port > (in_port_t)-1) {
-			parser->error = "Port number is too high";
-			return -1;
-		}
+	first = parser->cur;
+	while (parser->cur < parser->end && i_isdigit(*parser->cur))
 		parser->cur++;
-		count++;
+
+	if (parser->cur == first)
+		return 0;
+	if (net_str2port(t_strdup_until(first, parser->cur), &port) < 0) {
+		parser->error = "Invalid port number";
+		return -1;
 	}
 
-	if (count > 0) {
-		if (auth != NULL) {
-			auth->port = port;
-			auth->have_port = TRUE;
-		}
-		return 1;
+	if (auth != NULL) {
+		auth->port = port;
+		auth->have_port = TRUE;
 	}
-	return 0;
+	return 1;
 }
 
 int uri_parse_authority(struct uri_parser *parser,
--- a/src/lmtp/client.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lmtp/client.h	Sat Aug 29 14:26:30 2015 +0300
@@ -66,7 +66,7 @@
 	time_t last_input;
 
 	struct ip_addr remote_ip, local_ip;
-	unsigned int remote_port, local_port;
+	in_port_t remote_port, local_port;
 
 	struct mail_user *raw_mail_user;
 	const char *my_domain;
--- a/src/lmtp/commands.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lmtp/commands.c	Sat Aug 29 14:26:30 2015 +0300
@@ -240,7 +240,10 @@
 		else if (strcmp(key, "host") == 0)
 			set->host = value;
 		else if (strcmp(key, "port") == 0) {
-			set->port = atoi(value);
+			if (net_str2port(value, &set->port) < 0) {
+				i_error("proxy: Invalid port number %s", value);
+				return FALSE;
+			}
 			port_set = TRUE;
 		} else if (strcmp(key, "proxy_timeout") == 0)
 			set->timeout_msecs = atoi(value)*1000;
@@ -1269,7 +1272,8 @@
 {
 	const char *const *tmp;
 	struct ip_addr remote_ip;
-	unsigned int remote_port = 0, ttl = UINT_MAX, timeout_secs = 0;
+	in_port_t remote_port = 0;
+	unsigned int ttl = UINT_MAX, timeout_secs = 0;
 	bool args_ok = TRUE;
 
 	if (!client_is_trusted(client)) {
@@ -1282,8 +1286,7 @@
 			if (net_addr2ip(*tmp + 5, &remote_ip) < 0)
 				args_ok = FALSE;
 		} else if (strncasecmp(*tmp, "PORT=", 5) == 0) {
-			if (str_to_uint(*tmp + 5, &remote_port) < 0 ||
-			    remote_port == 0 || remote_port > 65535)
+			if (net_str2port(*tmp + 5, &remote_port) < 0)
 				args_ok = FALSE;
 		} else if (strncasecmp(*tmp, "TTL=", 4) == 0) {
 			if (str_to_uint(*tmp + 4, &ttl) < 0)
--- a/src/lmtp/lmtp-proxy.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/lmtp/lmtp-proxy.h	Sat Aug 29 14:26:30 2015 +0300
@@ -13,13 +13,13 @@
 
 	/* the original client's IP/port that connected to the proxy */
 	struct ip_addr source_ip;
-	unsigned int source_port;
+	in_port_t source_port;
 	unsigned int proxy_ttl;
 };
 
 struct lmtp_proxy_rcpt_settings {
 	const char *host;
-	unsigned int port;
+	in_port_t port;
 	unsigned int timeout_msecs;
 	enum lmtp_client_protocol protocol;
 	struct lmtp_recipient_params params;
--- a/src/login-common/client-common-auth.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/client-common-auth.c	Sat Aug 29 14:26:30 2015 +0300
@@ -98,9 +98,12 @@
 			reply_r->hostip = value;
 		else if (strcmp(key, "source_ip") == 0)
 			reply_r->source_ip = value;
-		else if (strcmp(key, "port") == 0)
-			reply_r->port = atoi(value);
-		else if (strcmp(key, "destuser") == 0)
+		else if (strcmp(key, "port") == 0) {
+			if (net_str2port(value, &reply_r->port) < 0) {
+				i_error("Auth service returned invalid "
+					"port number: %s", value);
+			}
+		} else if (strcmp(key, "destuser") == 0)
 			reply_r->destuser = value;
 		else if (strcmp(key, "pass") == 0)
 			reply_r->password = value;
--- a/src/login-common/client-common.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/client-common.h	Sat Aug 29 14:26:30 2015 +0300
@@ -60,7 +60,7 @@
 	/* for proxying */
 	const char *host, *hostip, *source_ip;
 	const char *destuser, *password, *proxy_mech;
-	unsigned int port;
+	in_port_t port;
 	unsigned int proxy_timeout_msecs;
 	unsigned int proxy_refresh_secs;
 	enum login_proxy_ssl_flags ssl_flags;
@@ -108,8 +108,8 @@
 	struct ip_addr local_ip;
 	struct ip_addr ip;
 	struct ip_addr real_remote_ip, real_local_ip;
-	unsigned int local_port, remote_port;
-	unsigned int real_local_port, real_remote_port;
+	in_port_t local_port, remote_port;
+	in_port_t real_local_port, real_remote_port;
 	struct ssl_proxy *ssl_proxy;
 	const struct login_settings *set;
 	const struct master_service_ssl_settings *ssl_set;
--- a/src/login-common/login-common.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/login-common.h	Sat Aug 29 14:26:30 2015 +0300
@@ -2,6 +2,7 @@
 #define LOGIN_COMMON_H
 
 #include "lib.h"
+#include "net.h"
 #include "login-settings.h"
 
 /* Used only for string sanitization */
@@ -22,9 +23,9 @@
 	const char *process_name;
 
 	/* e.g. 143, 110 */
-	unsigned int default_port;
+	in_port_t default_port;
 	/* e.g. 993, 995. if there is no ssl port, use 0. */
-	unsigned int default_ssl_port;
+	in_port_t default_ssl_port;
 
 	/* if value is NULL, LOGIN_DEFAULT_SOCKET is used as the default */
 	const char *default_login_socket;
--- a/src/login-common/login-proxy-state.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/login-proxy-state.c	Sat Aug 29 14:26:30 2015 +0300
@@ -79,7 +79,7 @@
 
 struct login_proxy_record *
 login_proxy_state_get(struct login_proxy_state *state,
-		      const struct ip_addr *ip, unsigned int port)
+		      const struct ip_addr *ip, in_port_t port)
 {
 	struct login_proxy_record *rec, key;
 
--- a/src/login-common/login-proxy-state.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/login-proxy-state.h	Sat Aug 29 14:26:30 2015 +0300
@@ -5,7 +5,7 @@
 
 struct login_proxy_record {
 	struct ip_addr ip;
-	unsigned int port;
+	in_port_t port;
 
 	/* these are tracking connect()s, not necessarily logins: */
 	unsigned int num_waiting_connections;
@@ -18,7 +18,7 @@
 
 struct login_proxy_record *
 login_proxy_state_get(struct login_proxy_state *state,
-		      const struct ip_addr *ip, unsigned int port);
+		      const struct ip_addr *ip, in_port_t port);
 
 void login_proxy_state_notify(struct login_proxy_state *state,
 			      const char *user);
--- a/src/login-common/login-proxy.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/login-proxy.c	Sat Aug 29 14:26:30 2015 +0300
@@ -42,7 +42,7 @@
 
 	struct ip_addr ip, source_ip;
 	char *host;
-	unsigned int port;
+	in_port_t port;
 	unsigned int connect_timeout_msecs;
 	unsigned int notify_refresh_secs;
 	unsigned int reconnect_count;
@@ -215,7 +215,7 @@
 {
 	string_t *str = t_str_new(128);
 	struct ip_addr local_ip;
-	unsigned int local_port;
+	in_port_t local_port;
 
 	str_printfa(str, "proxy(%s): ", proxy->client->virtual_user);
 	if (!proxy->connected) {
@@ -472,7 +472,7 @@
 }
 
 bool login_proxy_is_ourself(const struct client *client, const char *host,
-			    unsigned int port, const char *destuser)
+			    in_port_t port, const char *destuser)
 {
 	struct ip_addr ip;
 
@@ -502,7 +502,7 @@
 	return proxy->host;
 }
 
-unsigned int login_proxy_get_port(const struct login_proxy *proxy)
+in_port_t login_proxy_get_port(const struct login_proxy *proxy)
 {
 	return proxy->port;
 }
--- a/src/login-common/login-proxy.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/login-common/login-proxy.h	Sat Aug 29 14:26:30 2015 +0300
@@ -25,7 +25,7 @@
 struct login_proxy_settings {
 	const char *host;
 	struct ip_addr ip, source_ip;
-	unsigned int port;
+	in_port_t port;
 	unsigned int connect_timeout_msecs;
 	/* send a notification about proxy connection to proxy-notify pipe
 	   every n seconds */
@@ -47,7 +47,7 @@
 /* Return TRUE if host/port/destuser combination points to same as current
    connection. */
 bool login_proxy_is_ourself(const struct client *client, const char *host,
-			    unsigned int port, const char *destuser);
+			    in_port_t port, const char *destuser);
 
 /* Detach proxy from client. This is done after the authentication is
    successful and all that is left is the dummy proxying. */
@@ -60,7 +60,7 @@
 struct ostream *login_proxy_get_ostream(struct login_proxy *proxy);
 
 const char *login_proxy_get_host(const struct login_proxy *proxy) ATTR_PURE;
-unsigned int login_proxy_get_port(const struct login_proxy *proxy) ATTR_PURE;
+in_port_t login_proxy_get_port(const struct login_proxy *proxy) ATTR_PURE;
 enum login_proxy_ssl_flags
 login_proxy_get_ssl_flags(const struct login_proxy *proxy) ATTR_PURE;
 
--- a/src/master/master-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/master/master-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -62,7 +62,7 @@
 static const struct setting_define inet_listener_setting_defines[] = {
 	DEF(SET_STR, name),
 	DEF(SET_STR, address),
-	DEF(SET_UINT, port),
+	DEF(SET_IN_PORT, port),
 	DEF(SET_BOOL, ssl),
 	DEF(SET_BOOL, reuse_port),
 	DEF(SET_BOOL, haproxy),
@@ -320,7 +320,7 @@
 		struct inet_listener_settings *set = *sets;
 
 		if (set->port != 0) {
-			str = t_strdup_printf("%d:%s", set->port, set->address);
+			str = t_strdup_printf("%u:%s", set->port, set->address);
 			array_append(all_listeners, &str, 1);
 		}
 	}
--- a/src/master/service-listen.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/master/service-listen.c	Sat Aug 29 14:26:30 2015 +0300
@@ -159,7 +159,7 @@
 
 #ifdef HAVE_SYSTEMD
 static int
-systemd_listen_fd(const struct ip_addr *ip, unsigned int port, int *fd_r)
+systemd_listen_fd(const struct ip_addr *ip, in_port_t port, int *fd_r)
 {
 	static int sd_fds = -1;
 	int fd, fd_max;
@@ -191,7 +191,7 @@
         struct service *service = l->service;
 	enum net_listen_flags flags = 0;
 	const struct inet_listener_settings *set = l->set.inetset.set;
-	unsigned int port = set->port;
+	in_port_t port = set->port;
 	int fd;
 
 #ifdef HAVE_SYSTEMD
@@ -251,7 +251,7 @@
 }
 
 #ifdef HAVE_SYSTEMD
-static int get_socket_info(int fd, unsigned int *family, unsigned int *port)
+static int get_socket_info(int fd, unsigned int *family, in_port_t *port)
 {
 	union sockaddr_union {
 		struct sockaddr sa;
@@ -260,6 +260,7 @@
 	} sockaddr;
 	socklen_t l;
 
+	// FIXME(Stephan): why -1?
 	if (port) *port = -1;
 	if (family) *family = -1;
 
@@ -304,7 +305,8 @@
 	for (fd = SD_LISTEN_FDS_START; fd <= fd_max; fd++) {
 		if (sd_is_socket_inet(fd, 0, SOCK_STREAM, 1, 0) > 0) {
 			int found = FALSE;
-			unsigned int port, family;
+			in_port_t port;
+			unsigned int family;
 			get_socket_info(fd, &family, &port);
 			
 			array_foreach(&service_list->services, services) {
--- a/src/master/service.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/master/service.c	Sat Aug 29 14:26:30 2015 +0300
@@ -6,6 +6,7 @@
 #include "aqueue.h"
 #include "hash.h"
 #include "str.h"
+#include "net.h"
 #include "master-service.h"
 #include "master-service-settings.h"
 #include "service.h"
@@ -114,8 +115,7 @@
 static struct service_listener *
 service_create_one_inet_listener(struct service *service,
 				 const struct inet_listener_settings *set,
-				 const char *address, const struct ip_addr *ip,
-				 const char **error_r)
+				 const char *address, const struct ip_addr *ip)
 {
 	struct service_listener *l;
 
@@ -130,11 +130,6 @@
 	l->inet_address = p_strdup(service->list->pool, address);
 	l->name = set->name;
 
-	if (set->port > 65535) {
-		*error_r = t_strdup_printf("Invalid port: %u", set->port);
-		return NULL;
-	}
-
 	return l;
 }
 
@@ -173,10 +168,7 @@
 
 		for (i = 0; i < ips_count; i++) {
 			l = service_create_one_inet_listener(service, set,
-							     address, &ips[i],
-							     error_r);
-			if (l == NULL)
-				return -1;
+							     address, &ips[i]);
 			array_append(&service->listeners, &l, 1);
 		}
 		service->have_inet_listeners = TRUE;
--- a/src/pop3-login/client.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/pop3-login/client.c	Sat Aug 29 14:26:30 2015 +0300
@@ -38,7 +38,7 @@
 static bool cmd_xclient(struct pop3_client *client, const char *args)
 {
 	const char *const *tmp;
-	unsigned int remote_port;
+	in_port_t remote_port;
 	bool args_ok = TRUE;
 
 	if (!client->common.trusted) {
@@ -51,8 +51,7 @@
 			if (net_addr2ip(*tmp + 5, &client->common.ip) < 0)
 				args_ok = FALSE;
 		} else if (strncasecmp(*tmp, "PORT=", 5) == 0) {
-			if (str_to_uint(*tmp + 5, &remote_port) < 0 ||
-			    remote_port == 0 || remote_port > 65535)
+			if (net_str2port(*tmp + 5, &remote_port) < 0)
 				args_ok = FALSE;
 			else
 				client->common.remote_port = remote_port;
--- a/src/replication/aggregator/aggregator-settings.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/replication/aggregator/aggregator-settings.c	Sat Aug 29 14:26:30 2015 +0300
@@ -61,7 +61,7 @@
 
 static const struct setting_define aggregator_setting_defines[] = {
 	DEF(SET_STR, replicator_host),
-	DEF(SET_UINT, replicator_port),
+	DEF(SET_IN_PORT, replicator_port),
 
 	SETTING_DEFINE_LIST_END
 };
--- a/src/replication/aggregator/aggregator-settings.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/replication/aggregator/aggregator-settings.h	Sat Aug 29 14:26:30 2015 +0300
@@ -3,7 +3,7 @@
 
 struct aggregator_settings {
 	const char *replicator_host;
-	unsigned int replicator_port;
+	in_port_t replicator_port;
 };
 
 extern const struct setting_parser_info aggregator_setting_parser_info;
--- a/src/replication/aggregator/replicator-connection.c	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/replication/aggregator/replicator-connection.c	Sat Aug 29 14:26:30 2015 +0300
@@ -19,7 +19,8 @@
 struct replicator_connection {
 	char *path;
 	struct ip_addr *ips;
-	unsigned int ips_count, ip_idx, port;
+	unsigned int ips_count, ip_idx;
+	in_port_t port;
 
 	int fd;
 	struct io *io;
@@ -229,7 +230,7 @@
 
 struct replicator_connection *
 replicator_connection_create_inet(const struct ip_addr *ips,
-				  unsigned int ips_count, unsigned int port,
+				  unsigned int ips_count, in_port_t port,
 				  replicator_sync_callback_t *callback)
 {
 	struct replicator_connection *conn;
--- a/src/replication/aggregator/replicator-connection.h	Sat Aug 29 14:21:27 2015 +0300
+++ b/src/replication/aggregator/replicator-connection.h	Sat Aug 29 14:26:30 2015 +0300
@@ -10,7 +10,7 @@
 				  replicator_sync_callback_t *callback);
 struct replicator_connection *
 replicator_connection_create_inet(const struct ip_addr *ips,
-				  unsigned int ips_count, unsigned int port,
+				  unsigned int ips_count, in_port_t port,
 				  replicator_sync_callback_t *callback);
 void replicator_connection_destroy(struct replicator_connection **conn);