vsock

package
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
var ErrUnsupported = errors.New("vsock: unsupported platform")

ErrUnsupported is returned on operating systems that do not support AF_VSOCK.

Functions

func Dial

func Dial(cid, port uint32) (net.Conn, error)

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()
}

func DialContext

func DialContext(ctx context.Context, cid, port uint32) (net.Conn, error)

DialContext connects to the given vsock context ID and port, respecting cancellation and deadlines from ctx.

func Listen

func Listen(port uint32) (net.Listener, error)

Listen announces on the given vsock port using CIDAny.

Example
package main

import (
	"github.com/tmc/apple/x/guest/vsock"
)

func main() {
	ln, err := vsock.Listen(1024)
	if err != nil {
		return
	}
	defer ln.Close()
}

func ListenCID

func ListenCID(cid, port uint32) (net.Listener, error)

ListenCID announces on the given vsock context ID and port.

Types

type Addr

type Addr struct {
	ContextID uint32
	Port      uint32
}

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

func (Addr) Network

func (Addr) Network() string

Network returns the address's network name, "vsock".

func (Addr) String

func (a Addr) String() string

String formats the address as "cid:port".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL