xoraddr.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package stun
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "net"
  7. "strconv"
  8. )
  9. const (
  10. familyIPv4 uint16 = 0x01
  11. familyIPv6 uint16 = 0x02
  12. )
  13. // XORMappedAddress implements XOR-MAPPED-ADDRESS attribute.
  14. //
  15. // RFC 5389 Section 15.2
  16. type XORMappedAddress struct {
  17. IP net.IP
  18. Port int
  19. }
  20. func (a XORMappedAddress) String() string {
  21. return net.JoinHostPort(a.IP.String(), strconv.Itoa(a.Port))
  22. }
  23. // isIPv4 returns true if ip with len of net.IPv6Len seems to be ipv4.
  24. func isIPv4(ip net.IP) bool {
  25. // Optimized for performance. Copied from net.IP.To4.
  26. return isZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff
  27. }
  28. // Is p all zeros?
  29. func isZeros(p net.IP) bool {
  30. for i := 0; i < len(p); i++ {
  31. if p[i] != 0 {
  32. return false
  33. }
  34. }
  35. return true
  36. }
  37. // ErrBadIPLength means that len(IP) is not net.{IPv6len,IPv4len}.
  38. var ErrBadIPLength = errors.New("invalid length of IP value")
  39. // AddToAs adds XOR-MAPPED-ADDRESS value to m as t attribute.
  40. func (a XORMappedAddress) AddToAs(m *Message, t AttrType) error {
  41. var (
  42. family = familyIPv4
  43. ip = a.IP
  44. )
  45. if len(a.IP) == net.IPv6len {
  46. if isIPv4(ip) {
  47. ip = ip[12:16] // like in ip.To4()
  48. } else {
  49. family = familyIPv6
  50. }
  51. } else if len(ip) != net.IPv4len {
  52. return ErrBadIPLength
  53. }
  54. value := make([]byte, 32+128)
  55. value[0] = 0 // first 8 bits are zeroes
  56. xorValue := make([]byte, net.IPv6len)
  57. copy(xorValue[4:], m.TransactionID[:])
  58. bin.PutUint32(xorValue[0:4], magicCookie)
  59. bin.PutUint16(value[0:2], family)
  60. bin.PutUint16(value[2:4], uint16(a.Port^magicCookie>>16))
  61. xorBytes(value[4:4+len(ip)], ip, xorValue)
  62. m.Add(t, value[:4+len(ip)])
  63. return nil
  64. }
  65. // AddTo adds XOR-MAPPED-ADDRESS to m. Can return ErrBadIPLength
  66. // if len(a.IP) is invalid.
  67. func (a XORMappedAddress) AddTo(m *Message) error {
  68. return a.AddToAs(m, AttrXORMappedAddress)
  69. }
  70. // GetFromAs decodes XOR-MAPPED-ADDRESS attribute value in message
  71. // getting it as for t type.
  72. func (a *XORMappedAddress) GetFromAs(m *Message, t AttrType) error {
  73. v, err := m.Get(t)
  74. if err != nil {
  75. return err
  76. }
  77. family := bin.Uint16(v[0:2])
  78. if family != familyIPv6 && family != familyIPv4 {
  79. return newDecodeErr("xor-mapped address", "family",
  80. fmt.Sprintf("bad value %d", family),
  81. )
  82. }
  83. ipLen := net.IPv4len
  84. if family == familyIPv6 {
  85. ipLen = net.IPv6len
  86. }
  87. // Ensuring len(a.IP) == ipLen and reusing a.IP.
  88. if len(a.IP) < ipLen {
  89. a.IP = a.IP[:cap(a.IP)]
  90. for len(a.IP) < ipLen {
  91. a.IP = append(a.IP, 0)
  92. }
  93. }
  94. a.IP = a.IP[:ipLen]
  95. for i := range a.IP {
  96. a.IP[i] = 0
  97. }
  98. if len(v) <= 4 {
  99. return io.ErrUnexpectedEOF
  100. }
  101. if err := CheckOverflow(t, len(v[4:]), len(a.IP)); err != nil {
  102. return err
  103. }
  104. a.Port = int(bin.Uint16(v[2:4])) ^ (magicCookie >> 16)
  105. xorValue := make([]byte, 4+TransactionIDSize)
  106. bin.PutUint32(xorValue[0:4], magicCookie)
  107. copy(xorValue[4:], m.TransactionID[:])
  108. xorBytes(a.IP, v[4:], xorValue)
  109. return nil
  110. }
  111. // GetFrom decodes XOR-MAPPED-ADDRESS attribute in message and returns
  112. // error if any. While decoding, a.IP is reused if possible and can be
  113. // rendered to invalid state (e.g. if a.IP was set to IPv6 and then
  114. // IPv4 value were decoded into it), be careful.
  115. //
  116. // Example:
  117. //
  118. // expectedIP := net.ParseIP("213.141.156.236")
  119. // expectedIP.String() // 213.141.156.236, 16 bytes, first 12 of them are zeroes
  120. // expectedPort := 21254
  121. // addr := &XORMappedAddress{
  122. // IP: expectedIP,
  123. // Port: expectedPort,
  124. // }
  125. // // addr were added to message that is decoded as newMessage
  126. // // ...
  127. //
  128. // addr.GetFrom(newMessage)
  129. // addr.IP.String() // 213.141.156.236, net.IPv4Len
  130. // expectedIP.String() // d58d:9cec::ffff:d58d:9cec, 16 bytes, first 4 are IPv4
  131. // // now we have len(expectedIP) = 16 and len(addr.IP) = 4.
  132. func (a *XORMappedAddress) GetFrom(m *Message) error {
  133. return a.GetFromAs(m, AttrXORMappedAddress)
  134. }