func NewProtocol() stack.NetworkProtocol {
	return &protocol{}
}
NewProtocol creates a new protocol ipv4 protocol descriptor. This is exported
only for tests that short-circuit the stack. Regular use of the protocol is
done via the stack, which gets a protocol descriptor from the init() function
below.
package ipv4
import (
	"log"
	"sync/atomic"
	"github.com/brewlin/net-protocol/pkg/buffer"
	tcpip "github.com/brewlin/net-protocol/protocol"
	"github.com/brewlin/net-protocol/protocol/header"
	"github.com/brewlin/net-protocol/protocol/network/fragmentation"
	"github.com/brewlin/net-protocol/protocol/network/hash"
	"github.com/brewlin/net-protocol/stack"
)
const (
	ProtocolName = "ipv4"
	ProtocolNumber = header.IPv4ProtocolNumber
	maxTotalSize = 0xffff
	buckets = 2048
)
type endpoint struct {
	nicid tcpip.NICID
	id stack.NetworkEndpointID
	linkEP stack.LinkEndpoint
	dispatcher stack.TransportDispatcher
	echoRequests chan echoRequest
	fragmentation *fragmentation.Fragmentation
}
func (p *protocol) NewEndpoint(nicid tcpip.NICID, addr tcpip.Address, linkAddrCache stack.LinkAddressCache,
	dispatcher stack.TransportDispatcher, linkEP stack.LinkEndpoint) (stack.NetworkEndpoint, *tcpip.Error) {
	e := &endpoint{
		nicid:        nicid,
		id:           stack.NetworkEndpointID{LocalAddress: addr},
		linkEP:       linkEP,
		dispatcher:   dispatcher,
		echoRequests: make(chan echoRequest, 10),
		fragmentation: fragmentation.NewFragmentation(fragmentation.HighFragThreshold,
			fragmentation.LowFragThreshold, fragmentation.DefaultReassembleTimeout),
	}
	go e.echoReplier()
	return e, nil
}
func (e *endpoint) DefaultTTL() uint8 {
	return 255
}
func (e *endpoint) MTU() uint32 {
	return calculateMTU(e.linkEP.MTU())
}
func (e *endpoint) Capabilities() stack.LinkEndpointCapabilities {
	return e.linkEP.Capabilities()
}
func (e *endpoint) NICID() tcpip.NICID {
	return e.nicid
}
func (e *endpoint) ID() *stack.NetworkEndpointID {
	return &e.id
}
func (e *endpoint) MaxHeaderLength() uint16 {
	return e.linkEP.MaxHeaderLength() + header.IPv4MinimumSize
}
func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable, payload buffer.VectorisedView,
	protocol tcpip.TransportProtocolNumber, ttl uint8) *tcpip.Error {
	ip := header.IPv4(hdr.Prepend(header.IPv4MinimumSize))
	length := uint16(hdr.UsedLength() + payload.Size())
	id := uint32(0)
	if length > header.IPv4MaximumHeaderSize+8 {
		id = atomic.AddUint32(&ids[hashRoute(r, protocol)%buckets], 1)
	}
	ip.Encode(&header.IPv4Fields{
		IHL:         header.IPv4MinimumSize,
		TotalLength: length,
		ID:          uint16(id),
		TTL:         ttl,
		Protocol:    uint8(protocol),
		SrcAddr:     r.LocalAddress,
		DstAddr:     r.RemoteAddress,
	})
	ip.SetChecksum(^ip.CalculateChecksum())
	r.Stats().IP.PacketsSent.Increment()
	log.Printf("@网络层 ipv4: 接受传输层数据 封装ip头部信息，写入链路层网卡数据 send ipv4 packet %d bytes, proto: 0x%x", hdr.UsedLength()+payload.Size(), protocol)
	return e.linkEP.WritePacket(r, hdr, payload, ProtocolNumber)
}
func (e *endpoint) HandlePacket(r *stack.Route, vv buffer.VectorisedView) {
	log.Println("@网络层 ipv4: handlePacket 数据包处理")
	h := header.IPv4(vv.First())
	if !h.IsValid(vv.Size()) {
		return
	}
	hlen := int(h.HeaderLength())
	tlen := int(h.TotalLength())
	vv.TrimFront(hlen)
	vv.CapLength(tlen - hlen)
	more := (h.Flags() & header.IPv4FlagMoreFragments) != 0
	if more || h.FragmentOffset() != 0 {
		last := h.FragmentOffset() + uint16(vv.Size()) - 1
		var ready bool
		vv, ready = e.fragmentation.Process(hash.IPv4FragmentHash(h), h.FragmentOffset(), last, more, vv)
		if !ready {
			return
		}
	}
	p := h.TransportProtocol()
	if p == header.ICMPv4ProtocolNumber {
		e.handleICMP(r, vv)
		return
	}
	r.Stats().IP.PacketsDelivered.Increment()
	log.Printf("@网络层 ipv4: handlePacket 分发协议处理，recv ipv4 packet %d bytes, proto: 0x%x", tlen, p)
	e.dispatcher.DeliverTransportPacket(r, p, vv)
}
func (e *endpoint) Close() {
	close(e.echoRequests)
}
type protocol struct{}