2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009, 2010 Daniel Borkmann.
4 * Subject to the GPL, version 2.
10 #include <netinet/in.h> /* for ntohs() */
11 #include <asm/byteorder.h>
24 #if defined(__LITTLE_ENDIAN_BITFIELD)
25 __extension__
uint16_t res1
:4,
35 #elif defined(__BIG_ENDIAN_BITFIELD)
36 __extension__
uint16_t doff
:4,
47 # error "Adjust your <asm/byteorder.h> defines"
54 #define tprintf_flag(flag, str, prev) ({ \
57 tprintf("%s%s", (prev) ? " " : "", str); \
63 static void tcp(struct pkt_buff
*pkt
)
65 struct tcphdr
*tcp
= (struct tcphdr
*) pkt_pull(pkt
, sizeof(*tcp
));
67 const char *src_name
, *dest_name
;
73 src
= ntohs(tcp
->source
);
74 dest
= ntohs(tcp
->dest
);
76 src_name
= lookup_port_tcp(src
);
77 dest_name
= lookup_port_tcp(dest
);
80 tprintf("Port (%u", src
);
82 tprintf(" (%s%s%s)", colorize_start(bold
), src_name
,
84 tprintf(" => %u", dest
);
86 tprintf(" (%s%s%s)", colorize_start(bold
), dest_name
,
89 tprintf("SN (0x%x), ", ntohl(tcp
->seq
));
90 tprintf("AN (0x%x), ", ntohl(tcp
->ack_seq
));
91 tprintf("DataOff (%u), ", tcp
->doff
);
92 tprintf("Res (%u), ", tcp
->res1
);
94 v
= tprintf_flag(tcp
->fin
, "FIN", v
);
95 v
= tprintf_flag(tcp
->syn
, "SYN", v
);
96 v
= tprintf_flag(tcp
->rst
, "RST", v
);
97 v
= tprintf_flag(tcp
->psh
, "PSH", v
);
98 v
= tprintf_flag(tcp
->ack
, "ACK", v
);
99 v
= tprintf_flag(tcp
->urg
, "URG", v
);
100 v
= tprintf_flag(tcp
->ece
, "ECE", v
);
101 v
= tprintf_flag(tcp
->cwr
, "CWR", v
);
103 tprintf("Window (%u), ", ntohs(tcp
->window
));
104 tprintf("CSum (0x%.4x), ", ntohs(tcp
->check
));
105 tprintf("UrgPtr (%u)", ntohs(tcp
->urg_ptr
));
109 static void tcp_less(struct pkt_buff
*pkt
)
111 struct tcphdr
*tcp
= (struct tcphdr
*) pkt_pull(pkt
, sizeof(*tcp
));
113 const char *src_name
, *dest_name
;
118 src
= ntohs(tcp
->source
);
119 dest
= ntohs(tcp
->dest
);
121 src_name
= lookup_port_tcp(src
);
122 dest_name
= lookup_port_tcp(dest
);
124 tprintf(" TCP %u", src
);
126 tprintf("(%s%s%s)", colorize_start(bold
), src_name
,
128 tprintf("/%u", dest
);
130 tprintf("(%s%s%s)", colorize_start(bold
), dest_name
,
132 tprintf(" F%s",colorize_start(bold
));
149 tprintf("%s Win %u S/A 0x%x/0x%x", colorize_end(),
150 ntohs(tcp
->window
), ntohl(tcp
->seq
), ntohl(tcp
->ack_seq
));
153 struct protocol tcp_ops
= {
156 .print_less
= tcp_less
,