From 3b99a513ecd189d2e36bbf2b12c568cb89126f87 Mon Sep 17 00:00:00 2001 From: HD Moore Date: Mon, 26 Oct 2009 22:49:43 +0000 Subject: [PATCH] Fixes #352 and fixes #350. Can no longer reproduce memory corruption or packet loss with this code git-svn-id: file:///home/svn/framework3/trunk@7287 4d416f70-5f16-0410-b530-b9f4589650da --- external/pcaprub/extconf.rb | 4 ++++ external/pcaprub/pcaprub.c | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/external/pcaprub/extconf.rb b/external/pcaprub/extconf.rb index 2ae18dadcb..1cae414b0b 100644 --- a/external/pcaprub/extconf.rb +++ b/external/pcaprub/extconf.rb @@ -14,4 +14,8 @@ else have_library("pcap", "pcap_setnonblock") end +if ( RUBY_VERSION =~ /^1\.9/ ) + $CFLAGS += " -DRUBY_19" +end + create_makefile("pcaprub") diff --git a/external/pcaprub/pcaprub.c b/external/pcaprub/pcaprub.c index aa58501dd0..127c24597d 100644 --- a/external/pcaprub/pcaprub.c +++ b/external/pcaprub/pcaprub.c @@ -1,5 +1,9 @@ #include "ruby.h" + +#ifndef RUBY_19 #include "rubysig.h" +#endif + #include @@ -27,7 +31,7 @@ typedef struct rbpcap { typedef struct rbpcapjob { struct pcap_pkthdr hdr; - char *pkt; + unsigned char *pkt; int wtf; } rbpcapjob_t; @@ -328,7 +332,7 @@ rbpcap_dump(VALUE self, VALUE caplen, VALUE pktlen, VALUE packet) pcap_dump( (u_char*)rbp->pdt, &pcap_hdr, - RSTRING_PTR(packet) + (unsigned char *)RSTRING_PTR(packet) ); return self; @@ -360,7 +364,7 @@ rbpcap_inject(VALUE self, VALUE payload) static void rbpcap_handler(rbpcapjob_t *job, struct pcap_pkthdr *hdr, u_char *pkt){ - job->pkt = pkt; + job->pkt = (unsigned char *)pkt; job->hdr = *hdr; } @@ -376,16 +380,18 @@ rbpcap_next(VALUE self) if(! rbpcap_ready(rbp)) return self; pcap_setnonblock(rbp->pd, 1, eb); +#ifndef RUBY_19 TRAP_BEG; - +#endif ret = pcap_dispatch(rbp->pd, 1, (pcap_handler) rbpcap_handler, (u_char *)&job); - +#ifndef RUBY_19 TRAP_END; +#endif if(rbp->type == OFFLINE && ret <= 0) return Qnil; if(ret > 0 && job.hdr.caplen > 0) - return rb_str_new(job.pkt, job.hdr.caplen); + return rb_str_new((char *) job.pkt, job.hdr.caplen); return Qnil; }