]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added support for connecting UDP sockets.
authorStephan Bosch <stephan@rename-it.nl>
Sat, 4 Oct 2014 14:48:03 +0000 (17:48 +0300)
committerStephan Bosch <stephan@rename-it.nl>
Sat, 4 Oct 2014 14:48:03 +0000 (17:48 +0300)
src/lib/net.c
src/lib/net.h

index 4ba71811db0afb26fe6d8252363f768fd51e0f24..08c469292d364226b954a504c527079c54fc8d37 100644 (file)
@@ -182,7 +182,7 @@ static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
 #endif
 
 static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
-                              const struct ip_addr *my_ip, bool blocking)
+                              const struct ip_addr *my_ip, int sock_type, bool blocking)
 {
        union sockaddr_union so;
        int fd, ret, opt = 1;
@@ -195,7 +195,7 @@ static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
        /* create the socket */
        memset(&so, 0, sizeof(so));
         so.sin.sin_family = ip->family;
-       fd = socket(ip->family, SOCK_STREAM, 0);
+       fd = socket(ip->family, sock_type, 0);
 
        if (fd == -1) {
                i_error("socket() failed: %m");
@@ -204,7 +204,8 @@ static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
 
        /* set socket options */
        (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
-       (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
+       if (sock_type == SOCK_STREAM)
+               (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
        if (!blocking)
                net_set_nonblock(fd, TRUE);
 
@@ -242,13 +243,19 @@ static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port,
 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);
+       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,
                            const struct ip_addr *my_ip)
 {
-       return net_connect_ip_full(ip, port, my_ip, TRUE);
+       return net_connect_ip_full(ip, port, my_ip, SOCK_STREAM, TRUE);
+}
+
+int net_connect_udp(const struct ip_addr *ip, unsigned int port,
+                              const struct ip_addr *my_ip)
+{
+       return net_connect_ip_full(ip, port, my_ip, SOCK_DGRAM, FALSE);
 }
 
 int net_try_bind(const struct ip_addr *ip)
index dd74af10641aa7f2ea2274a126ad7929d4c70d52..c64f7461d5310d4edb91e6f10f91623c00a5d9f1 100644 (file)
@@ -67,13 +67,16 @@ 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. The socket and connect() is
+/* 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,
                   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,
                            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,
+                   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 */