Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BridgedNetwork implementation #151

Merged
merged 5 commits into from
Dec 20, 2023
Merged
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
43 changes: 42 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
// virtual machine send and receive packets on the same physical interface but have distinct network layers.
//
// The BridgedNetwork can be used with a BridgedNetworkDeviceAttachment to set up a network device NetworkDeviceConfiguration.
// TODO(codehex): implement...
// see: https://developer.apple.com/documentation/virtualization/vzbridgednetworkinterface?language=objc
type BridgedNetwork interface {
objc.NSObject
Expand All @@ -39,6 +38,48 @@ type BridgedNetwork interface {
LocalizedDisplayName() string
}

// NewBridgedNetwork creates a new BridgedNetwork with identifier.
//
// This is only supported on macOS 11 and newer, error will
// be returned on older versions.
func NetworkInterfaces() []BridgedNetwork {
nsArray := objc.NewNSArray(
C.VZBridgedNetworkInterface_networkInterfaces(),
)
ptrs := nsArray.ToPointerSlice()
networkInterfaces := make([]BridgedNetwork, len(ptrs))
for i, ptr := range ptrs {
networkInterfaces[i] = &baseBridgedNetwork{
pointer: objc.NewPointer(ptr),
}
}
return networkInterfaces
}

type baseBridgedNetwork struct {
*pointer
}

func (*baseBridgedNetwork) NetworkInterfaces() []BridgedNetwork {
return NetworkInterfaces()
}

// Identifier returns the unique identifier for this interface.
//
// The identifier is the BSD name associated with the interface (e.g. "en0").
func (b *baseBridgedNetwork) Identifier() string {
cstring := (*char)(C.VZBridgedNetworkInterface_identifier(objc.Ptr(b)))
return cstring.String()
}

// LocalizedDisplayName returns a display name if available (e.g. "Ethernet").
//
// If no display name is available, the identifier is returned.
func (b *baseBridgedNetwork) LocalizedDisplayName() string {
cstring := (*char)(C.VZBridgedNetworkInterface_localizedDisplayName(objc.Ptr(b)))
return cstring.String()
}

// Network device attachment using network address translation (NAT) with outside networks.
//
// Using the NAT attachment type, the host serves as router and performs network address translation
Expand Down
3 changes: 3 additions & 0 deletions virtualization_11.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config,
void *newVZFileHandleSerialPortAttachment(int readFileDescriptor, int writeFileDescriptor);
void *newVZFileSerialPortAttachment(const char *filePath, bool shouldAppend, void **error);
void *newVZVirtioConsoleDeviceSerialPortConfiguration(void *attachment);
void *VZBridgedNetworkInterface_networkInterfaces(void);
const char *VZBridgedNetworkInterface_identifier(void *networkInterface);
const char *VZBridgedNetworkInterface_localizedDisplayName(void *networkInterface);
void *newVZBridgedNetworkDeviceAttachment(void *networkInterface);
void *newVZNATNetworkDeviceAttachment(void);
void *newVZFileHandleNetworkDeviceAttachment(int fileDescriptor);
Expand Down
44 changes: 44 additions & 0 deletions virtualization_11.m
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,50 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config,
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract Return the list of network interfaces available for bridging.
@discussion
A bridged interface is shared between the virtual machine and the host system. Both host and virtual machine send and receive packets on the same physical interface but have distinct network layers.

VZBridgedNetworkInterface cannot be instantiated directly. It can be used with a VZBridgedNetworkDeviceAttachment to set up a network device VZNetworkDeviceConfiguration.

@seealso VZBridgedNetworkDeviceAttachment
@seealso VZNATNetworkDeviceAttachment
@seealso VZNetworkDeviceConfiguration
*/
void *VZBridgedNetworkInterface_networkInterfaces()
{
if (@available(macOS 11, *)) {
return [VZBridgedNetworkInterface networkInterfaces]; // NSArray<VZBridgedNetworkInterface *>
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract Return the unique identifier for this interface. The identifier is the BSD name associated with the interface (e.g. "en0").
*/
const char *VZBridgedNetworkInterface_identifier(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [[(VZBridgedNetworkInterface *)networkInterface identifier] UTF8String];
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract Return a display name if available (e.g. "Ethernet").
*/
const char *VZBridgedNetworkInterface_localizedDisplayName(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [[(VZBridgedNetworkInterface *)networkInterface localizedDisplayName] UTF8String];
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

/*!
@abstract Create a new Network device attachment bridging a host physical interface with a virtual network device.
@param networkInterface a network interface that bridges a physical interface.
Expand Down
Loading