package socket

import (
	
	
	
	

	
	
)

// Socket holds the magic socket's mapped-address tables: the bidirectional maps
// between transport addresses and the synthetic IPv6 ULAs that quic-go uses to
// address paths. It is the Go analog of the Rust Socket's mapped_addrs
// (iroh/src/socket.rs:332).
//
// A Socket is created by [NewSocket] and shared by a [MagicConn] and its
// [Transports]. It is safe for concurrent use. The zero Socket is not usable;
// use [NewSocket].
type Socket struct {
	// endpointAddrs maps endpoint ids to endpoint-id mapped addresses used for
	// initial packets before a concrete path is selected.
	endpointAddrs *AddrMap[key.EndpointID, EndpointIDMappedAddr]

	// relayAddrs maps (relay url, endpoint id) pairs to relay mapped addresses.
	// Keyed by the URL's string form because netaddr.RelayURL wraps a pointer
	// and is not reliably comparable across separately-parsed URLs.
	relayAddrs *AddrMap[relayMapKey, RelayMappedAddr]
	// relayByKey recovers the original RelayKey. relayHeard is the sentinel
	// of a list of its entries, most recently used first.
	relayMu    sync.Mutex
	relayByKey map[relayMapKey]*relayEntry
	relayHeard relayEntry

	// customAddrs maps a custom address (by its string key) to a custom mapped
	// address.
	customAddrs *AddrMap[string, CustomMappedAddr]
	// customByKey recovers the original netaddr.CustomAddr from its string key.
	customMu    sync.Mutex
	customByKey map[string]netaddr.CustomAddr

	closed atomic.Bool
}

type relayMapKey struct {
	url string
	eid key.EndpointID
}

// relayEntry is one relay mapping in the recency list.
type relayEntry struct {
	RelayKey
	key        relayMapKey
	prev, next *relayEntry
}

// maxRelayAddrs bounds the relay mapped-address table. A datagram from any
// endpoint id the relay forwards creates a mapping before QUIC parses it, and
// only a remote that completes a handshake is ever evicted, so a relay-side
// peer cycling endpoint ids could otherwise retain state for the life of the
// socket. A full table drops the mapping used longest ago; a remote still
// exchanging datagrams refreshes its mapping on every one and keeps it. The
// bound matches maxLocalAddrs, the other per-remote table in this package.
const maxRelayAddrs = maxLocalAddrs

// RelayKey identifies a relay path: a relay URL together with the remote
// endpoint reached through it. It is the key type of the relay mapped-address
// table.
type RelayKey struct {
	URL netaddr.RelayURL
	EID key.EndpointID
}

// NewSocket returns a ready Socket with empty mapped-address tables.
func () *Socket {
	 := &Socket{
		endpointAddrs: NewAddrMap[key.EndpointID, EndpointIDMappedAddr](
			NewEndpointIDMappedAddr,
			func( EndpointIDMappedAddr) netip.Addr { return .Addr() },
		),
		relayAddrs: NewAddrMap[relayMapKey, RelayMappedAddr](
			NewRelayMappedAddr,
			func( RelayMappedAddr) netip.Addr { return .Addr() },
		),
		relayByKey: make(map[relayMapKey]*relayEntry),
		customAddrs: NewAddrMap[string, CustomMappedAddr](
			NewCustomMappedAddr,
			func( CustomMappedAddr) netip.Addr { return .Addr() },
		),
		customByKey: make(map[string]netaddr.CustomAddr),
	}
	.relayHeard.prev, .relayHeard.next = &.relayHeard, &.relayHeard
	return 
}

// Close marks the socket closed. Subsequent sends are dropped (blackholed) so
// quic-go's loss recovery handles in-flight datagrams rather than seeing a hard
// error. It is idempotent.
func ( *Socket) () { .closed.Store(true) }

// IsClosed reports whether the socket has been closed.
func ( *Socket) () bool { return .closed.Load() }

// EndpointIDMappedAddrFor returns the endpoint-id mapped address for id,
// allocating one on first use.
func ( *Socket) ( key.EndpointID) EndpointIDMappedAddr {
	return .endpointAddrs.Get()
}

// LookupEndpointID returns the endpoint id for an endpoint-id mapped address, if
// known.
func ( *Socket) ( EndpointIDMappedAddr) (key.EndpointID, bool) {
	return .endpointAddrs.Lookup(.Addr())
}

