shadowbq/pcaprub — pull request #18 by bturner-r7 (2013-07-03 06:27:40+00:00)
Title: Fix each_packet and each_data on Windows

## Fix `each_data` and `each_packet` method on Windows

When using the `each_data` or `each_packet` methods with WinPcap, I often see this error:

Errno::EBADF: Bad file descriptor
c:/pcaprub/test/test_pcaprub_unit.rb:98:in `each'
c:/pcaprub/test/test_pcaprub_unit.rb:98:in `block in test_pcap_next'

This appears to because the `rb_thread_wait_fd` doesn't like the file descriptor returned from `pcap_fileno`. On Ruby 1.9, `rb_thread_wait_fd` is a no-op if there is only one thread running, so sometimes this bug goes undetected. But when wrapping `each` calls in `Timeout` blocks (or other threads), the error above always occurs.

This pull request changes the behavior to use `pcap_getevent` (WinPcap extension) and `WaitForSingleObject`. Because `WaitFoSingleObject` is a blocking call, I wrap it with `rb_thread_blocking_region`. This fixes the bad file descriptor bug on Windows and should not sacrifice performance.

On Ruby 1.8, where `rb_thread_blocking_region` is not supported, I use `rb_thread_polling` which pauses for 100 milliseconds between reads. This may mean some packets are missed. Ruby 1.8 is now EOL so hopefully this isn't too bad.

I also am removing support for monitor mode on Windows as I've found WinPcap doesn't support monitor mode since 4.0.2.