rust-pcap / pcap

Rust language pcap library
Apache License 2.0
619 stars 142 forks source link

Async support on Windows? #281

Closed robs-zeynet closed 1 year ago

robs-zeynet commented 1 year ago

Hi,

I naively tried to get code very similar to ./examples/streamlisten.rs to compile on Windows and got a bunch of errors like:

error[E0432]: unresolved import `tokio::io::unix`
  --> C:\Users\robsh\.cargo\registry\src\github.com-1ecc6299db9ec823\pcap-0.11.0\src\stream.rs:14:16
   |
14 | use tokio::io::unix::AsyncFd;
   |                ^^^^ could not find `unix` in `io`

error[E0412]: cannot find type `RawFd` in this scope
    --> C:\Users\robsh\.cargo\registry\src\github.com-1ecc6299db9ec823\pcap-0.11.0\src\lib.rs:1407:9
     |
1407 |     fd: RawFd,
     |         ^^^^^ not found in this scope

error[E0425]: cannot find value `InvalidRawFd` in this scope
    --> C:\Users\robsh\.cargo\registry\src\github.com-1ecc6299db9ec823\pcap-0.11.0\src\lib.rs:1415:24
     |
1415 |             return Err(InvalidRawFd);
     |                        ^^^^^^^^^^^^ not found in this scope

Digging a bit, src/stream.rs depends on using sockets like an Fd which doesn't seem to work in Windows (https://stackoverflow.com/questions/69114288/rust-tcpstreamas-raw-fd-on-windows). So my question is: has any one looked at implementing something like this for windows? If not, if someone had a pointer of where to start, I could try to take a pass. If someone has looked at it and decided it was hard, it would be great to understand why.

Thanks in advance for a great utility :-)

Stargateur commented 1 year ago

To be able to have decent async in windows one must use pcap_getevent():

HANDLE pcap_getevent(pcap_t *p); Returns the handle of the event associated with the interface. This event can be passed to functions like WaitForSingleObject() or WaitForMultipleObjects() to wait until the driver's buffer contains some data without performing a read.

After, one must find a way to deal with this, I do not know exactly how. Maybe https://crates.io/crates/miow => https://docs.rs/miow/0.5.0/miow/iocp/struct.CompletionPort.html#method.add_handle

robs-zeynet commented 1 year ago

FYI: due to a change in requirements for my project, I no longer need async on windows and thus probably won't have time to work on this. Should we close the Issue or leave it open in case someone else decides to take this up?

Thanks for the reply!