8bc468
From b9ae48172fff77d41b5cf19d334ccbe002ac0686 Mon Sep 17 00:00:00 2001
8bc468
From: Adrian Reber <areber@redhat.com>
8bc468
Date: Tue, 7 Dec 2021 09:10:14 +0000
8bc468
Subject: [PATCH] util: make page-server IPv6 safe
8bc468
8bc468
The function run_tcp_server() was the last place CRIU was still using
8bc468
the IPv4 only function inet_ntoa(). It was only used during a print, so
8bc468
that it did not really break anything, but with this commit the output
8bc468
is now no longer:
8bc468
8bc468
 Accepted connection from 0.0.0.0:58396
8bc468
8bc468
but correctly displaying the IPv6 address
8bc468
8bc468
 Accepted connection from ::1:58398
8bc468
8bc468
if connecting via IPv6.
8bc468
8bc468
Signed-off-by: Adrian Reber <areber@redhat.com>
8bc468
---
8bc468
 criu/util.c | 15 +++++++++++----
8bc468
 1 file changed, 11 insertions(+), 4 deletions(-)
8bc468
8bc468
diff --git a/criu/util.c b/criu/util.c
8bc468
index 2917102fd..822822186 100644
8bc468
--- a/criu/util.c
8bc468
+++ b/criu/util.c
8bc468
@@ -1098,7 +1098,7 @@ out:
8bc468
 int run_tcp_server(bool daemon_mode, int *ask, int cfd, int sk)
8bc468
 {
8bc468
 	int ret;
8bc468
-	struct sockaddr_in caddr;
8bc468
+	struct sockaddr_storage caddr;
8bc468
 	socklen_t clen = sizeof(caddr);
8bc468
 
8bc468
 	if (daemon_mode) {
8bc468
@@ -1126,14 +1126,20 @@ int run_tcp_server(bool daemon_mode, int *ask, int cfd, int sk)
8bc468
 		return -1;
8bc468
 
8bc468
 	if (sk >= 0) {
8bc468
+		char port[6];
8bc468
+		char address[INET6_ADDRSTRLEN];
8bc468
 		*ask = accept(sk, (struct sockaddr *)&caddr, &clen);
8bc468
 		if (*ask < 0) {
8bc468
 			pr_perror("Can't accept connection to server");
8bc468
 			goto err;
8bc468
-		} else
8bc468
-			pr_info("Accepted connection from %s:%u\n",
8bc468
-					inet_ntoa(caddr.sin_addr),
8bc468
-					(int)ntohs(caddr.sin_port));
8bc468
+		}
8bc468
+		ret = getnameinfo((struct sockaddr *)&caddr, clen, address, sizeof(address), port, sizeof(port),
8bc468
+				  NI_NUMERICHOST | NI_NUMERICSERV);
8bc468
+		if (ret) {
8bc468
+			pr_err("Failed converting address: %s\n", gai_strerror(ret));
8bc468
+			goto err;
8bc468
+		}
8bc468
+		pr_info("Accepted connection from %s:%s\n", address, port);
8bc468
 		close(sk);
8bc468
 	}
8bc468
 
8bc468
-- 
8bc468
2.34.1
8bc468