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