File: uattrs.go

package info (click to toggle)
golang-github-pion-stun 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 868 kB
  • sloc: sh: 50; makefile: 40
file content (66 lines) | stat: -rw-r--r-- 1,541 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

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
}