summaryrefslogtreecommitdiff
path: root/vendor/github.com/pion/stun/textattrs.go
blob: efdfbd9571dafa725107ce88f2f017abf5593eff (plain)
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
package stun

// NewUsername returns Username with provided value.
func NewUsername(username string) Username {
	return Username(username)
}

// Username represents USERNAME attribute.
//
// RFC 5389 Section 15.3
type Username []byte

func (u Username) String() string {
	return string(u)
}

const maxUsernameB = 513

// AddTo adds USERNAME attribute to message.
func (u Username) AddTo(m *Message) error {
	return TextAttribute(u).AddToAs(m, AttrUsername, maxUsernameB)
}

// GetFrom gets USERNAME from message.
func (u *Username) GetFrom(m *Message) error {
	return (*TextAttribute)(u).GetFromAs(m, AttrUsername)
}

// NewRealm returns Realm with provided value.
// Must be SASL-prepared.
func NewRealm(realm string) Realm {
	return Realm(realm)
}

// Realm represents REALM attribute.
//
// RFC 5389 Section 15.7
type Realm []byte

func (n Realm) String() string {
	return string(n)
}

const maxRealmB = 763

// AddTo adds NONCE to message.
func (n Realm) AddTo(m *Message) error {
	return TextAttribute(n).AddToAs(m, AttrRealm, maxRealmB)
}

// GetFrom gets REALM from message.
func (n *Realm) GetFrom(m *Message) error {
	return (*TextAttribute)(n).GetFromAs(m, AttrRealm)
}

const softwareRawMaxB = 763

// Software is SOFTWARE attribute.
//
// RFC 5389 Section 15.10
type Software []byte

func (s Software) String() string {
	return string(s)
}

// NewSoftware returns *Software from string.
func NewSoftware(software string) Software {
	return Software(software)
}

// AddTo adds Software attribute to m.
func (s Software) AddTo(m *Message) error {
	return TextAttribute(s).AddToAs(m, AttrSoftware, softwareRawMaxB)
}

// GetFrom decodes Software from m.
func (s *Software) GetFrom(m *Message) error {
	return (*TextAttribute)(s).GetFromAs(m, AttrSoftware)
}

// Nonce represents NONCE attribute.
//
// RFC 5389 Section 15.8
type Nonce []byte

// NewNonce returns new Nonce from string.
func NewNonce(nonce string) Nonce {
	return Nonce(nonce)
}

func (n Nonce) String() string {
	return string(n)
}

const maxNonceB = 763

// AddTo adds NONCE to message.
func (n Nonce) AddTo(m *Message) error {
	return TextAttribute(n).AddToAs(m, AttrNonce, maxNonceB)
}

// GetFrom gets NONCE from message.
func (n *Nonce) GetFrom(m *Message) error {
	return (*TextAttribute)(n).GetFromAs(m, AttrNonce)
}

// TextAttribute is helper for adding and getting text attributes.
type TextAttribute []byte

// AddToAs adds attribute with type t to m, checking maximum length. If maxLen
// is less than 0, no check is performed.
func (v TextAttribute) AddToAs(m *Message, t AttrType, maxLen int) error {
	if err := CheckOverflow(t, len(v), maxLen); err != nil {
		return err
	}
	m.Add(t, v)
	return nil
}

// GetFromAs gets t attribute from m and appends its value to reseted v.
func (v *TextAttribute) GetFromAs(m *Message, t AttrType) error {
	a, err := m.Get(t)
	if err != nil {
		return err
	}
	*v = a
	return nil
}