func NewPortManager() *PortManager {
	return &PortManager{allocatedPorts: make(map[portDescriptor]bindAddresses)}
}
NewPortManager creates new PortManager.
package ports
import (
	"math"
	"math/rand"
	"sync"
	"sync/atomic"
	"github.com/google/netstack/tcpip"
)
const (
	FirstEphemeral = 16000
	numEphemeralPorts = math.MaxUint16 - FirstEphemeral + 1
	anyIPAddress tcpip.Address = ""
)
type portDescriptor struct {
	network   tcpip.NetworkProtocolNumber
	transport tcpip.TransportProtocolNumber
	port      uint16
}
type PortManager struct {
	mu             sync.RWMutex
	allocatedPorts map[portDescriptor]bindAddresses
	hint uint32
}
type portNode struct {
	reuse bool
	refs  int
}
type deviceNode map[tcpip.NICID]portNode
func (d deviceNode) isAvailable(reuse bool, bindToDevice tcpip.NICID) bool {
	if bindToDevice == 0 {
		if !reuse {
			return false
		}
		for _, p := range d {
			if !p.reuse {
				return false
			}
		}
		return true
	}
	if p, ok := d[0]; ok {
		if !reuse || !p.reuse {
			return false
		}
	}
	if p, ok := d[bindToDevice]; ok {
		if !reuse || !p.reuse {
			return false
		}
	}
	return true
}
type bindAddresses map[tcpip.Address]deviceNode
func (b bindAddresses) isAvailable(addr tcpip.Address, reuse bool, bindToDevice tcpip.NICID) bool {
	if addr == anyIPAddress {
		for _, d := range b {
			if !d.isAvailable(reuse, bindToDevice) {
				return false
			}
		}
		return true
	}
	if d, ok := b[anyIPAddress]; ok {
		if !d.isAvailable(reuse, bindToDevice) {
			return false
		}
	}
	if d, ok := b[addr]; ok {
		if !d.isAvailable(reuse, bindToDevice) {
			return false
		}
	}
	return true
}