dovecot-2.0: liblib: Added net_try_bind()

dovecot at dovecot.org dovecot at dovecot.org
Wed May 19 19:24:42 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/40964653930c
changeset: 11340:40964653930c
user:      Timo Sirainen <tss at iki.fi>
date:      Wed May 19 18:24:23 2010 +0200
description:
liblib: Added net_try_bind()

diffstat:

 src/lib/network.c |  24 +++++++++++++++++++++++-
 src/lib/network.h |   2 ++
 2 files changed, 25 insertions(+), 1 deletions(-)

diffs (53 lines):

diff -r ed633bfb8c59 -r 40964653930c src/lib/network.c
--- a/src/lib/network.c	Wed May 19 18:14:53 2010 +0200
+++ b/src/lib/network.c	Wed May 19 18:24:23 2010 +0200
@@ -193,7 +193,6 @@
 	if (my_ip != NULL) {
 		sin_set_ip(&so, my_ip);
 		if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
-			/* failed, set it back to INADDR_ANY */
 			i_error("bind(%s) failed: %m", net_ip2addr(my_ip));
 			close_keep_errno(fd);
 			return -1;
@@ -218,6 +217,29 @@
 	return fd;
 }
 
+int net_try_bind(const struct ip_addr *ip)
+{
+	union sockaddr_union so;
+	int fd;
+
+	/* create the socket */
+	memset(&so, 0, sizeof(so));
+        so.sin.sin_family = ip->family;
+	fd = socket(ip->family, SOCK_STREAM, 0);
+	if (fd == -1) {
+		i_error("socket() failed: %m");
+		return -1;
+	}
+
+	sin_set_ip(&so, ip);
+	if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
+		close_keep_errno(fd);
+		return -1;
+	}
+	(void)close(fd);
+	return 0;
+}
+
 int net_connect_unix(const char *path)
 {
 	union {
diff -r ed633bfb8c59 -r 40964653930c src/lib/network.h
--- a/src/lib/network.h	Wed May 19 18:14:53 2010 +0200
+++ b/src/lib/network.h	Wed May 19 18:24:23 2010 +0200
@@ -49,6 +49,8 @@
 /* Connect to socket with ip address */
 int net_connect_ip(const struct ip_addr *ip, unsigned int 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);
 /* Connect to named UNIX socket */
 int net_connect_unix(const char *path);
 /* Try to connect to UNIX socket for give number of seconds when connect()


More information about the dovecot-cvs mailing list