wireguard.lists.zx2c4.com archive mirror
 help / color / mirror / Atom feed
* wireguard-go: IpcGetOperation: return peers in sorted order
@ 2020-02-15 22:42 mikma.wg
  2020-02-15 22:50 ` Jason A. Donenfeld
  0 siblings, 1 reply; 2+ messages in thread
From: mikma.wg @ 2020-02-15 22:42 UTC (permalink / raw)
  To: wireguard

[-- Attachment #1: Type: text/plain, Size: 534 bytes --]

Hello,

I have an improvement to IpcGetOperation in wireguard-go.

uapi: IpcGetOperation: return peers in sorted order

Sort peers based on the public key.
The pros of using a sorted peer list is that the order doesn't change in
each ipc operation, or execution of the "wg showconf" command. Which 
could be the case previously with an unsorted peer list.

The output from git format-patch is attached. The patch is also 
available at 
https://cgit.m7n.se/pub/wireguard-go/commit/?id=027bf58651f1a7b2be1bedfde187e5277a13f48e

/Mikael

[-- Attachment #2: 0001-uapi-IpcGetOperation-return-peers-in-sorted-order.patch --]
[-- Type: text/x-patch, Size: 2182 bytes --]

From 027bf58651f1a7b2be1bedfde187e5277a13f48e Mon Sep 17 00:00:00 2001
From: Mikael Magnusson <mikma@users.sourceforge.net>
Date: Sun, 22 Sep 2019 23:13:30 +0200
Subject: [PATCH] uapi: IpcGetOperation: return peers in sorted order

Sort peers based on the public key.
The pros of using a sorted peer list is that the order doesn't change in
each ipc operation, or execution of the "wg showconf" command. Which could
be the case previously with an unsorted peer list.

Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
---
 device/uapi.go | 41 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/device/uapi.go b/device/uapi.go
index 72611ab..bd66451 100644
--- a/device/uapi.go
+++ b/device/uapi.go
@@ -7,9 +7,11 @@ package device
 
 import (
 	"bufio"
+	"bytes"
 	"fmt"
 	"io"
 	"net"
+	"sort"
 	"strconv"
 	"strings"
 	"sync/atomic"
@@ -30,6 +32,42 @@ func (s IPCError) ErrorCode() int64 {
 	return s.int64
 }
 
+type PeerInfo struct {
+	pubkey NoisePublicKey
+	pubkeySlice []byte
+	peer *Peer
+}
+
+type PeerInfoList []PeerInfo
+
+func (list PeerInfoList) Len() int {
+	return len(list)
+}
+
+func (list PeerInfoList) Less(i, j int) bool {
+	k1 := list[i].pubkeySlice
+	k2 := list[j].pubkeySlice
+
+	return bytes.Compare(k1, k2) == -1;
+}
+
+func (list PeerInfoList) Swap(i, j int) {
+	list[i], list[j] = list[j], list[i]
+}
+
+func (device *Device) GetSortedPeers() PeerInfoList {
+	peers := make(PeerInfoList, 0, len(device.peers.keyMap))
+	for pubkey, peer := range device.peers.keyMap {
+		info := PeerInfo{}
+		info.pubkey = pubkey
+		info.pubkeySlice = info.pubkey[:]
+		info.peer = peer
+		peers = append(peers, info)
+	}
+	sort.Sort(peers)
+	return peers
+}
+
 func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError {
 	lines := make([]string, 0, 100)
 	send := func(line string) {
@@ -65,7 +103,8 @@ func (device *Device) IpcGetOperation(socket *bufio.Writer) *IPCError {
 
 		// serialize each peer state
 
-		for _, peer := range device.peers.keyMap {
+		for _, peerInfo := range device.GetSortedPeers() {
+			peer := peerInfo.peer
 			peer.RLock()
 			defer peer.RUnlock()
 
-- 
2.17.1


[-- Attachment #3: Type: text/plain, Size: 148 bytes --]

_______________________________________________
WireGuard mailing list
WireGuard@lists.zx2c4.com
https://lists.zx2c4.com/mailman/listinfo/wireguard

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: wireguard-go: IpcGetOperation: return peers in sorted order
  2020-02-15 22:42 wireguard-go: IpcGetOperation: return peers in sorted order mikma.wg
@ 2020-02-15 22:50 ` Jason A. Donenfeld
  0 siblings, 0 replies; 2+ messages in thread
From: Jason A. Donenfeld @ 2020-02-15 22:50 UTC (permalink / raw)
  To: mikma.wg; +Cc: WireGuard mailing list


[-- Attachment #1.1: Type: text/plain, Size: 1502 bytes --]

The show command presently sorts things based on last handshake time:
https://git.zx2c4.com/wireguard-tools/tree/src/show.c#n27

It sounds like you'd like the output of showconf to be sorted too so that
peers have some stable output order; perhaps you have diffing configs in
mind?

The kernel will output peers in the order that they were added, iirc. Maybe
wireguard-go should do the same?

Alternatively, we could sort this in the showconf code? But then we need
some sorting criteria, and people might wish instead for things to match
the input order of the original file.

In other words, either showconf sorts, or wireguard-go retains its input
order. I think I'd prefer the latter. Do you have a preference?

Jason


On Sat, Feb 15, 2020, 23:42 <mikma.wg@lists.m7n.se> wrote:

> Hello,
>
> I have an improvement to IpcGetOperation in wireguard-go.
>
> uapi: IpcGetOperation: return peers in sorted order
>
> Sort peers based on the public key.
> The pros of using a sorted peer list is that the order doesn't change in
> each ipc operation, or execution of the "wg showconf" command. Which
> could be the case previously with an unsorted peer list.
>
> The output from git format-patch is attached. The patch is also
> available at
>
> https://cgit.m7n.se/pub/wireguard-go/commit/?id=027bf58651f1a7b2be1bedfde187e5277a13f48e
>
> /Mikael
> _______________________________________________
> WireGuard mailing list
> WireGuard@lists.zx2c4.com
> https://lists.zx2c4.com/mailman/listinfo/wireguard
>

[-- Attachment #1.2: Type: text/html, Size: 2508 bytes --]

[-- Attachment #2: Type: text/plain, Size: 148 bytes --]

_______________________________________________
WireGuard mailing list
WireGuard@lists.zx2c4.com
https://lists.zx2c4.com/mailman/listinfo/wireguard

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-15 22:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-15 22:42 wireguard-go: IpcGetOperation: return peers in sorted order mikma.wg
2020-02-15 22:50 ` Jason A. Donenfeld

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).