Skip to content

Commit 4b49ca4

Browse files
committed
wgengine/magicsock: update comments on what implements conn.Bind
The comment still said *magicsock.Conn implemented wireguard-go conn.Bind. That wasn't accurate anymore. A doc #cleanup. Change-Id: I7fd003b939497889cc81147bfb937b93e4f6865c Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
1 parent 10f1c90 commit 4b49ca4

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

wgengine/magicsock/magicsock.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ func (m *peerMap) deleteEndpoint(ep *endpoint) {
268268
}
269269

270270
// A Conn routes UDP packets and actively manages a list of its endpoints.
271-
// It implements wireguard/conn.Bind.
272271
type Conn struct {
273272
// This block mirrors the contents and field order of the Options
274273
// struct. Initialized once at construction, then constant.
@@ -1280,6 +1279,9 @@ var errNetworkDown = errors.New("magicsock: network down")
12801279

12811280
func (c *Conn) networkDown() bool { return !c.networkUp.Load() }
12821281

1282+
// Send implements conn.Bind.
1283+
//
1284+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.Send
12831285
func (c *Conn) Send(buffs [][]byte, ep conn.Endpoint) error {
12841286
n := int64(len(buffs))
12851287
metricSendData.Add(n)
@@ -1952,7 +1954,7 @@ func (c *connBind) receiveDERP(buffs [][]byte, sizes []int, eps []conn.Endpoint)
19521954
defer health.ReceiveDERP.Exit()
19531955

19541956
for dm := range c.derpRecvCh {
1955-
if c.Closed() {
1957+
if c.isClosed() {
19561958
break
19571959
}
19581960
n, ep := c.processDERPReadResult(dm, buffs[0])
@@ -2989,6 +2991,8 @@ func (c *Conn) DERPs() int {
29892991
}
29902992

29912993
// Bind returns the wireguard-go conn.Bind for c.
2994+
//
2995+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind
29922996
func (c *Conn) Bind() conn.Bind {
29932997
return c.bind
29942998
}
@@ -3004,6 +3008,12 @@ type connBind struct {
30043008
closed bool
30053009
}
30063010

3011+
var _ conn.Bind = (*connBind)(nil)
3012+
3013+
// BatchSize returns the number of buffers expected to be passed to
3014+
// the ReceiveFuncs, and the maximum expected to be passed to SendBatch.
3015+
//
3016+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.BatchSize
30073017
func (c *connBind) BatchSize() int {
30083018
// TODO(raggi): determine by properties rather than hardcoding platform behavior
30093019
switch runtime.GOOS {
@@ -3017,6 +3027,8 @@ func (c *connBind) BatchSize() int {
30173027
// Open is called by WireGuard to create a UDP binding.
30183028
// The ignoredPort comes from wireguard-go, via the wgcfg config.
30193029
// We ignore that port value here, since we have the local port available easily.
3030+
//
3031+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.Open
30203032
func (c *connBind) Open(ignoredPort uint16) ([]conn.ReceiveFunc, uint16, error) {
30213033
c.mu.Lock()
30223034
defer c.mu.Unlock()
@@ -3035,11 +3047,15 @@ func (c *connBind) Open(ignoredPort uint16) ([]conn.ReceiveFunc, uint16, error)
30353047

30363048
// SetMark is used by wireguard-go to set a mark bit for packets to avoid routing loops.
30373049
// We handle that ourselves elsewhere.
3050+
//
3051+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.SetMark
30383052
func (c *connBind) SetMark(value uint32) error {
30393053
return nil
30403054
}
30413055

30423056
// Close closes the connBind, unless it is already closed.
3057+
//
3058+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.Close
30433059
func (c *connBind) Close() error {
30443060
c.mu.Lock()
30453061
defer c.mu.Unlock()
@@ -3063,8 +3079,8 @@ func (c *connBind) Close() error {
30633079
return nil
30643080
}
30653081

3066-
// Closed reports whether c is closed.
3067-
func (c *connBind) Closed() bool {
3082+
// isClosed reports whether c is closed.
3083+
func (c *connBind) isClosed() bool {
30683084
c.mu.Lock()
30693085
defer c.mu.Unlock()
30703086
return c.closed
@@ -3370,7 +3386,9 @@ func packIPPort(ua netip.AddrPort) []byte {
33703386
return b
33713387
}
33723388

3373-
// ParseEndpoint is called by WireGuard to connect to an endpoint.
3389+
// ParseEndpoint implements conn.Bind; it's called by WireGuard to connect to an endpoint.
3390+
//
3391+
// See https://pkg.go.dev/golang.zx2c4.com/wireguard/conn#Bind.ParseEndpoint
33743392
func (c *Conn) ParseEndpoint(nodeKeyStr string) (conn.Endpoint, error) {
33753393
k, err := key.ParseNodePublicUntyped(mem.S(nodeKeyStr))
33763394
if err != nil {

0 commit comments

Comments
 (0)