@@ -99,33 +99,35 @@ func (device *Device) IpcGetOperation(w io.Writer) error {
9999 sendf ("fwmark=%d" , device .net .fwmark )
100100 }
101101
102- // serialize each peer state
103-
104102 for _ , peer := range device .peers .keyMap {
105- peer .RLock ()
106- defer peer .RUnlock ()
107-
108- keyf ("public_key" , (* [32 ]byte )(& peer .handshake .remoteStatic ))
109- keyf ("preshared_key" , (* [32 ]byte )(& peer .handshake .presharedKey ))
110- sendf ("protocol_version=1" )
111- if peer .endpoint != nil {
112- sendf ("endpoint=%s" , peer .endpoint .DstToString ())
113- }
114-
115- nano := atomic .LoadInt64 (& peer .stats .lastHandshakeNano )
116- secs := nano / time .Second .Nanoseconds ()
117- nano %= time .Second .Nanoseconds ()
118-
119- sendf ("last_handshake_time_sec=%d" , secs )
120- sendf ("last_handshake_time_nsec=%d" , nano )
121- sendf ("tx_bytes=%d" , atomic .LoadUint64 (& peer .stats .txBytes ))
122- sendf ("rx_bytes=%d" , atomic .LoadUint64 (& peer .stats .rxBytes ))
123- sendf ("persistent_keepalive_interval=%d" , atomic .LoadUint32 (& peer .persistentKeepaliveInterval ))
124-
125- device .allowedips .EntriesForPeer (peer , func (prefix netip.Prefix ) bool {
126- sendf ("allowed_ip=%s" , prefix .String ())
127- return true
128- })
103+ // Serialize peer state.
104+ // Do the work in an anonymous function so that we can use defer.
105+ func () {
106+ peer .RLock ()
107+ defer peer .RUnlock ()
108+
109+ keyf ("public_key" , (* [32 ]byte )(& peer .handshake .remoteStatic ))
110+ keyf ("preshared_key" , (* [32 ]byte )(& peer .handshake .presharedKey ))
111+ sendf ("protocol_version=1" )
112+ if peer .endpoint != nil {
113+ sendf ("endpoint=%s" , peer .endpoint .DstToString ())
114+ }
115+
116+ nano := atomic .LoadInt64 (& peer .stats .lastHandshakeNano )
117+ secs := nano / time .Second .Nanoseconds ()
118+ nano %= time .Second .Nanoseconds ()
119+
120+ sendf ("last_handshake_time_sec=%d" , secs )
121+ sendf ("last_handshake_time_nsec=%d" , nano )
122+ sendf ("tx_bytes=%d" , atomic .LoadUint64 (& peer .stats .txBytes ))
123+ sendf ("rx_bytes=%d" , atomic .LoadUint64 (& peer .stats .rxBytes ))
124+ sendf ("persistent_keepalive_interval=%d" , atomic .LoadUint32 (& peer .persistentKeepaliveInterval ))
125+
126+ device .allowedips .EntriesForPeer (peer , func (prefix netip.Prefix ) bool {
127+ sendf ("allowed_ip=%s" , prefix .String ())
128+ return true
129+ })
130+ }()
129131 }
130132 }()
131133
0 commit comments