Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/macOS/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ require github.com/Code-Hex/vz/v3 v3.0.0-00010101000000-000000000000
require (
github.com/Code-Hex/go-infinity-channel v1.0.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sys v0.36.0 // indirect
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ require (
golang.org/x/mod v0.22.0
)

require golang.org/x/sys v0.36.0 // indirect
require golang.org/x/sys v0.36.0
56 changes: 55 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package vz

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization -framework vmnet
# include "virtualization_11.h"
# include "virtualization_13.h"
# include "virtualization_26.h"
*/
import "C"
import (
Expand Down Expand Up @@ -260,6 +261,55 @@ func (f *FileHandleNetworkDeviceAttachment) MaximumTransmissionUnit() int {
return f.mtu
}

// VmnetNetworkDeviceAttachment represents a vmnet network device attachment.
//
// This attachment is used to connect a virtual machine to a vmnet network.
// The attachment is created with a VmnetNetwork and can be used with a VirtioNetworkDeviceConfiguration.
// see: https://developer.apple.com/documentation/virtualization/vzvmnetnetworkdeviceattachment?language=objc
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
type VmnetNetworkDeviceAttachment struct {
*pointer

*baseNetworkDeviceAttachment
}

func (*VmnetNetworkDeviceAttachment) String() string {
return "VmnetNetworkDeviceAttachment"
}

func (v *VmnetNetworkDeviceAttachment) Network() *VmnetNetwork {
network := C.VZVmnetNetworkDeviceAttachment_network(objc.Ptr(v))
return &VmnetNetwork{
pointer: objc.NewPointer(network),
}
}

var _ NetworkDeviceAttachment = (*VmnetNetworkDeviceAttachment)(nil)

// NewVmnetNetworkDeviceAttachment creates a new VmnetNetworkDeviceAttachment with network.
//
// This is only supported on macOS 26 and newer, error will
// be returned on older versions.
func NewVmnetNetworkDeviceAttachment(network *VmnetNetwork) (*VmnetNetworkDeviceAttachment, error) {
if err := macOSAvailable(26); err != nil {
return nil, err
}

attachment := &VmnetNetworkDeviceAttachment{
pointer: objc.NewPointer(
C.newVZVmnetNetworkDeviceAttachment(
objc.Ptr(network),
),
),
}
objc.SetFinalizer(attachment, func(self *VmnetNetworkDeviceAttachment) {
objc.Release(self)
})
return attachment, nil
}

// NetworkDeviceAttachment for a network device attachment.
// see: https://developer.apple.com/documentation/virtualization/vznetworkdeviceattachment?language=objc
type NetworkDeviceAttachment interface {
Expand Down Expand Up @@ -373,6 +423,10 @@ func (m *MACAddress) String() string {
return cstring.String()
}

func (m *MACAddress) ethernetAddress() C.ether_addr_t {
return C.getVZMACAddressEthernetAddress(objc.Ptr(m))
}

func (m *MACAddress) HardwareAddr() net.HardwareAddr {
hw, _ := net.ParseMAC(m.String())
return hw
Expand Down
54 changes: 54 additions & 0 deletions pkg/xpc/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package xpc

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
# include "xpc.h"
*/
import "C"
import (
"runtime/cgo"
"unsafe"
)

// Error represents an XPC rich error.
// see: https://developer.apple.com/documentation/xpc/xpc_rich_error_t?language=objc
type Error = Object

var _ error = Error{}

// newError creates a new Error from an existing xpc_rich_error_t.
// internal use only.
func newError(richErr unsafe.Pointer) *Error {
if richErr == nil {
return nil
}
return NewObject(richErr)
}

// wrapError wraps an existing xpc_rich_error_t into an Error and returns a handle.
// intended to be called from C.
//
//export wrapError
func wrapError(richErr unsafe.Pointer) uintptr {
obj := newError(richErr)
if obj == nil {
return 0
}
obj.handle = cgo.NewHandle(obj)
return uintptr(obj.handle)
}

// unwrapError unwraps a handle into an *Error.
func unwrapError(handle uintptr) *Error {
if handle == 0 {
return nil
}
return cgo.Handle(handle).Value().(*Error)
}

// Error implements the error interface.
func (e Error) Error() string {
desc := C.xpcRichErrorCopyDescription(e.XpcObject)
defer C.free(unsafe.Pointer(desc))
return C.GoString(desc)
}
100 changes: 100 additions & 0 deletions pkg/xpc/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package xpc

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation
# include "xpc.h"
*/
import "C"
import (
"runtime/cgo"
"unsafe"
)

// Listener represents an XPC listener.
type Listener struct {
// Exported for use in other packages since unimplemented XPC API may require direct access to xpc_listener_t.
XpcListener unsafe.Pointer
queue unsafe.Pointer
sessionHandler cgo.Handle
}

// SessionHandler is a function that handles incoming sessions.
type SessionHandler func(session *Session)

type listenerOption struct {
sessionHandler SessionHandler
messageHandler MessageHandler
cancellationHandler CancellationHandler
peerRequirement *PeerRequirement
}

// NewListener creates a new XPC listener for the given service name.
// You need to call [Listener.Activate] to start accepting incoming connections.
func NewListener(service string, handler SessionHandler) (*Listener, error) {
cname := C.CString(service)
defer C.free(unsafe.Pointer(cname))
// Use a serial dispatch queue for the listener,
// because the vmnet framework API does not seem to work well with concurrent queues.
// For example, vmnet_network_create fails when using a concurrent queue.
queue := C.dispatchQueueCreateSerial(cname)
cgoSessionHandler := cgo.NewHandle(handler)

var err_out unsafe.Pointer
ptr := C.xpcListenerCreate(
cname,
queue,
C.XPC_LISTENER_CREATE_INACTIVE,
C.uintptr_t(cgoSessionHandler),
&err_out,
)
if err_out != nil {
C.dispatchRelease(queue)
cgoSessionHandler.Delete()
return nil, newError(err_out)
}
return &Listener{
XpcListener: ptr,
queue: queue,
sessionHandler: cgoSessionHandler,
}, nil
}

// Activate starts the listener to accept incoming connections.
// see: https://developer.apple.com/documentation/xpc/xpc_listener_activate
func (l *Listener) Activate() error {
var err_out unsafe.Pointer
C.xpcListenerActivate(l.XpcListener, &err_out)
if err_out != nil {
return newError(err_out)
}
return nil
}

// Close stops the listener from accepting incoming connections.
// see: https://developer.apple.com/documentation/xpc/xpc_listener_cancel
func (l *Listener) Close() error {
C.xpcListenerCancel(l.XpcListener)

C.xpcRelease(l.XpcListener)
if l.sessionHandler != 0 {
l.sessionHandler.Delete()
l.sessionHandler = 0
}
C.dispatchRelease(l.queue)
return nil
}

// SetPeerRequirement sets the peer requirement for the listener.
// see: https://developer.apple.com/documentation/xpc/xpc_listener_set_peer_requirement
func (l *Listener) SetPeerRequirement(req *PeerRequirement) {
C.xpcListenerSetPeerRequirement(l.XpcListener, req.XpcObject)
}

// String returns a description of the listener.
// see: https://developer.apple.com/documentation/xpc/xpc_listener_copy_description
func (l *Listener) String() string {
desc := C.xpcListenerCopyDescription(l.XpcListener)
defer C.free(unsafe.Pointer(desc))
return C.GoString(desc)
}
145 changes: 145 additions & 0 deletions pkg/xpc/object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package xpc

/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
# include "xpc.h"
*/
import "C"
import (
"runtime"
"runtime/cgo"
"unsafe"
)

// Object represents a generic XPC object.
// see: https://developer.apple.com/documentation/xpc/xpc_object_t?language=objc
type Object struct {
// Exported for use in other packages since unimplemented XPC API may require direct access to xpc_object_t.
XpcObject unsafe.Pointer
handle cgo.Handle
}

// NewObject creates a new Object from an existing xpc_object_t.
func NewObject(xpcObj unsafe.Pointer) *Object {
if xpcObj == nil {
return nil
}
return &Object{XpcObject: xpcObj}
}

// wrapObject wraps an existing xpc_object_t into an Object and returns a handle.
// intended to be called from C.
//
//export wrapObject
func wrapObject(o unsafe.Pointer) uintptr {
obj := NewObject(o)
obj.handle = cgo.NewHandle(obj)
return uintptr(obj.handle)
}

// unwrapObject unwraps a handle into an *Object.
func unwrapObject(handle uintptr) *Object {
if handle == 0 {
return nil
}
return cgo.Handle(handle).Value().(*Object)
}

// NewDictionary creates a new empty XPC dictionary object.
// You can customize the dictionary object using DictionaryEntry.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_create_empty()?language=objc
func NewDictionary(entries ...DictionaryEntry) *Object {
obj := NewObject(C.xpcDictionaryCreateEmpty())
for _, e := range entries {
e(obj)
}
return obj
}

// Retain retains the XPC object and returns itself.
// see: https://developer.apple.com/documentation/xpc/xpc_retain?language=objc
func (o *Object) Retain() *Object {
C.xpcRetain(o.XpcObject)
runtime.SetFinalizer(o, func(o *Object) {
o.release()
})
return o
}

// release releases the XPC object.
// see: https://developer.apple.com/documentation/xpc/xpc_release?language=objc
func (o *Object) release() {
if o.handle != 0 {
o.handle.Delete()
o.handle = 0
}
C.xpcRelease(o.XpcObject)
}

// DictionaryGetString retrieves a string value from the XPC dictionary object by key.
// Returns an empty string if the key does not exist.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_get_string(_:_:)?language=objc
func (o *Object) DictionaryGetString(key string) string {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
val := C.xpcDictionaryGetString(o.XpcObject, cKey)
return C.GoString(val)
}

// DictionaryGetValue retrieves a value from the XPC dictionary object by key.
// Returns nil if the key does not exist.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_get_value(_:_:)?language=objc
func (o *Object) DictionaryGetValue(key string) *Object {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
val := C.xpcDictionaryGetValue(o.XpcObject, cKey)
if val == nil {
return nil
}
return NewObject(val)
}

// DictionaryEntry defines a function type for customizing reply objects.
type DictionaryEntry func(*Object)

// WithString sets a string value in the reply dictionary.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_set_string(_:_:_:)?language=objc
func WithString(key, value string) DictionaryEntry {
return func(o *Object) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
cValue := C.CString(value)
defer C.free(unsafe.Pointer(cValue))
C.xpcDictionarySetString(o.XpcObject, cKey, cValue)
}
}

// WithValue sets a value in the reply dictionary.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_set_value(_:_:_:)?language=objc
func WithValue(key string, val *Object) DictionaryEntry {
return func(o *Object) {
cKey := C.CString(key)
defer C.free(unsafe.Pointer(cKey))
C.xpcDictionarySetValue(o.XpcObject, cKey, val.XpcObject)
}
}

// DictionaryCreateReply creates a new reply XPC dictionary object based on the current object.
// see: https://developer.apple.com/documentation/xpc/xpc_dictionary_create_reply(_:)?language=objc
// You can customize the reply dictionary using DictionaryEntry.
func (o *Object) DictionaryCreateReply(entries ...DictionaryEntry) *Object {
reply := C.xpcDictionaryCreateReply(o.XpcObject)
obj := NewObject(unsafe.Pointer(reply))
for _, entry := range entries {
entry(obj)
}
return obj
}

// String returns the description of the XPC object.
// see: https://developer.apple.com/documentation/xpc/xpc_copy_description(_:)?language=objc
func (o *Object) String() string {
cs := C.xpcCopyDescription(o.XpcObject)
defer C.free(unsafe.Pointer(cs))
return C.GoString(cs)
}
Loading
Loading