From: mikma.wg@lists.m7n.se
To: wireguard@lists.zx2c4.com
Subject: wireguard-go: IpcGetOperation: return peers in sorted order
Date: Sat, 15 Feb 2020 23:42:45 +0100 [thread overview]
Message-ID: <cdf774f4-694e-22ab-966a-c57a1b9ff10a@m7n.se> (raw)
[-- 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
next reply other threads:[~2020-02-15 22:42 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-15 22:42 mikma.wg [this message]
2020-02-15 22:50 ` Jason A. Donenfeld
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cdf774f4-694e-22ab-966a-c57a1b9ff10a@m7n.se \
--to=mikma.wg@lists.m7n.se \
--cc=wireguard@lists.zx2c4.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).