1 /*
2  * Copyright (c) 2011 Daisuke Miyamoto. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *		notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *		notice, this list of conditions and the following disclaimer in the
11  *		documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.	IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <sys/un.h>
37 #include <arpa/inet.h>
38 #include <sys/time.h>
39 #include <netdb.h>
40 
41 #include <pcap.h>
42 
43 #include "pt_std.h"
44 #include "pt_tcp.h"
45 #include "pt_mesg.h"
46 #include "pt_send.h"
47 
48 #include "proto_tcp.h"
49 
50 extern int enable_sound;
51 extern int trace;
52 
53 /* process tcp header */
54 void
packter_tcp(u_char * p,u_int len,char * srcip,char * dstip,int flag,char * mesgbuf)55 packter_tcp(u_char *p, u_int len, char *srcip, char *dstip, int flag, char *mesgbuf)
56 {
57 	struct tcphdr *th;
58 	char mesg[PACKTER_BUFSIZ];
59 
60 	if (len < TCP_HDRLEN){
61 		return;
62 	}
63 	else {
64 		th = (struct tcphdr *)p;
65 	}
66 
67 	memset((void *)&mesg, '\0', PACKTER_BUFSIZ);
68 
69 	if (th->th_flags & TH_SYN){
70 		flag += (PACKTER_TCP_SYN - PACKTER_TCP_ACK);	/* PACKTER_TCP_SYN(6) */
71 	}
72 	else if (th->th_flags & (TH_FIN|TH_RST)){
73 		flag += (PACKTER_TCP_FIN - PACKTER_TCP_ACK);	/* PACKTER_TCP_FIN(6) */
74 	}
75 
76 	if (trace == PACKTER_FALSE){
77 		sprintf(mesgbuf, "TCP src:%s(%d) dst:%s(%d)",
78 						srcip, ntohs(th->th_sport), dstip, ntohs(th->th_dport));
79 	}
80 
81 	packter_mesg(mesg, srcip, dstip, ntohs(th->th_sport),ntohs(th->th_dport), flag, mesgbuf);
82 	packter_send(mesg);
83 
84 	if (enable_sound == PACKTER_TRUE){
85 		char se[PACKTER_BUFSIZ];
86 		memset((void *)&se, '\0', PACKTER_BUFSIZ);
87 		snprintf(se, PACKTER_BUFSIZ, "%sse%d.wav", PACKTER_SE, flag);
88 		packter_send(se);
89 	}
90 	return;
91 }
92