Merge Samba3 and Samba4 together
[bbaumbach/samba-autobuild/.git] / examples / pcap2nbench / tcp.cpp
1 /*\
2  *  pcap2nbench - Converts libpcap network traces to nbench input
3  *  Copyright (C) 2004  Jim McDonough <jmcd@us.ibm.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  *  Written by Anthony Liguori <aliguori@us.ibm.com>
19 \*/
20
21 #include <netinet/in.h>
22
23 #include "tcp.hpp"
24
25 tcp::tcp(const uint8_t *data, size_t size)
26 {
27   if (size < 18) {
28     std::cerr << "Invalid TCP header" << std::endl;
29   }
30
31   memcpy(&src_port, data, 2);
32   src_port = ntohs(src_port);
33   memcpy(&dst_port, data + 2, 2);
34   dst_port = ntohs(dst_port);
35   memcpy(&seq_number, data + 4, 4);
36   seq_number = ntohl(seq_number);
37   memcpy(&ack_number, data + 8, 4);
38   ack_number = ntohl(ack_number);
39   length = ((data[12] & 0xF0) >> 4) * 4;
40   flags = ((data[12] & 0x0F) << 8) | data[13];
41   memcpy(&window_size, data + 14, 2);
42   window_size = ntohs(window_size);
43   memcpy(&checksum, data + 16, 2);
44   checksum = ntohs(checksum);
45 }
46
47 std::ostream &operator<<(std::ostream &lhs, const tcp &rhs)
48 {
49   lhs << "Source Port: " << rhs.src_port << std::endl
50       << "Destination Port: " << rhs.dst_port << std::endl
51       << "Sequence Number: " << rhs.seq_number << std::endl
52       << "Ack Number: " << rhs.ack_number << std::endl
53       << "Length: " << (uint16_t)(rhs.length) << std::endl
54       << "Flags: " << rhs.flags << std::endl
55       << "Window Size: " << rhs.window_size << std::endl
56       << "Checksum: " << rhs.checksum << std::endl;
57
58   return lhs;
59 }