func (s *Stack) NowNanoseconds() int64 {
	return s.clock.NowNanoseconds()
}
NowNanoseconds implements tcpip.Clock.NowNanoseconds.
package stack
import (
	"encoding/binary"
	"sync"
	"sync/atomic"
	"time"
	"github.com/google/netstack/rand"
	"github.com/google/netstack/sleep"
	"github.com/google/netstack/tcpip"
	"github.com/google/netstack/tcpip/buffer"
	"github.com/google/netstack/tcpip/header"
	"github.com/google/netstack/tcpip/iptables"
	"github.com/google/netstack/tcpip/ports"
	"github.com/google/netstack/tcpip/seqnum"
	"github.com/google/netstack/waiter"
	"golang.org/x/time/rate"
)
const (
	ageLimit = 1 * time.Minute
	resolutionTimeout = 1 * time.Second
	resolutionAttempts = 3
	DefaultTOS = 0
)
type transportProtocolState struct {
	proto          TransportProtocol
	defaultHandler func(r *Route, id TransportEndpointID, pkt tcpip.PacketBuffer) bool
}
type TCPProbeFunc func(s TCPEndpointState)
type TCPCubicState struct {
	WLastMax                float64
	WMax                    float64
	T                       time.Time
	TimeSinceLastCongestion time.Duration
	C                       float64
	K                       float64
	Beta                    float64
	WC                      float64
	WEst                    float64
}
type TCPEndpointID struct {
	LocalPort uint16
	LocalAddress tcpip.Address
	RemotePort uint16
	RemoteAddress tcpip.Address
}
type TCPFastRecoveryState struct {
	Active bool
	First seqnum.Value
	Last seqnum.Value
	MaxCwnd int
	HighRxt seqnum.Value
	RescueRxt seqnum.Value
}
type TCPReceiverState struct {
	RcvNxt seqnum.Value
	RcvAcc seqnum.Value
	RcvWndScale uint8
	PendingBufUsed seqnum.Size
	PendingBufSize seqnum.Size
}
type TCPSenderState struct {
	LastSendTime time.Time
	DupAckCount int
	SndCwnd int
	Ssthresh int
	SndCAAckCount int
	Outstanding int
	SndWnd seqnum.Size
	SndUna seqnum.Value
	SndNxt seqnum.Value
	RTTMeasureSeqNum seqnum.Value
	RTTMeasureTime time.Time
	Closed bool
	SRTT time.Duration
	RTO time.Duration
	RTTVar time.Duration
	SRTTInited bool
	MaxPayloadSize int
	SndWndScale uint8
	MaxSentAck seqnum.Value
	FastRecovery TCPFastRecoveryState
	Cubic TCPCubicState
}
type TCPSACKInfo struct {
	Blocks []header.SACKBlock
	ReceivedBlocks []header.SACKBlock
	MaxSACKED seqnum.Value
}
type RcvBufAutoTuneParams struct {
	MeasureTime time.Time
	CopiedBytes int
	PrevCopiedBytes int
	RcvBufSize int
	RTT time.Duration
	RTTVar time.Duration
	RTTMeasureSeqNumber seqnum.Value
	RTTMeasureTime time.Time
	Disabled bool
}
type TCPEndpointState struct {
	ID TCPEndpointID
	SegTime time.Time
	RcvBufSize int
	RcvBufUsed int
	RcvAutoParams RcvBufAutoTuneParams
	RcvClosed bool
	SendTSOk bool
	RecentTS uint32
	TSOffset uint32
	SACKPermitted bool
	SACK TCPSACKInfo
	SndBufSize int
	SndBufUsed int
	SndClosed bool
	SndBufInQueue seqnum.Size
	PacketTooBigCount int
	SndMTU int
	Receiver TCPReceiverState
	Sender TCPSenderState
}
type ResumableEndpoint interface {
	Resume(*Stack)
}
type uniqueIDGenerator uint64
func (u *uniqueIDGenerator) UniqueID() uint64 {
	return atomic.AddUint64((*uint64)(u), 1)
}
type Stack struct {
	transportProtocols map[tcpip.TransportProtocolNumber]*transportProtocolState
	networkProtocols   map[tcpip.NetworkProtocolNumber]NetworkProtocol
	linkAddrResolvers  map[tcpip.NetworkProtocolNumber]LinkAddressResolver
	rawFactory RawFactory
	demux *transportDemuxer
	stats tcpip.Stats
	linkAddrCache *linkAddrCache
	mu               sync.RWMutex
	nics             map[tcpip.NICID]*NIC
	forwarding       bool
	cleanupEndpoints map[TransportEndpoint]struct{}
	routeTable []tcpip.Route
	*ports.PortManager
	tcpProbeFunc TCPProbeFunc
	clock tcpip.Clock
	handleLocal bool
	tables iptables.IPTables
	resumableEndpoints []ResumableEndpoint
	icmpRateLimiter *ICMPRateLimiter
	seed uint32
	ndpConfigs NDPConfigurations
	autoGenIPv6LinkLocal bool
	ndpDisp NDPDispatcher
	uniqueIDGenerator UniqueID
}
type UniqueID interface {
	UniqueID() uint64
}
type Options struct {
	NetworkProtocols []NetworkProtocol
	TransportProtocols []TransportProtocol
	Clock tcpip.Clock
	Stats tcpip.Stats
	HandleLocal bool
	UniqueID UniqueID
	NDPConfigs NDPConfigurations
	AutoGenIPv6LinkLocal bool
	NDPDisp NDPDispatcher
	RawFactory RawFactory
}
type TransportEndpointInfo struct {
	NetProto   tcpip.NetworkProtocolNumber
	TransProto tcpip.TransportProtocolNumber
	ID TransportEndpointID
	BindNICID tcpip.NICID
	BindAddr  tcpip.Address
	RegisterNICID tcpip.NICID
}
func (*TransportEndpointInfo) IsEndpointInfo() {}
func New(opts Options) *Stack {
	clock := opts.Clock
	if clock == nil {
		clock = &tcpip.StdClock{}
	}
	if opts.UniqueID == nil {
		opts.UniqueID = new(uniqueIDGenerator)
	}
	opts.NDPConfigs.validate()
	s := &Stack{
		transportProtocols:   make(map[tcpip.TransportProtocolNumber]*transportProtocolState),
		networkProtocols:     make(map[tcpip.NetworkProtocolNumber]NetworkProtocol),
		linkAddrResolvers:    make(map[tcpip.NetworkProtocolNumber]LinkAddressResolver),
		nics:                 make(map[tcpip.NICID]*NIC),
		cleanupEndpoints:     make(map[TransportEndpoint]struct{}),
		linkAddrCache:        newLinkAddrCache(ageLimit, resolutionTimeout, resolutionAttempts),
		PortManager:          ports.NewPortManager(),
		clock:                clock,
		stats:                opts.Stats.FillIn(),
		handleLocal:          opts.HandleLocal,
		icmpRateLimiter:      NewICMPRateLimiter(),
		seed:                 generateRandUint32(),
		ndpConfigs:           opts.NDPConfigs,
		autoGenIPv6LinkLocal: opts.AutoGenIPv6LinkLocal,
		uniqueIDGenerator:    opts.UniqueID,
		ndpDisp:              opts.NDPDisp,
	}
	for _, netProto := range opts.NetworkProtocols {
		s.networkProtocols[netProto.Number()] = netProto
		if r, ok := netProto.(LinkAddressResolver); ok {
			s.linkAddrResolvers[r.LinkAddressProtocol()] = r
		}
	}
	for _, transProto := range opts.TransportProtocols {
		s.transportProtocols[transProto.Number()] = &transportProtocolState{
			proto: transProto,
		}
	}
	s.rawFactory = opts.RawFactory
	s.demux = newTransportDemuxer(s)
	return s
}
func (s *Stack) UniqueID() uint64 {
	return s.uniqueIDGenerator.UniqueID()
}
func (s *Stack) SetNetworkProtocolOption(network tcpip.NetworkProtocolNumber, option interface{}) *tcpip.Error {
	netProto, ok := s.networkProtocols[network]
	if !ok {
		return tcpip.ErrUnknownProtocol
	}
	return netProto.SetOption(option)
}
func (s *Stack) NetworkProtocolOption(network tcpip.NetworkProtocolNumber, option interface{}) *tcpip.Error {
	netProto, ok := s.networkProtocols[network]
	if !ok {
		return tcpip.ErrUnknownProtocol
	}
	return netProto.Option(option)
}
func (s *Stack) SetTransportProtocolOption(transport tcpip.TransportProtocolNumber, option interface{}) *tcpip.Error {
	transProtoState, ok := s.transportProtocols[transport]
	if !ok {
		return tcpip.ErrUnknownProtocol
	}
	return transProtoState.proto.SetOption(option)
}
func (s *Stack) TransportProtocolOption(transport tcpip.TransportProtocolNumber, option interface{}) *tcpip.Error {
	transProtoState, ok := s.transportProtocols[transport]
	if !ok {
		return tcpip.ErrUnknownProtocol
	}
	return transProtoState.proto.Option(option)
}
func (s *Stack) SetTransportProtocolHandler(p tcpip.TransportProtocolNumber, h func(*Route, TransportEndpointID, tcpip.PacketBuffer) bool) {
	state := s.transportProtocols[p]
	if state != nil {
		state.defaultHandler = h
	}
}