forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_route4.go
74 lines (64 loc) · 1.78 KB
/
f_route4.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaRoute4Unspec = iota
tcaRoute4ClassID
tcaRoute4To
tcaRoute4From
tcaRoute4IIf
tcaRoute4Police
tcaRoute4Act
)
// Route4 contains attributes of the route discipline
type Route4 struct {
ClassID *uint32
To *uint32
From *uint32
IIf *uint32
}
// unmarshalRoute4 parses the Route4-encoded data and stores the result in the value pointed to by info.
func unmarshalRoute4(data []byte, info *Route4) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case tcaRoute4ClassID:
info.ClassID = uint32Ptr(ad.Uint32())
case tcaRoute4To:
info.To = uint32Ptr(ad.Uint32())
case tcaRoute4From:
info.From = uint32Ptr(ad.Uint32())
case tcaRoute4IIf:
info.IIf = uint32Ptr(ad.Uint32())
default:
return fmt.Errorf("unmarshalRoute4()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return ad.Err()
}
// marshalRoute4 returns the binary encoding of Route4
func marshalRoute4(info *Route4) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Route4: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.ClassID != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaRoute4ClassID, Data: uint32Value(info.ClassID)})
}
if info.To != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaRoute4To, Data: uint32Value(info.To)})
}
if info.From != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaRoute4From, Data: uint32Value(info.From)})
}
if info.IIf != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaRoute4IIf, Data: uint32Value(info.IIf)})
}
return marshalAttributes(options)
}