package basichost

import (
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	relayv2 
	
	
	
	

	logging 
	ma 
	msmux 
)

var log = logging.Logger("basichost")

var (
	// DefaultNegotiationTimeout is the default value for HostOpts.NegotiationTimeout.
	DefaultNegotiationTimeout = 10 * time.Second

	// DefaultAddrsFactory is the default value for HostOpts.AddrsFactory.
	DefaultAddrsFactory = func( []ma.Multiaddr) []ma.Multiaddr { return  }
)

// AddrsFactory functions can be passed to New in order to override
// addresses returned by Addrs.
type AddrsFactory func([]ma.Multiaddr) []ma.Multiaddr

// BasicHost is the basic implementation of the host.Host interface. This
// particular host implementation:
//   - uses a protocol muxer to mux per-protocol streams
//   - uses an identity service to send + receive node information
//   - uses a nat service to establish NAT port mappings
type BasicHost struct {
	ctx       context.Context
	ctxCancel context.CancelFunc
	// ensures we shutdown ONLY once
	closeSync sync.Once
	// keep track of resources we need to wait on before shutting down
	refCount sync.WaitGroup

	network      network.Network
	psManager    *pstoremanager.PeerstoreManager
	mux          *msmux.MultistreamMuxer[protocol.ID]
	ids          identify.IDService
	hps          *holepunch.Service
	pings        *ping.PingService
	cmgr         connmgr.ConnManager
	eventbus     event.Bus
	relayManager *relaysvc.RelayManager

	negtimeout time.Duration

	emitters struct {
		evtLocalProtocolsUpdated event.Emitter
	}

	autoNATMx sync.RWMutex
	autoNat   autonat.AutoNAT

	autonatv2      *autonatv2.AutoNAT
	addressManager *addrsManager
}

var _ host.Host = (*BasicHost)(nil)

// HostOpts holds options that can be passed to NewHost in order to
// customize construction of the *BasicHost.
type HostOpts struct {
	// EventBus sets the event bus. Will construct a new event bus if omitted.
	EventBus event.Bus

	// MultistreamMuxer is essential for the *BasicHost and will use a sensible default value if omitted.
	MultistreamMuxer *msmux.MultistreamMuxer[protocol.ID]

	// NegotiationTimeout determines the read and write timeouts when negotiating
	// protocols for streams. If 0 or omitted, it will use
	// DefaultNegotiationTimeout. If below 0, timeouts on streams will be
	// deactivated.
	NegotiationTimeout time.Duration

	// AddrsFactory holds a function which can be used to override or filter the result of Addrs.
	// If omitted, there's no override or filtering, and the results of Addrs and AllAddrs are the same.
	AddrsFactory AddrsFactory

	// NATManager takes care of setting NAT port mappings, and discovering external addresses.
	// If omitted, this will simply be disabled.
	NATManager func(network.Network) NATManager

	// ConnManager is a libp2p connection manager
	ConnManager connmgr.ConnManager

	// EnablePing indicates whether to instantiate the ping service
	EnablePing bool

	// EnableRelayService enables the circuit v2 relay (if we're publicly reachable).
	EnableRelayService bool
	// RelayServiceOpts are options for the circuit v2 relay.
	RelayServiceOpts []relayv2.Option

	// UserAgent sets the user-agent for the host.
	UserAgent string

	// ProtocolVersion sets the protocol version for the host.
	ProtocolVersion string

	// DisableSignedPeerRecord disables the generation of Signed Peer Records on this host.
	DisableSignedPeerRecord bool

	// EnableHolePunching enables the peer to initiate/respond to hole punching attempts for NAT traversal.
	EnableHolePunching bool
	// HolePunchingOptions are options for the hole punching service
	HolePunchingOptions []holepunch.Option

	// EnableMetrics enables the metrics subsystems
	EnableMetrics bool
	// PrometheusRegisterer is the PrometheusRegisterer used for metrics
	PrometheusRegisterer prometheus.Registerer
	// AutoNATv2MetricsTracker tracks AutoNATv2 address reachability metrics
	AutoNATv2MetricsTracker MetricsTracker

	// ObservedAddrsManager maps our local listen addresses to external publicly observed addresses.
	ObservedAddrsManager ObservedAddrsManager

	AutoNATv2 *autonatv2.AutoNAT
}

// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func ( network.Network,  *HostOpts) (*BasicHost, error) {
	if  == nil {
		 = &HostOpts{}
	}
	if .EventBus == nil {
		.EventBus = eventbus.NewBus()
	}

	,  := pstoremanager.NewPeerstoreManager(.Peerstore(), .EventBus, )
	if  != nil {
		return nil, 
	}

	,  := context.WithCancel(context.Background())
	 := &BasicHost{
		network:    ,
		psManager:  ,
		mux:        msmux.NewMultistreamMuxer[protocol.ID](),
		negtimeout: DefaultNegotiationTimeout,
		eventbus:   .EventBus,
		ctx:        ,
		ctxCancel:  ,
	}

	if .emitters.evtLocalProtocolsUpdated,  = .eventbus.Emitter(&event.EvtLocalProtocolsUpdated{}, eventbus.Stateful);  != nil {
		return nil, 
	}

	if .MultistreamMuxer != nil {
		.mux = .MultistreamMuxer
	}

	 := []identify.Option{
		identify.UserAgent(.UserAgent),
		identify.ProtocolVersion(.ProtocolVersion),
	}

	// we can't set this as a default above because it depends on the *BasicHost.
	if .DisableSignedPeerRecord {
		 = append(, identify.DisableSignedPeerRecord())
	}
	if .EnableMetrics {
		 = append(,
			identify.WithMetricsTracer(
				identify.NewMetricsTracer(identify.WithRegisterer(.PrometheusRegisterer))))
	}

	.ids,  = identify.NewIDService(, ...)
	if  != nil {
		return nil, fmt.Errorf("failed to create Identify service: %s", )
	}

	 := DefaultAddrsFactory
	if .AddrsFactory != nil {
		 = .AddrsFactory
	}

	var  NATManager
	if .NATManager != nil {
		 = .NATManager(.Network())
	}

	if .AutoNATv2 != nil {
		.autonatv2 = .AutoNATv2
	}

	var  autonatv2Client // avoid typed nil errors
	if .autonatv2 != nil {
		 = .autonatv2
	}

	// Create addCertHashes function with interface assertion for swarm
	 := func( []ma.Multiaddr) []ma.Multiaddr {
		return 
	}
	if ,  := .Network().(interface {
		( []ma.Multiaddr) []ma.Multiaddr
	});  {
		 = .
	}

	.addressManager,  = newAddrsManager(
		.eventbus,
		,
		,
		.Network().ListenAddresses,
		,
		.ObservedAddrsManager,
		,
		.EnableMetrics,
		.PrometheusRegisterer,
		.DisableSignedPeerRecord,
		.Peerstore().PrivKey(.ID()),
		.Peerstore(),
		.ID(),
	)
	if  != nil {
		return nil, fmt.Errorf("failed to create address service: %w", )
	}

	if .EnableHolePunching {
		if .EnableMetrics {
			 := []holepunch.Option{
				holepunch.WithMetricsTracer(holepunch.NewMetricsTracer(holepunch.WithRegisterer(.PrometheusRegisterer)))}
			.HolePunchingOptions = append(, .HolePunchingOptions...)

		}
		.hps,  = holepunch.NewService(, .ids, .addressManager.HolePunchAddrs, .HolePunchingOptions...)
		if  != nil {
			return nil, fmt.Errorf("failed to create hole punch service: %w", )
		}
	}

	if uint64(.NegotiationTimeout) != 0 {
		.negtimeout = .NegotiationTimeout
	}

	if .ConnManager == nil {
		.cmgr = &connmgr.NullConnMgr{}
	} else {
		.cmgr = .ConnManager
		.Notify(.cmgr.Notifee())
	}

	if .EnableRelayService {
		if .EnableMetrics {
			// Prefer explicitly provided metrics tracer
			 := []relayv2.Option{
				relayv2.WithMetricsTracer(
					relayv2.NewMetricsTracer(relayv2.WithRegisterer(.PrometheusRegisterer)))}
			.RelayServiceOpts = append(, .RelayServiceOpts...)
		}
		.relayManager = relaysvc.NewRelayManager(, .RelayServiceOpts...)
	}

	if .EnablePing {
		.pings = ping.NewPingService()
	}

	.SetStreamHandler(.newStreamHandler)

	return , nil
}

