| 1 | //go:build !windows && !darwin |
| 2 | // +build !windows,!darwin |
| 3 | |
| 4 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 5 | |
| 6 | import ( |
| 7 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 8 | |
| 9 | "golang.org/x/net/ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/" |
| 10 | "golang.org/x/net/ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/" |
| 11 | ) |
| 12 | |
| 13 | // This is the required size of the OOB buffer to pass to ReadMsgUDP. |
| 14 | var udpOOBSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/udpOOBSize. = func() int { |
| 15 | // We can't know whether we'll get an IPv4 control message or an |
| 16 | // IPv6 control message ahead of time. To get around this, we size |
| 17 | // the buffer equal to the largest of the two. |
| 18 | |
| 19 | oob4 := ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.NewControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/NewControlMessage().(ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.FlagDstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/FlagDst. | ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.FlagInterfacescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/FlagInterface.) |
| 20 | oob6 := ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.NewControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/NewControlMessage().(ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.FlagDstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/FlagDst. | ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.FlagInterfacescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/FlagInterface.) |
| 21 | |
| 22 | if len(oob4) > len(oob6) { |
| 23 | return len(oob4) |
| 24 | } |
| 25 | |
| 26 | return len(oob6) |
| 27 | }() |
| 28 | |
| 29 | // SessionUDP holds the remote address and the associated |
| 30 | // out-of-band data. |
| 31 | type SessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP# struct { |
| 32 | raddrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#raddr. *netscip-go gomod github.com/golang/go/src go1.25.0 net/.UDPAddrscip-go gomod github.com/golang/go/src go1.25.0 net/UDPAddr# |
| 33 | contextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#context. []byte |
| 34 | } |
| 35 | |
| 36 | // RemoteAddr returns the remote network address. |
| 37 | func (s *SessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#) RemoteAddrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#RemoteAddr().() netscip-go gomod github.com/golang/go/src go1.25.0 net/.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Addr# { return s.raddrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#raddr. } |
| 38 | |
| 39 | // ReadFromSessionUDP acts just like net.UDPConn.ReadFrom(), but returns a session object instead of a |
| 40 | // net.UDPAddr. |
| 41 | func ReadFromSessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ReadFromSessionUDP().(conn *netscip-go gomod github.com/golang/go/src go1.25.0 net/.UDPConnscip-go gomod github.com/golang/go/src go1.25.0 net/UDPConn#, b []byte) (int, *SessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#, error) { |
| 42 | oob := make([]byte, udpOOBSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/udpOOBSize.) |
| 43 | n, oobn, _, raddr, err := conn.ReadMsgUDPscip-go gomod github.com/golang/go/src go1.25.0 net/UDPConn#ReadMsgUDP().(b, oob) |
| 44 | if err != nil { |
| 45 | return n, nil, err |
| 46 | } |
| 47 | return n, &SessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#{raddr, oob[:oobn]}, err |
| 48 | } |
| 49 | |
| 50 | // WriteToSessionUDP acts just like net.UDPConn.WriteTo(), but uses a *SessionUDP instead of a net.Addr. |
| 51 | func WriteToSessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/WriteToSessionUDP().(conn *netscip-go gomod github.com/golang/go/src go1.25.0 net/.UDPConnscip-go gomod github.com/golang/go/src go1.25.0 net/UDPConn#, b []byte, session *SessionUDPscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#) (int, error) { |
| 52 | oob := correctSourcescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/correctSource().(session.contextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#context.) |
| 53 | n, _, err := conn.WriteMsgUDPscip-go gomod github.com/golang/go/src go1.25.0 net/UDPConn#WriteMsgUDP().(b, oob, session.raddrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SessionUDP#raddr.) |
| 54 | return n, err |
| 55 | } |
| 56 | |
| 57 | func setUDPSocketOptionsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/setUDPSocketOptions().(conn *netscip-go gomod github.com/golang/go/src go1.25.0 net/.UDPConnscip-go gomod github.com/golang/go/src go1.25.0 net/UDPConn#) error { |
| 58 | // Try setting the flags for both families and ignore the errors unless they |
| 59 | // both error. |
| 60 | err6 := ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.NewPacketConnscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/NewPacketConn().(conn).SetControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/PacketConn#SetControlMessage().(ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.FlagDstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/FlagDst.|ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.FlagInterfacescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/FlagInterface., true) |
| 61 | err4 := ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.NewPacketConnscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/NewPacketConn().(conn).SetControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/PacketConn#SetControlMessage().(ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.FlagDstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/FlagDst.|ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.FlagInterfacescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/FlagInterface., true) |
| 62 | if err6 != nil && err4 != nil { |
| 63 | return err4 |
| 64 | } |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // parseDstFromOOB takes oob data and returns the destination IP. |
| 69 | func parseDstFromOOBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/parseDstFromOOB().(oob []byte) netscip-go gomod github.com/golang/go/src go1.25.0 net/.IPscip-go gomod github.com/golang/go/src go1.25.0 net/IP# { |
| 70 | // Start with IPv6 and then fallback to IPv4 |
| 71 | // TODO(fastest963): Figure out a way to prefer one or the other. Looking at |
| 72 | // the lvl of the header for a 0 or 41 isn't cross-platform. |
| 73 | cm6 := new(ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.ControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#) |
| 74 | if cm6.Parsescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#Parse().(oob) == nil && cm6.Dstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#Dst. != nil { |
| 75 | return cm6.Dstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#Dst. |
| 76 | } |
| 77 | cm4 := new(ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.ControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#) |
| 78 | if cm4.Parsescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#Parse().(oob) == nil && cm4.Dstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#Dst. != nil { |
| 79 | return cm4.Dstscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#Dst. |
| 80 | } |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | // correctSource takes oob data and returns new oob data with the Src equal to the Dst |
| 85 | func correctSourcescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/correctSource().(oob []byte) []byte { |
| 86 | dst := parseDstFromOOBscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/parseDstFromOOB().(oob) |
| 87 | if dst == nil { |
| 88 | return nil |
| 89 | } |
| 90 | // If the dst is definitely an IPv6, then use ipv6's ControlMessage to |
| 91 | // respond otherwise use ipv4's because ipv6's marshal ignores ipv4 |
| 92 | // addresses. |
| 93 | if dst.To4scip-go gomod github.com/golang/go/src go1.25.0 net/IP#To4().() == nil { |
| 94 | cm := new(ipv6scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/.ControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#) |
| 95 | cm.Srcscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#Src. = dst |
| 96 | oob = cm.Marshalscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv6`/ControlMessage#Marshal().() |
| 97 | } else { |
| 98 | cm := new(ipv4scip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/.ControlMessagescip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#) |
| 99 | cm.Srcscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#Src. = dst |
| 100 | oob = cm.Marshalscip-go gomod golang.org/x/net v0.57.0 `golang.org/x/net/ipv4`/ControlMessage#Marshal().() |
| 101 | } |
| 102 | return oob |
| 103 | } |