Skip to content

Commit be82404

Browse files
committed
move nshandle into top level gofile
1 parent 1e4e8a6 commit be82404

File tree

2 files changed

+66
-58
lines changed

2 files changed

+66
-58
lines changed

netns.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Package netns allows ultra-simple network namespace handling. NsHandles
2+
// can be retrieved and set. Note that the current namespace is thread
3+
// local so actions that set and reset namespaces should use LockOSThread
4+
// to make sure the namespace doesn't change due to a goroutine switch.
5+
// It is best to close NsHandles when you are done with them. This can be
6+
// accomplished via a `defer ns.Close()` on the handle. Changing namespaces
7+
// requires elevated privileges, so in most cases this code needs to be run
8+
// as root.
9+
package netns
10+
11+
import (
12+
"fmt"
13+
"syscall"
14+
)
15+
// NsHandle is a handle to a network namespace. It can be cast directly
16+
// to an int and used as a file descriptor.
17+
type NsHandle int
18+
19+
// Equal determines if two network handles refer to the same network
20+
// namespace. This is done by comparing the device and inode that the
21+
// file descripors point to.
22+
func (ns NsHandle) Equal(other NsHandle) bool {
23+
if ns == other {
24+
return true
25+
}
26+
var s1, s2 syscall.Stat_t
27+
if err := syscall.Fstat(int(ns), &s1); err != nil {
28+
return false
29+
}
30+
if err := syscall.Fstat(int(other), &s2); err != nil {
31+
return false
32+
}
33+
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
34+
}
35+
36+
// String shows the file descriptor number and its dev and inode.
37+
func (ns NsHandle) String() string {
38+
var s syscall.Stat_t
39+
if ns == -1 {
40+
return "NS(None)"
41+
}
42+
if err := syscall.Fstat(int(ns), &s); err != nil {
43+
return fmt.Sprintf("NS(%d: unknown)", ns)
44+
}
45+
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
46+
}
47+
48+
// IsOpen returns true if Close() has not been called.
49+
func (ns NsHandle) IsOpen() bool {
50+
return ns != -1
51+
}
52+
53+
// Close closes the NsHandle and resets its file descriptor to -1.
54+
// It is not safe to use an NsHandle after Close() is called.
55+
func (ns *NsHandle) Close() error {
56+
if err := syscall.Close(int(*ns)); err != nil {
57+
return err
58+
}
59+
(*ns) = -1
60+
return nil
61+
}
62+
63+
// Get an empty (closed) NsHandle
64+
func None() NsHandle {
65+
return NsHandle(-1)
66+
}

netns_linux.go

-58
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// Package netns allows ultra-simple network namespace handling. NsHandles
2-
// can be retrieved and set. Note that the current namespace is thread
3-
// local so actions that set and reset namespaces should use LockOSThread
4-
// to make sure the namespace doesn't change due to a goroutine switch.
5-
// It is best to close NsHandles when you are done with them. This can be
6-
// accomplished via a `defer ns.Close()` on the handle. Changing namespaces
7-
// requires elevated privileges, so in most cases this code needs to be run
8-
// as root.
91
package netns
102

113
import (
@@ -39,56 +31,6 @@ func Setns(ns NsHandle, nstype int) (err error) {
3931
return
4032
}
4133

42-
// NsHandle is a handle to a network namespace. It can be cast directly
43-
// to an int and used as a file descriptor.
44-
type NsHandle int
45-
46-
// Equal determines if two network handles refer to the same network
47-
// namespace. This is done by comparing the device and inode that the
48-
// file descripors point to.
49-
func (ns NsHandle) Equal(other NsHandle) bool {
50-
var s1, s2 syscall.Stat_t
51-
if err := syscall.Fstat(int(ns), &s1); err != nil {
52-
return false
53-
}
54-
if err := syscall.Fstat(int(other), &s2); err != nil {
55-
return false
56-
}
57-
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
58-
}
59-
60-
// String shows the file descriptor number and its dev and inode.
61-
func (ns NsHandle) String() string {
62-
var s syscall.Stat_t
63-
if ns == -1 {
64-
return "NS(None)"
65-
}
66-
if err := syscall.Fstat(int(ns), &s); err != nil {
67-
return fmt.Sprintf("NS(%d: unknown)", ns)
68-
}
69-
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino)
70-
}
71-
72-
// IsOpen returns true if Close() has not been called.
73-
func (ns NsHandle) IsOpen() bool {
74-
return ns != -1
75-
}
76-
77-
// Close closes the NsHandle and resets its file descriptor to -1.
78-
// It is not safe to use an NsHandle after Close() is called.
79-
func (ns *NsHandle) Close() error {
80-
if err := syscall.Close(int(*ns)); err != nil {
81-
return err
82-
}
83-
(*ns) = -1
84-
return nil
85-
}
86-
87-
// Get an empty (closed) NsHandle
88-
func None() NsHandle {
89-
return NsHandle(-1)
90-
}
91-
9234
// Set sets the current network namespace to the namespace represented
9335
// by NsHandle.
9436
func Set(ns NsHandle) (err error) {

0 commit comments

Comments
 (0)