// Start starts background tasks in the host
// TODO: Return error and handle it in the caller?
func ( *BasicHost) () {
	.psManager.Start()
	if .autonatv2 != nil {
		 := .autonatv2.Start()
		if  != nil {
			log.Error("autonat v2 failed to start", "err", )
		}
	}
	// register to be notified when the network's listen addrs change,
	// so we can update our address set and push events if needed
	.Network().Notify(.addressManager.NetNotifee())
	if  := .addressManager.Start();  != nil {
		log.Error("address service failed to start", "err", )
	}

	.ids.Start()
}

// newStreamHandler is the remote-opened stream handler for network.Network
// TODO: this feels a bit wonky
func ( *BasicHost) ( network.Stream) {
	 := time.Now()

	if .negtimeout > 0 {
		if  := .SetDeadline(time.Now().Add(.negtimeout));  != nil {
			log.Debug("setting stream deadline", "err", )
			.Reset()
			return
		}
	}

	, ,  := .Mux().Negotiate()
	 := time.Since()
	if  != nil {
		if  == io.EOF {
			 := slog.LevelDebug
			if  > time.Second*10 {
				 = slog.LevelWarn
			}
			log.Log(context.Background(), , "protocol EOF", "remote_peer", .Conn().RemotePeer(), "duration", )
		} else {
			log.Debug("protocol mux failed", "err", , "duration", , "stream_id", .ID(), "remote_peer", .Conn().RemotePeer(), "remote_multiaddr", .Conn().RemoteMultiaddr())
		}
		.ResetWithError(network.StreamProtocolNegotiationFailed)
		return
	}

	if .negtimeout > 0 {
		if  := .SetDeadline(time.Time{});  != nil {
			log.Debug("resetting stream deadline", "err", )
			.Reset()
			return
		}
	}

	if  := .SetProtocol();  != nil {
		log.Debug("error setting stream protocol", "err", )
		.ResetWithError(network.StreamResourceLimitExceeded)
		return
	}

	log.Debug("negotiated", "protocol", , "duration", )

	(, )
}

// ID returns the (local) peer.ID associated with this Host
func ( *BasicHost) () peer.ID {
	return .Network().LocalPeer()
}

// Peerstore returns the Host's repository of Peer Addresses and Keys.
func ( *BasicHost) () peerstore.Peerstore {
	return .Network().Peerstore()
}

// Network returns the Network interface of the Host
func ( *BasicHost) () network.Network {
	return .network
}

// Mux returns the Mux multiplexing incoming streams to protocol handlers
func ( *BasicHost) () protocol.Switch {
	return .mux
}

// IDService returns
func ( *BasicHost) () identify.IDService {
	return .ids
}

func ( *BasicHost) () event.Bus {
	return .eventbus
}

// SetStreamHandler sets the protocol handler on the Host's Mux.
// This is equivalent to:
//
//	host.Mux().SetHandler(proto, handler)
//
// (Thread-safe)
func ( *BasicHost) ( protocol.ID,  network.StreamHandler) {
	.Mux().AddHandler(, func( protocol.ID,  io.ReadWriteCloser) error {
		 := .(network.Stream)
		()
		return nil
	})
	.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
		Added: []protocol.ID{},
	})
}

// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function to do protocol comparisons
func ( *BasicHost) ( protocol.ID,  func(protocol.ID) bool,  network.StreamHandler) {
	.Mux().AddHandlerWithFunc(, , func( protocol.ID,  io.ReadWriteCloser) error {
		 := .(network.Stream)
		()
		return nil
	})
	.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
		Added: []protocol.ID{},
	})
}

// RemoveStreamHandler returns ..
func ( *BasicHost) ( protocol.ID) {
	.Mux().RemoveHandler()
	.emitters.evtLocalProtocolsUpdated.Emit(event.EvtLocalProtocolsUpdated{
		Removed: []protocol.ID{},
	})
}

// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// (Thread-safe)
func ( *BasicHost) ( context.Context,  peer.ID,  ...protocol.ID) ( network.Stream,  error) {
	if ,  := .Deadline(); ! {
		if .negtimeout > 0 {
			var  context.CancelFunc
			,  = context.WithTimeout(, .negtimeout)
			defer ()
		}
	}

	// If the caller wants to prevent the host from dialing, it should use the NoDial option.
	if ,  := network.GetNoDial(); ! {
		 := .Connect(, peer.AddrInfo{ID: })
		if  != nil {
			return nil, 
		}
	}

	,  := .Network().NewStream(network.WithNoDial(, "already dialed"), )
	if  != nil {
		// TODO: It would be nicer to get the actual error from the swarm,
		// but this will require some more work.
		if errors.Is(, network.ErrNoConn) {
			return nil, errors.New("connection failed")
		}
		return nil, fmt.Errorf("failed to open stream: %w", )
	}
	defer func() {
		if  != nil &&  != nil {
			.ResetWithError(network.StreamProtocolNegotiationFailed)
		}
	}()

	// Wait for any in-progress identifies on the connection to finish. This
	// is faster than negotiating.
	//
	// If the other side doesn't support identify, that's fine. This will
	// just be a no-op.
	select {
	case <-.ids.IdentifyWait(.Conn()):
	case <-.Done():
		return nil, fmt.Errorf("identify failed to complete: %w", .Err())
	}

	,  := .preferredProtocol(, )
	if  != nil {
		return nil, 
	}

	if  != "" {
		if  := .SetProtocol();  != nil {
			return nil, 
		}
		 := msmux.NewMSSelect(, )
		return &streamWrapper{
			Stream: ,
			rw:     ,
		}, nil
	}

	// Negotiate the protocol in the background, obeying the context.
	var  protocol.ID
	 := make(chan error, 1)
	go func() {
		,  = msmux.SelectOneOf(, )
		 <- 
	}()
	select {
	case  = <-:
		if  != nil {
			return nil, fmt.Errorf("failed to negotiate protocol: %w", )
		}
	case <-.Done():
		.ResetWithError(network.StreamProtocolNegotiationFailed)
		// wait for `SelectOneOf` to error out because of resetting the stream.
		<-
		return nil, fmt.Errorf("failed to negotiate protocol: %w", .Err())
	}

	if  := .SetProtocol();  != nil {
		.ResetWithError(network.StreamResourceLimitExceeded)
		return nil, 
	}
	_ = .Peerstore().AddProtocols(, ) // adding the protocol to the peerstore isn't critical
	return , nil
}

func ( *BasicHost) ( peer.ID,  []protocol.ID) (protocol.ID, error) {
	,  := .Peerstore().SupportsProtocols(, ...)
	if  != nil {
		return "", 
	}

	var  protocol.ID
	if len() > 0 {
		 = [0]
	}
	return , nil
}

// Connect ensures there is a connection between this host and the peer with
// given peer.ID. If there is not an active connection, Connect will issue a
// h.Network.Dial, and block until a connection is open, or an error is returned.
// Connect will absorb the addresses in pi into its internal peerstore.
// It will also resolve any /dns4, /dns6, and /dnsaddr addresses.
func ( *BasicHost) ( context.Context,  peer.AddrInfo) error {
	// absorb addresses into peerstore
	.Peerstore().AddAddrs(.ID, .Addrs, peerstore.TempAddrTTL)

	,  := network.GetForceDirectDial()
	,  := network.GetAllowLimitedConn()
	if ! {
		 := .Network().Connectedness(.ID)
		if  == network.Connected || ( &&  == network.Limited) {
			return nil
		}
	}

	return .dialPeer(, .ID)
}

