dissector: remove redundant code
[netsniff-ng.git] / proto_tcp.c
blob7eff200ee2b8c3c4299998c8564518dbb32823fd
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <endian.h>
11 #include <netinet/in.h> /* for ntohs() */
12 #include <asm/byteorder.h>
14 #include "proto.h"
15 #include "protos.h"
16 #include "dissector_eth.h"
17 #include "built_in.h"
18 #include "pkt_buff.h"
20 struct tcphdr {
21 uint16_t source;
22 uint16_t dest;
23 uint32_t seq;
24 uint32_t ack_seq;
25 #if defined(__LITTLE_ENDIAN_BITFIELD)
26 __extension__ uint16_t res1:4,
27 doff:4,
28 fin:1,
29 syn:1,
30 rst:1,
31 psh:1,
32 ack:1,
33 urg:1,
34 ece:1,
35 cwr:1;
36 #elif defined(__BIG_ENDIAN_BITFIELD)
37 __extension__ uint16_t doff:4,
38 res1:4,
39 cwr:1,
40 ece:1,
41 urg:1,
42 ack:1,
43 psh:1,
44 rst:1,
45 syn:1,
46 fin:1;
47 #else
48 # error "Adjust your <asm/byteorder.h> defines"
49 #endif
50 uint16_t window;
51 uint16_t check;
52 uint16_t urg_ptr;
53 } __attribute__((packed));
55 static void tcp(struct pkt_buff *pkt)
57 struct tcphdr *tcp = (struct tcphdr *) pkt_pull(pkt, sizeof(*tcp));
58 uint16_t src, dest;
59 char *src_name, *dest_name;
61 if (tcp == NULL)
62 return;
64 src = ntohs(tcp->source);
65 dest = ntohs(tcp->dest);
67 src_name = lookup_port_tcp(src);
68 dest_name = lookup_port_tcp(dest);
70 tprintf(" [ TCP ");
71 tprintf("Port (%u", src);
72 if (src_name)
73 tprintf(" (%s%s%s)", colorize_start(bold), src_name,
74 colorize_end());
75 tprintf(" => %u", dest);
76 if (dest_name)
77 tprintf(" (%s%s%s)", colorize_start(bold), dest_name,
78 colorize_end());
79 tprintf("), ");
80 tprintf("SN (0x%x), ", ntohl(tcp->seq));
81 tprintf("AN (0x%x), ", ntohl(tcp->ack_seq));
82 tprintf("DataOff (%u), ", tcp->doff);
83 tprintf("Res (%u), ", tcp->res1);
84 tprintf("Flags (");
85 if (tcp->fin)
86 tprintf("FIN ");
87 if (tcp->syn)
88 tprintf("SYN ");
89 if (tcp->rst)
90 tprintf("RST ");
91 if (tcp->psh)
92 tprintf("PSH ");
93 if (tcp->ack)
94 tprintf("ACK ");
95 if (tcp->urg)
96 tprintf("URG ");
97 if (tcp->ece)
98 tprintf("ECE ");
99 if (tcp->cwr)
100 tprintf("CWR ");
101 tprintf("), ");
102 tprintf("Window (%u), ", ntohs(tcp->window));
103 tprintf("CSum (0x%.4x), ", ntohs(tcp->check));
104 tprintf("UrgPtr (%u)", ntohs(tcp->urg_ptr));
105 tprintf(" ]\n");
108 static void tcp_less(struct pkt_buff *pkt)
110 struct tcphdr *tcp = (struct tcphdr *) pkt_pull(pkt, sizeof(*tcp));
111 uint16_t src, dest;
112 char *src_name, *dest_name;
114 if (tcp == NULL)
115 return;
117 src = ntohs(tcp->source);
118 dest = ntohs(tcp->dest);
120 src_name = lookup_port_tcp(src);
121 dest_name = lookup_port_tcp(dest);
123 tprintf(" TCP %u", src);
124 if(src_name)
125 tprintf("(%s%s%s)", colorize_start(bold), src_name,
126 colorize_end());
127 tprintf("/%u", dest);
128 if(dest_name)
129 tprintf("(%s%s%s)", colorize_start(bold), dest_name,
130 colorize_end());
131 tprintf(" F%s",colorize_start(bold));
132 if (tcp->fin)
133 tprintf(" FIN");
134 if (tcp->syn)
135 tprintf(" SYN");
136 if (tcp->rst)
137 tprintf(" RST");
138 if (tcp->psh)
139 tprintf(" PSH");
140 if (tcp->ack)
141 tprintf(" ACK");
142 if (tcp->urg)
143 tprintf(" URG");
144 if (tcp->ece)
145 tprintf(" ECE");
146 if (tcp->cwr)
147 tprintf(" CWR");
148 tprintf("%s Win %u S/A 0x%x/0x%x", colorize_end(),
149 ntohs(tcp->window), ntohl(tcp->seq), ntohl(tcp->ack_seq));
152 struct protocol tcp_ops = {
153 .key = 0x06,
154 .print_full = tcp,
155 .print_less = tcp_less,