Index | Thread | Search

From:
"Omar Polo" <op@omarpolo.com>
Subject:
ifconfig wg: "humanify" last handshake
To:
tech@openbsd.org
Date:
Wed, 06 Aug 2025 16:34:24 +0200

Download raw body.

Thread
Hello tech,

tiny usability diff.  the rationale for it is that as time passes, the
hander is (to me) to make sense out of the number of seconds since the
last handshake, so optionally using a bigger time unit seems nice to me.

example:

# ifconfig wg0
wg0: flags=80c3<UP,BROADCAST,RUNNING,NOARP,MULTICAST> mtu 1420
        index 4 priority 0 llprio 3
        [...]
                tx: 150444, rx: 356648
                last handshake: 5626 seconds ago
                [...]

with diff below it becomes

# ./obj/ifconfig wg0
[...]
                last handshake: 93 minutes ago

(i think/hope noone parses ifconfig output to extract the raw number
of seconds in wg interfaces)


diff /usr/src
path + /usr/src
commit - 519f96cc72e084b9b4bd8458c870af05786040f2
blob - a97d833f7cb4a299daf3cbe3ac8836933deae4fb
file + sbin/ifconfig/ifconfig.c
--- sbin/ifconfig/ifconfig.c
+++ sbin/ifconfig/ifconfig.c
@@ -5941,6 +5941,31 @@ process_wg_commands(void)
 	}
 }
 
+static char *
+wg_humanify(time_t delta)
+{
+	static char		 buf[64];
+	int			 r;
+
+	if (delta > 60 * 60 * 24 * 2)
+		r = snprintf(buf, sizeof(buf), "%lld days ago",
+		    delta / (60 * 60 * 24));
+	else if (delta > 60 * 60 * 2)
+		r = snprintf(buf, sizeof(buf), "%lld hours ago",
+		    delta / (60 * 60));
+	else if (delta > 60 * 2)
+		r = snprintf(buf, sizeof(buf), "%lld minutes ago",
+		    delta / 60);
+	else
+		r = snprintf(buf, sizeof(buf), "%lld seconds ago",
+		    delta);
+
+	if (r == -1 || (size_t)r >= sizeof(buf))
+		strlcpy(buf, "some time ago", sizeof(buf));
+
+	return buf;
+}
+
 void
 wg_status(int ifaliases)
 {
@@ -6007,9 +6032,12 @@ wg_status(int ifaliases)
 			    wg_peer->p_txbytes, wg_peer->p_rxbytes);
 
 			if (wg_peer->p_last_handshake.tv_sec != 0) {
+				time_t d;
+
 				clock_gettime(CLOCK_REALTIME, &now);
-				printf("\t\tlast handshake: %lld seconds ago\n",
-				    now.tv_sec - wg_peer->p_last_handshake.tv_sec);
+				d = now.tv_sec - wg_peer->p_last_handshake.tv_sec;
+				printf("\t\tlast handshake: %s\n",
+				    wg_humanify(d));
 			}