forked from jsimonetti/rtnetlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetns.go
54 lines (46 loc) · 1.24 KB
/
netns.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
package rtnetlink
import (
"os"
"path/filepath"
"github.com/jsimonetti/rtnetlink/internal/unix"
)
// NetNS represents a Linux network namespace
type NetNS struct {
file *os.File
pid uint32
}
// NewNetNS returns a new NetNS from the given type
// When an uint32 is given simply the pid value is set
// When a string is given a namespace file is opened with the name and the file descriptor is set
// The file descriptor should be closed after use with the Close() method
func NewNetNS[T string | uint32](t T) (*NetNS, error) {
if name, ok := any(t).(string); ok {
file, err := os.Open(filepath.Join("/var/run/netns", name))
if err != nil {
return nil, err
}
return &NetNS{file: file}, nil
}
return &NetNS{pid: any(t).(uint32)}, nil
}
// Type returns either unix.IFLA_NET_NS_FD or unix.IFLA_NET_NS_PID according ns data type
func (n *NetNS) Type() uint16 {
if n.file != nil {
return unix.IFLA_NET_NS_FD
}
return unix.IFLA_NET_NS_PID
}
// Value returns either a file descriptor value or the pid value of the ns
func (n *NetNS) Value() uint32 {
if n.file != nil {
return uint32(n.file.Fd())
}
return n.pid
}
// Close closes the file descriptor
func (n *NetNS) Close() error {
if n.file != nil {
return n.file.Close()
}
return nil
}