Changeset 5418
- Timestamp:
- 02/14/08 13:50:56 (8 months ago)
- Files:
-
- framework3/trunk/external/pcaprub/extconf.rb (modified) (1 diff)
- framework3/trunk/external/pcaprub/pcaprub.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
framework3/trunk/external/pcaprub/extconf.rb
r5139 r5418 1 1 require 'mkmf' 2 2 3 have_library("pcap", "pcap_open_live") 3 if /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") 11 else 12 have_library("pcap", "pcap_open_live") 13 end 14 4 15 create_makefile("pcaprub") framework3/trunk/external/pcaprub/pcaprub.c
r5139 r5418 3 3 4 4 #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 7 10 8 11 static VALUE rb_cPcap; … … 25 28 } 26 29 30 27 31 static VALUE 28 32 rbpcap_s_lookupdev(VALUE self) 29 33 { 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 33 61 dev = pcap_lookupdev(eb); 34 62 if (dev == NULL) { 35 63 rb_raise(rb_eRuntimeError, "%s", eb); 36 64 } 37 return rb_str_new2(dev); 65 ret_dev = rb_str_new2(dev); 66 #endif 67 return ret_dev; 38 68 } 39 69 … … 119 149 } 120 150 121 static VALUE 122 rbpcap_open_live(VALUE self, VALUE interface, VALUE snaplen, VALUE promisc, VALUE timeout) 151 152 static VALUE 153 rbpcap_open_live(VALUE self, VALUE iface,VALUE snaplen,VALUE promisc, VALUE timeout) 123 154 { 124 155 char eb[PCAP_ERRBUF_SIZE]; … … 126 157 int promisc_value = 0; 127 158 128 if(TYPE(i nterface) != T_STRING)159 if(TYPE(iface) != T_STRING) 129 160 rb_raise(rb_eArgError, "interface must be a string"); 130 161 if(TYPE(snaplen) != T_FIXNUM) … … 148 179 rbp->type = LIVE; 149 180 memset(rbp->iface, 0, sizeof(rbp->iface)); 150 strncpy(rbp->iface, RSTRING(i nterface)->ptr, sizeof(rbp->iface) - 1);181 strncpy(rbp->iface, RSTRING(iface)->ptr, sizeof(rbp->iface) - 1); 151 182 152 183 rbp->pd = pcap_open_live( 153 RSTRING(i nterface)->ptr,184 RSTRING(iface)->ptr, 154 185 NUM2INT(snaplen), 155 186 promisc_value, … … 165 196 166 197 static VALUE 167 rbpcap_open_live_s(VALUE class, VALUE i nterface, VALUE snaplen, VALUE promisc, VALUE timeout)198 rbpcap_open_live_s(VALUE class, VALUE iface, VALUE snaplen, VALUE promisc, VALUE timeout) 168 199 { 169 200 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); 172 202 } 173 203 … … 217 247 218 248 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 220 258 return INT2NUM(pcap_inject(rbp->pd, RSTRING(payload)->ptr, RSTRING(payload)->len)); 259 #endif 221 260 } 222 261
