Guy Harris <[EMAIL PROTECTED]> writes:

[Sorry for the duplicate]

> > For example, each time pcap_dispatch() or pcap_loop() calls the packet
> > handler function, a (u_char *) pointer to the new packet data is passed
> > to the packet handler; once I'm done with the packet, should I free()
> > this memory?
> 
> No.  That memory is managed by libpcap; note that if you want to
> preserve the contents of the packet data, you must make a copy of it, as
> libpcap makes no guarantee that the data in the packet will remain at
> that location once the packet handler function returns (in fact, it
> probably *won't* remain there, other stuff will be read into the buffer
> if "pcap_dispatch()" or "pcap_loop()" reads another bufferful of packets
> from the kernel).

But it would be a good thing to be able to specify it's own buffer allocation
policy. I sent a patch a while ago, but nobody answered (note it had a bug).
I attach a new version here.

Common subdirectories: libpcap.orig/CVS and libpcap/CVS
Common subdirectories: libpcap.orig/SUNOS4 and libpcap/SUNOS4
Common subdirectories: libpcap.orig/bpf and libpcap/bpf
Common subdirectories: libpcap.orig/lbl and libpcap/lbl
diff -up libpcap.orig/pcap-int.h libpcap/pcap-int.h
--- libpcap.orig/pcap-int.h	Thu Dec 21 11:29:23 2000
+++ libpcap/pcap-int.h	Mon May 28 10:32:24 2001
@@ -105,6 +105,8 @@ struct pcap {
 	struct bpf_program fcode;
 
 	char errbuf[PCAP_ERRBUF_SIZE];
+
+        void *(*alloc)(size_t size);
 };
 
 /*
diff -up libpcap.orig/pcap.c libpcap/pcap.c
--- libpcap.orig/pcap.c	Sat Dec 16 11:43:31 2000
+++ libpcap/pcap.c	Mon May 28 10:36:07 2001
@@ -46,6 +46,7 @@ static const char rcsid[] =
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <errno.h>
 
 #ifdef HAVE_OS_PROTO_H
 #include "os-proto.h"
@@ -56,10 +57,22 @@ static const char rcsid[] =
 int
 pcap_dispatch(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
 {
-
+        int ret;
+        
+        if ( p->alloc && ! p->buffer && ! (p->buffer = p->alloc(p->bufsize)) ) {
+                snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
+                return -1;
+        }
+        
 	if (p->sf.rfile != NULL)
-		return (pcap_offline_read(p, cnt, callback, user));
-	return (pcap_read(p, cnt, callback, user));
+		ret = (pcap_offline_read(p, cnt, callback, user));
+	else
+                ret = (pcap_read(p, cnt, callback, user));
+
+        if ( ret > 0 )
+                p->buffer = NULL;
+        
+        return ret;
 }
 
 int
@@ -67,6 +80,11 @@ pcap_loop(pcap_t *p, int cnt, pcap_handl
 {
 	register int n;
 
+        if ( p->alloc && ! (p->buffer = p->alloc(p->bufsize)) ) {
+                snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
+                return -1;
+        }
+        
 	for (;;) {
 		if (p->sf.rfile != NULL)
 			n = pcap_offline_read(p, cnt, callback, user);
@@ -203,6 +221,14 @@ pcap_open_dead(int linktype, int snaplen
 	return p;
 }
 
+
+void pcap_set_alloc_func(pcap_t *p, void *(*alloc)(size_t size)) 
+{
+        free(p->buffer);
+        p->buffer = NULL;
+        p->alloc = alloc;
+}
+
 void
 pcap_close(pcap_t *p)
 {
@@ -218,9 +244,14 @@ pcap_close(pcap_t *p)
 			(void)fclose(p->sf.rfile);
 		if (p->sf.base != NULL)
 			free(p->sf.base);
-	} else if (p->buffer != NULL)
+	} else if (p->buffer != NULL && ! p->alloc )
 		free(p->buffer);
 	
 	pcap_freecode(&p->fcode);
 	free(p);
 }
+
+
+
+
+
diff -up libpcap.orig/pcap.h libpcap/pcap.h
--- libpcap.orig/pcap.h	Sat Oct 28 02:01:31 2000
+++ libpcap/pcap.h	Mon May 28 10:32:24 2001
@@ -157,6 +157,12 @@ int	pcap_is_swapped(pcap_t *);
 int	pcap_major_version(pcap_t *);
 int	pcap_minor_version(pcap_t *);
 
+/*
+ * Set allocation function,
+ * this allow application to have their own buffer managment.
+ */
+        void pcap_set_alloc_func(pcap_t *p, void *(*alloc)(size_t size));
+    
 /* XXX */
 FILE	*pcap_file(pcap_t *);
 int	pcap_fileno(pcap_t *);

-- 
Yoann Vandoorselaere | In a world without walls or fences,
MandrakeSoft         | what use do we have for windows or gates?

Reply via email to