From: Timo Sirainen Date: Mon, 25 Nov 2019 13:16:09 +0000 (+0200) Subject: lib: connection: Cleanup connection_update_properties() X-Git-Tag: 2.3.9~9 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1c64f03b2299022a327dba91c9b6a3c30ab15493;p=thirdparty%2Fdovecot%2Fcore.git lib: connection: Cleanup connection_update_properties() 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. --- diff --git a/src/lib/connection.c b/src/lib/connection.c index c532f4ade6..d1c222a487 100644 --- a/src/lib/connection.c +++ b/src/lib/connection.c @@ -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; }