ipv4_header.hpp
Go to the documentation of this file.
00001 //
00002 // ipv4_header.hpp
00003 // ~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #ifndef IPV4_HEADER_HPP
00012 #define IPV4_HEADER_HPP
00013 
00014 #include <algorithm>
00015 #include <boost/asio/ip/address_v4.hpp>
00016 
00017 // Packet header for IPv4.
00018 //
00019 // The wire format of an IPv4 header is:
00020 // 
00021 // 0               8               16                             31
00022 // +-------+-------+---------------+------------------------------+      ---
00023 // |       |       |               |                              |       ^
00024 // |version|header |    type of    |    total length in bytes     |       |
00025 // |  (4)  | length|    service    |                              |       |
00026 // +-------+-------+---------------+-+-+-+------------------------+       |
00027 // |                               | | | |                        |       |
00028 // |        identification         |0|D|M|    fragment offset     |       |
00029 // |                               | |F|F|                        |       |
00030 // +---------------+---------------+-+-+-+------------------------+       |
00031 // |               |               |                              |       |
00032 // | time to live  |   protocol    |       header checksum        |   20 bytes
00033 // |               |               |                              |       |
00034 // +---------------+---------------+------------------------------+       |
00035 // |                                                              |       |
00036 // |                      source IPv4 address                     |       |
00037 // |                                                              |       |
00038 // +--------------------------------------------------------------+       |
00039 // |                                                              |       |
00040 // |                   destination IPv4 address                   |       |
00041 // |                                                              |       v
00042 // +--------------------------------------------------------------+      ---
00043 // |                                                              |       ^
00044 // |                                                              |       |
00045 // /                        options (if any)                      /    0 - 40
00046 // /                                                              /     bytes
00047 // |                                                              |       |
00048 // |                                                              |       v
00049 // +--------------------------------------------------------------+      ---
00050 
00051 class ipv4_header
00052 {
00053 public:
00054     ipv4_header() { std::fill(rep_, rep_ + sizeof(rep_), 0); }
00055 
00056     unsigned char version() const { return (rep_[0] >> 4) & 0xF; }
00057     unsigned short header_length() const { return (rep_[0] & 0xF) * 4; }
00058     unsigned char type_of_service() const { return rep_[1]; }
00059     unsigned short total_length() const { return decode(2, 3); }
00060     unsigned short identification() const { return decode(4, 5); }
00061     bool dont_fragment() const { return (rep_[6] & 0x40) != 0; }
00062     bool more_fragments() const { return (rep_[6] & 0x20) != 0; }
00063     unsigned short fragment_offset() const { return decode(6, 7) & 0x1FFF; }
00064     unsigned int time_to_live() const { return rep_[8]; }
00065     unsigned char protocol() const { return rep_[9]; }
00066     unsigned short header_checksum() const { return decode(10, 11); }
00067 
00068     boost::asio::ip::address_v4 source_address() const
00069     {
00070         boost::asio::ip::address_v4::bytes_type bytes
00071             = { { rep_[12], rep_[13], rep_[14], rep_[15] } };
00072         return boost::asio::ip::address_v4(bytes);
00073     }
00074 
00075     boost::asio::ip::address_v4 destination_address() const
00076     {
00077         boost::asio::ip::address_v4::bytes_type bytes
00078             = { { rep_[16], rep_[17], rep_[18], rep_[19] } };
00079         return boost::asio::ip::address_v4(bytes);
00080     }
00081 
00082     friend std::istream& operator>>(std::istream& is, ipv4_header& header)
00083     {
00084         is.read(reinterpret_cast<char*>(header.rep_), 20);
00085         if (header.version() != 4)
00086             is.setstate(std::ios::failbit);
00087         std::streamsize options_length = header.header_length() - 20;
00088         if (options_length < 0 || options_length > 40)
00089             is.setstate(std::ios::failbit);
00090         else
00091             is.read(reinterpret_cast<char*>(header.rep_) + 20, options_length);
00092         return is;
00093     }
00094 
00095 private:
00096     unsigned short decode(int a, int b) const
00097     { return (rep_[a] << 8) + rep_[b]; }
00098 
00099     unsigned char rep_[60];
00100 };
00101 
00102 #endif // IPV4_HEADER_HPP


rosping
Author(s): Kei Okada
autogenerated on Mon Oct 6 2014 11:00:33