-
Notifications
You must be signed in to change notification settings - Fork 0
/
iprule.go
68 lines (62 loc) · 1.45 KB
/
iprule.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
// Copyright 2023 Louis Royer and the NextMN-SRv6 contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT
package tasks
import (
"github.com/nextmn/srv6/internal/constants"
"github.com/nextmn/srv6/internal/iproute2"
)
// TaskIPRule
type TaskIPRule struct {
WithState
prefix string
family4 bool
table iproute2.Table
}
// Create a new Task for IPRule
func NewTaskIP6Rule(prefix string, tablename string) *TaskIPRule {
return &TaskIPRule{
WithState: NewState(),
family4: false,
prefix: prefix,
table: iproute2.NewTable(tablename, constants.RT_PROTO_NEXTMN),
}
}
// Create a new Task for IPRule
func NewTaskIP4Rule(prefix string, tablename string) *TaskIPRule {
return &TaskIPRule{
WithState: NewState(),
family4: true,
prefix: prefix,
table: iproute2.NewTable(tablename, constants.RT_PROTO_NEXTMN),
}
}
// Setup ip rules
func (t *TaskIPRule) RunInit() error {
if t.family4 {
if err := t.table.AddRule4(t.prefix); err != nil {
return err
}
} else {
if err := t.table.AddRule6(t.prefix); err != nil {
return err
}
}
t.state = true
return nil
}
// Delete ip rules
func (t *TaskIPRule) RunExit() error {
if t.family4 {
if err := t.table.DelRule4(t.prefix); err != nil {
return err
}
} else {
if err := t.table.DelRule6(t.prefix); err != nil {
return err
}
}
t.state = false
return nil
}