-
Notifications
You must be signed in to change notification settings - Fork 66
/
conn_linux.go
62 lines (51 loc) · 1.33 KB
/
conn_linux.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
//go:build linux
// +build linux
package vsock
import (
"context"
"github.com/mdlayher/socket"
"golang.org/x/sys/unix"
)
// A conn is the net.Conn implementation for connection-oriented VM sockets.
// We can use socket.Conn directly on Linux to implement all of the necessary
// methods.
type conn = socket.Conn
// dial is the entry point for Dial on Linux.
func dial(cid, port uint32, _ *Config) (*Conn, error) {
// TODO(mdlayher): Config default nil check and initialize. Pass options to
// socket.Config where necessary.
c, err := socket.Socket(unix.AF_VSOCK, unix.SOCK_STREAM, 0, "vsock", nil)
if err != nil {
return nil, err
}
sa := &unix.SockaddrVM{CID: cid, Port: port}
rsa, err := c.Connect(context.Background(), sa)
if err != nil {
_ = c.Close()
return nil, err
}
// TODO(mdlayher): getpeername(2) appears to return nil in the GitHub CI
// environment, so in the event of a nil sockaddr, fall back to the previous
// method of synthesizing the remote address.
if rsa == nil {
rsa = sa
}
lsa, err := c.Getsockname()
if err != nil {
_ = c.Close()
return nil, err
}
lsavm := lsa.(*unix.SockaddrVM)
rsavm := rsa.(*unix.SockaddrVM)
return &Conn{
c: c,
local: &Addr{
ContextID: lsavm.CID,
Port: lsavm.Port,
},
remote: &Addr{
ContextID: rsavm.CID,
Port: rsavm.Port,
},
}, nil
}