Realistic 3D camera system
3D camera system components
icmp_header.hpp
Go to the documentation of this file.
1 //
2 // icmp_header.hpp
3 // ~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 
11 #ifndef ICMP_HEADER_HPP
12 #define ICMP_HEADER_HPP
13 
14 #include <istream>
15 #include <ostream>
16 #include <algorithm>
17 
18 // ICMP header for both IPv4 and IPv6.
19 //
20 // The wire format of an ICMP header is:
21 //
22 // 0 8 16 31
23 // +---------------+---------------+------------------------------+ ---
24 // | | | | ^
25 // | type | code | checksum | |
26 // | | | | |
27 // +---------------+---------------+------------------------------+ 8 bytes
28 // | | | |
29 // | identifier | sequence number | |
30 // | | | v
31 // +-------------------------------+------------------------------+ ---
32 
34 {
35 public:
40 
41  icmp_header() { std::fill(rep_, rep_ + sizeof(rep_), 0); }
42 
43  unsigned char type() const { return rep_[0]; }
44  unsigned char code() const { return rep_[1]; }
45  unsigned short checksum() const { return decode(2, 3); }
46  unsigned short identifier() const { return decode(4, 5); }
47  unsigned short sequence_number() const { return decode(6, 7); }
48 
49  void type(unsigned char n) { rep_[0] = n; }
50  void code(unsigned char n) { rep_[1] = n; }
51  void checksum(unsigned short n) { encode(2, 3, n); }
52  void identifier(unsigned short n) { encode(4, 5, n); }
53  void sequence_number(unsigned short n) { encode(6, 7, n); }
54 
55  friend std::istream& operator>>(std::istream& is, icmp_header& header)
56  { return is.read(reinterpret_cast<char*>(header.rep_), 8); }
57 
58  friend std::ostream& operator<<(std::ostream& os, const icmp_header& header)
59  { return os.write(reinterpret_cast<const char*>(header.rep_), 8); }
60 
61 private:
62  unsigned short decode(int a, int b) const
63  { return (rep_[a] << 8) + rep_[b]; }
64 
65  void encode(int a, int b, unsigned short n)
66  {
67  rep_[a] = static_cast<unsigned char>(n >> 8);
68  rep_[b] = static_cast<unsigned char>(n & 0xFF);
69  }
70 
71  unsigned char rep_[8];
72 };
73 
74 template <typename Iterator>
76  Iterator body_begin, Iterator body_end)
77 {
78  unsigned int sum = (header.type() << 8) + header.code()
79  + header.identifier() + header.sequence_number();
80 
81  Iterator body_iter = body_begin;
82  while (body_iter != body_end)
83  {
84  sum += (static_cast<unsigned char>(*body_iter++) << 8);
85  if (body_iter != body_end)
86  sum += static_cast<unsigned char>(*body_iter++);
87  }
88 
89  sum = (sum >> 16) + (sum & 0xFFFF);
90  sum += (sum >> 16);
91  header.checksum(static_cast<unsigned short>(~sum));
92 }
93 
94 #endif // ICMP_HEADER_HPP
void identifier(unsigned short n)
Definition: icmp_header.hpp:52
void compute_checksum(icmp_header &header, Iterator body_begin, Iterator body_end)
Definition: icmp_header.hpp:75
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
friend std::ostream & operator<<(std::ostream &os, const icmp_header &header)
Definition: icmp_header.hpp:58
unsigned short identifier() const
Definition: icmp_header.hpp:46
void sequence_number(unsigned short n)
Definition: icmp_header.hpp:53
void type(unsigned char n)
Definition: icmp_header.hpp:49
void code(unsigned char n)
Definition: icmp_header.hpp:50
friend std::istream & operator>>(std::istream &is, icmp_header &header)
Definition: icmp_header.hpp:55
unsigned short checksum() const
Definition: icmp_header.hpp:45
unsigned char type() const
Definition: icmp_header.hpp:43
unsigned short sequence_number() const
Definition: icmp_header.hpp:47
void checksum(unsigned short n)
Definition: icmp_header.hpp:51
unsigned char code() const
Definition: icmp_header.hpp:44