github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/udp/hub_freebsd.go (about)

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