github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/tun/packetaddradaptar.go (about)

     1  package tun
     2  
     3  import (
     4  	"gvisor.dev/gvisor/pkg/tcpip"
     5  	"gvisor.dev/gvisor/pkg/tcpip/stack"
     6  
     7  	"github.com/v2fly/v2ray-core/v5/app/tun/device"
     8  	"github.com/v2fly/v2ray-core/v5/app/tun/tunsorter"
     9  )
    10  
    11  func NewDeviceWithSorter(overlay device.Device, sorter *tunsorter.TunSorter) device.Device {
    12  	return &packetAddrDevice{
    13  		Device: overlay,
    14  		sorter: sorter,
    15  	}
    16  }
    17  
    18  type packetAddrDevice struct {
    19  	device.Device
    20  	sorter *tunsorter.TunSorter
    21  
    22  	secondaryDispatcher stack.NetworkDispatcher
    23  }
    24  
    25  func (p *packetAddrDevice) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) {
    26  	buf := pkt.ToBuffer()
    27  	_, err := p.sorter.OnPacketReceived(buf.Flatten())
    28  	if err != nil {
    29  		p.secondaryDispatcher.DeliverNetworkPacket(protocol, pkt)
    30  	}
    31  }
    32  
    33  func (p *packetAddrDevice) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt stack.PacketBufferPtr) {
    34  	// TODO implement me
    35  	panic("implement me")
    36  }
    37  
    38  func (p *packetAddrDevice) Attach(dispatcher stack.NetworkDispatcher) {
    39  	p.secondaryDispatcher = dispatcher
    40  	p.Device.Attach(p)
    41  }