Port forwarding with VZVmnetNetworkDeviceAttachment

I have the following code for port forwarding in mac os virtualization

        var ipAddr = in_addr()
        
        // 1. Convert String to in_addr
        inet_pton(AF_INET, guestIP, &ipAddr)
        
        let status = vmnet_network_configuration_add_port_forwarding_rule(
            config,
            UInt8(IPPROTO_TCP),                        // TCP protocol
            sa_family_t(AF_INET),     // address family
            guestPort,                // internal port (guest)
            externalPort,             // external port (host)
            &ipAddr                   // internal address (guest IP)
        )
        
        if status == .VMNET_SUCCESS {
            print("✅ Port Forwarding set: Mac:\(externalPort) -> VM(\(guestIP)):\(guestPort)")
        } else {
            print("❌ Port Forwarding failed for \(guestIP): \(status.rawValue)")
        }

It is returning success but when i test it it does not work. Is there anything i am doing wrong? Please help me also in fixing this problem. Note: The app runs in sandbox i tried without sandboxing and it does not work either. Please refer to this link https://developer.apple.com/forums/thread/822025?login=true&page=1#884236022 how i am creating the VZVmnetNetworkDeviceAttachment

Is there anything i am doing wrong?

It’s hard to say without more context. Specifically:

  • What network have you assigned to the interface?
  • Has the guest successfully acquired an IP address?
  • Does that IP address match the IP address you’re passing in here (ipAddr)?
  • What value as you using for guestPort?
  • Are you sure that the guest is listening on the port? If you run a command on the guest to connect to it’s IP address and port (so ipAddr and guestPort), does that go through?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

ard to say without more context. Specifically:

The guest acquiring the ip address correct for test i have created web server on the guest listen on port 5264 when i try to acces the port directly with the ip address of the guest it is working. i can reach the guest ip from the host with the port 5264. so i forward port 8000 of host as external port in the sample code what i have provided and i access http://localhost:8000 on the host and it does not work. Hope i have give enough details.

Port forwarding with VZVmnetNetworkDeviceAttachment
 
 
Q