]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: connection: Cleanup connection_update_properties()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 25 Nov 2019 13:16:09 +0000 (15:16 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Tue, 26 Nov 2019 09:56:35 +0000 (11:56 +0200)
Add comments and change the code flows a bit to make it clearer what is
supposed to happen.

Fixes also a potential bug where getpeername() could return 0, but if
errno wasn't changed the following errno==ENOTSOCK could have wrongly
matched.

src/lib/connection.c

index c532f4ade6a78ac2c3846ccbb9e98e467444413f..d1c222a4871453c013525f20ee62af029043e88a 100644 (file)
@@ -381,22 +381,27 @@ connection_update_properties(struct connection *conn)
        int fd = (conn->fd_in < 0 ? conn->fd_out : conn->fd_in);
        struct net_unix_cred cred;
 
-       if (conn->remote_ip.family != 0)
+       if (conn->remote_ip.family != 0) {
+               /* remote IP was already set */
                i_assert(conn->remote_port != 0);
-       else if (fd < 0) {
+       } else if (conn->unix_peer_known) {
+               /* already checked */
+       } else if (fd < 0) {
                /* not connected yet - wait */
-       } else if (conn->fd_in != conn->fd_out || fd < 0 ||
-                net_getpeername(fd, &conn->remote_ip,
-                                &conn->remote_port) < 0 ||
-                conn->remote_ip.family == 0) {
-               conn->remote_ip.family = 0;
-               conn->remote_port = 0;
-
-               if (conn->unix_peer_known) {
-                       /* already known */
-               } else if (fd < 0 || errno == ENOTSOCK ||
-                     net_getunixcred(fd, &cred) < 0) {
-               } else {
+       } else {
+               if (net_getpeername(fd, &conn->remote_ip,
+                                   &conn->remote_port) == 0) {
+                       /* either TCP or UNIX socket connection */
+                       errno = 0;
+               }
+
+               if (conn->remote_ip.family != 0) {
+                       /* TCP connection */
+                       i_assert(conn->remote_port != 0);
+               } else if (errno == ENOTSOCK) {
+                       /* getpeername() already found out this can't be a UNIX
+                          socket connection */
+               } else if (net_getunixcred(fd, &cred) == 0) {
                        conn->remote_pid = cred.pid;
                        conn->remote_uid = cred.uid;
                }