github.com/xraypb/xray-core@v1.6.6/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/xraypb/xray-core/common/errors" 12 "github.com/xraypb/xray-core/common/net" 13 "github.com/xraypb/xray-core/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 var buf bytes.Buffer 33 enc := gob.NewEncoder(&buf) 34 udpAddr, ok := conn.LocalAddr().(*net.UDPAddr) 35 if !ok { 36 return 0, 0, 0, nil, errors.New("invalid local address") 37 } 38 if addr == nil { 39 return 0, 0, 0, nil, errors.New("invalid remote address") 40 } 41 enc.Encode(udpAddr) 42 enc.Encode(addr) 43 var reader io.Reader = &buf 44 noob, _ := reader.Read(oob) 45 return nBytes, noob, 0, addr, err 46 }