wgengine/magicsock: rename pconn field to pconn4, in prep for pconn6

This commit is contained in:
Brad Fitzpatrick 2020-03-19 08:49:30 -07:00
parent 5fa825f057
commit 7caa288213

View File

@ -45,7 +45,7 @@
// A Conn routes UDP packets and actively manages a list of its endpoints. // A Conn routes UDP packets and actively manages a list of its endpoints.
// It implements wireguard/device.Bind. // It implements wireguard/device.Bind.
type Conn struct { type Conn struct {
pconn *RebindingUDPConn pconn4 *RebindingUDPConn
pconnPort uint16 pconnPort uint16
epFunc func(endpoints []string) epFunc func(endpoints []string)
logf logger.Logf logf logger.Logf
@ -192,7 +192,7 @@ func Listen(opts Options) (*Conn, error) {
connCtx, connCtxCancel := context.WithCancel(context.Background()) connCtx, connCtxCancel := context.WithCancel(context.Background())
c := &Conn{ c := &Conn{
pconn: new(RebindingUDPConn), pconn4: new(RebindingUDPConn),
pconnPort: opts.Port, pconnPort: opts.Port,
sendLogLimit: rate.NewLimiter(rate.Every(1*time.Minute), 1), sendLogLimit: rate.NewLimiter(rate.Every(1*time.Minute), 1),
connCtx: connCtx, connCtx: connCtx,
@ -213,12 +213,12 @@ func Listen(opts Options) (*Conn, error) {
c.netChecker = &netcheck.Client{ c.netChecker = &netcheck.Client{
DERP: c.derps, DERP: c.derps,
Logf: logger.WithPrefix(c.logf, "netcheck: "), Logf: logger.WithPrefix(c.logf, "netcheck: "),
GetSTUNConn4: func() netcheck.STUNConn { return c.pconn }, GetSTUNConn4: func() netcheck.STUNConn { return c.pconn4 },
// TODO: add GetSTUNConn6 once Conn has a pconn6 // TODO: add GetSTUNConn6 once Conn has a pconn6
} }
c.ignoreSTUNPackets() c.ignoreSTUNPackets()
c.pconn.Reset(packetConn.(*net.UDPConn)) c.pconn4.Reset(packetConn.(*net.UDPConn))
c.ReSTUN("initial") c.ReSTUN("initial")
// We assume that LinkChange notifications are plumbed through well // We assume that LinkChange notifications are plumbed through well
@ -445,7 +445,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) (ipPorts []string, err er
c.ignoreSTUNPackets() c.ignoreSTUNPackets()
if localAddr := c.pconn.LocalAddr(); localAddr.IP.IsUnspecified() { if localAddr := c.pconn4.LocalAddr(); localAddr.IP.IsUnspecified() {
ips, loopback, err := interfaces.LocalAddresses() ips, loopback, err := interfaces.LocalAddresses()
if err != nil { if err != nil {
return nil, err return nil, err
@ -494,7 +494,7 @@ func stringsEqual(x, y []string) bool {
} }
func (c *Conn) LocalPort() uint16 { func (c *Conn) LocalPort() uint16 {
laddr := c.pconn.LocalAddr() laddr := c.pconn4.LocalAddr()
return uint16(laddr.Port) return uint16(laddr.Port)
} }
@ -606,7 +606,8 @@ func (c *Conn) Send(b []byte, ep conn.Endpoint) error {
c.logf("[unexpected] DERP BUG: attempting to send packet to DERP address %v", addr) c.logf("[unexpected] DERP BUG: attempting to send packet to DERP address %v", addr)
return nil return nil
} }
_, err := c.pconn.WriteTo(b, addr) // TODO(bradfitz): use pconn6 if non-nil and addr.IP is v6
_, err := c.pconn4.WriteTo(b, addr)
return err return err
case *AddrSet: case *AddrSet:
as = v as = v
@ -649,7 +650,8 @@ func (c *Conn) Send(b []byte, ep conn.Endpoint) error {
// The provided public key identifies the recipient. // The provided public key identifies the recipient.
func (c *Conn) sendAddr(addr *net.UDPAddr, pubKey key.Public, b []byte) error { func (c *Conn) sendAddr(addr *net.UDPAddr, pubKey key.Public, b []byte) error {
if !addr.IP.Equal(derpMagicIP) { if !addr.IP.Equal(derpMagicIP) {
_, err := c.pconn.WriteTo(b, addr) // TODO(bradfitz): use pconn6 if non-nil and addr.IP is v6
_, err := c.pconn4.WriteTo(b, addr)
return err return err
} }
@ -896,7 +898,7 @@ type udpReadResult struct {
// to c.udpRecvCh, skipping over (but handling) any STUN replies. // to c.udpRecvCh, skipping over (but handling) any STUN replies.
func (c *Conn) awaitUDP4(b []byte) { func (c *Conn) awaitUDP4(b []byte) {
for { for {
n, pAddr, err := c.pconn.ReadFrom(b) n, pAddr, err := c.pconn4.ReadFrom(b)
if err != nil { if err != nil {
select { select {
case c.udpRecvCh <- udpReadResult{err: err}: case c.udpRecvCh <- udpReadResult{err: err}:
@ -938,7 +940,7 @@ func (c *Conn) ReceiveIPv4(b []byte) (n int, ep conn.Endpoint, addr *net.UDPAddr
select { select {
case dm := <-c.derpRecvCh: case dm := <-c.derpRecvCh:
// Cancel the pconn read goroutine // Cancel the pconn read goroutine
c.pconn.SetReadDeadline(aLongTimeAgo) c.pconn4.SetReadDeadline(aLongTimeAgo)
// Wait for the UDP-reading goroutine to be done, since it's currently // Wait for the UDP-reading goroutine to be done, since it's currently
// the owner of the b []byte buffer: // the owner of the b []byte buffer:
select { select {
@ -954,7 +956,7 @@ func (c *Conn) ReceiveIPv4(b []byte) (n int, ep conn.Endpoint, addr *net.UDPAddr
c.bufferedIPv4From = um.addr c.bufferedIPv4From = um.addr
c.bufferedIPv4Packet = append(c.bufferedIPv4Packet[:0], b[:um.n]...) c.bufferedIPv4Packet = append(c.bufferedIPv4Packet[:0], b[:um.n]...)
} }
c.pconn.SetReadDeadline(time.Time{}) c.pconn4.SetReadDeadline(time.Time{})
case <-c.donec(): case <-c.donec():
return 0, nil, nil, errors.New("Conn closed") return 0, nil, nil, errors.New("Conn closed")
} }
@ -1105,7 +1107,7 @@ func (c *Conn) Close() error {
c.closed = true c.closed = true
c.connCtxCancel() c.connCtxCancel()
c.closeAllDerpLocked() c.closeAllDerpLocked()
return c.pconn.Close() return c.pconn4.Close()
} }
func (c *Conn) periodicReSTUN() { func (c *Conn) periodicReSTUN() {
@ -1154,19 +1156,19 @@ func (c *Conn) ReSTUN(why string) {
// It should be followed by a call to ReSTUN. // It should be followed by a call to ReSTUN.
func (c *Conn) Rebind() { func (c *Conn) Rebind() {
if c.pconnPort != 0 { if c.pconnPort != 0 {
c.pconn.mu.Lock() c.pconn4.mu.Lock()
if err := c.pconn.pconn.Close(); err != nil { if err := c.pconn4.pconn.Close(); err != nil {
c.logf("magicsock: link change close failed: %v", err) c.logf("magicsock: link change close failed: %v", err)
} }
packetConn, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", c.pconnPort)) packetConn, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", c.pconnPort))
if err == nil { if err == nil {
c.logf("magicsock: link change rebound port: %d", c.pconnPort) c.logf("magicsock: link change rebound port: %d", c.pconnPort)
c.pconn.pconn = packetConn.(*net.UDPConn) c.pconn4.pconn = packetConn.(*net.UDPConn)
c.pconn.mu.Unlock() c.pconn4.mu.Unlock()
return return
} }
c.logf("magicsock: link change unable to bind fixed port %d: %v, falling back to random port", c.pconnPort, err) c.logf("magicsock: link change unable to bind fixed port %d: %v, falling back to random port", c.pconnPort, err)
c.pconn.mu.Unlock() c.pconn4.mu.Unlock()
} }
c.logf("magicsock: link change, binding new port") c.logf("magicsock: link change, binding new port")
packetConn, err := net.ListenPacket("udp4", ":0") packetConn, err := net.ListenPacket("udp4", ":0")
@ -1174,7 +1176,7 @@ func (c *Conn) Rebind() {
c.logf("magicsock: link change failed to bind new port: %v", err) c.logf("magicsock: link change failed to bind new port: %v", err)
return return
} }
c.pconn.Reset(packetConn.(*net.UDPConn)) c.pconn4.Reset(packetConn.(*net.UDPConn))
} }
// AddrSet is a set of UDP addresses that implements wireguard/conn.Endpoint. // AddrSet is a set of UDP addresses that implements wireguard/conn.Endpoint.