my code looks likes this
import Virtualization
import vmnet
import Darwin
@available(macOS 26.0, *)
public class SharedVMNetManager {
public static let shared = SharedVMNetManager()
private var activeNetwork: vmnet_network_ref?
private var activeConfig: vmnet_network_configuration_ref?
// Track which MACs we have already told the kernel about
private var registeredMacs: Set<String> = []
private let lock = NSLock()
private init() {}
public func getAttachment(macAddress: String, reservedIP: String) -> VZVmnetNetworkDeviceAttachment? {
lock.lock()
defer { lock.unlock() }
var status: vmnet_return_t = .VMNET_SUCCESS
// 1. Check if we've already initialized the hardware switch
if let config = activeConfig, let network = activeNetwork {
// ONLY add the reservation if we haven't done it for this MAC yet
if !registeredMacs.contains(macAddress) {
addReservation(to: config, mac: macAddress, ip: reservedIP)
registeredMacs.insert(macAddress)
}
return VZVmnetNetworkDeviceAttachment(network: network)
}
// 2. First-time initialization
let mode: vmnet.operating_modes_t = .VMNET_SHARED_MODE
guard let config = vmnet_network_configuration_create(mode, &status),
status == .VMNET_SUCCESS else { return nil }
// Define Subnet
var subnet = in_addr(), mask = in_addr()
inet_pton(AF_INET, "192.168.142.1", &subnet)
inet_pton(AF_INET, "255.255.255.0", &mask)
vmnet_network_configuration_set_ipv4_subnet(config, &subnet, &mask)
// Define the Pool (Make sure your reserved IP is INSIDE this range)
/*var start = in_addr(), end = in_addr()
inet_pton(AF_INET, "192.168.142.10", &start)
inet_pton(AF_INET, "192.168.142.50", &end)
vmnet_network_configuration_set_ipv4_pool(config, &start, &end)*/
// 3. Register the first MAC before the network starts
addReservation(to: config, mac: macAddress, ip: reservedIP)
registeredMacs.insert(macAddress)
// 4. Commit and Create Network
guard let network = vmnet_network_create(config, &status),
status == .VMNET_SUCCESS else { return nil }
self.activeConfig = config
self.activeNetwork = network
return VZVmnetNetworkDeviceAttachment(network: network)
}
private func addReservation(to config: vmnet_network_configuration_ref, mac: String, ip: String) {
var macAddr = ether_addr()
guard let macPtr = ether_aton(mac) else { return }
macAddr = macPtr.pointee
var ipAddr = in_addr()
inet_pton(AF_INET, ip, &ipAddr)
// This tells the kernel's DHCP server: "If you see this MAC, give it this IP"
let status = vmnet_network_configuration_add_dhcp_reservation(config, &macAddr, &ipAddr)
if status != .VMNET_SUCCESS {
print("dhcp faliure \(status.rawValue)")
}
}
}
For the first virtual machine the ip address is getting reserved but from second vm onwards ip is not getting reserved it gets from dhcp server. this makes me not able to assign ip address to second virtual machine onwards thus we cant determine the ipadddress of second vm onwards. is there a way we can ask vmnet to refresh the config. is it possible? Or the only way is to create the network config with all the ip address on start of the app and take it from there. what if the user creates a new vm. how to handle this kind of situation. thanks in advance