-
Notifications
You must be signed in to change notification settings - Fork 4
/
gont.go
76 lines (62 loc) · 1.62 KB
/
gont.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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package gont
import (
"errors"
"os"
"path"
"github.com/vishvananda/netns"
"kernel.org/pub/linux/libs/security/libcap/cap"
)
const (
hostsFile = "/etc/hosts"
netnsDir = "/var/run/netns/"
baseVarDir = "/var/run/gont"
baseTmpDir = "/tmp/gont"
loopbackInterfaceName = "lo"
bridgeInterfaceName = "br"
)
// Option is the base type for all functional options.
type Option any
// CheckCaps checks if the current process has the required privileges to run Gont
func CheckCaps() error {
c := cap.GetProc()
if v, err := c.GetFlag(cap.Effective, cap.SYS_ADMIN); err != nil || !v {
return errors.New("missing NET_ADMIN capabilities")
}
return nil
}
// Identify returns the network and node name
// if the current process is running in a network netspace created by Gont
func Identify() (string, string, error) {
curHandle, err := netns.Get()
if err != nil {
return "", "", err
}
for _, network := range NetworkNames() {
for _, node := range NodeNames(network) {
f := path.Join("/var/run/gont", network, "nodes", node, "ns", "net")
handle, err := netns.GetFromPath(f)
if err != nil {
return "", "", err
}
if curHandle.Equal(handle) {
return network, node, nil
}
}
}
return "", "", os.ErrNotExist
}
// TestConnectivity performs ICMP ping tests between all pairs of nodes in the network
func TestConnectivity(hosts ...*Host) error {
for _, a := range hosts {
for _, b := range hosts {
if a != b {
if _, err := a.Ping(b); err != nil {
return err
}
}
}
}
return nil
}