You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udp.go 2.9KB

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