udp.go 2.9 KB

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