github.com/yaling888/clash@v1.53.0/listener/tun/ipstack/system/udp.go (about)

     1  package system
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	"github.com/yaling888/clash/listener/tun/ipstack/system/mars/nat"
     8  )
     9  
    10  type packet struct {
    11  	sender *nat.UDP
    12  	data   *nat.UDPElement
    13  	lAddr  netip.AddrPort
    14  }
    15  
    16  func (pkt *packet) Data() *[]byte {
    17  	if pkt.data != nil {
    18  		return pkt.data.Packet
    19  	}
    20  	return nil
    21  }
    22  
    23  func (pkt *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
    24  	a := addr.(*net.UDPAddr)
    25  	na, _ := netip.AddrFromSlice(a.IP)
    26  	na = na.WithZone(a.Zone)
    27  	if pkt.lAddr.Addr().Is4() {
    28  		na = na.Unmap()
    29  	}
    30  	return pkt.sender.WriteTo(b, netip.AddrPortFrom(na, uint16(a.Port)), pkt.lAddr)
    31  }
    32  
    33  func (pkt *packet) LocalAddr() net.Addr {
    34  	return net.UDPAddrFromAddrPort(pkt.lAddr)
    35  }
    36  
    37  func (pkt *packet) Drop() {
    38  	pkt.sender.PutUDPElement(pkt.data)
    39  	pkt.data = nil
    40  }