Skip to content

Commit 74eff93

Browse files
committed
Add VmnetNetwork serialization
Signed-off-by: Norio Nomura <[email protected]>
1 parent d9ee92f commit 74eff93

File tree

3 files changed

+88
-25
lines changed

3 files changed

+88
-25
lines changed

virtualization_26.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ uint32_t VZVmnetNetworkConfiguration_addDhcpReservation(void *config, ether_addr
3232
uint32_t VZVmnetNetworkConfiguration_setMtu(void *config, uint32_t mtu);
3333
// vmnet_network
3434
void *VZVmnetNetworkCreate(void *config, uint32_t *status);
35+
void *VZVmnetNetworkCreateWithSerialization(CFTypeRef serialization, uint32_t *status);
36+
CFTypeRef VZVmnetNetwork_copySerialization(void *network, uint32_t *status);
3537
void VZVmnetNetwork_getIPv6Prefix(void *network, struct in6_addr *prefix, uint8_t *prefix_len);
3638
void VZVmnetNetwork_getIPv4Subnet(void *network, struct in_addr *subnet, struct in_addr *mask);
3739
// VZVmnetNetworkDeviceAttachment

virtualization_26.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ uint32_t VZVmnetNetworkConfiguration_setMtu(void *config, uint32_t mtu)
156156
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
157157
}
158158

159+
// see: https://developer.apple.com/documentation/vmnet/vmnet_network_create_with_serialization(_:_:)?language=objc
160+
void *VZVmnetNetworkCreateWithSerialization(CFTypeRef serialization, uint32_t *status)
161+
{
162+
#ifdef INCLUDE_TARGET_OSX_26
163+
if (@available(macOS 26, *)) {
164+
return vmnet_network_create_with_serialization((xpc_object_t)serialization, status);
165+
}
166+
#endif
167+
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
168+
}
169+
170+
// https://developer.apple.com/documentation/vmnet/vmnet_network_copy_serialization(_:_:)?language=objc
171+
CFTypeRef VZVmnetNetwork_copySerialization(void *network, uint32_t *status)
172+
{
173+
#ifdef INCLUDE_TARGET_OSX_26
174+
if (@available(macOS 26, *)) {
175+
return vmnet_network_copy_serialization((vmnet_network_ref)network, status);
176+
}
177+
#endif
178+
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
179+
}
180+
159181
// see: https://developer.apple.com/documentation/vmnet/vmnet_network_get_ipv6_prefix(_:_:_:)?language=objc
160182
void VZVmnetNetwork_getIPv6Prefix(void *network, struct in6_addr *prefix, uint8_t *prefix_len)
161183
{

vmnet.go

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,70 @@ type VmnetNetwork struct {
296296
*pointer
297297
}
298298

299+
// NewVmnetNetwork creates a new VmnetNetwork with config.
300+
// This is only supported on macOS 26 and newer, error will
301+
// be returned on older versions.
302+
func NewVmnetNetwork(config *VmnetNetworkConfiguration) (*VmnetNetwork, error) {
303+
if err := macOSAvailable(26); err != nil {
304+
return nil, err
305+
}
306+
307+
var status VmnetReturn
308+
ptr := C.VZVmnetNetworkCreate(
309+
objc.Ptr(config),
310+
(*C.uint32_t)(unsafe.Pointer(&status)),
311+
)
312+
if !errors.Is(status, ErrVmnetSuccess) {
313+
return nil, fmt.Errorf("failed to create VmnetNetwork: %w", status)
314+
}
315+
network := &VmnetNetwork{
316+
pointer: objc.NewPointer(ptr),
317+
}
318+
objc.SetFinalizer(network, func(self *VmnetNetwork) {
319+
objc.Release(self)
320+
})
321+
return network, nil
322+
}
323+
324+
// NewVmnetNetworkWithSerialization creates a new VmnetNetwork from a serialized representation.
325+
// This is only supported on macOS 26 and newer, error will
326+
// be returned on older versions.
327+
// see: https://developer.apple.com/documentation/vmnet/vmnet_network_create_with_serialization(_:_:)?language=objc
328+
func NewVmnetNetworkWithSerialization(serialization uintptr) (*VmnetNetwork, error) {
329+
if err := macOSAvailable(26); err != nil {
330+
return nil, err
331+
}
332+
333+
var status VmnetReturn
334+
ptr := C.VZVmnetNetworkCreateWithSerialization(
335+
C.CFTypeRef(serialization),
336+
(*C.uint32_t)(unsafe.Pointer(&status)),
337+
)
338+
if !errors.Is(status, ErrVmnetSuccess) {
339+
return nil, fmt.Errorf("failed to create VmnetNetwork with serialization: %w", status)
340+
}
341+
network := &VmnetNetwork{
342+
pointer: objc.NewPointer(ptr),
343+
}
344+
objc.SetFinalizer(network, func(self *VmnetNetwork) {
345+
objc.Release(self)
346+
})
347+
return network, nil
348+
}
349+
350+
// CopySerialization returns a pointer to a serialized representation of the vmnet network.
351+
func (n *VmnetNetwork) CopySerialization() (uintptr, error) {
352+
var status VmnetReturn
353+
ptr := C.VZVmnetNetwork_copySerialization(
354+
objc.Ptr(n),
355+
(*C.uint32_t)(unsafe.Pointer(&status)),
356+
)
357+
if !errors.Is(status, ErrVmnetSuccess) {
358+
return 0, fmt.Errorf("failed to copy serialization: %w", status)
359+
}
360+
return uintptr(ptr), nil
361+
}
362+
299363
// IPv6Prefix returns the IPv6 prefix of the network.
300364
func (n *VmnetNetwork) IPv6Prefix() (netip.Prefix, error) {
301365
var prefix C.struct_in6_addr
@@ -345,28 +409,3 @@ func inAddrToIP(a C.struct_in_addr) net.IP {
345409
p := (*[4]byte)(unsafe.Pointer(&a))
346410
return net.IPv4(p[0], p[1], p[2], p[3])
347411
}
348-
349-
// NewVmnetNetwork creates a new VmnetNetwork with config.
350-
// This is only supported on macOS 26 and newer, error will
351-
// be returned on older versions.
352-
func NewVmnetNetwork(config *VmnetNetworkConfiguration) (*VmnetNetwork, error) {
353-
if err := macOSAvailable(26); err != nil {
354-
return nil, err
355-
}
356-
357-
var status VmnetReturn
358-
ptr := C.VZVmnetNetworkCreate(
359-
objc.Ptr(config),
360-
(*C.uint32_t)(unsafe.Pointer(&status)),
361-
)
362-
if !errors.Is(status, ErrVmnetSuccess) {
363-
return nil, fmt.Errorf("failed to create VmnetNetwork: %w", status)
364-
}
365-
network := &VmnetNetwork{
366-
pointer: objc.NewPointer(ptr),
367-
}
368-
objc.SetFinalizer(network, func(self *VmnetNetwork) {
369-
objc.Release(self)
370-
})
371-
return network, nil
372-
}

0 commit comments

Comments
 (0)