// dialPeer opens a connection to peer, and makes sure to identify
// the connection once it has been opened.
func ( *BasicHost) ( context.Context,  peer.ID) error {
	log.Debug("host dialing peer", "source_peer", .ID(), "destination_peer", )
	,  := .Network().DialPeer(, )
	if  != nil {
		return fmt.Errorf("failed to dial: %w", )
	}

	// TODO: Consider removing this? On one hand, it's nice because we can
	// assume that things like the agent version are usually set when this
	// returns. On the other hand, we don't _really_ need to wait for this.
	//
	// This is mostly here to preserve existing behavior.
	select {
	case <-.ids.IdentifyWait():
	case <-.Done():
		return fmt.Errorf("identify failed to complete: %w", .Err())
	}

	log.Debug("host finished dialing peer", "source_peer", .ID(), "destination_peer", )
	return nil
}

func ( *BasicHost) () connmgr.ConnManager {
	return .cmgr
}

// Addrs returns listening addresses.
// When used with AutoRelay, and if the host is not publicly reachable,
// this will not have the host's direct public addresses, it'll only have
// the relay addresses and private addresses.
func ( *BasicHost) () []ma.Multiaddr {
	return .addressManager.Addrs()
}

// AllAddrs returns all the addresses the host is listening on except circuit addresses.
func ( *BasicHost) () []ma.Multiaddr {
	return .addressManager.DirectAddrs()
}

// ConfirmedAddrs returns all addresses of the host grouped by their reachability
// as verified by autonatv2.
//
// Experimental: This API may change in the future without deprecation.
//
// Requires AutoNATv2 to be enabled.
func ( *BasicHost) () ( []ma.Multiaddr,  []ma.Multiaddr,  []ma.Multiaddr) {
	return .addressManager.ConfirmedAddrs()
}

// SetAutoNat sets the autonat service for the host.
func ( *BasicHost) ( autonat.AutoNAT) {
	.autoNATMx.Lock()
	defer .autoNATMx.Unlock()
	if .autoNat == nil {
		.autoNat = 
	}
}

// GetAutoNat returns the host's AutoNAT service, if AutoNAT is enabled.
//
// Deprecated: Use `BasicHost.Reachability` to get the host's reachability.
func ( *BasicHost) () autonat.AutoNAT {
	.autoNATMx.Lock()
	defer .autoNATMx.Unlock()
	return .autoNat
}

// Reachability returns the host's reachability status.
func ( *BasicHost) () network.Reachability {
	return *.addressManager.hostReachability.Load()
}

// Close shuts down the Host's services (network, etc).
func ( *BasicHost) () error {
	.closeSync.Do(func() {
		.ctxCancel()
		if .cmgr != nil {
			.cmgr.Close()
		}

		if .ids != nil {
			.ids.Close()
		}
		if .autoNat != nil {
			.autoNat.Close()
		}
		if .relayManager != nil {
			.relayManager.Close()
		}
		if .hps != nil {
			.hps.Close()
		}
		if .autonatv2 != nil {
			.autonatv2.Close()
		}

		_ = .emitters.evtLocalProtocolsUpdated.Close()

		if  := .network.Close();  != nil {
			log.Error("swarm close failed", "err", )
		}

		.addressManager.Close()
		.psManager.Close()
		if .Peerstore() != nil {
			.Peerstore().Close()
		}

		.refCount.Wait()

		if .Network().ResourceManager() != nil {
			.Network().ResourceManager().Close()
		}
	})

	return nil
}

type streamWrapper struct {
	network.Stream
	rw io.ReadWriteCloser
}

func ( *streamWrapper) ( []byte) (int, error) {
	return .rw.Read()
}

func ( *streamWrapper) ( []byte) (int, error) {
	return .rw.Write()
}

func ( *streamWrapper) () error {
	// Set a read deadline to prevent Close() from blocking indefinitely
	// waiting for the multistream-select handshake to complete.
	// This can happen when the remote peer is slow or unresponsive.
	// See: https://github.com/multiformats/go-multistream/issues/47
	_ = .Stream.SetReadDeadline(time.Now().Add(DefaultNegotiationTimeout))
	return .rw.Close()
}

func ( *streamWrapper) () error {
	// Flush the handshake before closing, but ignore the error. The other
	// end may have closed their side for reading.
	//
	// If something is wrong with the stream, the user will get on error on
	// read instead.
	if ,  := .rw.(interface{ () error });  {
		_ = .()
	}
	return .Stream.CloseWrite()
}