Fri Aug 28 17:17:53 2020 UTC ()
Show the time of the latest handshake properly formatted.


(tih)
diff -r1.4 -r1.5 src/usr.sbin/wgconfig/wgconfig.c

cvs diff -r1.4 -r1.5 src/usr.sbin/wgconfig/wgconfig.c (expand / switch to unified diff)

--- src/usr.sbin/wgconfig/wgconfig.c 2020/08/21 17:51:31 1.4
+++ src/usr.sbin/wgconfig/wgconfig.c 2020/08/28 17:17:53 1.5
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: wgconfig.c,v 1.4 2020/08/21 17:51:31 martin Exp $ */ 1/* $NetBSD: wgconfig.c,v 1.5 2020/08/28 17:17:53 tih Exp $ */
2 2
3/* 3/*
4 * Copyright (C) Ryota Ozaki <ozaki.ryota@gmail.com> 4 * Copyright (C) Ryota Ozaki <ozaki.ryota@gmail.com>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: wgconfig.c,v 1.4 2020/08/21 17:51:31 martin Exp $"); 33__RCSID("$NetBSD: wgconfig.c,v 1.5 2020/08/28 17:17:53 tih Exp $");
34 34
35#include <sys/ioctl.h> 35#include <sys/ioctl.h>
36 36
37#include <net/if.h> 37#include <net/if.h>
38#include <net/if_wg.h> 38#include <net/if_wg.h>
39 39
40#include <arpa/inet.h> 40#include <arpa/inet.h>
41 41
42#include <stdio.h> 42#include <stdio.h>
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45#include <err.h> 45#include <err.h>
46#include <unistd.h> 46#include <unistd.h>
@@ -231,54 +231,58 @@ ioctl_get(const char *interface) @@ -231,54 +231,58 @@ ioctl_get(const char *interface)
231 if (prop_dict == NULL) 231 if (prop_dict == NULL)
232 errx(EXIT_FAILURE, "prop_dictionary_internalize failed"); 232 errx(EXIT_FAILURE, "prop_dictionary_internalize failed");
233 233
234 free(buf); 234 free(buf);
235 close(sock); 235 close(sock);
236 236
237 return prop_dict; 237 return prop_dict;
238} 238}
239 239
240static void 240static void
241show_peer(prop_dictionary_t peer, const char *prefix, bool show_psk) 241show_peer(prop_dictionary_t peer, const char *prefix, bool show_psk)
242{ 242{
243 prop_object_t prop_obj; 243 prop_object_t prop_obj;
244 uint64_t sec; 244 time_t sec;
245 245
246 prop_obj = prop_dictionary_get(peer, "public_key"); 246 prop_obj = prop_dictionary_get(peer, "public_key");
247 if (prop_obj == NULL) { 247 if (prop_obj == NULL) {
248 warnx("peer without public-key"); 248 warnx("peer without public-key");
249 return; 249 return;
250 } 250 }
251 printf("%spublic-key: %s\n", prefix, format_key(prop_obj)); 251 printf("%spublic-key: %s\n", prefix, format_key(prop_obj));
252 252
253 prop_obj = prop_dictionary_get(peer, "endpoint"); 253 prop_obj = prop_dictionary_get(peer, "endpoint");
254 if (prop_obj == NULL) 254 if (prop_obj == NULL)
255 printf("%sendpoint: (none)\n", prefix); 255 printf("%sendpoint: (none)\n", prefix);
256 else 256 else
257 printf("%sendpoint: %s\n", prefix, format_endpoint(prop_obj)); 257 printf("%sendpoint: %s\n", prefix, format_endpoint(prop_obj));
258 258
259 if (show_psk) { 259 if (show_psk) {
260 prop_obj = prop_dictionary_get(peer, "preshared_key"); 260 prop_obj = prop_dictionary_get(peer, "preshared_key");
261 printf("%spreshared-key: %s\n", prefix, format_key(prop_obj)); 261 printf("%spreshared-key: %s\n", prefix, format_key(prop_obj));
262 } else { 262 } else {
263 printf("%spreshared-key: (hidden)\n", prefix); 263 printf("%spreshared-key: (hidden)\n", prefix);
264 } 264 }
265 265
266 handle_allowed_ips(peer, prefix); 266 handle_allowed_ips(peer, prefix);
267 267
268 if (prop_dictionary_get_uint64(peer, "last_handshake_time_sec", &sec)) 268 if (prop_dictionary_get_int64(peer, "last_handshake_time_sec", &sec)) {
269 printf("%slatest-handshake: %"PRIu64"\n", prefix, sec); 269 if (sec > 0)
270 else 270 printf("%slatest-handshake: %s", prefix, ctime(&sec));
 271 else
 272 printf("%slatest-handshake: (never)\n", prefix);
 273 } else {
271 printf("%slatest-handshake: (none)\n", prefix); 274 printf("%slatest-handshake: (none)\n", prefix);
 275 }
272} 276}
273 277
274static int 278static int
275cmd_show_all(const char *interface, int argc, char *argv[]) 279cmd_show_all(const char *interface, int argc, char *argv[])
276{ 280{
277 prop_dictionary_t prop_dict; 281 prop_dictionary_t prop_dict;
278 prop_object_t prop_obj; 282 prop_object_t prop_obj;
279 uint16_t port; 283 uint16_t port;
280 prop_array_t peers; 284 prop_array_t peers;
281 285
282 prop_dict = ioctl_get(interface); 286 prop_dict = ioctl_get(interface);
283 287
284 printf("interface: %s\n", interface); 288 printf("interface: %s\n", interface);