11 #ifndef ICMP_HEADER_HPP 12 #define ICMP_HEADER_HPP 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); }
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); }
56 {
return is.read(reinterpret_cast<char*>(header.rep_), 8); }
59 {
return os.write(reinterpret_cast<const char*>(header.rep_), 8); }
62 unsigned short decode(
int a,
int b)
const 63 {
return (rep_[a] << 8) + rep_[
b]; }
65 void encode(
int a,
int b,
unsigned short n)
67 rep_[a] =
static_cast<unsigned char>(n >> 8);
68 rep_[
b] =
static_cast<unsigned char>(n & 0xFF);
71 unsigned char rep_[8];
74 template <
typename Iterator>
76 Iterator body_begin, Iterator body_end)
78 unsigned int sum = (header.
type() << 8) + header.
code()
81 Iterator body_iter = body_begin;
82 while (body_iter != body_end)
84 sum += (
static_cast<unsigned char>(*body_iter++) << 8);
85 if (body_iter != body_end)
86 sum +=
static_cast<unsigned char>(*body_iter++);
89 sum = (sum >> 16) + (sum & 0xFFFF);
91 header.
checksum(static_cast<unsigned short>(~sum));
94 #endif // ICMP_HEADER_HPP
asio::basic_streambuf< Allocator > & b