Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 20 Jun 2008 00:55:29 GMT
From:      Rui Paulo <rpaulo@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 143801 for review
Message-ID:  <200806200055.m5K0tT7u007118@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=143801

Change 143801 by rpaulo@rpaulo_epsilon on 2008/06/20 00:55:06

	Lower timer for debugging purposes.
	Add helper module.
	Move find_conn() to helper module.
	Implement dumper_free().
	Add forward pointer to the other side of the connection.
	Implement resource dealocation when timer expires.

Affected files ...

.. //depot/projects/soc2008/rpaulo-tcpad/Makefile#8 edit
.. //depot/projects/soc2008/rpaulo-tcpad/dumper.c#6 edit
.. //depot/projects/soc2008/rpaulo-tcpad/dumper.h#4 edit
.. //depot/projects/soc2008/rpaulo-tcpad/handler.c#12 edit
.. //depot/projects/soc2008/rpaulo-tcpad/helper.c#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/helper.h#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/tcpad.h#8 edit
.. //depot/projects/soc2008/rpaulo-tcpad/timer.c#2 edit

Differences ...

==== //depot/projects/soc2008/rpaulo-tcpad/Makefile#8 (text+ko) ====

@@ -1,7 +1,7 @@
-# $P4: //depot/projects/soc2008/rpaulo-tcpad/Makefile#7 $
+# $P4: //depot/projects/soc2008/rpaulo-tcpad/Makefile#8 $
 
 PROG=tcpad
-SRCS=main.c device.c linkhdr.c handler.c dumper.c timer.c
+SRCS=main.c device.c linkhdr.c handler.c helper.c dumper.c timer.c
 CFLAGS+=-DDEBUG -ggdb
 WARNS=5
 LDADD=-lpcap

==== //depot/projects/soc2008/rpaulo-tcpad/dumper.c#6 (text+ko) ====

@@ -23,14 +23,17 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- * $P4: //depot/projects/soc2008/rpaulo-tcpad/dumper.c#5 $
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/dumper.c#6 $
  */
 
+#include <assert.h>
+#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/queue.h>
 #include <pcap.h>
 
+#include "debug.h"
 #include "dumper.h"
 
 /**
@@ -71,3 +74,23 @@
 	    pcap_dump((u_char *)pd, &dp->pheader, dp->headers);
 	pcap_dump_close(pd);
 }
+
+/**
+ * Free a list of packets and the head.
+ */
+void
+dumper_free(struct dumppkth *head)
+{
+	struct dumppkt *p1, *p2;
+
+	assert(head != NULL);
+
+	DPRINTF("freeing dumppkth struct @ %p\n", head);
+	p1 = TAILQ_FIRST(head);
+	while (p1 != NULL) {
+		p2 = TAILQ_NEXT(p1, entries);
+		free(p1);
+		p1 = p2;
+	}
+	free(head);
+}

==== //depot/projects/soc2008/rpaulo-tcpad/dumper.h#4 (text+ko) ====

@@ -23,7 +23,7 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- * $P4: //depot/projects/soc2008/rpaulo-tcpad/dumper.h#3 $
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/dumper.h#4 $
  */
 
 #ifndef _DUMPER_H_
@@ -41,7 +41,8 @@
 
 void	dumper_addpkt(struct dumppkth *head, const struct pcap_pkthdr *ph,
     const unsigned char *headers);
-void	dumper_error(pcap_t *p, const char *path, struct dumppkth *head);
+void	dumper_error(pcap_t *, const char *path, struct dumppkth *head);
+void	dumper_free(struct dumppkth *head);
 
 
 #endif /* _DUMPER_H_ */

==== //depot/projects/soc2008/rpaulo-tcpad/handler.c#12 (text+ko) ====

@@ -23,7 +23,7 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- * $P4: //depot/projects/soc2008/rpaulo-tcpad/handler.c#11 $
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/handler.c#12 $
  */
 
 #include <stdio.h>
@@ -43,12 +43,11 @@
 #include "linkhdr.h"
 #include "handler.h"
 #include "dumper.h"
+#include "helper.h"
 #include "tcpad.h"
 #include "debug.h"
 
 
