r20498: Two 64-bit warnings
[Samba.git] / examples / pcap2nbench / ip.cpp
blob02404c1d3cebed855d0a4c172e6abef92df8d65a
1 /*\
2 * pcap2nbench - Converts libpcap network traces to nbench input
3 * Copyright (C) 2004 Jim McDonough <jmcd@us.ibm.com>
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.
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.
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.
19 * Written by Anthony Liguori <aliguori@us.ibm.com>
20 \*/
22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include "ip.hpp"
27 ip::ip(const uint8_t *data, size_t length)
29 if (length < 20) {
30 std::cerr << "Invalid ip packet" << std::endl;
33 version = (data[0] >> 4) & 0x0F;
34 header_length = (data[0] & 0x0F) * 4;
35 dsfield = data[1];
36 memcpy(&total_length, data + 2, 2);
37 total_length = ntohs(total_length);
38 memcpy(&id, data + 4, 2);
39 id = ntohs(id);
40 flags = (data[6] >> 4) & 0x0F;
41 fragment_offset = ((data[6] & 0x0F) << 4) | data[7];
42 ttl = data[8];
43 protocol = data[9];
44 memcpy(&checksum, data + 10, 2);
45 checksum = ntohs(checksum);
46 memcpy(&source, data + 12, 4);
47 memcpy(&destination, data + 16, 4);
50 std::ostream &operator<<(std::ostream &lhs, const ip &rhs)
52 lhs << "Version: " << (uint32_t)rhs.version << std::endl
53 << "Header length: " << (uint32_t)rhs.header_length << std::endl
54 << "Differentiated Services Field: " << (uint32_t)rhs.dsfield << std::endl
55 << "Total Length: " << (uint32_t)rhs.total_length << std::endl
56 << "Identification: " << (uint32_t)rhs.id << std::endl
57 << "Flags: " << (uint32_t)rhs.flags << std::endl
58 << "Fragment offset: " << (uint32_t)rhs.fragment_offset << std::endl
59 << "TTL: " << (uint32_t)rhs.ttl << std::endl
60 << "Protocol: " << (uint32_t)rhs.protocol << std::endl
61 << "Checksum: " << (uint32_t)rhs.checksum << std::endl
62 << "Source: " << inet_ntoa(*((in_addr *)&rhs.source)) << std::endl
63 << "Destination: " << inet_ntoa(*((in_addr *)&rhs.destination)) << std::endl;
65 return lhs;