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

Can we create socket device ? #19

Open
amit-bst opened this issue May 18, 2021 · 9 comments
Open

Can we create socket device ? #19

amit-bst opened this issue May 18, 2021 · 9 comments

Comments

@amit-bst
Copy link

amit-bst commented May 18, 2021

Hi,

I was trying to create a socket device

In getVMConfig function :-

VZVirtioSocketDeviceConfiguration *socketVirtioConf = [[VZVirtioSocketDeviceConfiguration alloc] init];

NSArray *socketVirtioArray = [NSArray arrayWithObject:socketVirtioConf];
[conf setSocketDevices:socketVirtioArray];

In main after VZVirtualMachine init
VZVirtioSocketDevice *socketDevice = (VZVirtioSocketDevice *)[vm.socketDevices objectAtIndex:0]

I am able to get the socketDevice. But when i try to call [socketDevice setSocketListener:listener forPort:19923]; I get illegal hardware exception. Can you help me out ?

@JonasGessner
Copy link

Same here. Are you using an M1 or Intel Mac?

@mheese
Copy link

mheese commented Sep 8, 2021

@amit-bst @JonasGessner It took me a long time to figure this out. I had the same problem, but I was able to get passed this: what you need to pay attention to is that the setSocketListener:forPort: call must be performed on the same dispatch queue as all the other calls that interact with the virtual machine.

However, after I passed this point, I still have no clue on how to actually access connections. I have a delegate object for the listener (VZVirtioSocketListenerDelegate), but the protocol only has one method which says if you want to allow or reject connections. But I don't see any way to actually access the connection once I return true from there. (And trying to access the file descriptor from the object as passed into the function give me a bad file descriptor error.)

@amit-bst
Copy link
Author

amit-bst commented Sep 9, 2021

@mheese yes that was the issue, we were able to move forward, sorry forgot to post it here. Thank you so much !!

@mheese
Copy link

mheese commented Sep 10, 2021

@amit-bst were you able to use the listener though? I still can't figure this out...

@Code-Hex
Copy link

If you initialized VZVirtualMachine with the initWithConfiguration:queue: method, you need to call those methods on the same dispatch queue which is used when initialization.

See: Code-Hex/vz#9 (comment)

@dmarkey
Copy link

dmarkey commented Feb 21, 2022

Sorry for hijacking the thread, has anyone has any success implementing a VZVirtioSocketListenerDelegate in swift? I can get the connection in but it gets abruptly closed in the VM before it can write any date.

@nagypeterjob
Copy link

@dmarkey @mheese Could you guys figure out how to open the connection to the VM? Anything I do, when I call connect(toPort:, completionHandler:) I get "Error Domain=NSPOSIXErrorDomain Code=54 "Connection reset by peer".

I start to suspect, that the setSocketListener:forPort part have to be run inside of the VM as a "guest application" listener, and call connect from the host application, but I have no idea how to reference the VZVirtualMachine from code within the actual Virtual Machine. I am stuck.

I wouldn't even need this whole setSocketListener:forPort / connect(toPort:, completionHandler:) memery, if Apple would provide an easy way to get the VM's ip address

@cfergeau
Copy link

cfergeau commented Jul 4, 2022

I wouldn't even need this whole setSocketListener:forPort / connect(toPort:, completionHandler:) memery, if Apple would provide an easy way to get the VM's ip address

I don't know if there's an official API/documentation for this, but you can go from VM mac address to IP rather easily by parsing /var/db/dhcpd_leases

@nagypeterjob
Copy link

I wouldn't even need this whole setSocketListener:forPort / connect(toPort:, completionHandler:) memery, if Apple would provide an easy way to get the VM's ip address

I don't know if there's an official API/documentation for this, but you can go from VM mac address to IP rather easily by parsing /var/db/dhcpd_leases

Documentation: https://developer.apple.com/documentation/virtualization/sockets

Yup, I choose the MAC address way as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants