1 /*
2  * Packit -- network injection and capture tool
3  *
4  * Original author: Darren Bounds <dbounds@intrusense.com>
5  *
6  * Copyright 2002 Darren Bounds <dbounds@intrusense.com>
7  * Copyright 2017 Robert Krause <ruport@f00l.de>
8  * Copyright 2017 Sharad B
9  * Copyright 2020 David Polverari <david.polverari@gmail.com>
10  *
11  * SPDX-License-Identifier: GPL-2.0-or-later
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  * MA  02110-1301, USA.
27  *
28  * packit official page at https://github.com/resurrecting-open-source-projects/packit
29  */
30 
31 #include "print_tcp_hdr.h"
32 
print_tcp_hdr(u_int8_t * packet)33 void print_tcp_hdr(u_int8_t * packet)
34 {
35 	char flags[7];
36 
37 	struct libnet_tcp_hdr *tcphdr;
38 	struct servent *port_src, *port_dst;
39 
40 #ifdef DEBUG
41 	fprintf(stdout, "DEBUG: print_tcp_hdr()\n");
42 #endif
43 	port_src = malloc(sizeof(struct servent));
44 	port_dst = malloc(sizeof(struct servent));
45 	memset(port_src, 0, sizeof(struct servent));
46 	memset(port_dst, 0, sizeof(struct servent));
47 	memset(flags, 0, sizeof(flags));
48 	tcphdr = (struct libnet_tcp_hdr *)(packet + IPV4_H + g_hdr_len);
49 	if (tcphdr->th_flags & TH_URG)
50 		strcat(flags, "U");
51 	if (tcphdr->th_flags & TH_ACK)
52 		strcat(flags, "A");
53 	if (tcphdr->th_flags & TH_PUSH)
54 		strcat(flags, "P");
55 	if (tcphdr->th_flags & TH_RST)
56 		strcat(flags, "R");
57 	if (tcphdr->th_flags & TH_SYN)
58 		strcat(flags, "S");
59 	if (tcphdr->th_flags & TH_FIN)
60 		strcat(flags, "F");
61 	if (strlen(flags) == 0)
62 		strcpy(flags, "None");
63 	fprintf(stdout,
64 		"TCP header:  Src Port: %d  Dst Port: %d  Flag(s): %s\n",
65 		htons(tcphdr->th_sport), htons(tcphdr->th_dport), flags);
66 	fprintf(stdout, "\t     Window: %d  ", htons(tcphdr->th_win));
67 	if (tcphdr->th_seq > 0)
68 		fprintf(stdout, "Seqn: %lu  ", (u_long) ntohl(tcphdr->th_seq));
69 	if (tcphdr->th_ack > 0)
70 		fprintf(stdout, "Ackn: %lu  ", (u_long) ntohl(tcphdr->th_ack));
71 	if (tcphdr->th_urp)
72 		fprintf(stdout, "Urg: %d  ", ntohs(tcphdr->th_urp));
73 	fprintf(stdout, "\n");
74 }
75