ipn/ipnlocal: fix peerapi6 port being report as 0 in netstack mode

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1644/head
Brad Fitzpatrick 4 years ago
parent 9f105d3968
commit 6266cf8e36

@ -1327,7 +1327,7 @@ func (b *LocalBackend) getPeerAPIPortForTSMPPing(ip netaddr.IP) (port uint16, ok
defer b.mu.Unlock() defer b.mu.Unlock()
for _, pln := range b.peerAPIListeners { for _, pln := range b.peerAPIListeners {
if pln.ip.BitLen() == ip.BitLen() { if pln.ip.BitLen() == ip.BitLen() {
return uint16(pln.Port()), true return uint16(pln.port), true
} }
} }
return 0, false return 0, false
@ -1341,7 +1341,7 @@ func (b *LocalBackend) peerAPIServicesLocked() (ret []tailcfg.Service) {
} }
ret = append(ret, tailcfg.Service{ ret = append(ret, tailcfg.Service{
Proto: proto, Proto: proto,
Port: uint16(pln.Port()), Port: uint16(pln.port),
}) })
} }
return ret return ret
@ -1556,13 +1556,12 @@ func (b *LocalBackend) initPeerAPIListener() {
ln: ln, // nil for 2nd+ on netstack ln: ln, // nil for 2nd+ on netstack
lb: b, lb: b,
} }
var port int
if skipListen { if skipListen {
port = b.peerAPIListeners[0].Port() pln.port = b.peerAPIListeners[0].port
} else { } else {
port = pln.Port() pln.port = ln.Addr().(*net.TCPAddr).Port
} }
pln.urlStr = "http://" + net.JoinHostPort(a.IP.String(), strconv.Itoa(port)) pln.urlStr = "http://" + net.JoinHostPort(a.IP.String(), strconv.Itoa(pln.port))
b.logf("peerapi: serving on %s", pln.urlStr) b.logf("peerapi: serving on %s", pln.urlStr)
go pln.serve() go pln.serve()
b.peerAPIListeners = append(b.peerAPIListeners, pln) b.peerAPIListeners = append(b.peerAPIListeners, pln)

@ -218,11 +218,19 @@ func (s *peerAPIServer) listen(ip netaddr.IP, ifState *interfaces.State) (ln net
} }
type peerAPIListener struct { type peerAPIListener struct {
ps *peerAPIServer ps *peerAPIServer
ip netaddr.IP ip netaddr.IP
ln net.Listener // or nil for 2nd+ address family in netstack mdoe lb *LocalBackend
lb *LocalBackend
// ln is the Listener. It can be nil in netstack mode if there are more than
// 1 local addresses (e.g. both an IPv4 and IPv6). When it's nil, port
// and urlStr are still populated.
ln net.Listener
// urlStr is the base URL to access the peer API (http://ip:port/).
urlStr string urlStr string
// port is just the port of urlStr.
port int
} }
func (pln *peerAPIListener) Close() error { func (pln *peerAPIListener) Close() error {
@ -232,17 +240,6 @@ func (pln *peerAPIListener) Close() error {
return nil return nil
} }
func (pln *peerAPIListener) Port() int {
if pln.ln == nil {
return 0
}
ta, ok := pln.ln.Addr().(*net.TCPAddr)
if !ok {
return 0
}
return ta.Port
}
func (pln *peerAPIListener) serve() { func (pln *peerAPIListener) serve() {
if pln.ln == nil { if pln.ln == nil {
return return

Loading…
Cancel
Save