Changeset 5418

Show
Ignore:
Timestamp:
02/14/08 13:50:56 (8 months ago)
Author:
hdm
Message:

Commit a patch from warl0ck

Files:

Legend:

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

    r5139 r5418  
    11require 'mkmf' 
    22 
    3 have_library("pcap", "pcap_open_live") 
     3if /i386-mswin32/ =~ RUBY_PLATFORM 
     4    pcap_dir        = with_config("pcap-dir", "C:\WpdPack") 
     5    pcap_includedir = with_config("pcap-includedir", pcap_dir + "\\include") 
     6    pcap_libdir     = with_config("pcap-libdir", pcap_dir + "\\lib") 
     7 
     8    $CFLAGS  = "-DWIN32 -I#{pcap_includedir}" 
     9    $LDFLAGS = "/link /LIBPATH:#{pcap_libdir}" 
     10    have_library("wpcap", "pcap_open_live") 
     11else 
     12    have_library("pcap", "pcap_open_live") 
     13end 
     14 
    415create_makefile("pcaprub") 
  • framework3/trunk/external/pcaprub/pcaprub.c

    r5139 r5418  
    33 
    44#include <pcap.h> 
    5 #include <netinet/in.h> 
    6 #include <arpa/inet.h> 
     5 
     6#if !defined(WIN32) 
     7 #include <netinet/in.h> 
     8 #include <arpa/inet.h> 
     9#endif 
    710 
    811static VALUE rb_cPcap; 
     
    2528} 
    2629 
     30 
    2731static VALUE 
    2832rbpcap_s_lookupdev(VALUE self) 
    2933{ 
    30     char *dev; 
    31     char eb[PCAP_ERRBUF_SIZE]; 
    32          
     34    char *dev = NULL; 
     35    char eb[PCAP_ERRBUF_SIZE]; 
     36    VALUE ret_dev;  /* device string to return */ 
     37#if defined(WIN32)  /* pcap_lookupdev is broken on windows */     
     38    pcap_if_t *alldevs; 
     39    pcap_if_t *d; 
     40 
     41    /* Retrieve the device list from the local machine */ 
     42    if (pcap_findalldevs(&alldevs,eb) == -1) { 
     43        rb_raise(rb_eRuntimeError,"%s",eb); 
     44    } 
     45 
     46    /* Find the first interface with an address and not loopback */ 
     47    for(d = alldevs; d != NULL; d= d->next)  { 
     48        if(d->name && d->addresses && !(d->flags & PCAP_IF_LOOPBACK)) { 
     49            dev=d->name; 
     50            break; 
     51        } 
     52    } 
     53     
     54    if (dev == NULL) { 
     55        rb_raise(rb_eRuntimeError,"%s","No valid interfaces found, Make sure WinPcap is installed.\n"); 
     56    } 
     57    ret_dev = rb_str_new2(dev); 
     58    /* We don't need any more the device list. Free it */ 
     59    pcap_freealldevs(alldevs); 
     60#else 
    3361    dev = pcap_lookupdev(eb); 
    3462    if (dev == NULL) { 
    3563                rb_raise(rb_eRuntimeError, "%s", eb); 
    3664    } 
    37     return rb_str_new2(dev); 
     65    ret_dev = rb_str_new2(dev); 
     66#endif 
     67    return ret_dev; 
    3868} 
    3969 
     
    119149} 
    120150 
    121 static VALUE 
    122 rbpcap_open_live(VALUE self, VALUE interface, VALUE snaplen, VALUE promisc, VALUE timeout) 
     151 
     152static VALUE 
     153rbpcap_open_live(VALUE self, VALUE iface,VALUE snaplen,VALUE promisc, VALUE timeout) 
    123154{ 
    124155    char eb[PCAP_ERRBUF_SIZE]; 
     
    126157    int promisc_value = 0; 
    127158 
    128     if(TYPE(interface) != T_STRING) 
     159    if(TYPE(iface) != T_STRING) 
    129160        rb_raise(rb_eArgError, "interface must be a string"); 
    130161    if(TYPE(snaplen) != T_FIXNUM) 
     
    148179    rbp->type = LIVE; 
    149180    memset(rbp->iface, 0, sizeof(rbp->iface)); 
    150     strncpy(rbp->iface, RSTRING(interface)->ptr, sizeof(rbp->iface) - 1); 
     181    strncpy(rbp->iface, RSTRING(iface)->ptr, sizeof(rbp->iface) - 1); 
    151182 
    152183    rbp->pd = pcap_open_live( 
    153         RSTRING(interface)->ptr, 
     184        RSTRING(iface)->ptr, 
    154185        NUM2INT(snaplen), 
    155186        promisc_value, 
     
    165196 
    166197static VALUE 
    167 rbpcap_open_live_s(VALUE class, VALUE interface, VALUE snaplen, VALUE promisc, VALUE timeout) 
     198rbpcap_open_live_s(VALUE class, VALUE iface, VALUE snaplen, VALUE promisc, VALUE timeout) 
    168199{ 
    169200    VALUE iPcap = rb_funcall(rb_cPcap, rb_intern("new"), 0); 
    170  
    171     return rbpcap_open_live(iPcap, interface, snaplen, promisc, timeout); 
     201    return rbpcap_open_live(iPcap, iface, snaplen, promisc, timeout); 
    172202} 
    173203 
     
    217247 
    218248        if(! rbpcap_ready(rbp)) return self;  
    219          
     249#if defined(WIN32)    
     250    /* WinPcap does not have a pcap_inject call we use pcap_sendpacket, if it suceedes  
     251     * we simply return the amount of packets request to inject, else we fail. 
     252     */ 
     253    if(pcap_sendpacket(rbp->pd, RSTRING(payload)->ptr, RSTRING(payload)->len) != 0) { 
     254        rb_raise(rb_eRuntimeError, "%s", pcap_geterr(rbp->pd)); 
     255    } 
     256    return INT2NUM(RSTRING(payload)->len); 
     257#else 
    220258    return INT2NUM(pcap_inject(rbp->pd, RSTRING(payload)->ptr, RSTRING(payload)->len)); 
     259#endif 
    221260} 
    222261