// RelayMappedAddrFor returns the relay mapped address for the (url, eid) pair,
// allocating one on first use. Each call marks the pair as the most recently
// used; see [maxRelayAddrs].
func ( *Socket) ( netaddr.RelayURL,  key.EndpointID) RelayMappedAddr {
	 := relayMapKey{.String(), }
	.relayMu.Lock()
	defer .relayMu.Unlock()
	if ,  := .relayByKey[];  {
		.relayUnlink()
		.relayLinkFront()
	} else {
		if len(.relayByKey) >= maxRelayAddrs {
			.relayForget(.relayHeard.prev)
		}
		 := &relayEntry{RelayKey: RelayKey{URL: , EID: }, key: }
		.relayByKey[] = 
		.relayLinkFront()
	}
	return .relayAddrs.Get()
}

// relayForget drops the relay mapping e. Caller holds relayMu.
func ( *Socket) ( *relayEntry) {
	.relayUnlink()
	delete(.relayByKey, .key)
	.relayAddrs.Remove(.key)
}

// relayLinkFront puts e at the head of the recency list. Caller holds relayMu.
func ( *Socket) ( *relayEntry) {
	.prev, .next = &.relayHeard, .relayHeard.next
	.next.prev = 
	.relayHeard.next = 
}

// relayUnlink removes e from the recency list. Caller holds relayMu.
func ( *Socket) ( *relayEntry) {
	.prev.next, .next.prev = .next, .prev
	.prev, .next = nil, nil
}

// LookupRelay returns the (url, eid) pair for a relay mapped address, if known.
func ( *Socket) ( RelayMappedAddr) (RelayKey, bool) {
	,  := .relayAddrs.Lookup(.Addr())
	if ! {
		return RelayKey{}, false
	}
	.relayMu.Lock()
	,  := .relayByKey[]
	.relayMu.Unlock()
	if ! {
		return RelayKey{}, false
	}
	return .RelayKey, true
}

// CustomMappedAddrFor returns the custom mapped address for c, allocating one on
// first use and recording the reverse mapping back to c.
func ( *Socket) ( netaddr.CustomAddr) CustomMappedAddr {
	 := .String()
	.customMu.Lock()
	.customByKey[] = 
	.customMu.Unlock()
	return .customAddrs.Get()
}

// PathAddr classifies a QUIC connection's remote net.Addr into the magic
// socket's transport [Addr]: a real IP becomes an IP path; a relay or custom
// mapped ULA is reverse-looked-up through the mapped-address tables. An unknown
// mapped address (or one whose mapping has been forgotten) falls back to an IP
// path so the per-remote actor still tracks a stable address. remoteID is used
// for relay paths, which are keyed by (relay url, endpoint id).
func ( *Socket) ( key.EndpointID,  net.Addr) Addr {
	,  := addrPort()
	if ! {
		return Addr{}
	}
	switch Classify(.Addr()) {
	case KindRelay:
		if ,  := .LookupRelay(RelayMappedAddrFromAddr(.Addr()));  {
			return RelayAddr(.URL, .EID)
		}
		return IPAddr()
	case KindCustom:
		if ,  := .LookupCustom(CustomMappedAddr{a: .Addr()});  {
			return CustomAddr()
		}
		return IPAddr()
	default:
		return IPAddr()
	}
}

// EvictRemote drops the mapped addresses recorded for a reaped remote: the
// endpoint-id mapping for id, every relay mapping whose remote endpoint is id,
// and the custom mappings among addrs (the remote's known transport
// addresses). Without eviction the tables grow without bound under peer churn
// (the upstream Rust implementation has the same leak, iroh issue #4293). A
// mapping is regenerated on the next use of the same key, so evicting a remote
// that immediately returns only costs a fresh mapped address.
func ( *Socket) ( key.EndpointID,  []Addr) {
	.endpointAddrs.Remove()

	.relayMu.Lock()
	for ,  := range .relayByKey {
		if .EID ==  {
			.relayForget()
		}
	}
	.relayMu.Unlock()

	for ,  := range  {
		,  := .Custom()
		if ! {
			continue
		}
		 := .String()
		.customMu.Lock()
		delete(.customByKey, )
		.customMu.Unlock()
		.customAddrs.Remove()
	}
}

// LookupCustom returns the custom address for a custom mapped address, if known.
func ( *Socket) ( CustomMappedAddr) (netaddr.CustomAddr, bool) {
	,  := .customAddrs.Lookup(.Addr())
	if ! {
		return netaddr.CustomAddr{}, false
	}
	.customMu.Lock()
	,  := .customByKey[]
	.customMu.Unlock()
	return , 
}