}
func (cn *PeerConn) pexStatus() string {
- if !cn.bitExtensionEnabled(pp.ExtensionBitExtended) {
+ if !cn.bitExtensionEnabled(pp.ExtensionBitLtep) {
return "extended protocol disabled"
}
if cn.PeerExtensionIDs == nil {
}), ","),
cn.pex.numPending(),
)
-
}
}
// This returns the address to use if we want to dial the peer again. It incorporates the peer's
// advertised listen port.
func (c *PeerConn) dialAddr() PeerRemoteAddr {
- if !c.outgoing && c.PeerListenPort != 0 {
- switch addr := c.RemoteAddr.(type) {
- case *net.TCPAddr:
- dialAddr := *addr
- dialAddr.Port = c.PeerListenPort
- return &dialAddr
- case *net.UDPAddr:
- dialAddr := *addr
- dialAddr.Port = c.PeerListenPort
- return &dialAddr
- default:
- panic(addr)
- }
+ if c.outgoing || c.PeerListenPort == 0 {
+ return c.RemoteAddr
+ }
+ addrPort, err := addrPortFromPeerRemoteAddr(c.RemoteAddr)
+ if err != nil {
+ c.logger.Levelf(
+ log.Warning,
+ "error parsing %q for alternate dial port: %v",
+ c.RemoteAddr,
+ err,
+ )
+ return c.RemoteAddr
}
- return c.RemoteAddr
+ return netip.AddrPortFrom(addrPort.Addr(), uint16(c.PeerListenPort))
}
-func (c *PeerConn) pexEvent(t pexEventType) pexEvent {
+func (c *PeerConn) pexEvent(t pexEventType) (_ pexEvent, err error) {
f := c.pexPeerFlags()
- addr := c.dialAddr()
- return pexEvent{t, addr, f, nil}
+ dialAddr := c.dialAddr()
+ addr, err := addrPortFromPeerRemoteAddr(dialAddr)
+ if err != nil || !addr.IsValid() {
+ err = fmt.Errorf("parsing dial addr %q: %w", dialAddr, err)
+ return
+ }
+ return pexEvent{t, addr, f, nil}, nil
}
func (c *PeerConn) String() string {
return bytes.HasPrefix(pc.PeerID[:], []byte("-TR")) && pc.PeerID[7] == '-'
}
-func (pc *PeerConn) remoteAddrPort() Option[netip.AddrPort] {
- return Some(pc.conn.RemoteAddr().(interface {
- AddrPort() netip.AddrPort
- }).AddrPort())
-}
-
func (pc *PeerConn) remoteDialAddrPort() (netip.AddrPort, error) {
dialAddr := pc.dialAddr()
return addrPortFromPeerRemoteAddr(dialAddr)