github.com/EagleQL/Xray-core@v1.4.3/transport/internet/udp/hub_freebsd.go (about)

     1  // +build freebsd
     2  
     3  package udp
     4  
     5  import (
     6  	"bytes"
     7  	"encoding/gob"
     8  	"io"
     9  
    10  	"github.com/xtls/xray-core/common/net"
    11  	"github.com/xtls/xray-core/transport/internet"
    12  )
    13  
    14  // RetrieveOriginalDest from stored laddr, caddr
    15  func RetrieveOriginalDest(oob []byte) net.Destination {
    16  	dec := gob.NewDecoder(bytes.NewBuffer(oob))
    17  	var la, ra net.UDPAddr
    18  	dec.Decode(&la)
    19  	dec.Decode(&ra)
    20  	ip, port, err := internet.OriginalDst(&la, &ra)
    21  	if err != nil {
    22  		return net.Destination{}
    23  	}
    24  	return net.UDPDestination(net.IPAddress(ip), net.Port(port))
    25  }
    26  
    27  // ReadUDPMsg stores laddr, caddr for later use
    28  func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
    29  	nBytes, addr, err := conn.ReadFromUDP(payload)
    30  	var buf bytes.Buffer
    31  	enc := gob.NewEncoder(&buf)
    32  	enc.Encode(conn.LocalAddr().(*net.UDPAddr))
    33  	enc.Encode(addr)
    34  	var reader io.Reader = &buf
    35  	noob, _ := reader.Read(oob)
    36  	return nBytes, noob, 0, addr, err
    37  }