-static struct tcpc 	*find_conn(struct in_addr ipsrc, struct in_addr ipdst,
-    unsigned short sport, unsigned short dport);
 static void		print_packet(const unsigned char *bytes, const int linkhlen);
 
 void
@@ -148,9 +147,10 @@
 		}
 		if (cp->t_state == TCPS_LAST_ACK ||
 		    cp->t_state == TCPS_CLOSING) {
+			cp->rcp = rcp;
+			rcp->rcp = cp;
 			cp->t_state = TCPS_TIME_WAIT;
 			DPRINTF("connection down\n");
-			dumper_error(p, "a.cap", cp->pktshead);
 		}
 	} else if ((tcp->th_flags & TH_FLAGS) == (TH_FIN|TH_ACK)) {
 		if (cp == NULL || rcp == NULL)
@@ -184,22 +184,6 @@
 	}
 }
 
-static struct tcpc *
-find_conn(struct in_addr ipsrc, struct in_addr ipdst, unsigned short
-    sport, unsigned short dport)
-{
-	struct tcpc *cp;
-
-	LIST_FOREACH(cp, &tcpchead, entries) {
-		if (memcmp(&cp->sv4addr, &ipsrc, sizeof(struct in_addr)) == 0 &&
-		    memcmp(&cp->dv4addr, &ipdst, sizeof(struct in_addr)) == 0 &&
-		    cp->sport == sport && cp->dport == dport) {
-			return (cp);
-		}
-	}
-	return (NULL);
-}
-
 static void
 print_packet(const unsigned char *bytes, const int linkhlen)
 {

==== //depot/projects/soc2008/rpaulo-tcpad/tcpad.h#8 (text+ko) ====

@@ -23,7 +23,7 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- * $P4: //depot/projects/soc2008/rpaulo-tcpad/tcpad.h#7 $
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/tcpad.h#8 $
  */
 
 #ifndef _TCPAD_H_
@@ -42,6 +42,7 @@
 	int t_state;		/* TCP FSM state */
 	int isv6;
 	struct dumppkth *pktshead;
+	struct tcpc *rcp;
 };
 
 LIST_HEAD(tcpchead, tcpc) tcpchead;

==== //depot/projects/soc2008/rpaulo-tcpad/timer.c#2 (text+ko) ====

@@ -23,10 +23,11 @@
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  *
- * $P4: //depot/projects/soc2008/rpaulo-tcpad/timer.c#1 $
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/timer.c#2 $
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <signal.h>
 #include <pcap.h>
 #include <sys/queue.h>
@@ -36,9 +37,15 @@
 #include "debug.h"
 #include "tcpad.h"
 #include "timer.h"
+#include "helper.h"
+#include "dumper.h"
 
 static void	timer_sigalrm(int sig);
 
+/**
+ * @brief
+ * Timer setup.
+ */
 int
 timer_setup(void)
 {
@@ -51,23 +58,32 @@
 		firstime = 0;
 	}
 
-	itp.it_interval.tv_sec  = 30;
+	itp.it_interval.tv_sec  = 1;
 	itp.it_interval.tv_usec = 0;
-	itp.it_value.tv_sec  = 30;
+	itp.it_value.tv_sec  = 1;
 	itp.it_value.tv_usec = 0;
 
-	printf("%d\n", setitimer(ITIMER_REAL, &itp, NULL));
-	return 0;
+	return (setitimer(ITIMER_REAL, &itp, NULL));
 }
 
+/**
+ * @brief
+ * Signal catching function.
+ */
 static void
 timer_sigalrm(int __unused sig)
 {
-        struct tcpc *cp;
+        struct tcpc *cp, *cp_t;
 
-        LIST_FOREACH(cp, &tcpchead, entries) {
+        LIST_FOREACH_SAFE(cp, &tcpchead, entries, cp_t) {
 		if (cp->t_state == TCPS_TIME_WAIT) {
 			DPRINTF("freeing tcp connection @ %p\n", cp);
+			LIST_REMOVE(cp, entries);
+			if (cp->pktshead)
+				dumper_free(cp->pktshead);
+			cp->pktshead = NULL;
+			cp->rcp->pktshead = NULL;
+			free(cp);
 		}
         }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200806200055.m5K0tT7u007118>