8 "golang.org/x/net/ipv4"
9 "golang.org/x/net/ipv6"
12 // This is the required size of the OOB buffer to pass to ReadMsgUDP.
13 var udpOOBSize = func() int {
14 // We can't know whether we'll get an IPv4 control message or an
15 // IPv6 control message ahead of time. To get around this, we size
16 // the buffer equal to the largest of the two.
18 oob4 := ipv4.NewControlMessage(ipv4.FlagDst | ipv4.FlagInterface)
19 oob6 := ipv6.NewControlMessage(ipv6.FlagDst | ipv6.FlagInterface)
21 if len(oob4) > len(oob6) {
28 // SessionUDP holds the remote address and the associated
30 type SessionUDP struct {
35 // RemoteAddr returns the remote network address.
36 func (s *SessionUDP) RemoteAddr() net.Addr { return s.raddr }
38 // ReadFromSessionUDP acts just like net.UDPConn.ReadFrom(), but returns a session object instead of a
40 func ReadFromSessionUDP(conn *net.UDPConn, b []byte) (int, *SessionUDP, error) {
41 oob := make([]byte, udpOOBSize)
42 n, oobn, _, raddr, err := conn.ReadMsgUDP(b, oob)
46 return n, &SessionUDP{raddr, oob[:oobn]}, err
49 // WriteToSessionUDP acts just like net.UDPConn.WriteTo(), but uses a *SessionUDP instead of a net.Addr.
50 func WriteToSessionUDP(conn *net.UDPConn, b []byte, session *SessionUDP) (int, error) {
51 oob := correctSource(session.context)
52 n, _, err := conn.WriteMsgUDP(b, oob, session.raddr)
56 func setUDPSocketOptions(conn *net.UDPConn) error {
57 // Try setting the flags for both families and ignore the errors unless they
59 err6 := ipv6.NewPacketConn(conn).SetControlMessage(ipv6.FlagDst|ipv6.FlagInterface, true)
60 err4 := ipv4.NewPacketConn(conn).SetControlMessage(ipv4.FlagDst|ipv4.FlagInterface, true)
61 if err6 != nil && err4 != nil {
67 // parseDstFromOOB takes oob data and returns the destination IP.
68 func parseDstFromOOB(oob []byte) net.IP {
69 // Start with IPv6 and then fallback to IPv4
70 // TODO(fastest963): Figure out a way to prefer one or the other. Looking at
71 // the lvl of the header for a 0 or 41 isn't cross-platform.
72 cm6 := new(ipv6.ControlMessage)
73 if cm6.Parse(oob) == nil && cm6.Dst != nil {
76 cm4 := new(ipv4.ControlMessage)
77 if cm4.Parse(oob) == nil && cm4.Dst != nil {
83 // correctSource takes oob data and returns new oob data with the Src equal to the Dst
84 func correctSource(oob []byte) []byte {
85 dst := parseDstFromOOB(oob)
89 // If the dst is definitely an IPv6, then use ipv6's ControlMessage to
90 // respond otherwise use ipv4's because ipv6's marshal ignores ipv4
93 cm := new(ipv6.ControlMessage)
97 cm := new(ipv4.ControlMessage)