Mercurial > dovecot > core-2.2
diff src/lib/network.c @ 11340:40964653930c HEAD
liblib: Added net_try_bind()
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 19 May 2010 18:24:23 +0200 |
parents | 260e190306b0 |
children | bdb46ba87203 |
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 {