PERFORCE change 124140 for review

Matus Harvan mharvan at FreeBSD.org
Thu Jul 26 22:54:10 UTC 2007


http://perforce.freebsd.org/chv.cgi?CH=124140

Change 124140 by mharvan at mharvan_twoflower on 2007/07/26 22:53:22

	replaced my own linked list for fragment reassembly
	with queue(3) macros - segfaults fixed;)

Affected files ...

.. //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.c#11 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.h#6 edit

Differences ...

==== //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.c#11 (text+ko) ====

@@ -54,7 +54,9 @@
 
 /* fragment id for the next packet to be fragmented */
 uint frag_id = 0; // TODO: randomize
-frag_llist_t *frag_list;
+
+LIST_HEAD(frag_info_list_head, frag_info) frag_info_list =
+	LIST_HEAD_INITIALIZER(frag_info_list);
 
 /* server host */
 char *host;
@@ -223,11 +225,12 @@
 void timer_ev_handler(int fd, short ev_type, void *arg)
 {
     struct timeval tv;
-    frag_llist_t *p, *q;
+    struct frag_info *np, *np_temp;
     plugint *pl = current_pl;
 
     fprintf(stderr, "timer fired\n");
-
+    
+    /* check if too many ping requests have not failed */
     if (pl) {
 	if (! pl->ping_counter) {
 	    report_plugin_error(pl, PLUGIN_ERROR_PING);
@@ -237,18 +240,16 @@
     }
 
     /* fragment reassembly timeout */
-    // TODO: fragment reassembly timeout
-    for (p=frag_list; p;) {
-	q = p->next;
-	if (tv.tv_sec - p->tv_sec > FRAG_TIMEOUT) {
-	    free(p->bitmap);
-	    free(p->buf);
-	    free(p);
+    LIST_FOREACH_SAFE(np, &frag_info_list, frag_infos, np_temp) {
+	if (tv.tv_sec - np->tv_sec > FRAG_TIMEOUT) {
+	    LIST_REMOVE(np, frag_infos);
+	    free(np->bitmap);
+	    free(np->buf);
+	    free(np);
 	}
-	p = q;
     }
     
-    /* register a timer event */
+    /* register a timer event again */
     tv.tv_sec=1;
     tv.tv_usec=0;
     evtimer_set(&timer_ev, timer_ev_handler, NULL);
@@ -262,8 +263,8 @@
 {
     u_int8_t dispatch = *data;
     frag_hdr_t *frag_hdr = NULL;
-    frag_llist_t *p; /* pointer to frag info about the processed fragment */
-    frag_llist_t **pp; /* previous item before p */
+    struct frag_info *p;/* pointer to frag info about the processed fragment */
+    struct frag_info *np;
     struct timeval tv;
     int i;
     int dgram_reassembled;
@@ -290,22 +291,22 @@
 		frag_hdr->id, frag_hdr->size, frag_hdr->offset, len);
 
 	/* check if reassembly for this fragment has already started */
-	pp = &frag_list;
-	for(p = frag_list; p; p = p->next) {
-	    if (p->id == frag_hdr->id &&
-		p->size == frag_hdr->size) {
+	p = NULL;
+	LIST_FOREACH(np, &frag_info_list, frag_infos) {
+	    if (np->id == frag_hdr->id &&
+		np->size == frag_hdr->size) {
+		/* found in list */
+		fprintf(stderr, "found frag info in list\n");
+		p = np;
 		break;
 	    }
-	    pp = &p;
 	}
-	/* found in list */
-	if (p) {
-	    fprintf(stderr, "found frag info in list\n");
+
 	/* fragment info not found in list, start a new reassembly */
-	} else {
+	if (p == NULL) { 
 	    fprintf(stderr, "frag info NOT found in list\n");
 	    /* allocate memory */
-	    p = malloc(sizeof(frag_llist_t));
+	    p = malloc(sizeof(struct frag_info));
 	    if (!p) {
 		fprintf(stderr, "process_data_from_plugin - "
 			"fragment reassembly: out of memory\n");
@@ -332,11 +333,10 @@
 	    /* collect information about the fragments */
 	    // TODO: error checking
 	    (void) gettimeofday(&tv, NULL);
-	    p->next = frag_list;
-	    frag_list = p;
 	    p->id = frag_hdr->id;
 	    p->size = frag_hdr->size;
 	    memcpy(&p->tv_sec, &tv.tv_sec, sizeof(tv.tv_sec));
+	    LIST_INSERT_HEAD(&frag_info_list, p, frag_infos);
 	}
 	
 	if (frag_hdr->offset + len <= p->size) {
@@ -373,7 +373,7 @@
 	    tun_send(p->buf, p->size);
 	    
 	    /* remove fragment info from linked list */
-	    *pp = p->next;
+	    LIST_REMOVE(p, frag_infos);
 	    free(p->buf);
 	    free(p->bitmap);
 	    free(p);
@@ -632,8 +632,8 @@
     plugins->name = "tcp_2222";
     load_plugin("./plugin_tcp.so");
     plugins->name = "tcp_3333";
-    //    load_plugin("./plugin_icmp.so");
-    //    plugins->name = "icmp";
+    load_plugin("./plugin_icmp.so");
+    plugins->name = "icmp";
 
     if (server) {
 	/* initialize all plugins */

==== //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.h#6 (text+ko) ====

@@ -2,6 +2,7 @@
 #define _TUNNELD_H
 
 #include <sys/types.h>
+#include <sys/queue.h>
 
 #define PACKETLEN 1400   /* encapsulated data size */
 #define SOCKET_TIMEOUT 5 /* timeout for socket operations */
@@ -16,7 +17,7 @@
 		     * of the packet */
 } frag_hdr_t;
 
-typedef struct _frag_llist_t {
+struct frag_info {
     uint id;   /* Id of the whole packet, same in each fragment */
     uint size; /* length of the whole packet, same in each fragment */
     time_t tv_sec;   /* seconds after epoch when reassembly
@@ -24,8 +25,8 @@
     u_int8_t *bitmap;/* bitmap representing already 
 		      * received parts of the packet */
     char *buf;       /* buffer into which the fragment is reassembled */
-    struct _frag_llist_t *next;
-} frag_llist_t;
+    LIST_ENTRY(frag_info) frag_infos;
+};
 
 /*
  * code for setting timeout on a socket:


More information about the p4-projects mailing list