github.com/metacubex/mihomo@v1.18.5/listener/tunnel/packet.go (about)

     1  package tunnel
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/metacubex/mihomo/common/pool"
     7  )
     8  
     9  type packet struct {
    10  	pc      net.PacketConn
    11  	rAddr   net.Addr
    12  	payload []byte
    13  }
    14  
    15  func (c *packet) Data() []byte {
    16  	return c.payload
    17  }
    18  
    19  // WriteBack write UDP packet with source(ip, port) = `addr`
    20  func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
    21  	return c.pc.WriteTo(b, c.rAddr)
    22  }
    23  
    24  // LocalAddr returns the source IP/Port of UDP Packet
    25  func (c *packet) LocalAddr() net.Addr {
    26  	return c.rAddr
    27  }
    28  
    29  func (c *packet) Drop() {
    30  	_ = pool.Put(c.payload)
    31  	c.payload = nil
    32  }
    33  
    34  func (c *packet) InAddr() net.Addr {
    35  	return c.pc.LocalAddr()
    36  }