This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
/
attr_tram.go
125 lines (109 loc) · 3.28 KB
/
attr_tram.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
// 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 (
"crypto/sha256"
"encoding/binary"
"errors"
)
// A MessageIntegritySHA256 represents a STUN MESSAGE-INTEGRITY-SHA256
// attribute.
// If MessageIntegrity is nil, Marshal method of Message interface
// sets an approrpiate value.
type MessageIntegritySHA256 []byte
// Len implements the Len method of Attribute interface.
func (_ MessageIntegritySHA256) Len() int {
return sha256.Size
}
// A PasswordAlgorithms represents a STUN PASSWORD-ALGORITHMS
// attribute.
type PasswordAlgorithms []PasswordAlgorithm
// Len implements the Len method of Attribute interface.
func (pas PasswordAlgorithms) Len() int {
var l int
for _, pa := range pas {
l += roundup(pa.Len())
}
return l
}
func marshalPasswordAlgosAttr(b []byte, t int, attr Attribute, _ []byte) error {
l := attr.Len()
if len(b) < l {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, l)
b = b[4:]
for _, pa := range attr.(PasswordAlgorithms) {
binary.BigEndian.PutUint16(b[:2], uint16(pa.Number))
binary.BigEndian.PutUint16(b[2:4], uint16(len(pa.Params)))
copy(b[4:], pa.Params)
b = b[roundup(pa.Len()):]
}
return nil
}
func parsePasswordAlgosAttr(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 pas PasswordAlgorithms
for len(b) >= 4 {
pa := PasswordAlgorithm{Number: int(binary.BigEndian.Uint16(b[:2]))}
ll := int(binary.BigEndian.Uint16(b[2:4]))
pa.Params = make([]byte, ll)
copy(pa.Params, b[4:])
pas = append(pas, pa)
rl := roundup(4 + ll)
if rl > len(b) {
return nil, errors.New("invalid attribute")
}
b = b[rl:]
}
return pas, nil
}
// A PasswordAlgorithm represents a STUN PASSWORD-ALGORITHM attribute.
type PasswordAlgorithm struct {
Number int // algorithm number; 0x0001 for MD5, 0x0002 for SHA256
Params []byte // algorithm parameters
}
// Len implements the Len method of Attribute interface.
func (pa *PasswordAlgorithm) Len() int {
if pa == nil {
return 0
}
return 4 + len(pa.Params)
}
func marshalPasswordAlgoAttr(b []byte, t int, attr Attribute, _ []byte) error {
l := attr.Len()
if len(b) < l {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, l)
if pa, ok := attr.(*PasswordAlgorithm); ok && pa != nil {
binary.BigEndian.PutUint16(b[4:6], uint16(pa.Number))
binary.BigEndian.PutUint16(b[6:8], uint16(len(pa.Params)))
copy(b[8:], pa.Params)
}
return nil
}
func parsePasswordAlgoAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
pa := PasswordAlgorithm{Number: int(binary.BigEndian.Uint16(b[:2]))}
pa.Params = make([]byte, int(binary.BigEndian.Uint16(b[2:4])))
copy(pa.Params, b[4:])
return &pa, nil
}
// An AlternateDomain represents a STUN ALTERNATE-DOMAIN attribute.
type AlternateDomain string
// Len implements the Len method of Attribute interface.
func (ad AlternateDomain) Len() int {
return len(ad)
}
// An Origin represents a STUN ORIGIN attribute.
type Origin string
// Len implements the Len method of Attribute interface.
func (o Origin) Len() int {
return len(o)
}