forked from subutai-io/p2p
/
packet.go
144 lines (133 loc) · 3.93 KB
/
packet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2013, Cong Ding. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Cong Ding <dinggnu@gmail.com>
package stun
import (
"encoding/binary"
"errors"
"net"
"time"
)
type packet struct {
types uint16
length uint16
cookie uint32
id []byte // 12 bytes
attributes []attribute
}
func newPacket() *packet {
v := new(packet)
v.id = make([]byte, 12)
v.attributes = make([]attribute, 0, 10)
v.cookie = magicCookie
v.length = 0
return v
}
func newPacketFromBytes(packetBytes []byte) (*packet, error) {
if len(packetBytes) < 24 {
return nil, errors.New("Received data length too short.")
}
packet := newPacket()
packet.types = binary.BigEndian.Uint16(packetBytes[0:2])
packet.length = binary.BigEndian.Uint16(packetBytes[2:4])
packet.cookie = binary.BigEndian.Uint32(packetBytes[4:8])
packet.id = packetBytes[8:20]
for pos := uint16(20); pos < uint16(len(packetBytes)); {
types := binary.BigEndian.Uint16(packetBytes[pos : pos+2])
length := binary.BigEndian.Uint16(packetBytes[pos+2 : pos+4])
if pos+4+length > uint16(len(packetBytes)) {
return nil, errors.New("Received data format mismatch.")
}
value := packetBytes[pos+4 : pos+4+length]
attribute := newAttribute(types, value)
packet.addAttribute(*attribute)
pos += align(length) + 4
}
return packet, nil
}
func (v *packet) addAttribute(a attribute) {
v.attributes = append(v.attributes, a)
v.length += align(a.length) + 4
}
func (v *packet) bytes() []byte {
packetBytes := make([]byte, 8)
binary.BigEndian.PutUint16(packetBytes[0:2], v.types)
binary.BigEndian.PutUint16(packetBytes[2:4], v.length)
binary.BigEndian.PutUint32(packetBytes[4:8], v.cookie)
packetBytes = append(packetBytes, v.id...)
for _, a := range v.attributes {
buf := make([]byte, 2)
binary.BigEndian.PutUint16(buf, a.types)
packetBytes = append(packetBytes, buf...)
binary.BigEndian.PutUint16(buf, a.length)
packetBytes = append(packetBytes, buf...)
packetBytes = append(packetBytes, a.value...)
}
return packetBytes
}
func (v *packet) mappedAddr() *Host {
for _, a := range v.attributes {
if a.types == attribute_MAPPED_ADDRESS {
return a.address()
}
}
return nil
}
func (v *packet) changedAddr() *Host {
for _, a := range v.attributes {
if a.types == attribute_CHANGED_ADDRESS {
return a.address()
}
}
return nil
}
func (v *packet) xorMappedAddr() *Host {
for _, a := range v.attributes {
if (a.types == attribute_XOR_MAPPED_ADDRESS) || (a.types == attribute_XOR_MAPPED_ADDRESS_EXP) {
return a.xorMappedAddr()
}
}
return nil
}
// RFC 3489: Clients SHOULD retransmit the request starting with an interval
// of 100ms, doubling every retransmit until the interval reaches 1.6s.
// Retransmissions continue with intervals of 1.6s until a response is
// received, or a total of 9 requests have been sent.
func (v *packet) send(conn net.Conn) (*packet, error) {
timeout := 100
for i := 0; i < 9; i++ {
length, err := conn.Write(v.bytes())
if err != nil {
return nil, err
}
if length != len(v.bytes()) {
return nil, errors.New("Error in sending data.")
}
conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond))
if timeout < 1600 {
timeout *= 2
}
packetBytes := make([]byte, 1024)
length, err = conn.Read(packetBytes)
if err == nil {
return newPacketFromBytes(packetBytes[0:length])
} else {
if !err.(net.Error).Timeout() {
return nil, err
}
}
}
return nil, nil
}