From owner-p4-projects@FreeBSD.ORG Fri Jun 20 00:55:30 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 145FA1065672; Fri, 20 Jun 2008 00:55:30 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id CA12E106566C for ; Fri, 20 Jun 2008 00:55:29 +0000 (UTC) (envelope-from rpaulo@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id B0AAD8FC0A for ; Fri, 20 Jun 2008 00:55:29 +0000 (UTC) (envelope-from rpaulo@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m5K0tTpJ007120 for ; Fri, 20 Jun 2008 00:55:29 GMT (envelope-from rpaulo@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id m5K0tT7u007118 for perforce@freebsd.org; Fri, 20 Jun 2008 00:55:29 GMT (envelope-from rpaulo@FreeBSD.org) Date: Fri, 20 Jun 2008 00:55:29 GMT Message-Id: <200806200055.m5K0tT7u007118@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to rpaulo@FreeBSD.org using -f From: Rui Paulo To: Perforce Change Reviews Cc: Subject: PERFORCE change 143801 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 20 Jun 2008 00:55:30 -0000 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 +#include #include #include #include #include +#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 @@ -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 +#include #include #include #include @@ -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); } }