3dff4bda31de811a8fc516a36377b11f373fc338
[sfrench/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 2 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, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *  Written by Anthony Liguori <aliguori@us.ibm.com>
20 \*/
21
22 #include <netinet/in.h>
23
24 #include "tcp.hpp"
25
26 tcp::tcp(const uint8_t *data, size_t size)
27 {
28   if (size < 18) {
29     std::cerr << "Invalid TCP header" << std::endl;
30   }
31
32   memcpy(&src_port, data, 2);
33   src_port = ntohs(src_port);
34   memcpy(&dst_port, data + 2, 2);
35   dst_port = ntohs(dst_port);
36   memcpy(&seq_number, data + 4, 4);
37   seq_number = ntohl(seq_number);
38   memcpy(&ack_number, data + 8, 4);
39   ack_number = ntohl(ack_number);
40   length = ((data[12] & 0xF0) >> 4) * 4;
41   flags = ((data[12] & 0x0F) << 8) | data[13];
42   memcpy(&window_size, data + 14, 2);
43   window_size = ntohs(window_size);
44   memcpy(&checksum, data + 16, 2);
45   checksum = ntohs(checksum);
46 }
47
48 std::ostream &operator<<(std::ostream &lhs, const tcp &rhs)
49 {
50   lhs << "Source Port: " << rhs.src_port << std::endl
51       << "Destination Port: " << rhs.dst_port << std::endl
52       << "Sequence Number: " << rhs.seq_number << std::endl
53       << "Ack Number: " << rhs.ack_number << std::endl
54       << "Length: " << (uint16_t)(rhs.length) << std::endl
55       << "Flags: " << rhs.flags << std::endl
56       << "Window Size: " << rhs.window_size << std::endl
57       << "Checksum: " << rhs.checksum << std::endl;
58
59   return lhs;
60 }