]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
authorTimo Sirainen <tss@iki.fi>
Wed, 4 Aug 2010 14:06:05 +0000 (15:06 +0100)
committerTimo Sirainen <tss@iki.fi>
Wed, 4 Aug 2010 14:06:05 +0000 (15:06 +0100)
A lot of checks inside our code assumes that family is either AF_INET,
AF_INET6 or 0. struct ip_addr doesn't support anything else either, so having
AF_UNIX as family but without a way to get the socket name from the struct
isn't very helpful either.

src/lib/network.c
src/lib/network.h

index 33fc4334272b2d592e893bd665e5dc4ee1f1e7f1..64ed3f6622b85b0d814008fa71fa41d2e13bb570 100644 (file)
@@ -504,10 +504,13 @@ int net_accept(int fd, struct ip_addr *addr, unsigned int *port)
                else
                        return -2;
        }
-
-       if (addr != NULL) sin_get_ip(&so, addr);
-       if (port != NULL) *port = sin_get_port(&so);
-
+       if (so.sin.sin_family == AF_UNIX) {
+               if (addr != NULL) addr->family = 0;
+               if (port != NULL) *port = 0;
+       } else {
+               if (addr != NULL) sin_get_ip(&so, addr);
+               if (port != NULL) *port = sin_get_port(&so);
+       }
        return ret;
 }
 
@@ -630,10 +633,13 @@ int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
        addrlen = sizeof(so);
        if (getsockname(fd, &so.sa, &addrlen) == -1)
                return -1;
-
-        if (addr != NULL) sin_get_ip(&so, addr);
-       if (port != NULL) *port = sin_get_port(&so);
-
+       if (so.sin.sin_family == AF_UNIX) {
+               if (addr != NULL) addr->family = 0;
+               if (port != NULL) *port = 0;
+       } else {
+               if (addr != NULL) sin_get_ip(&so, addr);
+               if (port != NULL) *port = sin_get_port(&so);
+       }
        return 0;
 }
 
@@ -647,10 +653,13 @@ int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
        addrlen = sizeof(so);
        if (getpeername(fd, &so.sa, &addrlen) == -1)
                return -1;
-
-        if (addr != NULL) sin_get_ip(&so, addr);
-       if (port != NULL) *port = sin_get_port(&so);
-
+       if (so.sin.sin_family == AF_UNIX) {
+               if (addr != NULL) addr->family = 0;
+               if (port != NULL) *port = 0;
+       } else {
+               if (addr != NULL) sin_get_ip(&so, addr);
+               if (port != NULL) *port = sin_get_port(&so);
+       }
        return 0;
 }
 
index b0f2ac01f6faac21d027bbc77f9001c82a44116c..ade5d5ec4e92feaa26a27a10a211605b8adbe268 100644 (file)
@@ -84,7 +84,7 @@ int net_listen_unix(const char *path, int backlog);
    again. */
 int net_listen_unix_unlink_stale(const char *path, int backlog);
 /* Accept a connection on a socket. Returns -1 if the connection got closed,
-   -2 for other failures */
+   -2 for other failures. For UNIX sockets addr->family=port=0. */
 int net_accept(int fd, struct ip_addr *addr, unsigned int *port);
 
 /* Read data from socket, return number of bytes read,
@@ -103,9 +103,9 @@ const char *net_gethosterror(int error) ATTR_CONST;
    some error with name server) */
 int net_hosterror_notfound(int error) ATTR_CONST;
 
-/* Get socket local address/port */
+/* Get socket local address/port. For UNIX sockets addr->family=port=0. */
 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port);
-/* Get socket remote address/port */
+/* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port);
 /* Get UNIX socket name. */
 int net_getunixname(int fd, const char **name_r);