github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/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/v5/common/errors" 12 "github.com/v2fly/v2ray-core/v5/common/net" 13 "github.com/v2fly/v2ray-core/v5/transport/internet" 14 ) 15 16 // RetrieveOriginalDest from stored laddr, caddr 17 func RetrieveOriginalDest(oob []byte) net.Destination { 18 dec := gob.NewDecoder(bytes.NewBuffer(oob)) 19 var la, ra net.UDPAddr 20 dec.Decode(&la) 21 dec.Decode(&ra) 22 ip, port, err := internet.OriginalDst(&la, &ra) 23 if err != nil { 24 return net.Destination{} 25 } 26 return net.UDPDestination(net.IPAddress(ip), net.Port(port)) 27 } 28 29 // ReadUDPMsg stores laddr, caddr for later use 30 func ReadUDPMsg(conn *net.UDPConn, payload []byte, oob []byte) (int, int, int, *net.UDPAddr, error) { 31 nBytes, addr, err := conn.ReadFromUDP(payload) 32 if err != nil { 33 return nBytes, 0, 0, addr, err 34 } 35 36 var buf bytes.Buffer 37 enc := gob.NewEncoder(&buf) 38 localAddr, ok := conn.LocalAddr().(*net.UDPAddr) 39 if !ok { 40 return 0, 0, 0, nil, errors.New("invalid local address") 41 } 42 if addr == nil { 43 return 0, 0, 0, nil, errors.New("invalid remote address") 44 } 45 enc.Encode(localAddr) 46 enc.Encode(addr) 47 48 var reader io.Reader = &buf 49 noob, _ := reader.Read(oob) 50 51 return nBytes, noob, 0, addr, nil 52 }