-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathnetns_other.go
More file actions
68 lines (60 loc) · 2.23 KB
/
netns_other.go
File metadata and controls
68 lines (60 loc) · 2.23 KB
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
//go:build !linux
package rdns
import (
"context"
"fmt"
"net"
)
// NetNS represents a Linux network namespace.
// On non-Linux platforms, configuring a namespace returns an error.
type NetNS struct {
Name string
}
// RunInNetNS executes fn in the given network namespace. On non-Linux
// platforms this returns an error if a namespace is configured.
func RunInNetNS(ns *NetNS, fn func() error) error {
if ns == nil || ns.Name == "" {
return fn()
}
return fmt.Errorf("network namespaces are only supported on Linux")
}
// ListenInNetNS creates a net.Listener in the given network namespace.
func ListenInNetNS(ctx context.Context, ns *NetNS, network, address string, opts SocketOptions) (net.Listener, error) {
if ns != nil && ns.Name != "" {
return nil, fmt.Errorf("network namespaces are only supported on Linux")
}
lc := net.ListenConfig{Control: opts.dialerControl()}
return lc.Listen(ctx, network, address)
}
// ListenPacketInNetNS creates a net.PacketConn in the given network namespace.
func ListenPacketInNetNS(ctx context.Context, ns *NetNS, network, address string, opts SocketOptions) (net.PacketConn, error) {
if ns != nil && ns.Name != "" {
return nil, fmt.Errorf("network namespaces are only supported on Linux")
}
lc := net.ListenConfig{Control: opts.dialerControl()}
return lc.ListenPacket(ctx, network, address)
}
// ListenUDPInNetNS creates a *net.UDPConn in the given network namespace.
func ListenUDPInNetNS(ctx context.Context, ns *NetNS, network string, laddr *net.UDPAddr, opts SocketOptions) (*net.UDPConn, error) {
if ns != nil && ns.Name != "" {
return nil, fmt.Errorf("network namespaces are only supported on Linux")
}
lc := net.ListenConfig{Control: opts.dialerControl()}
pc, err := lc.ListenPacket(ctx, network, laddr.String())
if err != nil {
return nil, err
}
conn, ok := pc.(*net.UDPConn)
if !ok {
pc.Close()
return nil, fmt.Errorf("expected *net.UDPConn, got %T", pc)
}
return conn, nil
}
// DialInNetNS dials a connection in the given network namespace.
func DialInNetNS(ns *NetNS, network, address string, dialer *net.Dialer) (net.Conn, error) {
if ns != nil && ns.Name != "" {
return nil, fmt.Errorf("network namespaces are only supported on Linux")
}
return dialer.Dial(network, address)
}