[206] | 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 | #include <sys/socket.h>
|
---|
| 23 | #include <arpa/inet.h>
|
---|
| 24 | #include "ip.hpp"
|
---|
| 25 |
|
---|
| 26 | ip::ip(const uint8_t *data, size_t length)
|
---|
| 27 | {
|
---|
| 28 | if (length < 20) {
|
---|
| 29 | std::cerr << "Invalid ip packet" << std::endl;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | version = (data[0] >> 4) & 0x0F;
|
---|
| 33 | header_length = (data[0] & 0x0F) * 4;
|
---|
| 34 | dsfield = data[1];
|
---|
| 35 | memcpy(&total_length, data + 2, 2);
|
---|
| 36 | total_length = ntohs(total_length);
|
---|
| 37 | memcpy(&id, data + 4, 2);
|
---|
| 38 | id = ntohs(id);
|
---|
| 39 | flags = (data[6] >> 4) & 0x0F;
|
---|
| 40 | fragment_offset = ((data[6] & 0x0F) << 4) | data[7];
|
---|
| 41 | ttl = data[8];
|
---|
| 42 | protocol = data[9];
|
---|
| 43 | memcpy(&checksum, data + 10, 2);
|
---|
| 44 | checksum = ntohs(checksum);
|
---|
| 45 | memcpy(&source, data + 12, 4);
|
---|
| 46 | memcpy(&destination, data + 16, 4);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | std::ostream &operator<<(std::ostream &lhs, const ip &rhs)
|
---|
| 50 | {
|
---|
| 51 | lhs << "Version: " << (uint32_t)rhs.version << std::endl
|
---|
| 52 | << "Header length: " << (uint32_t)rhs.header_length << std::endl
|
---|
| 53 | << "Differentiated Services Field: " << (uint32_t)rhs.dsfield << std::endl
|
---|
| 54 | << "Total Length: " << (uint32_t)rhs.total_length << std::endl
|
---|
| 55 | << "Identification: " << (uint32_t)rhs.id << std::endl
|
---|
| 56 | << "Flags: " << (uint32_t)rhs.flags << std::endl
|
---|
| 57 | << "Fragment offset: " << (uint32_t)rhs.fragment_offset << std::endl
|
---|
| 58 | << "TTL: " << (uint32_t)rhs.ttl << std::endl
|
---|
| 59 | << "Protocol: " << (uint32_t)rhs.protocol << std::endl
|
---|
| 60 | << "Checksum: " << (uint32_t)rhs.checksum << std::endl
|
---|
| 61 | << "Source: " << inet_ntoa(*((in_addr *)&rhs.source)) << std::endl
|
---|
| 62 | << "Destination: " << inet_ntoa(*((in_addr *)&rhs.destination)) << std::endl;
|
---|
| 63 |
|
---|
| 64 | return lhs;
|
---|
| 65 | }
|
---|