/
nonceattribute.go
49 lines (40 loc) · 1.01 KB
/
nonceattribute.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
package stun
import (
"bytes"
"errors"
"github.com/willscott/goturn/common"
)
const (
Nonce stun.AttributeType = 0x15
)
type NonceAttribute struct {
}
func NewNonceAttribute() stun.Attribute {
return stun.Attribute(new(NonceAttribute))
}
func (h *NonceAttribute) Type() stun.AttributeType {
return Nonce
}
func (h *NonceAttribute) Encode(msg *stun.Message) ([]byte, error) {
buf := new(bytes.Buffer)
err := stun.WriteAttributeHeader(buf, stun.Attribute(h), msg)
buf.Write(msg.Credentials.Nonce)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (h *NonceAttribute) Decode(data []byte, length uint16, p *stun.Parser) error {
if uint16(len(data)) < length {
return errors.New("Truncated Nonce Attribute")
}
if length > 763 {
return errors.New("Nonce Length is too long")
}
p.Message.Credentials.Nonce = make([]byte, length)
copy(p.Message.Credentials.Nonce, data[0:length])
return nil
}
func (h *NonceAttribute) Length(msg *stun.Message) uint16 {
return uint16(len(msg.Credentials.Nonce))
}