This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathattr_turn.go
189 lines (159 loc) · 4.49 KB
/
attr_turn.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2015 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stun
import (
"encoding/binary"
"errors"
"time"
)
// A ChannelNumber represents a STUN CHANNEL-NUMBER attribute.
type ChannelNumber struct {
Number Type // channel number
}
// Len implements the Len method of Attribute interface.
func (cn *ChannelNumber) Len() int {
if cn == nil {
return 0
}
return 4
}
func marshalChannelNumberAttr(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+4 {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, 4)
binary.BigEndian.PutUint16(b[4:6], uint16(attr.(*ChannelNumber).Number))
return nil
}
func parseChannelNumberAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
return &ChannelNumber{Number: Type(binary.BigEndian.Uint16(b[:2]))}, nil
}
// A Lifetime represents a STUN LIFETIME attribute.
type Lifetime time.Duration
// Len implements the Len method of Attribute interface.
func (_ Lifetime) Len() int {
return 4
}
// A Data represents a STUN DATA attribute.
// It just refers to the underlying buffer when the returned value
// from ParseMessage.
type Data []byte
// Len implements the Len method of Attribute interface.
func (d Data) Len() int {
return len(d)
}
// A RequestedAddrFamily represents a STUN REQUESTED-ADDRESS-FAMILY
// attribute.
type RequestedAddrFamily struct {
ID int // identifier; 0x01 for IPv4, 0x02 for IPv6
}
// Len implements the Len method of Attribute interface.
func (af *RequestedAddrFamily) Len() int {
if af == nil {
return 0
}
return 4
}
func marshalRequestedAddrFamilyAttr(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+4 {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, 4)
b[4] = byte(attr.(*RequestedAddrFamily).ID)
return nil
}
func parseRequestedAddrFamilyAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
return &RequestedAddrFamily{ID: int(b[0])}, nil
}
// An EvenPort represents a STUN EVEN-PORT attribute.
type EvenPort struct {
R bool // request next-higher port number reservation
}
// Len implements the Len method of Attribute interface.
func (ep *EvenPort) Len() int {
if ep == nil {
return 0
}
return 1
}
func marshalEvenPortAttr(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+1 {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, 1)
if attr.(*EvenPort).R {
b[4] |= 0x80
}
return nil
}
func parseEvenPortAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
var ep EvenPort
if b[0]&0x80 != 0 {
ep.R = true
}
return &ep, nil
}
// A RequestedTransport represents a STUN REQUESTED-TRANSPORT
// attribute.
type RequestedTransport struct {
Protocol int // protocol number
}
// Len implements the Len method of Attribute interface.
func (rt *RequestedTransport) Len() int {
if rt == nil {
return 0
}
return 4
}
func marshalRequestedTransportAttr(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+4 {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, 4)
b[4] = byte(attr.(*RequestedTransport).Protocol)
return nil
}
func parseRequestedTransportAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
return &RequestedTransport{Protocol: int(b[0])}, nil
}
// A DontFragment represents a STUN DONT-FRAGMENT attribute.
type DontFragment struct{}
// Len implements the Len method of Attribute interface.
func (_ *DontFragment) Len() int {
return 0
}
func marshalDontFragmentAttr(b []byte, t int, _ Attribute, _ []byte) error {
if len(b) < 4 {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, 0)
return nil
}
func parseDontFragmentAttr(_ []byte, _, _ int, _ []byte, _, _ int) (Attribute, error) {
return &DontFragment{}, nil
}
// A ReservationToken represents a STUN RESERVATION-TOKEN attribute.
type ReservationToken []byte
// Len implements the Len method of Attribute interface.
func (_ ReservationToken) Len() int {
return 8
}
// A ConnectionID represents a STUN CONNECTION-ID attribute.
type ConnectionID uint
// Len implements the Len method of Attribute interface.
func (_ ConnectionID) Len() int {
return 4
}