2021-08-21 01:20:40 -04:00
|
|
|
//go:build freebsd
|
2020-02-13 09:35:11 -05:00
|
|
|
// +build freebsd
|
|
|
|
|
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"io"
|
2020-02-13 22:00:11 -05:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/transport/internet"
|
2020-02-13 09:35:11 -05:00
|
|
|
)
|
|
|
|
|
2020-02-13 22:11:33 -05:00
|
|
|
// RetrieveOriginalDest from stored laddr, caddr
|
2020-02-13 09:35:11 -05:00
|
|
|
func RetrieveOriginalDest(oob []byte) net.Destination {
|
|
|
|
dec := gob.NewDecoder(bytes.NewBuffer(oob))
|
|
|
|
var la, ra net.UDPAddr
|
|
|
|
dec.Decode(&la)
|
|
|
|
dec.Decode(&ra)
|
|
|
|
ip, port, err := internet.OriginalDst(&la, &ra)
|
|
|
|
if err != nil {
|
|
|
|
return net.Destination{}
|
|
|
|
}
|
|
|
|
return net.UDPDestination(net.IPAddress(ip), net.Port(port))
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:11:33 -05:00
|
|
|
// ReadUDPMsg stores laddr, caddr for later use
|
2020-02-13 09:35:11 -05:00
|
|
|
func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) {
|
|
|
|
nBytes, addr, err := conn.ReadFromUDP(payload)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := gob.NewEncoder(&buf)
|
|
|
|
enc.Encode(conn.LocalAddr().(*net.UDPAddr))
|
|
|
|
enc.Encode(addr)
|
|
|
|
var reader io.Reader = &buf
|
|
|
|
noob, _ := reader.Read(oob)
|
|
|
|
return nBytes, noob, 0, addr, err
|
|
|
|
}
|