MCPcopy
hub / github.com/WireGuard/wireguard-go / IpcGetOperation

Method IpcGetOperation

device/uapi.go:51–136  ·  view source on GitHub ↗

IpcGetOperation implements the WireGuard configuration protocol "get" operation. See https://www.wireguard.com/xplatform/#configuration-protocol for details.

(w io.Writer)

Source from the content-addressed store, hash-verified

49// IpcGetOperation implements the WireGuard configuration protocol "get" operation.
50// See https://www.wireguard.com/xplatform/#configuration-protocol for details.
51func (device *Device) IpcGetOperation(w io.Writer) error {
52 device.ipcMutex.RLock()
53 defer device.ipcMutex.RUnlock()
54
55 buf := byteBufferPool.Get().(*bytes.Buffer)
56 buf.Reset()
57 defer byteBufferPool.Put(buf)
58 sendf := func(format string, args ...any) {
59 fmt.Fprintf(buf, format, args...)
60 buf.WriteByte('\n')
61 }
62 keyf := func(prefix string, key *[32]byte) {
63 buf.Grow(len(key)*2 + 2 + len(prefix))
64 buf.WriteString(prefix)
65 buf.WriteByte('=')
66 const hex = "0123456789abcdef"
67 for i := 0; i < len(key); i++ {
68 buf.WriteByte(hex[key[i]>>4])
69 buf.WriteByte(hex[key[i]&0xf])
70 }
71 buf.WriteByte('\n')
72 }
73
74 func() {
75 // lock required resources
76
77 device.net.RLock()
78 defer device.net.RUnlock()
79
80 device.staticIdentity.RLock()
81 defer device.staticIdentity.RUnlock()
82
83 device.peers.RLock()
84 defer device.peers.RUnlock()
85
86 // serialize device related values
87
88 if !device.staticIdentity.privateKey.IsZero() {
89 keyf("private_key", (*[32]byte)(&device.staticIdentity.privateKey))
90 }
91
92 if device.net.port != 0 {
93 sendf("listen_port=%d", device.net.port)
94 }
95
96 if device.net.fwmark != 0 {
97 sendf("fwmark=%d", device.net.fwmark)
98 }
99
100 for _, peer := range device.peers.keyMap {
101 // Serialize peer state.
102 peer.handshake.mutex.RLock()
103 keyf("public_key", (*[32]byte)(&peer.handshake.remoteStatic))
104 keyf("preshared_key", (*[32]byte)(&peer.handshake.presharedKey))
105 peer.handshake.mutex.RUnlock()
106 sendf("protocol_version=1")
107 peer.endpoint.Lock()
108 if peer.endpoint.val != nil {

Callers 3

IpcGetMethod · 0.95
IpcHandleMethod · 0.95
BenchmarkUAPIGetFunction · 0.80

Implementers 4

NativeTuntun/tun_linux.go
netTuntun/netstack/tun.go
chTuntun/tuntest/tuntest.go
fakeTUNDeviceSizeddevice/device_test.go

Calls 9

ipcErrorfFunction · 0.85
GetMethod · 0.80
ResetMethod · 0.80
PutMethod · 0.80
EntriesForPeerMethod · 0.80
DstToStringMethod · 0.65
WriteMethod · 0.65
IsZeroMethod · 0.45
StringMethod · 0.45

Tested by 1

BenchmarkUAPIGetFunction · 0.64