Blob Blame History Raw
From 6eda08fbff4f911c5884876a33708cf17fa28a8b Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Thu, 29 Dec 2016 11:21:37 +0100
Subject: [PATCH] core: fix sockaddr length calculation for sockaddr_pretty()
 (#4966)

Let's simply store the socket address length in the SocketPeer object so
that we can use it when invoking sockaddr_pretty():

This fixes the issue described in #4943, but avoids calling
getpeername() twice.
(cherry picked from commit 41733ae1e0035c538505bc5be1ca5d67a80b4a82)
---
 src/core/socket.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/core/socket.c b/src/core/socket.c
index 0b1c4acfec..fe2020008f 100644
--- a/src/core/socket.c
+++ b/src/core/socket.c
@@ -64,6 +64,7 @@ struct SocketPeer {
 
         Socket *socket;
         union sockaddr_union peer;
+        socklen_t peer_salen;
 };
 
 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
@@ -490,8 +491,10 @@ static void peer_address_hash_func(const void *p, struct siphash *state) {
 
         if (s->peer.sa.sa_family == AF_INET)
                 siphash24_compress(&s->peer.in.sin_addr, sizeof(s->peer.in.sin_addr), state);
-        else
+        else if (s->peer.sa.sa_family == AF_INET6)
                 siphash24_compress(&s->peer.in6.sin6_addr, sizeof(s->peer.in6.sin6_addr), state);
+        else
+                assert_not_reached("Unknown address family.");
 }
 
 static int peer_address_compare_func(const void *a, const void *b) {
@@ -609,6 +612,7 @@ int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) {
                 return log_oom();
 
         remote->peer = sa.peer;
+        remote->peer_salen = salen;
 
         r = set_put(s->peers_by_address, remote);
         if (r < 0)
@@ -2196,7 +2200,7 @@ static void socket_enter_running(Socket *s, int cfd) {
                         } else if (r > 0 && p->n_ref > s->max_connections_per_source) {
                                 _cleanup_free_ char *t = NULL;
 
-                                sockaddr_pretty(&p->peer.sa, FAMILY_ADDRESS_SIZE(p->peer.sa.sa_family), true, false, &t);
+                                (void) sockaddr_pretty(&p->peer.sa, p->peer_salen, true, false, &t);
 
                                 log_unit_warning(UNIT(s),
                                                  "Too many incoming connections (%u) from source %s, dropping connection.",