diff --git a/external/pcaprub/pcaprub.c b/external/pcaprub/pcaprub.c index 01668ea3ef..7e63017159 100644 --- a/external/pcaprub/pcaprub.c +++ b/external/pcaprub/pcaprub.c @@ -389,14 +389,20 @@ static VALUE rbpcap_capture(VALUE self) { rbpcap_t *rbp; - + int fno = -1; + Data_Get_Struct(self, rbpcap_t, rbp); if(! rbpcap_ready(rbp)) return self; + fno = pcap_fileno(rbp->pd); + for(;;) { - VALUE packet = rbpcap_next(self); + VALUE packet; + if(fno != -1) rb_thread_wait_fd(fno); + + packet = rbpcap_next(self); if(packet == Qnil && rbp->type == OFFLINE) break; diff --git a/external/pcaprub/test_pcaprub.rb b/external/pcaprub/test_pcaprub.rb index 38a64acbcd..7b50fc8a92 100644 --- a/external/pcaprub/test_pcaprub.rb +++ b/external/pcaprub/test_pcaprub.rb @@ -81,14 +81,23 @@ class Pcap::UnitTest < Test::Unit::TestCase end def test_pcap_next -=begin d = Pcap.lookupdev o = Pcap.open_live(d, 1344, true, 1) + @c = 0 - t = Thread.new { while(true); @c += 1; end; } - x = o.next + t = Thread.new { while(true); @c += 1; select(nil, nil, nil, 0.10); end; } + + require 'timeout' + begin + Timeout.timeout(10) do + o.each do |pkt| + end + end + rescue ::Timeout::Error + end + t.kill -=end + puts "Background thread ticked #{@c} times while capture was running" true end