Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
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
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
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