packet.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2013, Cong Ding. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: Cong Ding <dinggnu@gmail.com>
  16. package stun
  17. import (
  18. "crypto/rand"
  19. "encoding/binary"
  20. "errors"
  21. )
  22. type packet struct {
  23. types uint16
  24. length uint16
  25. transID []byte // 4 bytes magic cookie + 12 bytes transaction id
  26. attributes []attribute
  27. }
  28. func newPacket() (*packet, error) {
  29. v := new(packet)
  30. v.transID = make([]byte, 16)
  31. binary.BigEndian.PutUint32(v.transID[:4], magicCookie)
  32. _, err := rand.Read(v.transID[4:])
  33. if err != nil {
  34. return nil, err
  35. }
  36. v.attributes = make([]attribute, 0, 10)
  37. v.length = 0
  38. return v, nil
  39. }
  40. func newPacketFromBytes(packetBytes []byte) (*packet, error) {
  41. if len(packetBytes) < 24 {
  42. return nil, errors.New("Received data length too short.")
  43. }
  44. pkt := new(packet)
  45. pkt.types = binary.BigEndian.Uint16(packetBytes[0:2])
  46. pkt.length = binary.BigEndian.Uint16(packetBytes[2:4])
  47. pkt.transID = packetBytes[4:20]
  48. pkt.attributes = make([]attribute, 0, 10)
  49. for pos := uint16(20); pos < uint16(len(packetBytes)); {
  50. types := binary.BigEndian.Uint16(packetBytes[pos : pos+2])
  51. length := binary.BigEndian.Uint16(packetBytes[pos+2 : pos+4])
  52. if pos+4+length > uint16(len(packetBytes)) {
  53. return nil, errors.New("Received data format mismatch.")
  54. }
  55. value := packetBytes[pos+4 : pos+4+length]
  56. attribute := newAttribute(types, value)
  57. pkt.addAttribute(*attribute)
  58. pos += align(length) + 4
  59. }
  60. return pkt, nil
  61. }
  62. func (v *packet) addAttribute(a attribute) {
  63. v.attributes = append(v.attributes, a)
  64. v.length += align(a.length) + 4
  65. }
  66. func (v *packet) bytes() []byte {
  67. packetBytes := make([]byte, 4)
  68. binary.BigEndian.PutUint16(packetBytes[0:2], v.types)
  69. binary.BigEndian.PutUint16(packetBytes[2:4], v.length)
  70. packetBytes = append(packetBytes, v.transID...)
  71. for _, a := range v.attributes {
  72. buf := make([]byte, 2)
  73. binary.BigEndian.PutUint16(buf, a.types)
  74. packetBytes = append(packetBytes, buf...)
  75. binary.BigEndian.PutUint16(buf, a.length)
  76. packetBytes = append(packetBytes, buf...)
  77. packetBytes = append(packetBytes, a.value...)
  78. }
  79. return packetBytes
  80. }
  81. func (v *packet) getSourceAddr() *Host {
  82. return v.getRawAddr(attributeSourceAddress)
  83. }
  84. func (v *packet) getMappedAddr() *Host {
  85. return v.getRawAddr(attributeMappedAddress)
  86. }
  87. func (v *packet) getChangedAddr() *Host {
  88. return v.getRawAddr(attributeChangedAddress)
  89. }
  90. func (v *packet) getOtherAddr() *Host {
  91. return v.getRawAddr(attributeOtherAddress)
  92. }
  93. func (v *packet) getRawAddr(attribute uint16) *Host {
  94. for _, a := range v.attributes {
  95. if a.types == attribute {
  96. return a.rawAddr()
  97. }
  98. }
  99. return nil
  100. }
  101. func (v *packet) getXorMappedAddr() *Host {
  102. addr := v.getXorAddr(attributeXorMappedAddress)
  103. if addr == nil {
  104. addr = v.getXorAddr(attributeXorMappedAddressExp)
  105. }
  106. return addr
  107. }
  108. func (v *packet) getXorAddr(attribute uint16) *Host {
  109. for _, a := range v.attributes {
  110. if a.types == attribute {
  111. return a.xorAddr(v.transID)
  112. }
  113. }
  114. return nil
  115. }