-
Notifications
You must be signed in to change notification settings - Fork 51
/
ports.go
93 lines (74 loc) · 1.79 KB
/
ports.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package acls
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/aporeto-inc/trireme-lib/policy"
)
// portAction captures the minimum and maximum ports for an action
type portAction struct {
min uint16
max uint16
policy *policy.FlowPolicy
}
// portActionList is a list of Port Actions
type portActionList []*portAction
// newPortAction parses a port spec and creates the action
func newPortAction(rule policy.IPRule) (*portAction, error) {
p := &portAction{}
if strings.Contains(rule.Port, ":") {
parts := strings.Split(rule.Port, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid port: %s", rule.Port)
}
port, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
p.min = uint16(port)
port, err = strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
p.max = uint16(port)
} else {
port, err := strconv.Atoi(rule.Port)
if err != nil {
return nil, err
}
p.min = uint16(port)
p.max = p.min
}
if p.min > p.max {
return nil, errors.New("min port is greater than max port")
}
p.policy = rule.Policy
return p, nil
}
func (p *portActionList) lookup(port uint16, preReported *policy.FlowPolicy) (report *policy.FlowPolicy, packet *policy.FlowPolicy, err error) {
report = preReported
// Scan the ports - TODO: better algorithm needed here
for _, pa := range *p {
if port >= pa.min && port <= pa.max {
// Check observed policies.
if pa.policy.ObserveAction.Observed() {
if report != nil {
continue
}
report = pa.policy
if pa.policy.ObserveAction.ObserveContinue() {
continue
}
packet = report
return report, packet, nil
}
packet = pa.policy
if report == nil {
report = packet
}
return report, packet, nil
}
}
return report, packet, errors.New("No match")
}