-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
82 lines (68 loc) · 1.4 KB
/
types.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
package connstate
import (
"context"
"github.com/vishvananda/netlink"
)
type Container struct {
ID string
PID int
Hostname string
// Stores extension data
Annotations map[string]string
}
type ContainerDriver interface {
ListContainer(context.Context) ([]Container, error)
}
type ConnectionState struct {
Container
NetNSID string
IPv4 TCPStates
IPv6 TCPStates
}
type ConnectionStatistics struct {
Container
NetNSID string
IPv4 TCPStatistics
IPv6 TCPStatistics
}
type TCPStatistics struct {
allocatedPorts map[uint16]bool
states map[uint8]uint64
}
func NewTCPStatistics() *TCPStatistics {
return &TCPStatistics{
allocatedPorts: make(map[uint16]bool),
states: make(map[uint8]uint64),
}
}
func (s *TCPStatistics) Record(socket *netlink.Socket) {
if socket == nil {
return
}
s.allocatedPorts[socket.ID.SourcePort] = true
s.states[socket.State]++
}
func (s TCPStatistics) AllocatedPortCount() int {
counter := 0
for _, allocated := range s.allocatedPorts {
if allocated {
counter++
}
}
return counter
}
func (s TCPStatistics) CountByState() map[uint8]uint64 {
shadow := make(map[uint8]uint64)
for k, v := range s.states {
shadow[k] = v
}
return shadow
}
type TCPStates []TCPState
type TCPState struct {
Socket netlink.Socket
TXBytes uint64
RXBytes uint64
}
// Return false if wanna bypass
type ContainerFilter func(Container) (pass bool)