// Package control implements the VPN-internal peer control protocol. // Peers exchange Ping packets over UDP on the VPN control port to maintain // liveness and discover external endpoints for direct connection attempts. package control import ( "encoding/binary" "fmt" "net/netip" ) const ( version = 1 Size = 59 // 1 version + 8 ID + 8 PingTS + 6 SrcV4 + 18 SrcV6 + 18 Dst ) // Ping is the single control packet type exchanged between VPN peers. // // In each peer pair, the peer with the lower VPN IP is the client: it sets ID // and PingTS and sends pings on a timer. The server echoes ID and PingTS back // in its response, allowing the client to compute RTT = now - PingTS. // // Both client and server populate SrcV4, SrcV6, and Dst on every packet so // endpoint information flows in both directions. // // Dst is the recipient's external endpoint as observed by the sender from the // WireGuard handshake source. Zero if the sender has not observed a handshake // from the recipient. type Ping struct { ID int64 // Client ping ID. PingTS int64 // Client ping send time in nanoseconds. SrcV4 netip.AddrPort // Sender's discovered IPv4 address and port. SrcV6 netip.AddrPort // Sender's discovered IPv6 address and port. Dst netip.AddrPort } // Marshal encodes p into a fixed-size 59-byte array. func (p Ping) Marshal() [Size]byte { var buf [Size]byte buf[0] = version binary.BigEndian.PutUint64(buf[1:9], uint64(p.ID)) binary.BigEndian.PutUint64(buf[9:17], uint64(p.PingTS)) if p.SrcV4.IsValid() { a4 := p.SrcV4.Addr().As4() copy(buf[17:21], a4[:]) binary.BigEndian.PutUint16(buf[21:23], p.SrcV4.Port()) } a16 := p.SrcV6.Addr().As16() copy(buf[23:39], a16[:]) binary.BigEndian.PutUint16(buf[39:41], p.SrcV6.Port()) a16 = p.Dst.Addr().As16() copy(buf[41:57], a16[:]) binary.BigEndian.PutUint16(buf[57:59], p.Dst.Port()) return buf } // Unmarshal decodes a Ping from a fixed-size 59-byte array. func Unmarshal(buf [Size]byte) (Ping, error) { if buf[0] != version { return Ping{}, fmt.Errorf("unknown ping version %d", buf[0]) } p := Ping{ ID: int64(binary.BigEndian.Uint64(buf[1:9])), PingTS: int64(binary.BigEndian.Uint64(buf[9:17])), } if addr := netip.AddrFrom4([4]byte(buf[17:21])); !addr.IsUnspecified() { p.SrcV4 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[21:23])) } if addr := netip.AddrFrom16([16]byte(buf[23:39])); !addr.IsUnspecified() { p.SrcV6 = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[39:41])) } if addr := netip.AddrFrom16([16]byte(buf[41:57])).Unmap(); !addr.IsUnspecified() { p.Dst = netip.AddrPortFrom(addr, binary.BigEndian.Uint16(buf[57:59])) } return p, nil }