]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
liblib: Added net_connect_ip_blocking().
authorTimo Sirainen <tss@iki.fi>
Sun, 11 Jul 2010 23:32:30 +0000 (00:32 +0100)
committerTimo Sirainen <tss@iki.fi>
Sun, 11 Jul 2010 23:32:30 +0000 (00:32 +0100)
src/lib/network.c
src/lib/network.h

index af3da1e96f6d8b19e999cc4bab5c026462268e2e..abaa8c9bfc3ddb02dcbd5510707ddbe23d3cfb51 100644 (file)
@@ -163,8 +163,8 @@ int net_connect_ip(const struct ip_addr *ip, unsigned int port,
 static
 #endif
 
-int net_connect_ip(const struct ip_addr *ip, unsigned int port,
-                  const struct ip_addr *my_ip)
+static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
+                              const struct ip_addr *my_ip, bool blocking)
 {
        union sockaddr_union so;
        int fd, ret, opt = 1;
@@ -187,7 +187,8 @@ int net_connect_ip(const struct ip_addr *ip, unsigned int port,
        /* set socket options */
        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
        setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
-       net_set_nonblock(fd, TRUE);
+       if (!blocking)
+               net_set_nonblock(fd, TRUE);
 
        /* set our own address */
        if (my_ip != NULL) {
@@ -217,6 +218,18 @@ int net_connect_ip(const struct ip_addr *ip, unsigned int port,
        return fd;
 }
 
+int net_connect_ip(const struct ip_addr *ip, unsigned int port,
+                  const struct ip_addr *my_ip)
+{
+       return net_connect_ip_full(ip, port, my_ip, FALSE);
+}
+
+int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
+                           const struct ip_addr *my_ip)
+{
+       return net_connect_ip_full(ip, port, my_ip, TRUE);
+}
+
 int net_try_bind(const struct ip_addr *ip)
 {
        union sockaddr_union so;
index 6ddd9fde86923d0e6eaf9f67f04488500389d034..b0f2ac01f6faac21d027bbc77f9001c82a44116c 100644 (file)
@@ -47,9 +47,13 @@ bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
 int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
 unsigned int net_ip_hash(const struct ip_addr *ip);
 
-/* Connect to socket with ip address */
+/* Connect to socket with ip address. The socket and connect() is
+   non-blocking. */
 int net_connect_ip(const struct ip_addr *ip, unsigned int port,
                   const struct ip_addr *my_ip);
+/* Like net_connect_ip(), but do a blocking connect(). */
+int net_connect_ip_blocking(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 */