package autonat

import (
	

	

	ma 
	manet 
)

type dialPolicy struct {
	allowSelfDials bool
	host           host.Host
}

// skipDial indicates that a multiaddress isn't worth attempted dialing.
// The same logic is used when the autonat client is considering if
// a remote peer is worth using as a server, and when the server is
// considering if a requested client is worth dialing back.
func ( *dialPolicy) ( ma.Multiaddr) bool {
	// skip relay addresses
	,  := .ValueForProtocol(ma.P_CIRCUIT)
	if  == nil {
		return true
	}

	if .allowSelfDials {
		return false
	}

	// skip private network (unroutable) addresses
	if !manet.IsPublicAddr() {
		return true
	}
	,  := manet.ToIP()
	if  != nil {
		return true
	}

	// Skip dialing addresses we believe are the local node's
	for ,  := range .host.Addrs() {
		,  := manet.ToIP()
		if  != nil {
			continue
		}
		if .Equal() {
			return true
		}
	}

	return false
}

// skipPeer indicates that the collection of multiaddresses representing a peer
// isn't worth attempted dialing. If one of the addresses matches an address
// we believe is ours, we exclude the peer, even if there are other valid
// public addresses in the list.
func ( *dialPolicy) ( []ma.Multiaddr) bool {
	 := .host.Addrs()
	 := make([]net.IP, 0)
	for ,  := range  {
		if ,  := .ValueForProtocol(ma.P_CIRCUIT);  != nil && manet.IsPublicAddr() {
			,  := manet.ToIP()
			if  != nil {
				continue
			}
			 = append(, )
		}
	}

	// if a public IP of the peer is one of ours: skip the peer.
	 := false
	for ,  := range  {
		if ,  := .ValueForProtocol(ma.P_CIRCUIT);  != nil && manet.IsPublicAddr() {
			,  := manet.ToIP()
			if  != nil {
				continue
			}

			for ,  := range  {
				if .Equal() {
					return true
				}
			}
			 = true
		}
	}

	if .allowSelfDials {
		return false
	}

	return !
}