Trouble getting a file descriptor from pyhandle

I want to use a twisted callback with a TUN interface that I've opened in Windows using Python. I am getting a PyHandle using win32file.CreateFile(). In order to use Twisted, I (think I) have to use a file descriptor. So I am trying to convert to an fd by doing this:

self.tunfd = msvcrt.open_osfhandle(self.tun_handle, 0)

Using zero as the second parameter is what's shown in many examples I've seen on the web while troubleshooting, and experimenting with the applicable flags has not resolved the issue. In each case I get this error:

self.tunfd = msvcrt.open_osfhandle(self.tun_handle, 0)
IOError: [Errno 22] Invalid argument

Why wouldn't this work? And is there an easier way to use Twisted with PyHandle?

Upvotes: 1

Views: 334

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

File descriptors on Windows are a trick. I don't know whether there's some way to get a file descriptor for a TUN device on Windows but even if you manage to get one somehow, it won't work with Twisted. On Windows, Twisted is limited to interacting with file descriptors that represent sockets. This has to do with the way the Windows APIs that accept file descriptors work.

You have two options. You can use IReactorWin32Events to monitor a Windows Event and then use some other Windows API to trigger the event when something interesting happens to tun_handle. Or perhaps you can use IOCPReactor to monitor the handle for interesting activity directly (unfortunately the IOCPReactor APIs for this are not as well documented - but I think registerHandle is probably the main thing).

Upvotes: 1

Related Questions