Mercurial > dovecot > core-2.2
changeset 11340:40964653930c HEAD
liblib: Added net_try_bind()
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 19 May 2010 18:24:23 +0200 |
parents | ed633bfb8c59 |
children | 7d3d6ee01bfc |
files | src/lib/network.c src/lib/network.h |
diffstat | 2 files changed, 25 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- 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 {
--- 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()