github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/bufio/nat_wait.go (about)

     1  package bufio
     2  
     3  import (
     4  	"github.com/sagernet/sing/common/buf"
     5  	M "github.com/sagernet/sing/common/metadata"
     6  	N "github.com/sagernet/sing/common/network"
     7  )
     8  
     9  func (c *bidirectionalNATPacketConn) CreatePacketReadWaiter() (N.PacketReadWaiter, bool) {
    10  	waiter, created := CreatePacketReadWaiter(c.NetPacketConn)
    11  	if !created {
    12  		return nil, false
    13  	}
    14  	return &waitBidirectionalNATPacketConn{c, waiter}, true
    15  }
    16  
    17  type waitBidirectionalNATPacketConn struct {
    18  	*bidirectionalNATPacketConn
    19  	readWaiter N.PacketReadWaiter
    20  }
    21  
    22  func (c *waitBidirectionalNATPacketConn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
    23  	return c.readWaiter.InitializeReadWaiter(options)
    24  }
    25  
    26  func (c *waitBidirectionalNATPacketConn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
    27  	buffer, destination, err = c.readWaiter.WaitReadPacket()
    28  	if err != nil {
    29  		return
    30  	}
    31  	if socksaddrWithoutPort(destination) == c.origin {
    32  		destination = M.Socksaddr{
    33  			Addr: c.destination.Addr,
    34  			Fqdn: c.destination.Fqdn,
    35  			Port: destination.Port,
    36  		}
    37  	}
    38  	return
    39  }