summaryrefslogtreecommitdiff
path: root/vendor/github.com/pion/stun/uattrs.go
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2021-11-29 01:46:27 +0100
committerkali kaneko (leap communications) <kali@leap.se>2021-11-29 18:14:16 +0100
commit18f52af5be3a9a0c73811706108f790d65ee9c67 (patch)
treee13cbacb47d56919caa9c44a2b45dec1497a7860 /vendor/github.com/pion/stun/uattrs.go
parentebcef0d57b6ecb5a40c6579f6be07182dd3033ba (diff)
[pkg] update vendor
Diffstat (limited to 'vendor/github.com/pion/stun/uattrs.go')
-rw-r--r--vendor/github.com/pion/stun/uattrs.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/vendor/github.com/pion/stun/uattrs.go b/vendor/github.com/pion/stun/uattrs.go
new file mode 100644
index 0000000..238d32d
--- /dev/null
+++ b/vendor/github.com/pion/stun/uattrs.go
@@ -0,0 +1,63 @@
+package stun
+
+import "errors"
+
+// UnknownAttributes represents UNKNOWN-ATTRIBUTES attribute.
+//
+// RFC 5389 Section 15.9
+type UnknownAttributes []AttrType
+
+func (a UnknownAttributes) String() string {
+ s := ""
+ if len(a) == 0 {
+ return "<nil>"
+ }
+ last := len(a) - 1
+ for i, t := range a {
+ s += t.String()
+ if i != last {
+ s += ", "
+ }
+ }
+ return s
+}
+
+// type size is 16 bit.
+const attrTypeSize = 4
+
+// AddTo adds UNKNOWN-ATTRIBUTES attribute to message.
+func (a UnknownAttributes) AddTo(m *Message) error {
+ v := make([]byte, 0, attrTypeSize*20) // 20 should be enough
+ // If len(a.Types) > 20, there will be allocations.
+ for i, t := range a {
+ v = append(v, 0, 0, 0, 0) // 4 times by 0 (16 bits)
+ first := attrTypeSize * i
+ last := first + attrTypeSize
+ bin.PutUint16(v[first:last], t.Value())
+ }
+ m.Add(AttrUnknownAttributes, v)
+ return nil
+}
+
+// ErrBadUnknownAttrsSize means that UNKNOWN-ATTRIBUTES attribute value
+// has invalid length.
+var ErrBadUnknownAttrsSize = errors.New("bad UNKNOWN-ATTRIBUTES size")
+
+// GetFrom parses UNKNOWN-ATTRIBUTES from message.
+func (a *UnknownAttributes) GetFrom(m *Message) error {
+ v, err := m.Get(AttrUnknownAttributes)
+ if err != nil {
+ return err
+ }
+ if len(v)%attrTypeSize != 0 {
+ return ErrBadUnknownAttrsSize
+ }
+ *a = (*a)[:0]
+ first := 0
+ for first < len(v) {
+ last := first + attrTypeSize
+ *a = append(*a, AttrType(bin.Uint16(v[first:last])))
+ first = last
+ }
+ return nil
+}