]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Use simple 16-bit integer for persistent keepalive
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 18 Apr 2018 05:24:33 +0000 (07:24 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 18 Apr 2018 05:24:33 +0000 (07:24 +0200)
Races for this aren't a huge problem.

peer.go
timers.go
uapi.go

diff --git a/peer.go b/peer.go
index 3a4f5f2fe300dc8b42ea0e8800c730d225ff27cd..8acdfbc02924a01e950c56459488b0e6b6db4556 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -13,13 +13,14 @@ const (
 )
 
 type Peer struct {
-       persistentKeepaliveInterval uint64
        isRunning                   AtomicBool
        mutex                       sync.RWMutex
        keyPairs                    KeyPairs
        handshake                   Handshake
        device                      *Device
        endpoint                    Endpoint
+       persistentKeepaliveInterval uint16
+       _                           uint32 // padding for alignment
 
        stats struct {
                txBytes           uint64 // bytes send to peer (endpoint)
index 76dffb990b89ed481c56d0b870f882a8da62aeb4..e118c387349f4cd33245a670372ad85b525a959b 100644 (file)
--- a/timers.go
+++ b/timers.go
@@ -105,7 +105,7 @@ func (peer *Peer) TimerAnyAuthenticatedPacketReceived() {
  * Push persistent keep-alive into the future
  */
 func (peer *Peer) TimerAnyAuthenticatedPacketTraversal() {
-       interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval)
+       interval := peer.persistentKeepaliveInterval
        if interval > 0 {
                duration := time.Duration(interval) * time.Second
                peer.timer.keepalivePersistent.Reset(duration)
@@ -199,7 +199,7 @@ func (peer *Peer) RoutineTimerHandler() {
        peer.timer.handshakeNew.Stop()
        peer.timer.zeroAllKeys.Stop()
 
-       interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval)
+       interval := peer.persistentKeepaliveInterval
        if interval > 0 {
                duration := time.Duration(interval) * time.Second
                peer.timer.keepalivePersistent.Reset(duration)
@@ -225,7 +225,7 @@ func (peer *Peer) RoutineTimerHandler() {
 
                case <-peer.timer.keepalivePersistent.Wait():
 
-                       interval := atomic.LoadUint64(&peer.persistentKeepaliveInterval)
+                       interval := peer.persistentKeepaliveInterval
                        if interval > 0 {
                                logDebug.Println(peer.String(), ": Send keep-alive (persistent)")
                                peer.timer.keepalivePassive.Stop()
diff --git a/uapi.go b/uapi.go
index 01cdab632687c91841905b384df8d54c2686e792..c795b73873211691c840ba4cb76205853cff1c0b 100644 (file)
--- a/uapi.go
+++ b/uapi.go
@@ -84,9 +84,7 @@ func ipcGetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
                        send(fmt.Sprintf("last_handshake_time_nsec=%d", nano))
                        send(fmt.Sprintf("tx_bytes=%d", peer.stats.txBytes))
                        send(fmt.Sprintf("rx_bytes=%d", peer.stats.rxBytes))
-                       send(fmt.Sprintf("persistent_keepalive_interval=%d",
-                               atomic.LoadUint64(&peer.persistentKeepaliveInterval),
-                       ))
+                       send(fmt.Sprintf("persistent_keepalive_interval=%d", peer.persistentKeepaliveInterval))
 
                        for _, ip := range device.routing.table.AllowedIPs(peer) {
                                send("allowed_ip=" + ip.String())
@@ -322,10 +320,8 @@ func ipcSetOperation(device *Device, socket *bufio.ReadWriter) *IPCError {
                                        return &IPCError{Code: ipcErrorInvalid}
                                }
 
-                               old := atomic.SwapUint64(
-                                       &peer.persistentKeepaliveInterval,
-                                       secs,
-                               )
+                               old := peer.persistentKeepaliveInterval
+                               peer.persistentKeepaliveInterval = uint16(secs)
 
                                // send immediate keep-alive