ipv4_header.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // ipv4_header.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef IPV4_HEADER_HPP
  11. #define IPV4_HEADER_HPP
  12. #include <algorithm>
  13. #include <boost/asio/ip/address_v4.hpp>
  14. // Packet header for IPv4.
  15. //
  16. // The wire format of an IPv4 header is:
  17. //
  18. // 0 8 16 31
  19. // +-------+-------+---------------+------------------------------+ ---
  20. // | | | | | ^
  21. // |version|header | type of | total length in bytes | |
  22. // | (4) | length| service | | |
  23. // +-------+-------+---------------+-+-+-+------------------------+ |
  24. // | | | | | | |
  25. // | identification |0|D|M| fragment offset | |
  26. // | | |F|F| | |
  27. // +---------------+---------------+-+-+-+------------------------+ |
  28. // | | | | |
  29. // | time to live | protocol | header checksum | 20 bytes
  30. // | | | | |
  31. // +---------------+---------------+------------------------------+ |
  32. // | | |
  33. // | source IPv4 address | |
  34. // | | |
  35. // +--------------------------------------------------------------+ |
  36. // | | |
  37. // | destination IPv4 address | |
  38. // | | v
  39. // +--------------------------------------------------------------+ ---
  40. // | | ^
  41. // | | |
  42. // / options (if any) / 0 - 40
  43. // / / bytes
  44. // | | |
  45. // | | v
  46. // +--------------------------------------------------------------+ ---
  47. class ipv4_header
  48. {
  49. public:
  50. ipv4_header() { std::fill(rep_, rep_ + sizeof(rep_), 0); }
  51. unsigned char version() const { return (rep_[0] >> 4) & 0xF; }
  52. unsigned short header_length() const { return (rep_[0] & 0xF) * 4; }
  53. unsigned char type_of_service() const { return rep_[1]; }
  54. unsigned short total_length() const { return decode(2, 3); }
  55. unsigned short identification() const { return decode(4, 5); }
  56. bool dont_fragment() const { return (rep_[6] & 0x40) != 0; }
  57. bool more_fragments() const { return (rep_[6] & 0x20) != 0; }
  58. unsigned short fragment_offset() const { return decode(6, 7) & 0x1FFF; }
  59. unsigned int time_to_live() const { return rep_[8]; }
  60. unsigned char protocol() const { return rep_[9]; }
  61. unsigned short header_checksum() const { return decode(10, 11); }
  62. boost::asio::ip::address_v4 source_address() const
  63. {
  64. boost::asio::ip::address_v4::bytes_type bytes
  65. = { { rep_[12], rep_[13], rep_[14], rep_[15] } };
  66. return boost::asio::ip::address_v4(bytes);
  67. }
  68. boost::asio::ip::address_v4 destination_address() const
  69. {
  70. boost::asio::ip::address_v4::bytes_type bytes
  71. = { { rep_[16], rep_[17], rep_[18], rep_[19] } };
  72. return boost::asio::ip::address_v4(bytes);
  73. }
  74. friend std::istream& operator>>(std::istream& is, ipv4_header& header)
  75. {
  76. is.read(reinterpret_cast<char*>(header.rep_), 20);
  77. if (header.version() != 4)
  78. is.setstate(std::ios::failbit);
  79. std::streamsize options_length = header.header_length() - 20;
  80. if (options_length < 0 || options_length > 40)
  81. is.setstate(std::ios::failbit);
  82. else
  83. is.read(reinterpret_cast<char*>(header.rep_) + 20, options_length);
  84. return is;
  85. }
  86. private:
  87. unsigned short decode(int a, int b) const
  88. { return (rep_[a] << 8) + rep_[b]; }
  89. unsigned char rep_[60];
  90. };
  91. #endif // IPV4_HEADER_HPP