Skip to content

Commit

Permalink
Merge pull request #7 from Code-Hex/add/macaddr
Browse files Browse the repository at this point in the history
Supported MAC Address
  • Loading branch information
Code-Hex authored Sep 12, 2021
2 parents a5adb62 + f00645a commit 7475877
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ If you want to use [`VZBridgedNetworkDeviceAttachment`](https://developer.apple.

## TODO

- [ ] [VZMACAddress](https://developer.apple.com/documentation/virtualization/vzmacaddress?language=objc)
- [x] [VZMACAddress](https://developer.apple.com/documentation/virtualization/vzmacaddress?language=objc)
- [ ] [VZVirtioSocketDeviceConfiguration](https://developer.apple.com/documentation/virtualization/sockets?language=objc)

## LICENSE

Expand Down
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func main() {
config.SetNetworkDevicesVirtualMachineConfiguration([]*vz.VirtioNetworkDeviceConfiguration{
networkConfig,
})
networkConfig.SetMacAddress(vz.NewRandomLocallyAdministeredMACAddress())

// entropy
entropyConfig := vz.NewVirtioEntropyDeviceConfiguration()
Expand Down
49 changes: 49 additions & 0 deletions network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package vz
*/
import "C"
import (
"net"
"os"
"runtime"
)
Expand Down Expand Up @@ -160,3 +161,51 @@ func NewVirtioNetworkDeviceConfiguration(attachment NetworkDeviceAttachment) *Vi
})
return config
}

func (v *VirtioNetworkDeviceConfiguration) SetMacAddress(macAddress *MACAddress) {
C.setNetworkDevicesVZMACAddress(v.Ptr(), macAddress.Ptr())
}

// MACAddress represents a media access control address (MAC address), the 48-bit ethernet address.
// see: https://developer.apple.com/documentation/virtualization/vzmacaddress?language=objc
type MACAddress struct {
pointer
}

// NewMACAddress creates a new MACAddress with net.HardwareAddr (MAC address).
func NewMACAddress(macAddr net.HardwareAddr) *MACAddress {
macAddrChar := charWithGoString(macAddr.String())
defer macAddrChar.Free()
ma := &MACAddress{
pointer: pointer{
ptr: C.newVZMACAddress(macAddrChar.CString()),
},
}
runtime.SetFinalizer(ma, func(self *MACAddress) {
self.Release()
})
return ma
}

// NewRandomLocallyAdministeredMACAddress creates a valid, random, unicast, locally administered address.
func NewRandomLocallyAdministeredMACAddress() *MACAddress {
ma := &MACAddress{
pointer: pointer{
ptr: C.newRandomLocallyAdministeredVZMACAddress(),
},
}
runtime.SetFinalizer(ma, func(self *MACAddress) {
self.Release()
})
return ma
}

func (m *MACAddress) String() string {
cstring := (*char)(C.getVZMACAddressString(m.Ptr()))
return cstring.String()
}

func (m *MACAddress) HardwareAddr() net.HardwareAddr {
hw, _ := net.ParseMAC(m.String())
return hw
}
4 changes: 4 additions & 0 deletions virtualization.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ void *newVZBridgedNetworkDeviceAttachment(void *networkInterface);
void *newVZNATNetworkDeviceAttachment(void);
void *newVZFileHandleNetworkDeviceAttachment(int fileDescriptor);
void *newVZVirtioNetworkDeviceConfiguration(void *attachment);
void setNetworkDevicesVZMACAddress(void *config, void *macAddress);
void *newVZVirtioEntropyDeviceConfiguration(void);
void *newVZVirtioBlockDeviceConfiguration(void *attachment);
void *newVZDiskImageStorageDeviceAttachment(const char *diskPath, bool readOnly, void **error);
void *newVZVirtioTraditionalMemoryBalloonDeviceConfiguration();
void *newVZVirtioSocketDeviceConfiguration();
void *newVZMACAddress(const char *macAddress);
void *newRandomLocallyAdministeredVZMACAddress();
const char *getVZMACAddressString(void *macAddress);

/* VirtualMachine */
void *newVZVirtualMachineWithDispatchQueue(void *config, void *queue, const char *vmid);
Expand Down
49 changes: 49 additions & 0 deletions virtualization.m
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,55 @@ void setStorageDevicesVZVirtualMachineConfiguration(void *config,
return vm;
}

/*!
@abstract Initialize the VZMACAddress from a string representation of a MAC address.
@param string
The string should be formatted representing the 6 bytes in hexadecimal separated by a colon character.
e.g. "01:23:45:ab:cd:ef"
The alphabetical characters can appear lowercase or uppercase.
@return A VZMACAddress or nil if the string is not formatted correctly.
*/
void *newVZMACAddress(const char *macAddress)
{
VZMACAddress *ret;
@autoreleasepool {
NSString *str = [NSString stringWithUTF8String:macAddress];
ret = [[VZMACAddress alloc] initWithString:str];
}
return ret;
}

/*!
@abstract Create a valid, random, unicast, locally administered address.
@discussion The generated address is not guaranteed to be unique.
*/
void *newRandomLocallyAdministeredVZMACAddress()
{
return [VZMACAddress randomLocallyAdministeredAddress];
}

/*!
@abstract Sets the media access control address of the device.
*/
void setNetworkDevicesVZMACAddress(void *config, void *macAddress)
{
[(VZNetworkDeviceConfiguration *)config setMACAddress:[(VZMACAddress *)macAddress copy]];
}

/*!
@abstract The address represented as a string.
@discussion
The 6 bytes are represented in hexadecimal form, separated by a colon character.
Alphabetical characters are lowercase.
The address is compatible with the parameter of -[VZMACAddress initWithString:].
*/
const char *getVZMACAddressString(void *macAddress)
{
return [[(VZMACAddress *)macAddress string] UTF8String];
}

/*!
@abstract Request that the guest turns itself off.
@param error If not nil, assigned with the error if the request failed.
Expand Down

0 comments on commit 7475877

Please sign in to comment.