Documentation
¶
Overview ¶
Package vsock provides stream connections and listeners over AF_VSOCK sockets on Darwin and Linux without CGo or external dependencies.
AF_VSOCK is the standard host-guest virtual socket address family used by Apple Virtualization.framework and Linux KVM/virtio-vsock. Each endpoint is addressed by a 32-bit context identifier (CID) and a 32-bit port (Addr).
Basic usage:
ln, err := vsock.Listen(1024)
if err != nil {
return err
}
defer ln.Close()
conn, err := vsock.Dial(vsock.CIDHost, 1024)
if err != nil {
return err
}
defer conn.Close()
On Darwin, sockaddr_vm is 12 bytes with a leading length byte; on Linux, sockaddr_vm is 16 bytes without a length prefix. This package handles both layouts transparently using only the Go standard library. On other platforms, Dial, DialContext, Listen, and ListenCID return ErrUnsupported.
Index ¶
Examples ¶
Constants ¶
const ( // CIDAny binds a listener to any available context ID (VMADDR_CID_ANY). CIDAny uint32 = 0xFFFFFFFF // CIDHypervisor addresses the hypervisor (VMADDR_CID_HYPERVISOR). CIDHypervisor uint32 = 0 // CIDLocal addresses the local host for loopback vsock connections (VMADDR_CID_LOCAL). CIDLocal uint32 = 1 // CIDHost addresses the host from within a guest VM (VMADDR_CID_HOST). CIDHost uint32 = 2 // PortAny requests an ephemeral port when binding (VMADDR_PORT_ANY). PortAny uint32 = 0xFFFFFFFF )
Well-known AF_VSOCK context identifiers (CIDs) and port values.
Variables ¶
var ErrUnsupported = errors.New("vsock: unsupported platform")
ErrUnsupported is returned on operating systems that do not support AF_VSOCK.
Functions ¶
func Dial ¶
Dial connects to the given vsock context ID and port.
Example ¶
package main
import (
"github.com/tmc/apple/x/guest/vsock"
)
func main() {
conn, err := vsock.Dial(vsock.CIDHost, 1024)
if err != nil {
return
}
defer conn.Close()
}
Output:
func DialContext ¶
DialContext connects to the given vsock context ID and port, respecting cancellation and deadlines from ctx.
Types ¶
type Addr ¶
Addr represents an AF_VSOCK endpoint address.
Example ¶
package main
import (
"fmt"
"github.com/tmc/apple/x/guest/vsock"
)
func main() {
addr := vsock.Addr{ContextID: vsock.CIDHost, Port: 1024}
fmt.Println(addr.Network(), addr.String())
}
Output: vsock 2:1024