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
/
attr_error.go
98 lines (86 loc) · 2.2 KB
/
attr_error.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
// 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"
)
// An Error represents a STUN ERROR-CODE attribute.
type Error struct {
Code int // code consists of class and number
Reason string // reason
}
// Len implements the Len method of Attribute interface.
func (e *Error) Len() int {
if e == nil {
return 0
}
return 4 + len(e.Reason)
}
// Class returns the error class.
func (e *Error) Class() int {
if e == nil {
return 0
}
return int(e.Code / 100 & 0x07)
}
// Number returns the error number.
func (e *Error) Number() int {
if e == nil {
return 0
}
return int(e.Code % 100)
}
func marshalErrorAttr(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+attr.Len() {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, attr.Len())
b[6], b[7] = byte(attr.(*Error).Class()), byte(attr.(*Error).Number())
copy(b[8:], attr.(*Error).Reason)
return nil
}
func parseErrorAttr(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
e := Error{Code: int(b[2]&0x07)*100 + int(b[3])}
e.Reason = string(b[4:l])
return &e, nil
}
// An UnknownAttrs represents a STUN UNKNOWN-ATTRIBUTES attribute.
type UnknownAttrs []int
// Len implements the Len method of Attribute interface.
func (ua UnknownAttrs) Len() int {
if len(ua) == 0 {
return 0
}
return 2 * len(ua)
}
func marshalUnknownAttrs(b []byte, t int, attr Attribute, _ []byte) error {
if len(b) < 4+attr.Len() {
return errors.New("short buffer")
}
marshalAttrTypeLen(b, t, attr.Len())
b = b[4:]
for _, t := range attr.(UnknownAttrs) {
if len(b) < 2 {
return errors.New("short buffer")
}
binary.BigEndian.PutUint16(b[:2], uint16(t))
b = b[2:]
}
return nil
}
func parseUnknownAttrs(b []byte, min, max int, _ []byte, _, l int) (Attribute, error) {
if min > l || l > max || len(b) < l {
return nil, errors.New("short attribute")
}
ua := make(UnknownAttrs, 0, l/2)
for len(b) > 1 {
ua = append(ua, int(binary.BigEndian.Uint16(b[:2])))
b = b[2:]
}
return ua, nil
}