Changeset 5534

Show
Ignore:
Timestamp:
06/22/08 12:14:11 (4 months ago)
Author:
hdm
Message:

This patch makes pcap.next non-blocking from a Ruby thread perspective. It does eat more CPU if there are no select() loops in the calling Ruby parent, but this isnt too common and never an issue for MSF

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework3/trunk/external/pcaprub/extconf.rb

    r5418 r5534  
    99    $LDFLAGS = "/link /LIBPATH:#{pcap_libdir}" 
    1010    have_library("wpcap", "pcap_open_live") 
     11        have_library("wpcap", "pcap_setnonblock") 
    1112else 
    1213    have_library("pcap", "pcap_open_live") 
     14        have_library("pcap", "pcap_setnonblock") 
    1315end 
    1416 
  • framework3/trunk/external/pcaprub/pcaprub.c

    r5418 r5534  
    1111static VALUE rb_cPcap; 
    1212 
    13 #define PCAPRUB_VERSION "0.7-dev" 
     13#define PCAPRUB_VERSION "0.8-dev" 
    1414 
    1515#define OFFLINE 1 
     
    2121    char type; 
    2222} rbpcap_t; 
     23 
     24 
     25typedef struct rbpcapjob { 
     26        struct pcap_pkthdr hdr; 
     27    char *pkt; 
     28        int wtf; 
     29} rbpcapjob_t; 
    2330 
    2431static VALUE 
     
    260267} 
    261268 
     269 
     270static void rbpcap_handler(rbpcapjob_t *job, struct pcap_pkthdr *hdr, u_char *pkt){ 
     271        job->pkt = pkt; 
     272        job->hdr = *hdr; 
     273} 
     274 
    262275static VALUE 
    263276rbpcap_next(VALUE self) 
    264277{ 
    265     char *pkt
    266     rbpcap_t *rbp
    267     struct pcap_pkthdr pkthdr
    268  
    269     Data_Get_Struct(self, rbpcap_t, rbp); 
    270          
     278    rbpcap_t *rbp
     279       rbpcapjob_t job
     280       char eb[PCAP_ERRBUF_SIZE]
     281        int ret;         
     282         
     283    Data_Get_Struct(self, rbpcap_t, rbp); 
    271284        if(! rbpcap_ready(rbp)) return self;  
    272          
    273     memset(&pkthdr, 0, sizeof(pkthdr)); 
     285        pcap_setnonblock(rbp->pd, 1, eb); 
    274286 
    275287    TRAP_BEG; 
    276     pkt = (char *)pcap_next(rbp->pd, &pkthdr); 
     288         
     289        while(! (ret = pcap_dispatch(rbp->pd, 1, (pcap_handler) rbpcap_handler, (u_char *)&job))) {              
     290                rb_thread_schedule(); 
     291        } 
     292     
    277293    TRAP_END; 
    278294 
    279     if(pkthdr.caplen > 0) 
    280         return rb_str_new(pkt, pkthdr.caplen); 
     295 
     296    if(job.hdr.caplen > 0) 
     297        return rb_str_new(job.pkt, job.hdr.caplen); 
    281298 
    282299    return Qnil; 
  • framework3/trunk/external/pcaprub/test_pcaprub.rb

    r5139 r5534  
    8080                assert_equal(Hash, r.class) 
    8181        end 
    82                                                  
     82 
     83        def test_pcap_next 
     84=begin 
     85                d = Pcap.lookupdev 
     86                o = Pcap.open_live(d, 1344, true, 1) 
     87                @c = 0 
     88                t = Thread.new { while(true); @c += 1; end; } 
     89                x = o.next 
     90                t.kill 
     91=end 
     92                true 
     93        end 
     94                                                         
    8395end