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