/
label.go
47 lines (35 loc) · 1.06 KB
/
label.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
package packet
import (
"bytes"
"fmt"
"github.com/bio-routing/tflow2/convert"
)
const bottomOfStackBit = 0x01
const lengthEXPAndBottomOfStack = 0x04
type LabelStackEntry uint32
// NewLabelStackEntry creates a new label stack entry
func NewLabelStackEntry(labelValue uint32) LabelStackEntry {
return LabelStackEntry(labelValue << lengthEXPAndBottomOfStack)
}
func (l LabelStackEntry) serialize(buf *bytes.Buffer, bottomOfStack bool) {
x := convert.Uint32Byte(uint32(l))
if bottomOfStack {
x[3] |= bottomOfStackBit
}
buf.Write(x[1:4])
}
func (l LabelStackEntry) isBottomOfStack() bool {
return l&bottomOfStackBit == bottomOfStackBit
}
// GetLabel gets the label
func (l LabelStackEntry) GetLabel() uint32 {
return uint32(l) >> lengthEXPAndBottomOfStack
}
func decodeLabelStackEntry(buf *bytes.Buffer) (LabelStackEntry, error) {
label := make([]byte, BytesPerLabel)
_, err := buf.Read(label)
if err != nil {
return LabelStackEntry(0), fmt.Errorf("read failed: %w", err)
}
return LabelStackEntry(convert.Uint32b([]byte{0, label[0], label[1], label[2]})), nil
}