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 3 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
31 changes: 31 additions & 0 deletions internal/objc/objc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ const char *getUUID()
NSString *uuid = [[NSUUID UUID] UUIDString];
return [uuid UTF8String];
}

const char *CStringFromNSString(void *ptr)
{
return [(NSString *)ptr UTF8String];
}

int LengthFromNSString(void *ptr)
{
return [(NSString *)ptr length];
}
*/
import "C"
import (
Expand Down Expand Up @@ -142,6 +152,27 @@ func (n *NSArray) ToPointerSlice() []unsafe.Pointer {
return ret
}

// NSString indicates NSString
type NSString struct {
*Pointer
}

// NewNSString creates a new NSString from pointer.
func NewNSString(p unsafe.Pointer) *NSString {
return &NSString{NewPointer(p)}
}

// CString converts *C.char from *NSString
func (s *NSString) CString() *C.char {
return C.CStringFromNSString(s.ptr())
}

// String converts Go string from *NSString
func (s *NSString) String() string {
length := C.LengthFromNSString(s.ptr())
return C.GoStringN(s.CString(), C.int(length))
}

// ConvertToNSMutableArray converts to NSMutableArray from NSObject slice in Go world.
func ConvertToNSMutableArray(s []NSObject) *Pointer {
ln := len(s)
Expand Down
36 changes: 36 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ type BridgedNetwork interface {
LocalizedDisplayName() string
}

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

func (b *baseBridgedNetwork) Identifier() string {
nsStirng := objc.NewNSString(
C.VZBridgedNetworkInterface_identifier(objc.Ptr(b)),
)
return nsStirng.String()
}

func (b *baseBridgedNetwork) LocalizedDisplayName() string {
nsStirng := objc.NewNSString(
C.VZBridgedNetworkInterface_localizedDisplayName(objc.Ptr(b)),
)
return nsStirng.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);
void *VZBridgedNetworkInterface_identifier(void *networkInterface);
void *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").
*/
void *VZBridgedNetworkInterface_identifier(void *networkInterface)
{
if (@available(macOS 11, *)) {
return [(VZBridgedNetworkInterface *)networkInterface identifier]; // NSString *
}

RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}

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

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